dongjoon-hyun opened a new pull request, #60782:
URL: https://github.com/apache/spark/pull/60782
### What changes were proposed in this pull request?
This PR fixes `ReorderJoin` so that a condition written above a filter
between inner joins is not evaluated on the rows that the filter removes
because its condition is NULL.
`ExtractFiltersAndInnerJoins` flattens the inner joins and the filters
between them into one list of plans and one list of conditions, and
`ReorderJoin` puts each condition into the first join that has its columns, in
the written order, combined with `AND`. A filter removes a row on which its
condition is NULL, so a condition written above the filter is not evaluated on
the row. When the filter condition and a condition above it read the same
plans, however, they go into the same join condition, where `NULL AND x`
evaluates `x`, and `x` can fail on the row, e.g. a division by zero in ANSI
mode.
This PR makes `ExtractFiltersAndInnerJoins`, whose only user is
`ReorderJoin`, return such a condition above the filter as `If(<filter
condition>, <condition>, false)`, which evaluates it only where the filter
condition is TRUE, wherever `ReorderJoin` or a later rule puts it. It does so
for a condition of a join or filter above a flattened filter that:
- only reads columns of the plans below the filter,
- can fail, i.e. `PredicateHelper.splitMovable` does not move it after a
barrier (it is not deterministic, `ExprUtils.canEvaluateUnconditionally`
rejects it, or it reads a column that can fail), and
- is above a filter whose condition can be NULL.
A condition that also reads a column of a plan joined above the filter, e.g.
a key of a join above it, is left as is, so it can still be a join key, and
`ReorderJoin` puts it into a join with that plan. A condition that reads no
column of the filter is left as is too, and can still be pushed below a join.
Above several such filters, a condition is guarded by each of them, the lowest
one outermost.
I also considered not flattening such a filter, as SPARK-62069 (#60711) does
for a lower join or filter with a throwable or non-deterministic condition.
`ReorderJoin` then still visits the joins below the top filter on their own,
without the condition above, and puts the filter condition into a join
condition there, and `PushPredicateThroughJoin` pushes the condition above into
the same join condition. With that change instead of this one, the query below
still fails.
### Why are the changes needed?
A query that should return no rows fails.
```scala
spark.conf.set("spark.sql.ansi.enabled", "true")
Seq((1, 5, 1, 1)).toDF("k", "f", "x", "v").write.saveAsTable("tb1")
Seq[(Int, Option[Int], Int)]((1, None, 1)).toDF("k", "g",
"w").write.saveAsTable("tb2")
Seq((1, 1)).toDF("k", "z").write.saveAsTable("tb3")
val (t1, t2, t3) = (spark.table("tb1"), spark.table("tb2"),
spark.table("tb3"))
// It should return no rows, but fails with DIVIDE_BY_ZERO.
t1.join(t2, t1("k") === t2("k")).where(t1("f") > t2("g") || t1("x") > 5)
.join(t3, t1("k") === t3("k"))
.where(lit(10) / (t1("v") - t2("w")) > 0)
.collect()
```
`t1.f > t2.g OR t1.x > 5` is NULL on the only row, since its `g` is NULL, so
the filter removes the row, and the division by zero for it (`t1.v - t2.w` is
`1 - 1`) is not evaluated. `ReorderJoin` puts `(k = k) AND ((f > g) OR (x > 5))
AND (10 / (v - w) > 0)` into the condition of the join of `tb1` and `tb2`,
which evaluates the division. The query fails in the same way with the division
in the `ON` clause of the join with `tb3`.
### Does this PR introduce _any_ user-facing change?
Yes. The query above returns no rows instead of failing. It fails in the
same way, with whole-stage codegen on and off, on
[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).
A condition that this PR guards stays in the condition of the join where the
filter condition goes, and is not pushed below it, e.g. a division that only
reads `tb1` above the filter is no longer pushed down to `tb1`.
### How was this patch tested?
Pass the CIs with the newly added test cases, which fail without this PR.
- `JoinOptimizationSuite`: `ReorderJoin` guards a division in the `ON`
clause above a lower filter whose condition can be NULL, which only reads
columns of the filter. It does not guard a condition that can not fail, a
division that also reads a column of the plan joined above the filter, or any
condition above a filter whose condition can not be NULL.
- `JoinSuite`: the query above returns no rows instead of failing with
`DIVIDE_BY_ZERO`.
I also ran the catalyst optimizer and planning suites, `JoinSuite` and the
`PlanStabilitySuite`s in `sql/core` locally. No golden files change. With a
temporary suite (not included), in ANSI mode with whole-stage codegen on and
off and in interpreted mode, the query above returns no rows, also with the
division in the `ON` clause of the join with `tb3`, with a division that also
reads `tb3`, and with a second filter between the joins whose condition is
NULL. With `CAST(tb1.s AS INT) = tb3.k` in the `ON` clause of the join with
`tb3`, it is still the key of that join, and a division that only reads `tb3`
is still pushed down to `tb3`.
These still fail, each because of a separate issue:
- With the joins of `tb1` and `tb2` and the filter in a subquery, the
subquery is not flattened, and `CombineFilters` merges the division, which is
pushed down into the subquery, into the filter as `<filter condition> AND
<division>`. #60771 fixes it.
- With the filter and the division as two stacked filters over all three
joins, e.g. `.join(t3, ...).where(<filter condition>).where(<division>)`,
`ReorderJoin` puts the condition of the lower filter, which is the top one of
the joins, into the join condition of `tb1` and `tb2`, and
`PushPredicateThroughJoin` pushes the division into the same join condition.
- With the condition that can be NULL in the `ON` clause of the lower join
instead of a filter, the division above it goes into the same join condition,
as `PushPredicateThroughJoin` does with 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]