bersprockets commented on code in PR #48627:
URL: https://github.com/apache/spark/pull/48627#discussion_r1853016997


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala:
##########
@@ -245,6 +266,55 @@ object RewritePredicateSubquery extends Rule[LogicalPlan] 
with PredicateHelper {
             condition = Some(newCondition)))
         }
       }
+    case a: Aggregate if 
exprsContainsAggregateInSubquery(a.aggregateExpressions) =>
+      // find expressions with an IN-subquery whose left-hand operand contains 
aggregates
+      val withInSubquery = 
a.aggregateExpressions.filter(exprContainsAggregateInSubquery(_))
+
+      // extract the aggregate expressions from withInSubquery
+      val inSubqueryMapping = withInSubquery.map { e =>
+        (e, extractAggregateExpressions(e))
+      }
+
+      val inSubqueryMap = inSubqueryMapping.toMap
+      // get all aggregate expressions found in left-hand operands of 
IN-subqueries
+      val aggregateExprs = inSubqueryMapping.flatMap(_._2)
+      // create aliases for each above aggregate expression
+      val aggregateExprAliases = aggregateExprs.map(a => Alias(a, 
toPrettySQL(a))())

Review Comment:
   > I have a question here: why this alias is needed?
   > 
   
   `aggregateExpressions` in `Aggregate` is defined as `Seq[NamedExpression]`. 
`Alias` is a `NamedExpression` but `AggregateExpression` is not. 
`aggregateExpressions` typically contains attributes (for pass-throughs) or 
aliases (for aggregate expressions).
   
   I add a comment in the code.



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

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