cloud-fan commented on code in PR #37074:
URL: https://github.com/apache/spark/pull/37074#discussion_r920154844


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -742,12 +742,28 @@ object ColumnPruning extends Rule[LogicalPlan] {
    * order, otherwise lower Projects can be missed.
    */
   private def removeProjectBeforeFilter(plan: LogicalPlan): LogicalPlan = plan 
transformUp {
-    case p1 @ Project(_, f @ Filter(_, p2 @ Project(_, child)))
+    case p1 @ Project(_, f @ Filter(e, p2 @ Project(_, child)))
       if p2.outputSet.subsetOf(child.outputSet) &&
         // We only remove attribute-only project.
-        p2.projectList.forall(_.isInstanceOf[AttributeReference]) =>
+        p2.projectList.forall(_.isInstanceOf[AttributeReference]) &&
+        !hasConflictingAttrsWithSubquery(e, child) =>
       p1.copy(child = f.copy(child = child))
   }
+
+  private def hasConflictingAttrsWithSubquery(predicate: Expression,
+    child: LogicalPlan): Boolean = {
+    def hasConflictingAttrs(s: SubqueryExpression): Boolean = {
+      s.references.intersect(child.outputSet).nonEmpty

Review Comment:
   I think the check here is incorrect. For the example query in the PR 
description: `exists#272 [(a#250 = a#266) && (b#251 = b#267) && (c#268 = 
c#268#277)]`
   
   The `e.references` of this EXISTS subquery is `[a#250, b#251, c#268]`, but 
what we want to check is `a#266`. I think we should get 
`subquery.joinCond.flatMap(_.references) -- subquery.references`.



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