[SPARK-53565][SQL] Fix missing grand total row for ROLLUP/CUBE on runtime-empty tables#54938
Draft
xiaoxuandev wants to merge 1 commit intoapache:masterfrom
Draft
[SPARK-53565][SQL] Fix missing grand total row for ROLLUP/CUBE on runtime-empty tables#54938xiaoxuandev wants to merge 1 commit intoapache:masterfrom
xiaoxuandev wants to merge 1 commit intoapache:masterfrom
Conversation
402c388 to
ba512b8
Compare
…time-empty tables
### What changes were proposed in this pull request?
Add a new optimizer rule `SplitEmptyGroupingSet` that splits an Aggregate over Expand containing an empty grouping set into a Union of two branches:
1. Non-empty branch: Aggregate over Expand with only the non-empty grouping sets.
2. Grand total branch: a no-group Aggregate that always produces exactly one row.
SQL standard requires that the empty grouping set (grand total) in ROLLUP/CUBE/GROUPING SETS always produces one row, even when the input relation is empty at runtime. A no-group Aggregate guarantees this because SQL defines that an aggregate without GROUP BY on empty input returns one row.
The rule runs in a Once batch before PropagateEmptyRelation. PropagateEmptyRelation is simplified to always propagate empty through Expand nodes, since after the split, Expand never contains an empty grouping set.
### Why are the changes needed?
Without this fix, queries like:
SELECT a, count(*) FROM empty_table GROUP BY a WITH ROLLUP
incorrectly return 0 rows instead of the expected grand total row `Row(null, 0)`. This applies to both compile-time empty relations (WHERE FALSE) and runtime-empty tables (real tables with 0 rows).
### Does this PR introduce _any_ user-facing change?
Yes. ROLLUP/CUBE/GROUPING SETS queries on empty tables now correctly produce the grand total row.
### How was this patch tested?
- Unit tests in SplitEmptyGroupingSetSuite: Union split, no-split without empty set, only-empty-set case, grouping ID preservation.
- Unit test in PropagateEmptyRelationSuite: Expand on empty child is always eliminated.
- End-to-end tests in DataFrameAggregateSuite: compile-time empty, runtime-empty tables, HAVING, multi-column ROLLUP/CUBE, NOT NULL columns, multiple aggregate functions, non-empty data coexistence.
### Was this patch authored or co-authored using generative AI tooling?
Yes, co-authored with Kiro.
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.
What changes were proposed in this pull request?
Add a new optimizer rule
SplitEmptyGroupingSetthat splits an Aggregate over Expand containing an empty grouping set into a Union of two branches:SQL standard requires that the empty grouping set (grand total) in ROLLUP/CUBE/GROUPING SETS always produces one row, even when the input relation is empty at runtime. A no-group Aggregate guarantees this because SQL defines that an aggregate without GROUP BY on empty input returns one row.
The rule runs in a Once batch before PropagateEmptyRelation. PropagateEmptyRelation is simplified to always propagate empty through Expand nodes, since after the split, Expand never contains an empty grouping set.
Why are the changes needed?
Without this fix, queries like:
incorrectly return 0 rows instead of the expected grand total row
Row(null, 0). This applies to both compile-time empty relations (WHERE FALSE) and runtime-empty tables (real tables with 0 rows).Does this PR introduce any user-facing change?
Yes. ROLLUP/CUBE/GROUPING SETS queries on empty tables now correctly produce the grand total row when the empty grouping set is present.
How was this patch tested?
Was this patch authored or co-authored using generative AI tooling?
Yes, co-authored with Kiro.