feat(stats): Set distinct_count to Exact(1) when filter narrows column to single value#21077
Open
asolimando wants to merge 1 commit intoapache:mainfrom
Open
feat(stats): Set distinct_count to Exact(1) when filter narrows column to single value#21077asolimando wants to merge 1 commit intoapache:mainfrom
asolimando wants to merge 1 commit intoapache:mainfrom
Conversation
When a filter predicate collapses a column's interval to a single value (e.g. d_qoy = 1), the output has exactly 1 distinct value. Previously the original Parquet NDV was propagated, inflating GROUP BY output estimates for CTE self-join patterns like Q31.
061df5a to
0613f1a
Compare
Member
Author
|
cc: @jonathanc-n |
adriangb
reviewed
Mar 20, 2026
| @@ -59,7 +59,7 @@ query TT | |||
| EXPLAIN SELECT * FROM test_table WHERE column1 = 1; | |||
Contributor
There was a problem hiding this comment.
Can we add some tests with e.g. floating point numbers, strings? I think it should work just the same but....
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?
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. Currentlydistinct_countis always demoted toInexact, losing this information.This matters for downstream optimizers that rely on
distinct_count, such as join cardinality estimation inestimate_inner_join_cardinality.What changes are included in this PR?
In
collect_new_statistics(filter.rs), when the post-filter interval haslower == upper(both non-null), setdistinct_counttoPrecision::Exact(1)instead of demoting the input NDV toInexact.Are these changes tested?
Yes, 4 unit tests:
a = 42) -> NDV becomesExact(1)a = 42 OR a = 22) -> interval does not collapse, NDV staysInexacta = 42 AND b > 10 AND c = 7) ->aandcgetExact(1),bstaysInexacta = 42, no min/max) -> interval analysis still resolves toExact(1)Are there any user-facing changes?
No breaking changes. Statistics consumers will now see
Exact(1)fordistinct_counton 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.