[SPARK-57775][SQL][4.1] Return one row for GROUP BY GROUPING SETS (()) over empty input#57222
Open
uros-b wants to merge 1 commit into
Open
[SPARK-57775][SQL][4.1] Return one row for GROUP BY GROUPING SETS (()) over empty input#57222uros-b wants to merge 1 commit into
uros-b wants to merge 1 commit into
Conversation
dongjoon-hyun
requested changes
Jul 13, 2026
Member
There was a problem hiding this comment.
Although this is a bug fix, I'd be more conservative when we deliver this to Spark 4.1.4, @uros-b . Instead of forcefully enforcing the behavior change, can we simply deliver this feature with spark.sql.analyzer.lowerEmptyGroupingSetToGlobalAggregate.enabled=false?
Member
|
May I ask if you already deliver this bug fix to your company product in this way, @uros-b ? |
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?
Note: this is a 4.1 backport of:
Lower the single-empty-grouping-set case to a global
Aggregate(no grouping expressions, noExpand), so it returns one (grand total) row over empty input, matching theGROUP BY-less form and the SQL standard.grouping_id()folds to the constant0, andgrouping()/grouping_id()inHAVING/ORDER BYresolve against that constant.Unlike the master/4.2 change, which lives in
GroupingAnalyticsTransformer(introduced later by SPARK-54961), branch-4.1 has no such object: the grouping-analytics lowering is still inline inAnalyzer.ResolveGroupingAnalytics, so the fix is re-implemented there. branch-4.1 also has no single-passGroupingAnalyticsResolver(grouping analytics is not supported by the single-pass resolver on 4.1, guarded out inResolverGuard), so the fix lands only in the legacy fixed-point analyzer.Why are the changes needed?
GROUP BY GROUPING SETS (())is a grand total, semantically identical to an aggregation with noGROUP BYclause. It was lowered to a groupedAggregateover anExpand(grouping byspark_grouping_id), so over empty input it returned zero rows instead of one. The same defect affected the equivalent emptyGROUP BY CUBE()andGROUP BY ROLLUP(), which also lower to a single empty grouping set.Does this PR introduce any user-facing change?
Yes, the new (correct) behavior is gated by an internal SQL config,
spark.sql.analyzer.lowerEmptyGroupingSetToGlobalAggregate.enabled(default true). When set to false, lowering reverts to the legacyExpand-based form (zero rows over empty input). The flag gates both the transformer lowering and thegrouping_id()resolution inHAVING/ORDER BY, so the off state reproduces pre-fix behavior identically.How was this patch tested?
Tested via golden cases in
grouping_set.sql(empty-input grand total,grouping_id()in SELECT/HAVING/ORDER BY,grouping()rejection, non-empty input), flag-off coverage ingrouping_set_grand_total_disabled.sql,ResolveGroupingAnalyticsSuite,DataFrameAggregateSuite, and the ConnectClientE2ETestSuite.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Opus 4.8