dongjoon-hyun opened a new pull request, #60711:
URL: https://github.com/apache/spark/pull/60711
### What changes were proposed in this pull request?
This PR fixes `ReorderJoin` so that a condition that it keeps in the
`Filter` above all the joins is not evaluated on the rows that a lower join
removes because its condition is NULL on them. This fixes a regression of
SPARK-61930 (#60677), which is only in the unreleased `master`, `branch-4.4`
and `branch-4.x`.
Since SPARK-61930, `ReorderJoin` keeps a throwable condition, and the
conditions that can fail written after it (`PredicateHelper.splitMovable`), in
one `Filter` above all the joins, in their written order, combined with `AND`.
They can come from the `ON` clauses of different joins. A join removes a row on
which its condition is NULL, so the condition of a join above it is not
evaluated on the row. In the `Filter`, however, `NULL AND x` evaluates `x`.
This PR keeps track of the join or filter that each condition is written in:
- `ExtractFiltersAndInnerJoins` returns the conditions of each join or
filter as a group, from the bottom up, instead of one list. `ReorderJoin` is
its only user.
- `PredicateHelper.splitMovableBy` splits items that each hold a conjunct,
as `splitMovable` splits conjuncts, so that `ReorderJoin` keeps the group of
each condition. `splitMovable` calls it, so its callers do not change.
- `ReorderJoin` wraps the kept conditions of each join or filter, except the
last one, as `coalesce(<their AND>, false)`. Its FALSE skips the conditions
after it, as the join or filter does. The kept conditions of one join or filter
stay in a plain `AND`, as they are written. So when all the kept conditions
come from one join or filter, as in the queries that SPARK-61930 fixed, the
plan does not change.
For the query below, the `Filter` above the joins changes as follows.
```
-- Before
Filter (CASE WHEN (f < 0) THEN cast(raise_error(...) as boolean) ELSE (f >
g) END AND ((10.0 / cast((k - w) as double)) > 0.0))
-- After
Filter (coalesce(CASE WHEN (f < 0) THEN cast(raise_error(...) as boolean)
ELSE (f > g) END, false) AND ((10.0 / cast((k - w) as double)) > 0.0))
```
I also considered two simpler options:
- Wrapping each kept condition except the last one, without the groups. It
changes the plans of the queries that SPARK-61930 fixed, and it skips a
condition after a NULL one in the same `ON` or `WHERE` clause, which `AND`
evaluates. So it can hide an error that the same condition raises over a join
of two.
- Not reordering the joins when a kept condition comes from a lower join.
Such a query would lose the reordering it gets in released versions, e.g. it
could keep a join without a condition that `ReorderJoin` replaces with joins
with conditions.
### Why are the changes needed?
A query that should succeed fails.
```sql
CREATE TABLE tb1(k INT, v INT, f INT) USING parquet;
CREATE TABLE tb2(k INT, g INT) USING parquet;
CREATE TABLE tb3(v INT, w INT) USING parquet;
INSERT INTO tb1 VALUES (1, 1, 5), (2, 2, 5);
INSERT INTO tb2 VALUES (1, NULL), (2, 1);
INSERT INTO tb3 VALUES (1, 1), (2, 0);
-- It should return 2, but fails with DIVIDE_BY_ZERO in ANSI mode.
SELECT t1.k FROM tb1 t1 JOIN tb2 t2 ON t1.k = t2.k AND
CASE WHEN t1.f < 0 THEN raise_error('neg') ELSE t1.f > t2.g END
JOIN tb3 t3 ON t1.v = t3.v AND 10 / (t1.k - t3.w) > 0;
```
The `CASE WHEN` is NULL on the row whose `k` is 1, since its `g` is NULL, so
the join with `t2` removes the row, and the division by zero for it (`t1.k -
t3.w` is `1 - 1`) is not evaluated. SPARK-61930 keeps the `CASE WHEN`, which is
throwable, and the division, which can fail and is written after it, in one
`Filter` above both joins, where `NULL AND <division>` evaluates the division.
### Does this PR introduce _any_ user-facing change?
Yes, within the unreleased `master`, `branch-4.4` and `branch-4.x`, because
SPARK-61930 is not released yet. The query above returns 2 instead of failing.
I confirmed that the query returns 2 in ANSI mode on the released
[v3.5.9](https://github.com/apache/spark/releases/tag/v3.5.9) (2026-07-12),
[v4.0.5](https://github.com/apache/spark/releases/tag/v4.0.5) (2026-10-21),
[v4.1.4](https://github.com/apache/spark/releases/tag/v4.1.4) (2026-10-21),
[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 `ReorderJoin` leaves the `CASE WHEN` in the condition of the join with
`t2`.
When the kept conditions come from more than one join or filter, the ones of
the lower joins or filters are now wrapped with `coalesce`, so they no longer
give inferred filters, such as `isnotnull(w)` from `10 / (k - w) > 0`, on the
inputs of the joins.
### How was this patch tested?
Pass the CIs with the newly added test cases, which fail without this PR.
- `JoinOptimizationSuite`: with `ReorderJoin` only, the kept conditions of
the lower join, a throwable condition and a division, are wrapped together as
`coalesce(<their AND>, false)`, ahead of the kept division of the upper join.
Without this PR, the three are in a plain `AND`.
- `JoinSuite`: the query above returns 2 instead of failing with
`DIVIDE_BY_ZERO`.
I also ran the catalyst optimizer suites, `JoinSuite` and the
`PlanStabilitySuite`s in `sql/core` locally. No golden files change. With a
temporary suite (not included), I ran the query above, the division in the
`WHERE` clause, the `CASE WHEN` in a filter between the joins, and a fourth
join with another division, each with whole-stage codegen on and off. All of
them return 2. A NULL `CASE WHEN` followed by the division in the same `ON`
clause still fails with `DIVIDE_BY_ZERO`, as it does over a join of two.
### 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]