dongjoon-hyun opened a new pull request, #60649:
URL: https://github.com/apache/spark/pull/60649
### What changes were proposed in this pull request?
This PR fixes `ReorderJoin` so that it does not move a throwable condition,
such as a `CASE WHEN` with `raise_error`, into the condition of a join.
For three or more inner joins, `ReorderJoin` takes the conditions of the
joins and of the filters over them together (`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, so the condition is evaluated on rows
that a join above removes. Since #44716 (SPARK-46707),
`PushPredicateThroughJoin` does not push a throwable condition below a join or
into its condition, but `ReorderJoin` ignores `Expression.throwable`.
This PR adds `canBeJoinCondition`, which is `canEvaluateWithinJoin` that
also rejects a throwable condition, and uses it in both cases of
`createOrderedJoin`. A throwable condition stays in the `Filter` above all the
joins, as `PushPredicateThroughJoin` keeps it in the filter above a join.
`StarSchemaDetection` only chooses the order of the joins, and
`CostBasedJoinReorder`, which runs after `ReorderJoin`, only takes the join
conditions, so they are not changed.
### 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);
-- 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;
```
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.
### Does this PR introduce _any_ user-facing change?
Yes. The query above returns 1. I confirmed that it 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 is now evaluated in
the `Filter` above all of them instead of in the condition of one of them. This
includes a throwable condition in the `ON` clause of one of the joins, since
`ReorderJoin` does not keep track of where a condition is written. 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 such a
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. They fail without this PR.
- `JoinOptimizationSuite`: two throwable conditions over three inner joins
stay in the filter above the joins. 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.
- `JoinSuite`: the query above returns 1 instead of failing.
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 the query above before and after this PR, along with the
`CASE WHEN` in the `ON` clause, a `CASE WHEN` over both `s` and `v`, 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.
### 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]