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



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/grouping.scala
##########
@@ -212,3 +212,27 @@ object GroupingID {
     if (SQLConf.get.integerGroupingIdEnabled) IntegerType else LongType
   }
 }
+
+
+object GroupingAnalytics {
+  def unapply(exprs: Seq[Expression])
+  : Option[(Seq[Seq[Expression]], Seq[Seq[Expression]], Seq[Expression])] = {
+    val (groupingSetExprs, others) = 
exprs.partition(_.isInstanceOf[GroupingSet])
+    if (groupingSetExprs.isEmpty) {
+      None
+    } else {
+      val groupingSets = groupingSetExprs.map(_.asInstanceOf[GroupingSet])
+      val groups = groupingSets.flatMap(_.groupByExprs) ++ others
+      val selectedGroupByExprs = groupingSets.map(_.selectedGroupByExprs)
+        .foldRight(Seq.empty[Seq[Expression]]) { (x, y) =>
+          if (y.isEmpty) {
+            x
+          } else {
+            for (a <- x; b <- y) yield b ++ a
+          }
+        }.map(others ++ _).map(_.distinct)

Review comment:
       ```
           }.map { g =>
             (others ++ g).distinct
           }
   ```
   ?




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