You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "uros-b (via GitHub)" <gi...@apache.org> on 2026/07/14 07:07:47 UTC

[PR] [SPARK-57775][SQL][4.0] Return one row for GROUP BY GROUPING SETS (()) over empty input [spark]

uros-b opened a new pull request, #57244:
URL: https://github.com/apache/spark/pull/57244

   ### What changes were proposed in this pull request?
   
   Note: this is a 4.0 backport of https://github.com/apache/spark/pull/56891.
   
   Lower the single-empty-grouping-set case to a global `Aggregate` (no grouping expressions, no `Expand`), so it returns one (grand total) row over empty input, matching the `GROUP BY`-less form and the SQL standard. `grouping_id()` folds to the constant `0`, and `grouping()`/`grouping_id()` in `HAVING`/`ORDER BY` resolve 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 in `Analyzer.ResolveGroupingAnalytics`, so the fix is re-implemented there. branch-4.1 also has no single-pass `GroupingAnalyticsResolver` (grouping analytics is not supported by the single-pass resolver on 4.1, guarded out in `ResolverGuard`), 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 no `GROUP BY` clause. It was lowered to a grouped `Aggregate` over an `Expand` (grouping by `spark_grouping_id`), so over empty input it returned zero rows instead of one. The same defect affected the equivalent empty `GROUP BY CUBE()` and `GROUP 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 legacy `Expand`-based form (zero rows over empty input). The flag gates both the transformer lowering and the `grouping_id()` resolution in `HAVING`/`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 in `grouping_set_grand_total_disabled.sql`, `ResolveGroupingAnalyticsSuite`, `DataFrameAggregateSuite`, and the Connect `ClientE2ETestSuite`.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Opus 4.8
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-57775][SQL][4.0] Return one row for GROUP BY GROUPING SETS (()) over empty input [spark]

Posted by "uros-b (via GitHub)" <gi...@apache.org>.
uros-b closed pull request #57244: [SPARK-57775][SQL][4.0] Return one row for GROUP BY GROUPING SETS (()) over empty input
URL: https://github.com/apache/spark/pull/57244


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-57775][SQL][4.0] Return one row for GROUP BY GROUPING SETS (()) over empty input [spark]

Posted by "uros-b (via GitHub)" <gi...@apache.org>.
uros-b commented on PR #57244:
URL: https://github.com/apache/spark/pull/57244#issuecomment-4978950518

   Same as https://github.com/apache/spark/pull/57222#issuecomment-4978947658, closing this backport PR.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org