Skip to content

feat: let other crates resolve Parquet expression statistics - #27852

Closed
adriangb wants to merge 13 commits into
apache:mainfrom
adriangb:expr-stats-resolver-hooks
Closed

adriangb wants to merge 13 commits into
apache:mainfrom
adriangb:expr-stats-resolver-hooks

Conversation

@adriangb

Copy link
Copy Markdown
Contributor

Note

Stacked on #27736. GitHub shows the commits of both PRs. Only the last two belong to this one: compare view.

Which issue does this PR close?

Rationale for this change

#27736 prunes Parquet files on struct fields. A function from another crate, such as variant_get on a shredded Variant, cannot use its Parquet statistics yet. It can need several leaves, and the layout can differ between files.

What changes are included in this PR?

 PruningPredicate
-  StructFieldMatcher::bind -> StructFieldLeaf
+  FileStatisticsResolvers::bind
+    ParquetStatisticsResolver::bind(expr, file schemas)    # get_field built in, others registered
+      -> BoundExprStatistics::statistics(kind, &dyn ParquetLeafStatistics)
pub trait ParquetStatisticsResolver {
    // Once per file. None = no statistics for `expr` in this file.
    fn bind(&self, expr: &Arc<dyn PhysicalExpr>, arrow_schema: &Schema, parquet_schema: &SchemaDescriptor)
        -> Option<Arc<dyn BoundExprStatistics>>;
}

pub trait BoundExprStatistics {
    fn statistics(&self, kind: ExprStatisticsKind, stats: &dyn ParquetLeafStatistics) -> Option<ArrayRef>;
    fn page_index_leaf(&self) -> Option<usize> { None }   // opt in to page pruning
    fn bloom_filter_leaf(&self) -> Option<usize> { None } // opt in to bloom filters
}

ParquetSource::with_statistics_resolver(Arc::new(MyVariantResolver))
# Commit
1 feat: the resolver traits. Struct fields become the built-in resolver, with the same behavior
2 test: resolvers that use only the public API

This is a draft for discussion of the API. #26282 proposes a capability on the function for the same use case. The right shape is easier to judge with a real Variant implementation.

What is the testing strategy for this PR?

Are there any user-facing changes?

Change Breaking?
New ParquetStatisticsResolver, BoundExprStatistics, ParquetLeafStatistics, ExprStatisticsKind, leaf_index_by_path and ParquetSource::with_statistics_resolver No
A ParquetSource with a custom resolver does not serialize to proto New limitation

🤖 Generated with Claude Code

adriangb and others added 13 commits November 29, 2026 17:41
`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. The 57.0.0
upgrade guide describes the new import path.

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>
`PruningStatistics` can only answer for a column, but a data source can
also know the statistics of an expression, such as `get_field(s, 'a')`.

`StatsExpr` holds an expression and the binding that the source made for
it. The new `*_for_expr` methods take a `StatsExpr` and return `None` by
default, so existing implementations keep compiling.

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.

Add an optional expression next to each required column, and a
`StatsKey` that `ColumnReferenceCount` returns. Every helper that reads
statistics takes a `StatsKey`, and `RequiredColumns::stat_key_expr`
replaces the per-statistic `*_column_expr` wrappers. 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>
A source can now pass a `StatisticsExprMatcher` to the predicate builder.
The rewrite asks the matcher to bind each expression, outermost first. A
bound expression becomes a statistics key, like a column. An unbound
expression falls back to the keys inside it.

The key carries the binding, so the source never looks the expression up
again. Looking it up by value would hash the whole subtree at every node,
which is quadratic for a deep predicate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Parquet stores statistics for each struct field, but a predicate on a
struct field such as `s['a'] > 15` cannot prune row groups or pages yet.

Add `parquet_struct_field_pruning.slt` with the current output, so the
next commits show what changes. It covers row groups, two files, the page
index, a dynamic TopK filter, struct type drift, a long `IN` list, a hash
join dynamic filter, and a struct field that is NULL in a non-nullable
struct.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Parquet stores statistics for every struct field leaf, but a predicate
on `s['a']` could not use them.

`StructFieldMatcher` binds `get_field` to its Parquet leaf, once per
file. Row group pruning, full match detection, dynamic filter pruning
and EXPLAIN use it. A missing null count stays unknown, because reading
it as zero prunes rows that `IS NULL` must return.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The page index of a struct field leaf prunes pages the same way as the
page index of a column. A page predicate reads one key, so the filter
accepts a predicate whose single key is a struct field.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The expected output is the output before bloom filters apply to struct
fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A bloom filter needs the literals that a key must equal.
`LiteralGuarantee` only understands columns, so the analysis stands a
placeholder column in for each statistics key and maps the result back
to the key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A struct field leaf holds every non-null value of the field without
conversion, so a value missing from its bloom filter is not in the field.
Keys that read the same leaf share one bloom filter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A struct field has one leaf, but a function from another crate can need
more. A shredded variant stores a value in a typed leaf or in a fallback
leaf, and the layout differs between files.

`ParquetStatisticsResolver` binds an expression to the leaves of one
file, and `BoundExprStatistics` reads them. The struct field matcher
becomes the built-in resolver, with the same behavior. A source with a
custom resolver does not serialize to proto.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A fake `variant_get` and a sidecar leaf for a cast use only the public
API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added documentation Improvements or additions to documentation 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 30, 2026
@adriangb adriangb closed this Dec 1, 2026
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 documentation Improvements or additions to documentation 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.

1 participant