allisonwang-db commented on a change in pull request #32089:
URL: https://github.com/apache/spark/pull/32089#discussion_r610808277



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala
##########
@@ -268,6 +268,15 @@ private[spark] object QueryCompilationErrors {
       s"(valid range is [1, $size])", t.origin.line, t.origin.startPosition)
   }
 
+  def groupByPositionRefersToAggregateFunctionError(
+      index: Int,
+      expr: Expression,
+      t: TreeNode[_]): Throwable = {
+    new AnalysisException(s"GROUP BY $index refers to an expression that is or 
contains " +
+      "an aggregate function. Aggregate functions are not allowed in GROUP BY, 
" +
+      s"but got ${expr.sql}", t.origin.line, t.origin.startPosition)
+  }
+
   def groupByPositionRangeError(index: Int, size: Int, t: TreeNode[_]): 
Throwable = {
     new AnalysisException(s"GROUP BY position $index is not in select list " +
       s"(valid range is [1, $size])", t.origin.line, t.origin.startPosition)

Review comment:
       ditto

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
##########
@@ -1816,7 +1816,13 @@ class Analyzer(override val catalogManager: 
CatalogManager)
         aggs: Seq[Expression]): Expression = expr match {
       case ordinal @ UnresolvedOrdinal(index) =>
         if (index > 0 && index <= aggs.size) {

Review comment:
       We can use `withPosition` here so the error doesn't need a TreeNode 
field as a parameter.
   ```scala
   withPosition(ordinal) {
     ...
     throw 
QueryCompilationErrors.groupByPositionRefersToAggregateFunctionError(index, 
ordinalExpr)
   }
   ```

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala
##########
@@ -268,6 +268,15 @@ private[spark] object QueryCompilationErrors {
       s"(valid range is [1, $size])", t.origin.line, t.origin.startPosition)
   }
 
+  def groupByPositionRefersToAggregateFunctionError(
+      index: Int,
+      expr: Expression,
+      t: TreeNode[_]): Throwable = {
+    new AnalysisException(s"GROUP BY $index refers to an expression that is or 
contains " +
+      "an aggregate function. Aggregate functions are not allowed in GROUP BY, 
" +
+      s"but got ${expr.sql}", t.origin.line, t.origin.startPosition)

Review comment:
       Instead of using `t.origin.line` and `t.origin.startPosition`, we can 
wrap the code with `withPosition` so we can remove `t` here (see comment in 
Anayzer).




-- 
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:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to