adriangb opened a new pull request, #27893:
URL: https://github.com/apache/datafusion/pull/27893

   ## Which issue does this PR close?
   
   - Part of https://github.com/apache/datafusion/issues/20871. It is split out 
of https://github.com/apache/datafusion/pull/27736, which stacks on it.
   
   ## Rationale for this change
   
   A row group is *fully matched* when its statistics prove that every row 
passes the filter. The scan proves this by pruning with the inverse of the 
filter. A row where the filter is NULL does not pass, so the inverse must also 
keep every container that can hold a NULL.
   
   Today the Parquet crate adds the null checks before `PruningPredicate` 
rewrites the predicate:
   
   ```text
   Parquet crate:   NOT(P) OR a IS NULL OR b IS NULL      one term for each 
nullable column in P
   pruning crate:   rewrite(...)                          a IS NULL becomes 
a_null_count > 0
   ```
   
   That needs a source expression for every input of the predicate, so the 
Parquet crate collects the columns of `P` by hand. This only works for columns. 
It is the one thing that blocks statistics on other expressions, such as struct 
fields in https://github.com/apache/datafusion/pull/27736, from working here 
without a special case.
   
   After the rewrite, there is nothing to collect. `RequiredColumns` already 
lists what the predicate reads.
   
   ## What changes are included in this PR?
   
   `PruningPredicate::inverse_for_full_match` builds the inverse, and adds the 
null checks after the rewrite:
   
   ```text
   rewrite(NOT P) OR a_null_count > 0 OR ...      one term for each nullable 
column that rewrite(NOT P) reads
   ```
   
   ```diff
   -let inverted = build_inverted_predicate(predicate, arrow_schema)?;   // 
datafusion-datasource-parquet
   +let inverted = predicate.inverse_for_full_match()?;                  // 
datafusion-pruning
   ```
   
   The Parquet module `pruning.rs` only held `build_inverted_predicate`, so it 
is removed.
   
   Two details differ from the old code:
   
   | | Before | Now |
   |---|---|---|
   | Columns with a null check | Every nullable column in `P` | Every nullable 
column that the inverse reads |
   | Literal guarantees of the inverse | Kept only if `P` has no nullable 
column (a top-level `OR` removes them) | Always removed |
   
   **Columns that the inverse does not read.** The rewrite replaces a 
comparison that it cannot use with `true`. So it prunes a container only if the 
comparisons that it reads are FALSE on their own, and `FALSE AND x` is FALSE 
for every `x`, also for NULL. A column that is not read cannot change the 
result. For `i > 5 OR j + 1 > 3`, where every `i` is greater than 5 and `j` 
holds a NULL, the old code found no full match and the new code finds one. 
`TRUE OR NULL` is TRUE, so that is correct.
   
   **Literal guarantees.** A guarantee prunes a container on its own, before a 
null check can keep it. For `i != 1`, the inverse `i = 1` has the guarantee `i 
IN (1)`. It would prune a container that holds a NULL and no 1, where `i != 1` 
is NULL. The Parquet row group and page statistics do not answer `contained`, 
so this changes no result today.
   
   ## What is the testing strategy for this PR?
   
   New unit tests in `pruning_predicate.rs`:
   
   | Test | Asserts |
   |---|---|
   | `full_match_inverse_keeps_containers_with_nulls` | The exact inverse 
expression, and the result for containers with no NULL, a NULL, a missing null 
count, and a row that fails |
   | `full_match_inverse_has_no_null_term_for_a_non_nullable_column` | The 
exact inverse expression |
   | `full_match_inverse_ignores_columns_that_it_does_not_read` | The `i > 5 OR 
j + 1 > 3` case above |
   | `full_match_inverse_has_no_literal_guarantees` | The `i != 1` case above |
   
   The existing row group, page index and `LIMIT` pruning tests cover the 
callers. They pass without changes to their expectations.
   
   ## Are there any user-facing changes?
   
   - New public method `PruningPredicate::inverse_for_full_match`.
   - A scan can prove a full match in more cases, as described above. This can 
change `row_groups_pruned_statistics` (fully matched) and `LIMIT` pruning 
metrics.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to