LuciferYang commented on code in PR #60711:
URL: https://github.com/apache/spark/pull/60711#discussion_r4633374214


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/joins.scala:
##########
@@ -95,17 +95,36 @@ object ReorderJoin extends Rule[LogicalPlan] with 
PredicateHelper {
     }
   }
 
+  /**
+   * Returns true if the conditions that may not move (see `splitMovable`) are 
written in at most
+   * one join or filter. `groups` are the conditions of each join or filter, 
from the bottom up.
+   */
+  private def keptInOneGroup(
+      groups: Seq[Seq[Expression]],
+      input: Seq[(LogicalPlan, InnerLike)]): Boolean = {
+    val (_, kept) = splitMovableBy(
+      groups.zipWithIndex.flatMap { case (group, i) => group.map((_, i)) },
+      input.map(_._1))(_._1)
+    kept.map(_._2).distinct.size <= 1
+  }
+
   def apply(plan: LogicalPlan): LogicalPlan = plan.transformWithPruning(
     _.containsPattern(INNER_LIKE_JOIN), ruleId) {
-    case p @ ExtractFiltersAndInnerJoins(input, conditions)
-        if input.size > 2 && conditions.nonEmpty =>
+    // A join or filter removes a row on which its condition is NULL, so the 
conditions written
+    // above it are not evaluated on the row. In the filter that keeps the 
conditions that may not
+    // move above all the joins, `NULL AND x` evaluates `x`. So the joins are 
not reordered when
+    // the kept conditions come from more than one join or filter.
+    case p @ ExtractFiltersAndInnerJoins(input, conditionGroups)
+        if input.size > 2 && conditionGroups.nonEmpty &&
+          keptInOneGroup(conditionGroups, input) =>

Review Comment:
   `keptInOneGroup` only sees the matched chain. When the division after the 
lower `ON` clause's `CASE WHEN` is outside that chain (a `WHERE` or later `ON` 
clause of a rejected chain, a subquery, right-deep joins), the inner chain 
passes, the `CASE WHEN` is lifted into a `Filter`, and `CombineFilters` merges 
the division after it.
   
   With your data, the `WHERE` form (so not a separate issue), a four-way form 
dividing by `t1.k - t3.w`, and subquery and right-deep forms return 2 on 4.2.0 
but fail here with `DIVIDE_BY_ZERO`. With a `rand()` filter instead of the 
`CASE WHEN`, `(... WHERE rand(0) < 0.5) ab, c, d WHERE <keys> AND ab.amt + 
d.lim > 0` plans two `BroadcastNestedLoopJoin`s where master uses hash joins.
   
   Could `ReorderJoin` reorder only when every kept condition is in the top 
join or filter? That fixes these locally but also skips chains whose kept 
conditions all sit in a lower join or filter.



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