You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Extract NDV (distinct_count) statistics from Parquet metadata (#19957)
## Which issue does this PR close?
- Part of #15265
Related: #18628, #8227
(I am not sure if an new issue specifically for the scope of the PR is
needed, happy to create it if needed)
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes#123` indicates that this PR will close issue #123.
-->
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->
This work originates from a discussion in datafusion-distributed about
improving the `TaskEstimator` API:
datafusion-contrib/datafusion-distributed#296 (comment)
We agreed that improved statistics support in DataFusion would benefit
both projects. For distributed-datafusion, better cardinality estimation
helps decide how to split computation across network boundaries.
This also benefits DataFusion directly, as CBO is already in place, for
example, join cardinality estimation
([`joins/utils.rs:586-646`](https://github.com/apache/datafusion/blob/main/datafusion/physical-plan/src/joins/utils.rs#L586-L646))
uses `distinct_count` via `max_distinct_count` to compute join
selectivity.
Currently this field is always `Absent` when reading from Parquet, so
this PR fills that gap.
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
Commit 1 - Reading NDV from Parquet files:
- Extract `distinct_count` from Parquet row group column statistics
- Single row group with NDV -> `Precision::Exact(ndv)`
- Multiple row groups with NDV -> `Precision::Inexact(max)` as
conservative lower bound
- No NDV available -> `Precision::Absent`
Commit 2 - Statistics propagation (can be split to a separate PR, if
preferred):
- `Statistics::try_merge()`: use max as conservative lower bound instead
of discarding NDV
- `Projection`: preserve NDV for single-column expressions as upper
bound
I'm including the second commit to showcase how I intend to use the
statistics, but these changes can be split to a follow-up PR to keep
review scope limited.
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
Yes, 7 unit tests are added for NDV extraction:
- Single/multiple row groups with NDV
- Partial NDV availability across row groups
- Multiple columns with different NDV values
- Integration test reading a real Parquet file with distinct_count
statistics (following the pattern in
[`row_filter.rs:685-696`](https://github.com/apache/datafusion/blob/main/datafusion/datasource-parquet/src/row_filter.rs#L685-L696),
using `parquet_to_arrow_schema` to derive the schema from the file)
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
No breaking changes. Statistics consumers will now see populated
`distinct_count` values when available in Parquet metadata.
Disclaimer: I used AI (Claude Code) to assist translating my ideas into
code as I am still ramping up with the codebase and especially with Rust
(guidance on both aspects is highly appreciated). I have a good
understanding of the core concepts (statistics, CBO etc.) and have
carefully double-checked that the PR matches my intentions and
understanding.
cc: @gabotechs@jayshrivastava@NGA-TRAN@gene-bordegaray
0 commit comments