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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeJoinCondition.scala:
##########
@@ -30,16 +30,24 @@ object OptimizeJoinCondition extends Rule[LogicalPlan] with 
PredicateHelper {
   override def apply(plan: LogicalPlan): LogicalPlan = 
plan.transformWithPruning(
     _.containsPattern(JOIN), ruleId) {
     case j @ Join(_, _, _, condition, _) if condition.nonEmpty =>
-      val newCondition = 
condition.map(_.transformWithPruning(_.containsPattern(OR), ruleId) {
-        case Or(EqualTo(l, r), And(IsNull(c1), IsNull(c2)))
-          if (l.semanticEquals(c1) && r.semanticEquals(c2))
-            || (l.semanticEquals(c2) && r.semanticEquals(c1)) =>
-          EqualNullSafe(l, r)
-        case Or(And(IsNull(c1), IsNull(c2)), EqualTo(l, r))
-          if (l.semanticEquals(c1) && r.semanticEquals(c2))
-            || (l.semanticEquals(c2) && r.semanticEquals(c1)) =>
-          EqualNullSafe(l, r)
-      })
+      val newCondition = condition.map(optimizeCondition)
       j.copy(condition = newCondition)
   }
+
+  // Rewriting the pattern to EqualNullSafe maps NULL to FALSE, so only 
recurse through And/Or.
+  private def optimizeCondition(condition: Expression): Expression = condition 
match {
+    case Or(EqualTo(l, r), And(IsNull(c1), IsNull(c2)))
+      if (l.semanticEquals(c1) && r.semanticEquals(c2))
+        || (l.semanticEquals(c2) && r.semanticEquals(c1)) =>
+      EqualNullSafe(l, r)
+    case Or(And(IsNull(c1), IsNull(c2)), EqualTo(l, r))
+      if (l.semanticEquals(c1) && r.semanticEquals(c2))
+        || (l.semanticEquals(c2) && r.semanticEquals(c1)) =>
+      EqualNullSafe(l, r)
+    case And(left, right) =>
+      And(optimizeCondition(left), optimizeCondition(right))

Review Comment:
   Please keep the OR tree-pattern guard and return the original AND/OR node 
when neither child changes. Without those checks, this rule now traverses and 
allocates a fresh Boolean tree for every unrelated join condition that the 
previous `transformWithPruning` skipped.



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