attilapiros commented on code in PR #48627:
URL: https://github.com/apache/spark/pull/48627#discussion_r1852972513
##########
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?
So instead of this:
```
!: +- Project [cast(c3#19 as bigint) AS c3#25L]
+- Join ExistenceJoin(exists#28), (sum(col2)#27L = c3#25L)
!: +- Project [c3#19]
:- Aggregate [sum(col2#21) AS sum(col2)#27L]
```
Can we just keep the old expression, like this:
```
!: +- Project [cast(c3#19 as bigint) AS c3#25L]
+- Join ExistenceJoin(exists#28), (sum(col2#21) = c3#25L)
!: +- Project [c3#19]
:- Aggregate [sum(col2#21)]
```
--
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]