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



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala
##########
@@ -315,19 +314,28 @@ object PullupCorrelatedPredicates extends 
Rule[LogicalPlan] with PredicateHelper
       case ListQuery(sub, children, exprId, childOutputs, conditions) if 
children.nonEmpty =>
         val (newPlan, newCond) = pullOutCorrelatedPredicates(sub, outerPlans)
         ListQuery(newPlan, children, exprId, childOutputs, 
getJoinCondition(newCond, conditions))
+      case LateralSubquery(sub, children, exprId, conditions) if 
children.nonEmpty =>
+        val (newPlan, newCond) = decorrelate(sub, outerPlans)
+        LateralSubquery(newPlan, children, exprId, getJoinCondition(newCond, 
conditions))
     }
   }
 
   /**
    * Pull up the correlated predicates and rewrite all subqueries in an 
operator tree..
    */
   def apply(plan: LogicalPlan): LogicalPlan = plan.transformUpWithPruning(
-    _.containsAnyPattern(SCALAR_SUBQUERY, EXISTS_SUBQUERY, LIST_SUBQUERY)) {
+    _.containsPattern(PLAN_EXPRESSION)) {
     case f @ Filter(_, a: Aggregate) =>
       rewriteSubQueries(f, Seq(a, a.child))
-    // Only a few unary nodes (Project/Filter/Aggregate) can contain 
subqueries.
+    // Only a few unary nodes (Project/Filter/Aggregate/LateralJoin) can 
contain subqueries.
     case q: UnaryNode =>
-      rewriteSubQueries(q, q.children)
+      val newPlan = rewriteSubQueries(q, q.children)
+      // Preserve the original output of the node.

Review comment:
       After decorrelation, the right child can have domain attributes 
preserved which can change its output, for example
    
   // lateral subquery (before)
   Project [a, b]
   +- Filter [outer(c) = c]
         +- LocalRelation [a, b, c]
   
   // lateral subquery (after decorrelate)
   Project [a, b, c]
   +- LocalRelation [a, b, c]




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