Skip to content

feat: expression-keyed statistics pruning (struct fields, pluggable resolvers) - #27736

Draft
adriangb wants to merge 20 commits into
apache:mainfrom
adriangb:expr-stats-pruning
Draft

adriangb wants to merge 20 commits into
apache:mainfrom
adriangb:expr-stats-pruning

Conversation

@adriangb

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

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?

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 ExprLiteralGuarantees. 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, #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

@github-actions github-actions Bot added physical-expr Changes to the physical-expr crates core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) catalog Related to the catalog crate common Related to common crate datasource Changes to the datasource crate labels Nov 25, 2026
adriangb and others added 20 commits November 24, 2026 23:44
Records the current behavior: predicates on `get_field(s, 'a')` get no
pruning predicate, so every row group and page is read. Later commits
update the expected output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`PruningStatistics` lives in `datafusion-common`, which cannot name
`PhysicalExpr`. Asking a statistics source for the statistics of an
expression (such as `get_field(s, 'a')` or `variant_get(v, 'a')`)
needs the trait to live next to `PhysicalExpr`.

This is a mechanical move of `datafusion_common::pruning` to
`datafusion_physical_expr_common::pruning` and an update of the
imports. `datafusion_common` cannot re-export it, because
`physical-expr-common` depends on it, so the old path is removed.
`datafusion_pruning::PruningStatistics` keeps working.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`PruningStatistics` only returns statistics for whole columns. Add
`min_values_for_expr`, `max_values_for_expr`, `null_counts_for_expr`
and `contained_for_expr`, so a source can answer for an expression such
as `get_field(s, 'a')` or `variant_get(v, 'a')`.

The defaults return `None`, so existing implementations keep working
and prune nothing for such expressions. `CompositePruningStatistics`
forwards the new methods to its members.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`build_predicate_expression` takes the schema, the unhandled-predicate
hook and the IN-list limit as separate arguments and passes all three
to each recursive call. Struct field pruning adds one more read-only
setting (which expressions have their own statistics).

Bundle them in `PredicateBuildContext`. No behavior change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`RequiredColumns` and `PruningExpressionBuilder` identify the statistics
they read by a bare column. Pruning on `get_field(s, 'a')` or
`variant_get(v, 'a')` needs statistics for an expression that is not a
column.

This adds an optional expression next to each required column and a
`StatsKey` that `ColumnReferenceCount` returns. The statistics record
batch reads expression statistics through `*_for_expr`. Every key is
still a plain column, so behavior does not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`PruningPredicate` only reads statistics for columns, so a predicate on
`get_field(s, 'a')` or `variant_get(v, 'a')` cannot be pruned. The
pruning crate cannot know which such expressions a statistics source
can answer: Parquet knows struct fields, and a variant extension knows
shredded variant paths.

Add `StatisticsExprMatcher`, set with
`PruningPredicateBuilder::with_statistics_expr_matcher`. A matched
expression that reads one column and has a non-nested type becomes one
statistics key: the predicate replaces it with its statistics columns
and reads them through `PruningStatistics::*_for_expr`.

`RequiredColumns::single_column` returns `None` when a key is an
expression. The new `single_key` returns the column or expression.

No caller sets a matcher yet, so plans do not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Parquet stores statistics for every leaf column, including struct
fields and shredded variant columns, but predicates on `get_field` or
`variant_get` cannot use them.

Add `ParquetStatisticsResolver`. A resolver says which expressions it
may answer and reads their statistics from `ParquetLeafStatistics`, a
view of the leaf column statistics of a file's row groups. Resolvers
can come from outside DataFusion through
`ParquetSource::with_statistics_resolver`. The resolvers of a scan are
also the `StatisticsExprMatcher` for its pruning predicates.

`GetFieldStatisticsResolver` is always present. It maps
`get_field(s, 'a', ...)` to the leaf of that struct field and rejects
paths through maps or lists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The page index only accepts predicates whose statistics come from one
column, so `s['a'] >= 85` read every page of a row group.

A resolver can now name the one Parquet leaf whose pages hold an
expression, through `ParquetStatisticsResolver::page_index_leaf`. The
page filter prunes that leaf's pages, and `PagesPruningStatistics`
answers the resolvers through `ParquetLeafStatistics`, for that leaf
only, because the pages of other leaves do not line up. The page index
load check uses the same leaf.

The default returns `None`, so a resolver that needs several leaves
(such as a shredded variant) prunes row groups only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Show that a crate outside DataFusion can register a
`ParquetStatisticsResolver` for its own function. A stand-in for a
shredded `variant_get` reads bounds from the shredded `typed_value` leaf,
and only uses them for row groups where the binary fallback `value`
leaf is null in every row. Without the resolver, nothing is pruned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Records the current behavior: `ORDER BY s['a'] DESC LIMIT 3` gets no
pruning predicate for its dynamic filter, while the same query on a plain
column does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`RowGroupPruner` re-checks row groups as a dynamic filter (such as a TopK
bound) tightens. It built its pruning predicate without a statistics
matcher and read statistics with the default resolvers only, so it could
never prune on a struct field or a custom resolver's expression.

Pass it the scan's `ParquetStatisticsResolvers` for both. The TopK slt
does not change yet: its filter `s['a'] IS NULL OR ...` still becomes
`true`, because `IS NULL` only accepts a column.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`IS NULL` and `IS NOT NULL` only accepted a bare column, so
`get_field(s, 'a') IS NULL` became `true`. A dynamic TopK filter on a
nullable key always has the shape `key IS NULL OR key > x`, so the
`true` arm made the whole filter unprunable.

Accept any statistics key and read its null count through
`null_counts_for_expr`. For a struct field the Parquet leaf null count
also counts rows where an ancestor is null, which matches `get_field`.
`ORDER BY s['a'] DESC LIMIT 3` now skips 4 of 5 row groups, as the same
query on a plain column does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Records the current behavior: when a file stores a struct field as INT
and the table declares DOUBLE, `s['a'] > 6.5` is checked as
`CAST(get_field(s, 'a') AS DOUBLE) > 6.5` and prunes nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The schema adapter reads a struct field whose file type differs from the
table type as `CAST(get_field(s, 'a') AS T)`. The comparison rewrite only
looked through casts down to a bare column, so such predicates could not
be pruned when the cast stays (for example INT to DOUBLE against 6.5).

`rewrite_expr_to_prunable` now takes the statistics key and stops at it,
so CAST, TRY_CAST, negation and NOT over a key are handled like over a
column. Order preservation is still checked for each cast.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A boolean column used directly as a predicate (`WHERE flag`, `WHERE NOT
flag`) is pruned with its min and max. A boolean struct field
(`WHERE s['flag']`) was not, because the check only accepted a bare
column and looked at the column's own type.

Accept any statistics key and check the type of the key expression,
since the root of a boolean struct field is a struct.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With a raised `max_in_list_size`, a 21-value IN list on `s['a']` is
pruned through the per-value rewrite (one equality per value). The next
commit gives it the compact form without changing the pruning result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An IN list longer than 20 values (when `max_in_list_size` allows it) is
checked with one compact set-intersection test against min and max. For
a struct field it fell back to one equality check per value, because the
compact form only accepted a bare column.

Accept any statistics key, taking the value type from the key expression.
The pruning result does not change, only the size of the predicate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A predicate such as `s['a'] = 5` or `s['a'] IN (1, 2)` gives a literal
guarantee that a bloom filter can check, but `LiteralGuarantee` only
describes columns, so struct fields got none.

Before `LiteralGuarantee::analyze` runs, each distinct matched
expression is replaced with a placeholder column of a reserved name. The
guarantees on placeholders are mapped back to their expressions as
`ExprLiteralGuarantee`s, and `prune` checks them with
`PruningStatistics::contained_for_expr`. `LiteralGuarantee` and the
existing column guarantees do not change.

`always_true` counts expression guarantees, so a predicate whose only
pruning power is such a guarantee is kept. `literal_exprs` lists the
expressions, so a source can load only the bloom filters it needs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Records the current behavior: the file has bloom filters for its struct
field leaves, but `s['a'] = 50` does not use them, so both row groups
are read although neither holds 50. The test also checks that `!=` and
values present in Int8, Date and Decimal fields keep their rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Parquet writes a bloom filter per leaf column, so struct fields have
their own. DataFusion only loaded bloom filters for top-level columns,
so `s['a'] = 50` could not use them.

A `ParquetStatisticsResolver` can now name the leaf whose bloom filter
holds the values of an expression, through `bloom_filter_leaf`.
`GetFieldStatisticsResolver` names the struct field leaf. The opener
loads those filters for the predicate's `literal_exprs`, and
`BloomFilterStatistics` answers `contained_for_expr` from them, with the
same rules as for a column: it never reports that a value is present.

The default returns `None`. A shredded variant cannot use it, because
some of its values may live in the binary `value` column.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

catalog Related to the catalog crate common Related to common crate core Core DataFusion crate datasource Changes to the datasource crate physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support row group pruning for struct field predicates

1 participant