Skip to content

feat: statistics pruning for get_field on struct columns - #27734

Draft
adriangb wants to merge 9 commits into
apache:mainfrom
adriangb:get-field-stats-pruning
Draft

adriangb wants to merge 9 commits into
apache:mainfrom
adriangb:get-field-stats-pruning

Conversation

@adriangb

@adriangb adriangb commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

SELECT * FROM t WHERE s['a'] > 5 reads every Parquet row group. PruningPredicate only understands predicates on top-level columns, and PruningStatistics has no way to ask for the statistics of a struct field.

What changes are included in this PR?

 PruningStatistics (datafusion-common)
   min_values(&Column) / max_values / null_counts
+  min_values_for_field(&Column, path: &[String])   // default: None
+  max_values_for_field / null_counts_for_field

 PruningPredicate (datafusion-pruning)
   ColumnReferenceCount::One(Column)
+  ColumnReferenceCount::One(StatsKey { column, path })   // get_field(s, 'a', ...) is one key
   RequiredColumns: (Column, StatisticsType, Field)
+                   + field path per entry (empty = the column itself)
+  single_column() -> None when any key is a field path

 RowGroupPruningStatistics (datafusion-datasource-parquet)
+  *_for_field: field path -> Parquet leaf -> StatisticsConverter::from_column_index

 Page index (page_filter.rs)
-  requires RequiredColumns::single_column()
+  requires RequiredColumns::single_field()   // one column or one struct field
+  PagesPruningStatistics::*_for_field -> same leaf converter

A field path is only accepted when every step is a struct field and the leaf is not nested. Map lookups and paths below a list (per-element statistics) are rejected. datafusion-pruning does not depend on datafusion-functions, so it matches get_field by name. A function-agnostic accessor trait would let variant_get opt in later (#21306).

Review commit by commit:

  1. test: adds parquet_struct_field_pruning.slt with today's output (no pruning).
  2. refactor: keys RequiredColumns / PruningExpressionBuilder by column + path. No behavior change.
  3. feat: adds the *_for_field methods to PruningStatistics, with None defaults.
  4. feat: recognizes get_field in PruningPredicate. Plans gain a pruning_predicate, but nothing is pruned yet.
  5. feat: implements the methods for Parquet row groups. The slt diff shows the pruning.
  6. test: unit tests for rejected shapes and single_column().
  7. refactor: moves the field-to-leaf lookup into leaf_resolver::struct_field_leaf. No behavior change.
  8. test: adds a page-index slt case with today's output.
  9. feat: page index pruning for struct fields.

Not included: file-level pruning, which needs nested Statistics (#20707).

What is the testing strategy for this PR?

parquet_struct_field_pruning.slt covers 4 row groups, including a null struct and a null leaf, plus 2 files:

Query Before After
s['a'] > 15 4 → 4 matched 4 → 2 matched
s['a'] <= 2, 11 = s['a'], s['a'] = 20 4 → 4 4 → 1
s['a'] > 25 OR id < 2 4 → 4 4 → 3
s['a'] > s['b'] 4 → 4 4 → 4 (not prunable)
2 files, s['a'] < 10 2 → 2 2 → 1
1 row group of 10 pages, s['a'] >= 85 (page index) 0 → 0 pages 10 → 2 pages

Unit tests in pruning_predicate.rs cover the rejected shapes and single_column().

Are there any user-facing changes?

Fewer row groups are read for struct field predicates. EXPLAIN shows a pruning_predicate such as s.a_null_count@1 != row_count@2 AND s.a_max@0 > 15.

API: three new provided methods on PruningStatistics. Existing implementations keep compiling. RequiredColumns::single_column() now returns None for predicates that touch a struct field.

Door: two-way. Revert restores the old plans.

Blast radius: pruning. A wrong field-to-leaf mapping or wrong null count would prune row groups that hold matching rows (wrong results). The checks above and the null-struct / null-leaf slt cases target that.

🤖 Generated with Claude Code

adriangb and others added 6 commits November 24, 2026 20:58
Records the current behavior: predicates on `get_field(s, 'a')` get no
pruning predicate, so every row group is read. Later commits update the
expected output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`RequiredColumns` and `PruningExpressionBuilder` identify the statistics
they read by a bare column. Struct field pruning needs to read statistics
for `get_field(s, 'a')`, which is not a column.

This adds a field path next to each required column (empty for the
column itself) and a `StatsKey` that `ColumnReferenceCount` returns.
Every path is still empty, so behavior does not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`PruningStatistics` only returns statistics for a whole column. Add
`min_values_for_field`, `max_values_for_field` and
`null_counts_for_field`, which take a column and a struct field path.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`PruningPredicate` gives up on `get_field(s, 'a') > 5` because the
left side is not a column. Recognize `get_field(column, 'a', ...)`,
including chained calls, as one statistics key and replace the whole
access with its statistics column.

The pruning crate does not depend on `datafusion-functions`, so it
matches the function by its name. The key is only accepted when every
step is a struct field and the leaf is not nested: `get_field` on a
map is a runtime lookup, and a field below a list has per-element
statistics. `RequiredColumns::single_column` returns `None` for a
field path so that the page index does not read the root column.

No statistics source implements the new methods yet, so the plans gain
a pruning predicate but nothing is pruned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Implement the struct field methods of `PruningStatistics` for
`RowGroupPruningStatistics`. The field path resolves to the Parquet leaf
column, and its statistics go through the same untrusted-statistics
checks as a top-level column.

A path is rejected when a step is not a struct field, when the leaf is
nested, or when an ancestor is repeated. For a struct-only path, the leaf
null count includes rows where an ancestor struct is null. That matches
`get_field`, which returns null in that case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cover the cases that the slt cannot show directly: which expressions are
rejected (two fields, a field plus a column, a map lookup), and that a
field path is never reported as the single column of a predicate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) common Related to common crate datasource Changes to the datasource crate labels Nov 25, 2026
adriangb and others added 3 commits November 24, 2026 21:05
The page index needs the same field path to Parquet leaf lookup as row
group pruning. Move it into `struct_field_leaf` so both use one
implementation. No behavior change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Records the current behavior: the page index is not used for
`s['a'] >= 85`, so all 10 pages of the row group are read.

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

Add `RequiredColumns::single_field`, which also accepts one struct field
path. The page filter resolves the path to its Parquet leaf with
`struct_field_leaf`, and `PagesPruningStatistics` answers the struct
field methods from that leaf's converter. The page index load check uses
the same leaf lookup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
/// [`Self::min_values`].
///
/// The default returns `None`, so pruning keeps every container.
fn min_values_for_field(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open question: should this take an expression instead of a column + path?

An expression would also cover lower(col) backed by a precomputed column, or variant_get. A path only covers struct fields.

I used a path because PruningStatistics lives in datafusion-common, which cannot name PhysicalExpr (physical-expr-common depends on common). An expression-keyed API means moving this trait (and PrunableStatistics / CompositePruningStatistics) to physical-expr-common, with a re-export here.

A path can be widened later: expression methods with a default that maps get_field(col, ...) to these path methods. Should we move the trait now instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common Related to common crate datasource Changes to the datasource crate 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