feat: Use NDV for equality filter selectivity calculation#20789
Merged
alamb merged 8 commits intoapache:mainfrom Apr 3, 2026
Merged
feat: Use NDV for equality filter selectivity calculation#20789alamb merged 8 commits intoapache:mainfrom
alamb merged 8 commits intoapache:mainfrom
Conversation
kosiew
requested changes
Mar 20, 2026
Contributor
There was a problem hiding this comment.
@jonathanc-n
Thanks for the working on this.
I do think there’s one correctness issue in the new singleton-selectivity branch that should be addressed before merge, and I left one small follow-up suggestion that could make the logic easier to read.
Contributor
|
run benchmarks |
|
🤖 Benchmark completed (GKE) | trigger Details
Resource Usagetpch — base (merge-base)
tpch — branch
|
|
🤖 Benchmark completed (GKE) | trigger Details
Resource Usagetpcds — base (merge-base)
tpcds — branch
|
|
🤖 Benchmark completed (GKE) | trigger Details
Resource Usageclickbench_partitioned — base (merge-base)
clickbench_partitioned — branch
|
Contributor
Author
|
No perf changes should occur here, either this or #21077 would need to be merged before we can test performance |
github-merge-queue Bot
pushed a commit
that referenced
this pull request
Apr 7, 2026
) ## Which issue does this PR close? - Part of #20766 Related: #20789 (uses NDV for equality filter selectivity, complementary - this PR improves the NDV output stats, that PR consumes them) ## Rationale for this change When a filter predicate collapses a column interval to a single value (e.g. `d_qoy = 1`), the output column can only have one distinct value. Currently `distinct_count` is always demoted to `Inexact`, losing this information. This matters for downstream optimizers that rely on `distinct_count`, such as join cardinality estimation in `estimate_inner_join_cardinality`. ## What changes are included in this PR? In `collect_new_statistics` (filter.rs), when the post-filter interval has `lower == upper` (both non-null), set `distinct_count` to `Precision::Exact(1)` instead of demoting the input NDV to `Inexact`. ## Are these changes tested? Yes, 4 unit tests: - Equality predicate (`a = 42`) -> NDV becomes `Exact(1)` - OR predicate (`a = 42 OR a = 22`) -> interval does not collapse, NDV stays `Inexact` - AND with mixed predicates (`a = 42 AND b > 10 AND c = 7`) -> `a` and `c` get `Exact(1)`, `b` stays `Inexact` - Equality with absent bounds (`a = 42`, no min/max) -> interval analysis still resolves to `Exact(1)` ## Are there any user-facing changes? No breaking changes. Statistics consumers will now see `Exact(1)` for `distinct_count` on columns constrained to a single value by filter predicates. Disclaimer: I used AI to assist in the code generation, I have manually reviewed the output and it matches my intention and understanding. --------- Co-authored-by: xudong.w <wxd963996380@gmail.com>
Dandandan
pushed a commit
to Dandandan/arrow-datafusion
that referenced
this pull request
Apr 8, 2026
…che#21077) ## Which issue does this PR close? - Part of apache#20766 Related: apache#20789 (uses NDV for equality filter selectivity, complementary - this PR improves the NDV output stats, that PR consumes them) ## Rationale for this change When a filter predicate collapses a column interval to a single value (e.g. `d_qoy = 1`), the output column can only have one distinct value. Currently `distinct_count` is always demoted to `Inexact`, losing this information. This matters for downstream optimizers that rely on `distinct_count`, such as join cardinality estimation in `estimate_inner_join_cardinality`. ## What changes are included in this PR? In `collect_new_statistics` (filter.rs), when the post-filter interval has `lower == upper` (both non-null), set `distinct_count` to `Precision::Exact(1)` instead of demoting the input NDV to `Inexact`. ## Are these changes tested? Yes, 4 unit tests: - Equality predicate (`a = 42`) -> NDV becomes `Exact(1)` - OR predicate (`a = 42 OR a = 22`) -> interval does not collapse, NDV stays `Inexact` - AND with mixed predicates (`a = 42 AND b > 10 AND c = 7`) -> `a` and `c` get `Exact(1)`, `b` stays `Inexact` - Equality with absent bounds (`a = 42`, no min/max) -> interval analysis still resolves to `Exact(1)` ## Are there any user-facing changes? No breaking changes. Statistics consumers will now see `Exact(1)` for `distinct_count` on columns constrained to a single value by filter predicates. Disclaimer: I used AI to assist in the code generation, I have manually reviewed the output and it matches my intention and understanding. --------- Co-authored-by: xudong.w <wxd963996380@gmail.com>
Rich-T-kid
pushed a commit
to Rich-T-kid/datafusion
that referenced
this pull request
Apr 21, 2026
## Which issue does this PR close? <!-- 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 apache#123` indicates that this PR will close issue apache#123. --> - Part of apache#20766 . ## Rationale for this change Used NDV for equality filter selectivity calculation. <!-- 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. --> ## What changes are included in this PR? Check if it is equality filter, and selectivity will be calculated using (1/NDV) <!-- 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. --> ## 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)? --> ## 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. --> --------- Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
Rich-T-kid
pushed a commit
to Rich-T-kid/datafusion
that referenced
this pull request
Apr 21, 2026
…che#21077) ## Which issue does this PR close? - Part of apache#20766 Related: apache#20789 (uses NDV for equality filter selectivity, complementary - this PR improves the NDV output stats, that PR consumes them) ## Rationale for this change When a filter predicate collapses a column interval to a single value (e.g. `d_qoy = 1`), the output column can only have one distinct value. Currently `distinct_count` is always demoted to `Inexact`, losing this information. This matters for downstream optimizers that rely on `distinct_count`, such as join cardinality estimation in `estimate_inner_join_cardinality`. ## What changes are included in this PR? In `collect_new_statistics` (filter.rs), when the post-filter interval has `lower == upper` (both non-null), set `distinct_count` to `Precision::Exact(1)` instead of demoting the input NDV to `Inexact`. ## Are these changes tested? Yes, 4 unit tests: - Equality predicate (`a = 42`) -> NDV becomes `Exact(1)` - OR predicate (`a = 42 OR a = 22`) -> interval does not collapse, NDV stays `Inexact` - AND with mixed predicates (`a = 42 AND b > 10 AND c = 7`) -> `a` and `c` get `Exact(1)`, `b` stays `Inexact` - Equality with absent bounds (`a = 42`, no min/max) -> interval analysis still resolves to `Exact(1)` ## Are there any user-facing changes? No breaking changes. Statistics consumers will now see `Exact(1)` for `distinct_count` on columns constrained to a single value by filter predicates. Disclaimer: I used AI to assist in the code generation, I have manually reviewed the output and it matches my intention and understanding. --------- Co-authored-by: xudong.w <wxd963996380@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
Used NDV for equality filter selectivity calculation.
What changes are included in this PR?
Check if it is equality filter, and selectivity will be calculated using (1/NDV)
Are these changes tested?
Are there any user-facing changes?