dongjoon-hyun opened a new pull request, #60677:
URL: https://github.com/apache/spark/pull/60677
### What changes were proposed in this pull request?
This PR fixes `ReorderJoin` so that it does not move into the condition of a
join a throwable condition, such as a `CASE WHEN` with `raise_error`, nor a
condition that can fail, such as a division in ANSI mode, written after a
non-deterministic or throwable one.
This is the same change as #60649, which was merged without approval and
reverted, with an end-to-end test added for Issue 2 and for the query below,
which #60646 (SPARK-61924) now allows.
For three or more inner joins, `ReorderJoin` takes the conditions of the
joins and of the filters over them together, in their written order
(`ExtractFiltersAndInnerJoins`). `createOrderedJoin` then puts each condition
into the condition of the first join whose inputs cover its references. That
join can be below the filter or the join where the condition is written. A
throwable condition is then evaluated on rows that a join above removes, and a
condition written after a non-deterministic or throwable one is evaluated ahead
of it, on rows that the `AND` short-circuit would have kept from it. Since
#44716 (SPARK-46707), `PushPredicateThroughJoin` does not push a throwable
condition below a join or into its condition, and #60646 keeps a condition that
can fail in place after a non-deterministic or throwable one, but `ReorderJoin`
does neither.
This PR adds `conditionsKeptAbove`, which splits the conditions as #60646
does in `PushPredicateThroughJoin`. From the first non-deterministic or
throwable condition on, the conditions stay in the `Filter` above all the
joins, in their written order, except the ones that can not fail:
- `ExprUtils.canEvaluateUnconditionally` accepts it, e.g. a comparison of
columns and literals.
- It reads no column of the joined plans that
`PredicateHelper.failableOutput` reports.
`createOrderedJoin` puts the other conditions into the joins as before, and
it no longer chooses the next plan to join by a condition kept above the joins.
`StarSchemaDetection` only chooses the order of the joins, and
`CostBasedJoinReorder`, which runs after `ReorderJoin`, only takes the join
conditions, so they are not changed.
This PR relies on #60646. A condition that this PR keeps in the filter above
the joins can be followed by a condition that can fail, and #60646 keeps
`PushPredicateThroughJoin` from moving that one into the condition of a join,
ahead of it. For example, the following query returns 6 rows on master and with
this PR. Without #60646, it fails with `DIVIDE_BY_ZERO` with this PR.
```sql
SELECT * FROM range(0, 10, 1, 1) t
JOIN range(0, 10, 1, 1) r ON t.id = r.id
AND CASE WHEN t.id = 3 THEN false WHEN t.id < 0 THEN raise_error('neg')
ELSE true END
JOIN range(0, 10, 1, 1) s ON r.id = s.id AND 10L / (t.id + s.id - 6) > 0;
```
### Why are the changes needed?
A query that should succeed fails.
```sql
CREATE TABLE s USING parquet AS SELECT * FROM VALUES (-1, 10), (1, 20) AS
s(id, value);
-- Issue 1: It should return 1, but fails with USER_RAISED_EXCEPTION.
SELECT s.id FROM s
JOIN VALUES (10), (20) AS v(value) ON s.value = v.value
JOIN VALUES (20) AS w(value) ON s.value = w.value
WHERE CASE WHEN s.id > 0 THEN true ELSE raise_error('boom') END;
-- Issue 2: It should return no row, but fails with DIVIDE_BY_ZERO in ANSI
mode.
SELECT * FROM range(0, 10, 1, 1) t JOIN range(0, 10, 1, 1) r ON t.id = r.id
JOIN range(0, 10, 1, 1) s ON r.id = s.id
WHERE rand(10) * 10 > 100 AND 10L / (t.id - 3) > 0;
```
In Issue 1, the join with `w` removes the row whose `id` is `-1`, so
`raise_error` should not run on it. `ReorderJoin` puts the `CASE WHEN` into the
condition of the join with `v`, which keeps that row. The same failure happens
when the `CASE WHEN` is in the `ON` clause of the join with `w`. With the join
with `w` only, the query returns 1, since `PushPredicateThroughJoin` keeps the
`CASE WHEN` above the join.
In Issue 2, `rand(10) * 10 > 100` is false on every row, so the division
should never run for `t.id = 3`. `ReorderJoin` puts the division into the
condition of the join of `t` and `r`, and `PushPredicateThroughJoin` then
pushes it into `t`. With this PR, `ReorderJoin` keeps the division in the
filter above the joins, after `rand(10) * 10 > 100`, and #60646 keeps it there.
### Does this PR introduce _any_ user-facing change?
Yes. Issue 1 returns 1, and Issue 2 returns no row. I confirmed that Issue 1
fails with `USER_RAISED_EXCEPTION` on the released
[v4.2.1](https://github.com/apache/spark/releases/tag/v4.2.1) (2026-10-21) and
[v4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0) (2026-10-27),
where the query with the join with `w` only returns 1. In earlier releases,
`raise_error` is not throwable (#57975, SPARK-58627), so that query fails too.
Over three or more inner joins, a throwable condition, and a condition that
can fail written after a non-deterministic or throwable one, are now evaluated
in the `Filter` above all of them instead of in the condition of one of them.
This includes such a condition in the `ON` clause of one of the joins, since
`ReorderJoin` does not keep track of where a condition is written. So such a
condition no longer serves as a join key either. For example, in ANSI mode,
`t.id + 1 = r.id` written after `rand() < 0.5` no longer joins `t` and `r` by a
key, as with #60646 for a join of two. If such a condition is the only one
between two of the relations, they are now joined without a condition, so with
`spark.sql.crossJoin.enabled=false` the query fails with `Detected implicit
cartesian product`. A two-way join with a throwable condition in the `WHERE`
clause already fails in the same way.
### How was this patch tested?
Pass the CIs with the newly added test cases. Except for the query in the
first section, they fail without this PR.
- `JoinOptimizationSuite`: two throwable conditions over three inner joins
stay in the filter above the joins, in their written order. Without this PR,
the one over `x` goes into the condition of the first join and the one over `y`
and `z` into that of the last join.
- `JoinOptimizationSuite`: with `ReorderJoin` only, after `rand(0) > 0.1`, a
division and a condition over a column that can fail stay in the filter above
the joins, in their written order, while a comparison of columns and a division
written before `rand(0) > 0.1` go into the joins.
- `JoinOptimizationSuite`: `createOrderedJoin` joins the first relation with
the one that has a condition that goes into the join, not with the one whose
only condition is kept above the joins.
- `JoinSuite`: Issue 1 returns 1 instead of failing.
- `JoinSuite`: Issue 2 returns no row instead of failing, and the query in
the first section returns 6 rows. The latter returns 6 rows on master too, and
checks that this PR and #60646 work together.
I also ran the catalyst optimizer suites, the join suites and the
`PlanStabilitySuite`s in `sql/core`, and the join golden files of
`SQLQueryTestSuite` locally. No golden files change. With a temporary suite
(not included), I ran Issue 1 before and after this PR, along with the `CASE
WHEN` in the `ON` clause, a `CASE WHEN` over two of the relations, a comma
join, and a `raise_error` without column references. I ran each with
whole-stage codegen on and off, with star schema detection, and with
`spark.sql.crossJoin.enabled=false`. All of them fail before this PR and return
the right rows after it. I also ran Issue 2 and the query in the first section,
with whole-stage codegen on and off, on the master before #60646, with this PR,
with #60646, and with both. Only both return the right rows for all of them.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
--
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]