fix: preserve groupless aggregate subquery cardinality - #25002
Closed
ryux1 wants to merge 2 commits into
Closed
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #25002 +/- ##
==========================================
- Coverage 81.69% 81.68% -0.01%
==========================================
Files 1126 1126
Lines 415193 415296 +103
Branches 415193 415296 +103
==========================================
+ Hits 339182 339252 +70
- Misses 56079 56082 +3
- Partials 19932 19962 +30 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
|
Hi @ryux1 I think @mohammadnaqvi04 was already planning to work on this and has a PR #25008 I will close this one. Please always coordinate with the original issue author before opening a PR to avoid duplicates. |
Contributor
Author
|
Understood—thanks for catching it. I missed #25008 when I checked for competing work. I’ll coordinate on the issue before starting similar work in DataFusion going forward. |
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?
EXISTS/INsubqueries with groupless aggregates hit the count bug #24960.Rationale for this change
A groupless aggregate always emits one row unless an operator such as
HAVINGorLIMIT 0removes it. CorrelatedEXISTSandINpredicates currently decorrelate that aggregate into semi/anti joins, which loses the empty-input aggregate row. As a result,EXISTSdrops outer rows without an inner match,INmisses comparisons against values such ascount(*) = 0, andNOT INcan fail planning with a two-column null-aware anti join.What changes are included in this PR?
EXISTSandNOT EXISTSaccording to that cardinality.INandNOT INto scalar-subquery comparisons so the existing scalar decorrelator preserves empty-input aggregate values.HAVING,LIMIT, grouped aggregates, and uncorrelated subqueries on their existing paths.What is the testing strategy for this PR?
The
issue_24960.sltregression file covers top-levelEXISTS,NOT EXISTS,IN, andNOT IN, plus embeddedEXISTS. It also verifies thatHAVINGandLIMIT 0are not treated as guaranteed-one-row cases.Plan-level optimizer tests directly cover all four predicate forms at top level and inside another predicate, plus projection, alias, and
HAVINGcardinality boundaries. Codecov reports zero missing changed lines in the optimizer implementation.Locally passing:
cargo test --test sqllogictests -- issue_24960.slt subquery.sltcargo test -p datafusion-optimizer --lib(779 passed)PATH=/home/ryu/.cargo/bin:$PATH ./dev/rust_lint.shcargo fmt --all -- --checkAre there any user-facing changes?
Yes. Correlated
EXISTSandINpredicates over groupless aggregates now preserve SQL aggregate cardinality and return the correct rows. There are no public API changes.Implementation and validation were completed with AI coding assistance under the account owner's direction.