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

   ## Which issue does this PR close?
   
   - Closes https://github.com/apache/datafusion/issues/20871.
   - Part of https://github.com/apache/datafusion/issues/24119 and 
https://github.com/apache/datafusion/issues/21306.
   - Alternative to https://github.com/apache/datafusion/pull/27734, which keys 
statistics by a column + struct field path. This PR keys them by an expression, 
so a function from outside DataFusion (such as `variant_get`) can plug in.
   
   ## Rationale for this change
   
   `WHERE s['a'] > 5`, `WHERE s['a'] = 50` and `ORDER BY s['a'] DESC LIMIT 3` 
read every Parquet row group, even though Parquet stores min/max, null counts 
and bloom filters for each struct field leaf. `PruningPredicate` and 
`PruningStatistics` only know top-level columns. The same gap blocks pruning on 
a shredded `variant_get(v, 'a')`.
   
   ## What changes are included in this PR?
   
   ```text
   PruningPredicateBuilder
     .with_statistics_expr_matcher(m)       // "this expr has its own 
statistics"
     key = Column | matched expr (one column, non-nested type)
     comparisons, IS [NOT] NULL, CAST/TRY_CAST/NOT, boolean, compact IN, 
literal guarantees
     -> 
PruningStatistics::{min_values,max_values,null_counts,contained}_for_expr(&Arc<dyn
 PhysicalExpr>)
   
   ParquetSource::with_statistics_resolver(Arc<dyn ParquetStatisticsResolver>)
     ParquetStatisticsResolvers (also the matcher of the scan's predicates)
       GetFieldStatisticsResolver (built in, downcasts GetFieldFunc)
     resolver reads ParquetLeafStatistics       // row groups and pages
       page_index_leaf / bloom_filter_leaf      // opt-in, default None
   ```
   
   Literal guarantees: before `LiteralGuarantee::analyze`, each matched 
expression is replaced with a placeholder column, and the guarantees are mapped 
back to `ExprLiteralGuarantee`s. `LiteralGuarantee` does not change.
   
   Review commit by commit. Each `test:` commit records today's output, and the 
next commit's slt diff shows the change.
   
   | # | Commit |
   |---|---|
   | 1 | `test:` slt for struct field pruning |
   | 2 | `refactor:` move `PruningStatistics` to 
`datafusion-physical-expr-common` (mechanical) |
   | 3 | `feat:` `*_for_expr` methods on `PruningStatistics` |
   | 4 | `refactor:` `PredicateBuildContext` |
   | 5 | `refactor:` key statistics by a column or an expression |
   | 6 | `feat:` `StatisticsExprMatcher` |
   | 7 | `feat:` Parquet resolvers, row groups |
   | 8 | `feat:` Parquet resolvers, page index |
   | 9 | `test:` a resolver from outside DataFusion (fake `variant_get`) |
   | 10-11 | `test:` / `feat:` dynamic TopK pruner gets the resolvers |
   | 12 | `feat:` IS [NOT] NULL on keys |
   | 13-14 | `test:` / `feat:` CAST, TRY_CAST, NOT over keys (struct type 
drift) |
   | 15 | `feat:` boolean keys |
   | 16-17 | `test:` / `feat:` compact IN on keys |
   | 18 | `feat:` literal guarantees on keys |
   | 19-20 | `test:` / `feat:` bloom filters on struct fields |
   
   Not included: hash-join (`HashTableLookupExpr`) filters on keys, file-level 
pruning (needs nested `Statistics`, 
https://github.com/apache/datafusion/issues/20707), and a real `variant_get` 
resolver.
   
   ## What is the testing strategy for this PR?
   
   `parquet_struct_field_pruning.slt`:
   
   | Case | Before | After |
   |---|---|---|
   | `s['a'] > 15`, 4 row groups | 4 → 4 | 4 → 2 |
   | `s['a'] >= 85`, 10 pages (page index) | 0 → 0 | 10 → 2 pages |
   | `ORDER BY s['a'] DESC LIMIT 3`, 5 row groups | 0 pruned by dynamic filter 
| 4 pruned (same as a plain column) |
   | file `INT`, table `DOUBLE`, `s['a'] > 6.5` | 3 → 3 | 3 → 1 |
   | `s['a'] = 50` with bloom filters | 2 → 2 | 0 row groups read (bloom) |
   | `s['a'] != 50`, Int8/Date/Decimal equality | rows kept | rows kept |
   
   `expr_statistics_resolver.rs` (core parquet integration test) registers a 
fake shredded-variant resolver from outside DataFusion: it uses `typed_value` 
bounds only for row groups where the fallback `value` is null in every row. 
Unit tests in `pruning_predicate.rs` cover rejected shapes, `single_column()` / 
`single_key()`, guarantees (`a = 1 OR b = 2` gives none, `CAST(key) = lit` 
gives none), and `always_true()` with only an expression guarantee.
   
   ## Are there any user-facing changes?
   
   Fewer row groups and pages are read for struct field predicates.
   
   API changes (`api change`):
   - `datafusion_common::pruning` moves to 
`datafusion_physical_expr_common::pruning`. 
`datafusion_pruning::PruningStatistics` still works.
   - New provided methods on `PruningStatistics` (default `None`).
   - `RequiredColumns::single_column()` returns `None` when a key is an 
expression. `single_key()` is new.
   - New: `StatisticsExprMatcher`, `ExprLiteralGuarantee`, 
`ParquetStatisticsResolver`, `ParquetLeafStatistics`, 
`ParquetSource::with_statistics_resolver`.
   - A `ParquetSource` with custom resolvers does not serialize to proto 
(returns `None`).
   
   **Door:** two-way, apart from the module move, which downstream crates will 
notice.
   
   **Blast radius:** pruning. A wrong leaf mapping, null count or bloom check 
would skip row groups that hold matching rows. The null-struct, null-leaf, `!=` 
and typed-equality slt cases target that.
   
   🤖 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