Repository navigation
Prevent empty grouping sets from being eliminated on empty input - #22039
Merged
Merged
Conversation
kosiew
approved these changes
May 7, 2026
Contributor
There was a problem hiding this comment.
@xiedeyantu
Thanks for working on this.
Everything looks good overall. I just had one small suggestion around extending the test coverage a bit further.
| ---- | ||
| 55 | ||
|
|
||
| # grouping_sets_empty_input: GROUPING SETS (()) must produce one NULL row on empty input |
Contributor
There was a problem hiding this comment.
Nice addition to cover explicit GROUPING SETS(()). Since the optimizer change also protects ROLLUP and CUBE through has_empty_grouping_set, it would be great to add small empty-input regression cases for ROLLUP(v1) and CUBE(v1) as well. That would help lock in the full invariant covered by this change.
Member
Author
There was a problem hiding this comment.
Thanks for your review! I added tests for CUBE and ROLLUP.
Member
Author
There was a problem hiding this comment.
@kosiew If you have time, please take a look again. Thanks!
github-merge-queue
Bot
removed this pull request from the merge queue due to no response for status checks
May 11, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to no response for status checks
May 11, 2026
gstvg
pushed a commit
to gstvg/arrow-datafusion
that referenced
this pull request
May 14, 2026
…che#22039) ## Which issue does this PR close? - Closes apache#22040 ## Rationale for this change `GROUPING SETS (())` should return one row even when the input is empty, because the empty grouping set represents a global aggregate. In the current behavior, queries such as `WHERE false GROUP BY GROUPING SETS(())` can incorrectly be optimized into an empty result, returning 0 rows instead of the expected 1 row. This change fixes that SQL semantics bug. ## What changes are included in this PR? This PR makes two related fixes: 1. It updates the empty-relation propagation rule so aggregates with an empty grouping set are not eliminated. 2. It updates the grouped hash aggregate execution path so empty grouping sets are initialized correctly and still produce output on empty input. It also adds regression coverage in `grouping.slt` for: - `GROUPING SETS (())` on empty input - `COUNT(*)` on empty input - mixed grouping sets where only the empty grouping set should produce a row ## Are these changes tested? Yes. I ran: - `cargo test -p datafusion-sqllogictest -- grouping` - cargo test -p datafusion-optimizer -- propagate_empty - `cargo clippy --tests --all-features -p datafusion-physical-plan -p datafusion-optimizer -- -D warnings` - `cargo fmt --check` ## Are there any user-facing changes? Yes. Queries using `GROUPING SETS (())`, and other grouping-set forms that include the empty set such as `ROLLUP` and `CUBE`, will now return the correct row on empty input instead of incorrectly returning no rows.
This was referenced Jul 31, 2026
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
GROUPING SETS (())should return one row even when the input is empty, because the empty grouping set represents a global aggregate. In the current behavior, queries such asWHERE false GROUP BY GROUPING SETS(())can incorrectly be optimized into an empty result, returning 0 rows instead of the expected 1 row. This change fixes that SQL semantics bug.What changes are included in this PR?
This PR makes two related fixes:
It also adds regression coverage in
grouping.sltfor:GROUPING SETS (())on empty inputCOUNT(*)on empty inputAre these changes tested?
Yes. I ran:
cargo test -p datafusion-sqllogictest -- groupingcargo clippy --tests --all-features -p datafusion-physical-plan -p datafusion-optimizer -- -D warningscargo fmt --checkAre there any user-facing changes?
Yes. Queries using
GROUPING SETS (()), and other grouping-set forms that include the empty set such asROLLUPandCUBE, will now return the correct row on empty input instead of incorrectly returning no rows.