spark-test commented on code in PR #60785:
URL: https://github.com/apache/spark/pull/60785#discussion_r4659295450
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/planning/patterns.scala:
##########
@@ -195,9 +195,16 @@ object ExtractEquiJoinKeys extends Logging with
PredicateHelper {
case Join(left, right, joinType, condition, hint) =>
logDebug(s"Considering join on: $condition")
// Find equi-join predicates that can be evaluated before the join, and
thus can be used
- // as join keys.
+ // as join keys. A join computes its keys on every row of its inputs,
ahead of the rest of
+ // its condition, so a conjunct that may not be evaluated ahead of the
others (see
+ // `splitMovable`), e.g. one that can fail written after a throwable
one, is not a key. It
+ // stays in the other condition, in its written order. Only the
condition decides it, not
+ // the columns of the inputs that can fail, so that the keys do not
change when the inputs
+ // do, e.g. when AQE replaces them with query stages:
`NormalizeFloatingNumbers` only
+ // normalizes the keys that it finds in the optimizer.
val predicates = condition.map(splitConjunctivePredicates).getOrElse(Nil)
- val joinKeys = predicates.flatMap {
+ val notMovable = splitMovable(predicates, Nil)._2
Review Comment:
**Blocking (P1):** `splitMovable(predicates, Nil)` on patterns.scala:206
returns the first throwable or non-deterministic conjunct itself as not
movable, so a cross-side equality that is itself throwable, because it contains
raise_error, assert_true, a JSON constructor with FORMAT JSON or sequence with
a step, never becomes a key, even when it is written first. For example, the
base branch plans `a JOIN b ON a.id = coalesce(b.id, raise_error('missing
id'))` as a hash or sort-merge join, while with this change it becomes a
CartesianProduct or BroadcastNestedLoopJoin, and the same condition in a
stream-stream join with no other key fails to start. Computing the first
conjunct as a key differs from evaluating it in written order only when the
other side is empty or all of its compared values are NULL, which is the corner
the PR already accepts for an equality that can fail written first, so this
plan regression is not needed for the fix, and the description does not state
it.
See **Shared repair plan 1** in the review body.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/planning/patterns.scala:
##########
@@ -195,9 +195,16 @@ object ExtractEquiJoinKeys extends Logging with
PredicateHelper {
case Join(left, right, joinType, condition, hint) =>
logDebug(s"Considering join on: $condition")
// Find equi-join predicates that can be evaluated before the join, and
thus can be used
- // as join keys.
+ // as join keys. A join computes its keys on every row of its inputs,
ahead of the rest of
+ // its condition, so a conjunct that may not be evaluated ahead of the
others (see
+ // `splitMovable`), e.g. one that can fail written after a throwable
one, is not a key. It
+ // stays in the other condition, in its written order. Only the
condition decides it, not
+ // the columns of the inputs that can fail, so that the keys do not
change when the inputs
+ // do, e.g. when AQE replaces them with query stages:
`NormalizeFloatingNumbers` only
+ // normalizes the keys that it finds in the optimizer.
val predicates = condition.map(splitConjunctivePredicates).getOrElse(Nil)
- val joinKeys = predicates.flatMap {
+ val notMovable = splitMovable(predicates, Nil)._2
+ val joinKeys = predicates.filterNot(notMovable.contains).flatMap {
Review Comment:
**Blocking (P1):** `predicates.filterNot(notMovable.contains)` on
patterns.scala:207, and `case p if notMovable.contains(p)` on
patterns.scala:226, compare conjuncts with `==`. When the same equality that
can fail is written before and after a throwable conjunct, as in `CAST(a.s AS
INT) = b.z AND CASE WHEN a.x < 0 THEN raise_error('neg') ELSE true END AND
CAST(a.s AS INT) = b.z`, splitMovable returns only the second copy as not
movable, but both copies leave the keys; BooleanSimplification removes a
duplicate only when both operands of one And are equal, so both copies survive
the optimizer. The base branch plans this join as a hash or sort-merge join on
the cast, while with this change it has no key and runs as a CartesianProduct
or BroadcastNestedLoopJoin, and a stream-stream join with no other key fails to
start, although the PR's table keeps an equality written first as a key. No
test covers that side of the rule: the only ExtractEquiJoinKeysSuite case and
every new end-to-end
test write the throwable conjunct first, so neither this drop nor one that
ignored the written order would fail a test.
See **Shared repair plan 1** in the review body.
##########
sql/core/src/test/scala/org/apache/spark/sql/JoinSuite.scala:
##########
@@ -2115,6 +2115,49 @@ class JoinSuite extends SharedSparkSession with
AdaptiveSparkPlanHelper
}
}
}
+
+ test("SPARK-62241: do not compute an equality that can fail after a
throwable join " +
+ "condition as a join key") {
+ withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
+ withTable("c1", "c3") {
+ Seq((5, "abc"), (1, "7")).toDF("x", "s").write.saveAsTable("c1")
+ Seq(7).toDF("z").write.saveAsTable("c3")
+ // The CASE WHEN is false on the row whose s is 'abc', so the cast,
which fails on it, is
+ // not evaluated on it. A join key would be computed on every row.
+ checkAnswer(
+ sql(
+ """SELECT c1.x, c3.z FROM c1 JOIN c3 ON
+ | CASE WHEN c1.x > 3 THEN false WHEN c1.x >= 0 THEN true ELSE
raise_error('k') END
+ | AND CAST(c1.s AS INT) = c3.z
+ |""".stripMargin),
+ Row(1, 7))
+ }
+ }
+ }
+
+ test("SPARK-62241: AQE keeps the join keys after a throwable join
condition") {
+ withSQLConf(
+ SQLConf.ANSI_ENABLED.key -> "true",
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.ADAPTIVE_AUTO_BROADCASTJOIN_THRESHOLD.key -> "10MB") {
+ withTable("c1", "c3") {
+ Seq((1, "7"), (3, "-0")).toDF("x", "s").write.saveAsTable("c1")
+ Seq((7, 1), (0, 3)).toDF("z", "w").write.saveAsTable("c3")
+ // AQE plans the sort-merge join again as a broadcast hash join once
its inputs are query
+ // stages. The equality on `q`, a cast computed below the join, must
have the same role
+ // there: as a key that `NormalizeFloatingNumbers` did not normalize,
-0.0 would not
+ // match 0.0.
+ checkAnswer(
Review Comment:
**Nit (P3):** This test checks only the answer, but a key that
NormalizeFloatingNumbers did not normalize changes the answer only through a
hash lookup on raw doubles: a sort-merge join compares doubles with
`SQLOrderingUtil.compareDoubles`, the shuffle hashes -0.0 as 0.0, and an
equality left in the other condition also matches -0.0 with 0.0. Whether AQE
converts the join into a broadcast hash join depends on
spark.sql.adaptive.autoBroadcastJoinThreshold and on DynamicJoinSelection, and
when it does not, the test passes even when the key it guards is not
normalized. Asserting that the initial plan is a sort-merge join and that the
final adaptive plan contains a BroadcastHashJoinExec would make it fail for the
regression it is named for; JoinSuite already mixes in AdaptiveSparkPlanHelper.
**Verification:**
- **Regression:** With the key extraction changed to consult the failable
columns of the join inputs, the JoinSuite test of AQE re-planning after a
throwable join condition fails, and with the PR's extraction it passes with a
broadcast hash join in its final plan.
--
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]