AngersZhuuuu commented on a change in pull request #30145:
URL: https://github.com/apache/spark/pull/30145#discussion_r608352609



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
##########
@@ -1787,16 +1787,41 @@ class Analyzer(override val catalogManager: 
CatalogManager)
       // Replace the index with the corresponding expression in 
aggregateExpressions. The index is
       // a 1-base position of aggregateExpressions, which is output columns 
(select expression)
       case Aggregate(groups, aggs, child) if aggs.forall(_.resolved) &&
-        groups.exists(_.isInstanceOf[UnresolvedOrdinal]) =>
-        val newGroups = groups.map {
-          case u @ UnresolvedOrdinal(index) if index > 0 && index <= aggs.size 
=>
-            aggs(index - 1)
-          case ordinal @ UnresolvedOrdinal(index) =>
-            throw QueryCompilationErrors.groupByPositionRangeError(index, 
aggs.size, ordinal)
-          case o => o
-        }
+        groups.exists(containUnresolvedOrdinal) =>
+        val newGroups = groups.map((resolveGroupByExpressionOrdinal(_, aggs)))
         Aggregate(newGroups, aggs, child)
     }
+
+    private def containUnresolvedOrdinal(e: Expression): Boolean = e match {
+      case _: UnresolvedOrdinal => true
+      case Cube(_, groupByExprs) => 
groupByExprs.exists(containUnresolvedOrdinal)
+      case Rollup(_, groupByExprs) => 
groupByExprs.exists(containUnresolvedOrdinal)
+      case GroupingSets(_, flatGroupingSets, groupByExprs) =>
+        flatGroupingSets.exists(containUnresolvedOrdinal) ||
+          groupByExprs.exists(containUnresolvedOrdinal)

Review comment:
       > Can we simply do `case a: GroupingAnalytic a.children.exists...`?
   
   Done




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