dongjoon-hyun commented on code in PR #60711:
URL: https://github.com/apache/spark/pull/60711#discussion_r4633918044


##########
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:
   Thank you. I confirmed all of them with your data. The `WHERE` form, the 
four-way form, the subquery forms and the right-deep form return 2 on v4.2.1 
and v4.3.0, but fail with `DIVIDE_BY_ZERO` on master and with the previous 
commit, with whole-stage codegen on and off and in interpreted mode. With a 
`SELECT *` subquery, the `rand(0)` form planned two `BroadcastNestedLoopJoin`s 
where master and v4.3.0 use hash joins. I was wrong to call the `WHERE` form a 
separate issue.
   
   I took your suggestion: `ReorderJoin` now reorders the joins only when the 
conditions written below the top join or filter may all move, so the filter 
that keeps the rest takes the place of the top join or filter. 
`ExtractFiltersAndInnerJoins` and `splitMovable` are back as on master. All the 
forms above return 2, the `rand(0)` form uses hash joins again, and the queries 
of the earlier review still return the rows that the joins as written produce. 
The new tests cover the `WHERE` form and a non-deterministic filter below a 
join without a condition.
   
   For the chains that are no longer reordered, I tried comma joins written in 
an order without a join key between the first two, with a `raise_error` in the 
lower `ON` clause or a `rand()` filter between the joins. With `SELECT *` they 
still use hash joins, since the keys inferred from the `WHERE` clause connect 
them, and with fewer columns column pruning puts a `Project` above the lower 
join, which ends the chain, so they are reordered as before.
   



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