maropu commented on a change in pull request #30212:
URL: https://github.com/apache/spark/pull/30212#discussion_r547116200



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
##########
@@ -850,29 +850,62 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with 
SQLConfHelper with Logg
   }
 
   /**
-   * Add an [[Aggregate]] or [[GroupingSets]] to a logical plan.
+   * Add an [[Aggregate]] to a logical plan.
    */
   private def withAggregationClause(
       ctx: AggregationClauseContext,
       selectExpressions: Seq[NamedExpression],
       query: LogicalPlan): LogicalPlan = withOrigin(ctx) {
-    val groupByExpressions = expressionList(ctx.groupingExpressions)
-
-    if (ctx.GROUPING != null) {
-      // GROUP BY .... GROUPING SETS (...)
-      val selectedGroupByExprs =
-        ctx.groupingSet.asScala.map(_.expression.asScala.map(e => 
expression(e)).toSeq)
-      GroupingSets(selectedGroupByExprs.toSeq, groupByExpressions, query, 
selectExpressions)
-    } else {
-      // GROUP BY .... (WITH CUBE | WITH ROLLUP)?
-      val mappedGroupByExpressions = if (ctx.CUBE != null) {
-        Seq(Cube(groupByExpressions))
-      } else if (ctx.ROLLUP != null) {
-        Seq(Rollup(groupByExpressions))
+    if (ctx.groupingExpressionsWithGroupingAnalytics.isEmpty) {
+      val groupByExpressions = expressionList(ctx.groupingExpressions)
+      if (ctx.GROUPING != null) {
+        // GROUP BY .... GROUPING SETS (...)
+        val selectedGroupByExprs =
+          ctx.groupingSet.asScala.map(_.expression.asScala.map(e => 
expression(e)).toSeq)
+        Aggregate(Seq(GroupingSets(selectedGroupByExprs, groupByExpressions)),
+          selectExpressions, query)
       } else {
-        groupByExpressions
+        // GROUP BY .... (WITH CUBE | WITH ROLLUP)?
+        val mappedGroupByExpressions = if (ctx.CUBE != null) {
+          Seq(Cube(groupByExpressions.map(Seq(_))))
+        } else if (ctx.ROLLUP != null) {
+          Seq(Rollup(groupByExpressions.map(Seq(_))))
+        } else {
+          groupByExpressions
+        }
+        Aggregate(mappedGroupByExpressions, selectExpressions, query)
       }
-      Aggregate(mappedGroupByExpressions, selectExpressions, query)
+    } else {
+      val groupByExpressions =
+        ctx.groupingExpressionsWithGroupingAnalytics.asScala
+          .map(groupByExpr => {
+            val groupingAnalytics = groupByExpr.groupingAnalytics
+            if (groupingAnalytics != null) {
+              val selectedGroupByExprs = groupingAnalytics.groupingSet.asScala
+                .map(_.expression.asScala.map(e => expression(e)).toSeq)
+              if (groupingAnalytics.CUBE != null) {
+                // CUBE(A, B, (A, B), ()) is not supported.
+                if (selectedGroupByExprs.exists(_.isEmpty)) {
+                  throw new ParseException("Empty set in CUBE grouping sets is 
not supported.",
+                    groupingAnalytics)
+                }
+                Cube(selectedGroupByExprs)
+              } else if (groupingAnalytics.ROLLUP != null) {
+                // ROLLUP(A, B, (A, B), ()) is not supported.
+                if (selectedGroupByExprs.exists(_.isEmpty)) {
+                  throw new ParseException("Empty set in ROLLUP grouping sets 
is not supported.",
+                    groupingAnalytics)
+                }
+                Rollup(selectedGroupByExprs)
+              } else {
+                GroupingSets(selectedGroupByExprs, 
selectedGroupByExprs.flatten.distinct)

Review comment:
       Could you check `assert(groupingAnalytics.GROUPING != null)`?




----------------------------------------------------------------
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.

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



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

Reply via email to