spark-test commented on code in PR #60649:
URL: https://github.com/apache/spark/pull/60649#discussion_r4611197108
##########
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:
**Nit (P3):** This partition now leaves `keptAbove` out of the join, but the
search above it (joins.scala:99-110) still counts a kept condition as a link
between `left` and a candidate. For `FROM x, y, z WHERE T AND y.d = z.a AND x.b
= z.b`, with `T` throwable over x and y, the search picks y through `T`, and
this partition then builds `Join(x, y, Inner, None)`, although z had `x.b =
z.b`. That contradicts the method doc ("prefer those has at least one join
condition") and the new `@param keptAbove`. The optimized plan is still right
only because transformDown applies ReorderJoin again to the joins under the
Filter, where the kept conditions are absent. Skipping `keptAbove` in the
search's link test would make the method's own result match its doc.
**Verification:**
- **Behavior:** A plan test calls createOrderedJoin on three relations whose
first link from the leading relation is a kept throwable condition, while the
third relation has a condition that can go into the join, and expects the
leading relation joined first with the third on that condition, then with the
second, with the kept condition only in the Filter above.
##########
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:
**Nit (P3):** `comparePlans` normalizes both plans with `NormalizePlan`,
which sorts each Filter's conjuncts by hash code (NormalizePlan.scala:190-196),
so this check and the one at JoinOptimizationSuite.scala:148 pass whatever
order the kept conditions have in the Filter above the joins. That order is
what the change relies on: the Filter's `And` short-circuits, so `divide(b, f)
> 1` listed ahead of `Rand(0) > 0.1` runs on every joined row, and once #60646
lands the order is also what keeps it behind `Rand(0) > 0.1`.
FilterPushdownSuite checks this property directly with
`assertTopFilterConjuncts` for the same reason
(FilterPushdownSuite.scala:866-871); checking the top Filter's conjuncts in
written order in both new tests would catch such a reorder.
**Verification:**
- **Regression:** The join reorder plan tests check the conjuncts of the
Filter above the joins in written order, Rand(0) > 0.1 then divide(b, f) > 1
then q > 0 in one and the two raise_error conditions in the other, besides the
normalized plan comparison, so a change that reorders the kept conditions fails
them.
--
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]