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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/joins.scala:
##########
@@ -85,30 +110,31 @@ object ReorderJoin extends Rule[LogicalPlan] with 
PredicateHelper {
       val (right, innerJoinType) = conditionalJoin.getOrElse(rest.head)
 
       val joinedRefs = left.outputSet ++ right.outputSet
-      val (joinConditions, others) = conditions.partition(
-        e => e.references.subsetOf(joinedRefs) && canEvaluateWithinJoin(e))
+      val (joinConditions, others) = conditions.partition(e => 
e.references.subsetOf(joinedRefs) &&

Review Comment:
   Thank you. The search now skips `keptAbove` too, so `createOrderedJoin` 
itself prefers a plan with a condition that goes into the join. I added a plan 
test that calls `createOrderedJoin` directly, since `ReorderJoin` reaches the 
same plan when it runs again on the joins below the filter.
   



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/JoinOptimizationSuite.scala:
##########
@@ -126,4 +126,46 @@ class JoinOptimizationSuite extends PlanTest {
       comparePlans(optimized, queryAnswerPair._2.analyze)
     }
   }
+
+  test("SPARK-61930: reorder inner joins without moving a throwable condition 
into a join") {
+    val x = testRelation.subquery("x")
+    val y = testRelation1.subquery("y")
+    val z = testRelation.subquery("z")
+
+    // A throwable condition stays above the joins, so that it is not 
evaluated on the rows that
+    // a join removes. The first one could go into the condition of the first 
join, and the
+    // second one into that of the last join.
+    def raiseUnless(cond: Expression): Expression =
+      If(cond, Literal(true), IsNull(RaiseError(Literal("boom"))))
+    val throwables = raiseUnless("x.a".attr > 0) && raiseUnless("y.d".attr === 
"z.c".attr)
+    val query = x.join(y).join(z)
+      .where(("x.b".attr === "z.b".attr) && ("y.d".attr === "z.a".attr) && 
throwables)
+    val answer = x.join(z, condition = Some("x.b".attr === "z.b".attr))
+      .join(y, condition = Some("y.d".attr === "z.a".attr))
+      .where(throwables)
+      .select(Seq("x.a", "x.b", "x.c", "y.d", "z.a", "z.b", 
"z.c").map(_.attr): _*)
+
+    comparePlans(Optimize.execute(query.analyze), answer.analyze)
+  }
+
+  test("SPARK-61930: reorder inner joins without moving a condition that can 
fail ahead of a " +
+    "non-deterministic one") {
+    // The divisions are built in ANSI mode, where they can fail, and so can 
`q`, which one of them
+    // computes. After `rand(0) > 0.1`, a condition that can fail by itself or 
reads `q` stays above
+    // the joins, while one that can not fail goes into a join. So does a 
division written before
+    // `rand(0) > 0.1`. Only ReorderJoin runs, so that this does not depend on
+    // PushPredicateThroughJoin.
+    def divide(l: Expression, r: Expression): Expression =
+      Divide(l, r, NumericEvalContext(EvalMode.ANSI))
+    val x = testRelation.select($"a", $"b", divide($"a", $"b").as("q"))
+    val z = LocalRelation($"e".int, $"f".int)
+    val query = x.join(testRelation1).join(z).where($"a" === $"d" && $"d" === 
$"e" &&
+      divide($"b", $"d") > 1 && Rand(0) > 0.1 && divide($"b", $"f") > 1 && 
$"q" > 0 &&
+      $"b" > $"f")
+    val answer = x.join(testRelation1, condition = Some($"a" === $"d" && 
divide($"b", $"d") > 1))
+      .join(z, condition = Some($"d" === $"e" && $"b" > $"f"))
+      .where(Rand(0) > 0.1 && divide($"b", $"f") > 1 && $"q" > 0)
+
+    comparePlans(ReorderJoin(query.analyze), answer.analyze)

Review Comment:
   Thank you. Both plan tests now also check the conjuncts of the top filter in 
their written order, as `assertTopFilterConjuncts` does in 
`FilterPushdownSuite`. Reversing their order in the filter now fails both tests.
   



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