dongjoon-hyun opened a new pull request, #60760:
URL: https://github.com/apache/spark/pull/60760
### What changes were proposed in this pull request?
This PR fixes `PushPredicateThroughJoin` so that, for a filter over an inner
as-of join, or over an inner, left semi or left anti external lookup join, it
does not push a filter condition that can fail, such as a division in ANSI
mode, below the join when the join condition has a non-deterministic or
throwable conjunct, such as a `CASE WHEN` with `raise_error`.
#60725 (SPARK-62091) added the `afterBarrier` parameter of
`PredicateHelper.splitMovable` and sets it for a filter over an inner, left
semi or left anti `Join` whose condition has such a conjunct, so the filter's
conditions that can fail stay above the join. The rule has two other `Filter`
cases, which #60725 left out:
- `Filter` over `AsOfJoin` pushes the conditions over the left side below
the join. An inner as-of join drops the left rows that its condition gives no
match.
- `Filter` over `ExternalLookupJoin` pushes the conditions over the stream
side below the join, and, for an inner join, the ones over the lookup side onto
the lookup side and the ones over both sides into the join condition. An inner
or left semi lookup join drops the stream rows without a match, and a left anti
one drops the ones with a match.
Both split the filter's conditions without looking at the join condition, so
a condition that can fail was evaluated on rows that the throwable conjunct of
the join condition rejects.
This PR moves the check of #60725 into a helper, `hasBarrier(joinType,
condition)`, and uses it in the three `Filter` cases. `split` and
`splitLookupJoin` take an `afterBarrier` parameter and pass it to
`splitMovable`. The cases that push the join condition itself keep the default.
| Filter condition, with a throwable join condition | Before | After |
|---|---|---|
| Can fail, over the left side of an inner as-of join, e.g. `10 / (a.v - 9)
< 100` | Pushed below the join | Kept above the join |
| Can fail, over the stream side of an inner, left semi or left anti lookup
join, or over the lookup side of an inner one | Pushed below the join | Kept
above the join |
| Can fail, over both sides of an inner lookup join | Moved to the end of
the join condition | Kept above the join |
| Can not fail, e.g. `a.v > 9` | Pushed, or moved into the join condition |
Same, unless the conservative check can not tell |
| Any, over a left outer as-of or lookup join | Pushed below the join | Same
|
A left outer as-of or lookup join keeps every left or stream row, so it is
not changed.
### Why are the changes needed?
A query that should succeed fails. With ANSI mode, the default since Apache
Spark 4.0.0, a division is enough.
```sql
SET spark.sql.join.asofJoin.enabled=true;
CREATE TABLE ta(k INT, t INT, v INT, f INT) USING parquet;
CREATE TABLE tb(k INT, t INT, g INT) USING parquet;
INSERT INTO ta VALUES (1, 10, 9, 5);
INSERT INTO tb VALUES (1, 5, 9);
-- It should return no row, but fails with DIVIDE_BY_ZERO.
SELECT a.k FROM ta a ASOF JOIN tb b MATCH_CONDITION (a.t >= b.t)
ON a.k = b.k AND CASE WHEN a.f < 0 THEN raise_error('neg') ELSE a.f > b.g
END
WHERE 10 / (a.v - 9) < 100;
```
The `CASE WHEN` is false on the only pair, since `5 > 9` is false, so the
inner as-of join drops the only row of `ta`, and the division by zero should
never run. The same condition at the end of the `ON` clause returns no row.
Before this PR, `10 / (a.v - 9) < 100` is pushed into the filter on `ta`, below
the join.
The same happens for an external lookup join, e.g. with the
`SimpleExternalLookupDataSource` of `ExternalLookupJoinSuite`, whose lookup ids
are 1 and 2, over stream ids 1, 2 and 3:
```scala
val condition = stream("id") === lookup("id") &&
when(stream("id") < 0, raise_error(lit("neg"))).otherwise(stream("id") =!=
2)
// Inner and left semi joins: it should return the rows of id 1, but fails
with DIVIDE_BY_ZERO.
stream.join(lookup, condition, "inner").where(lit(10) / (stream("id") - 2) >
-100)
// Left anti join: it should return ids 2 and 3, but fails with
DIVIDE_BY_ZERO.
stream.join(lookup, condition, "left_anti").where(lit(10) / (stream("id") -
1) > -100)
```
### Does this PR introduce _any_ user-facing change?
No for the released versions. Both filter cases are not released yet: the
as-of one comes from #59702 (SPARK-60459) and the lookup one from #60198
(SPARK-60694), which are in `master`, `branch-4.x` and `branch-4.4`. On the
released [v4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0)
(2026-10-27), the as-of query above returns no row, with whole-stage codegen on
and off, since the rule does not push a filter through an as-of join there, and
there is no external lookup join.
In the unreleased branches, the queries above succeed. A filter condition
that can fail is no longer pushed below an inner as-of join, or below an inner,
left semi or left anti lookup join, or moved into the condition of an inner
lookup join, when the join condition has a non-deterministic or throwable
conjunct. A join without such a conjunct is not affected, and neither are most
filter conditions that can not fail. But the check of whether a condition can
fail (`ExprUtils.canEvaluateUnconditionally`) is conservative, so some of them
also stay above the join now, as for a `Join` since #60725.
### How was this patch tested?
Pass the CIs with the newly added test cases. They fail without this PR.
- `FilterPushdownSuite`: over an inner as-of join whose condition has a
throwable conjunct, an ANSI division over the left side stays above the join
while a comparison is pushed. Over a left outer as-of join, both are pushed.
- `PushPredicateThroughExternalLookupJoinSuite`: over an inner, left semi or
left anti lookup join whose condition has a throwing conjunct, an ANSI addition
over the stream side stays above the join while a comparison is pushed. Over a
left outer lookup join, both are pushed.
- `AsOfJoinSortMergeSQLSuite`: the as-of query above returns no row, with
the as-of join codegen off and on.
- `ExternalLookupJoinSuite`: the inner, left semi and left anti lookup joins
above return the rows that the join and the filter keep.
I also ran the catalyst optimizer suites, which include
`RewriteAsOfJoinSuite`, `ExternalLookupJoinSuite`, `AsOfJoinSQLSuite`,
`AsOfJoinSortMergeSQLSuite`, `DataFrameAsOfJoinSuite`,
`SortMergeAsOfJoinSuite`, `DataFrameSuite` and the `PlanStabilitySuite`s
locally. No golden files change. With a temporary suite (not included), I also
ran the as-of query with whole-stage codegen on and off, and through the
DataFrame API with `spark.sql.join.sortMergeAsOfJoin.enabled=true`. It fails
before this PR and returns no row after it, and the same condition at the end
of the `ON` clause returns no row before and after. With the division at the
end of the condition, the inner and left semi lookup joins return the same rows
before and after this PR as the filter forms above 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]