You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2021/07/14 23:48:37 UTC
[GitHub] [spark] allisonwang-db commented on a change in pull request #33284: [SPARK-36063][SQL] Optimize OneRowRelation subqueries
allisonwang-db commented on a change in pull request #33284:
URL: https://github.com/apache/spark/pull/33284#discussion_r670026456
##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala
##########
@@ -701,3 +702,43 @@ object RewriteLateralSubquery extends Rule[LogicalPlan] {
Join(left, newRight, joinType, newCond, JoinHint.NONE)
}
}
+
+/**
+ * This rule optimizes subqueries with OneRowRelation as leaf nodes.
+ */
+object OptimizeOneRowRelationSubquery extends Rule[LogicalPlan] {
+
+ object OneRowSubquery {
+ def unapply(plan: LogicalPlan): Option[Seq[NamedExpression]] = {
+ CollapseProject(EliminateSubqueryAliases(plan)) match {
+ case Project(projectList, _: OneRowRelation) => Some(stripOuterReferences(projectList))
+ case _ => None
+ }
+ }
+ }
+
+ /**
+ * Rewrite a subquery expression into one or more expressions. The rewrite can only be done
+ * if there is no nested subqueries in the subquery plan.
+ */
+ private def rewrite(plan: LogicalPlan): LogicalPlan = plan.transformUpWithSubqueries {
Review comment:
The reason why we need to check subqueries is to deal with nested subqueries:
```
Project [scalar-subquery [a]]
: +- Project [scalar-subquery [b]] <-- collapsible if transform with nested subqueries first
: : +- Project [outer(b) + 1]
: : +- OneRowRelation
: +- Project [outer(a) as b]
: +- OneRowRelation
+- Relation [a]
```
A subquery's plan should only be rewritten if it doesn't contain another correlated subquery. If we do not transform the nested subqueries first, we will miss out cases like the one above.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org