dongjoon-hyun opened a new pull request, #60646:
URL: https://github.com/apache/spark/pull/60646

   ### What changes were proposed in this pull request?
   
   This PR fixes `PushPredicateThroughJoin` so that it does not move a 
condition that can fail, such as a division in ANSI mode, ahead of a 
non-deterministic or throwable condition written before it.
   
   For a filter over a join, `PushPredicateThroughJoin` pushes a conjunct over 
one side into that side. For an inner join, it also moves a conjunct over both 
sides into the join condition. Either way, the conjunct is evaluated before the 
filter. Since #20069 (SPARK-22895), `split` moves every deterministic conjunct 
with `partition`, no matter where it is written, and #44716 (SPARK-46707) later 
kept a throwable one in place too. So a conjunct 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. This is the failure that #60157 
(SPARK-60980) fixed for `CombineFilters`, here reached through a join with a 
single filter.
   
   This PR splits the conjuncts with `span` instead, as `CombineFilters` does. 
After the first non-deterministic or throwable conjunct, a conjunct moves only 
when it can not fail:
   
   - `ExprUtils.canEvaluateUnconditionally` accepts it, e.g. a comparison of 
columns and literals.
   - It reads no column of either side that `PredicateHelper.failableOutput` 
(#60574, SPARK-61823) reports. An example is `q > 0`, where `q` is an alias of 
`10L / (id - 3)` in a `Project` below the join. Pushed into that side, the 
conjunct would then be pushed below the `Project` as `10L / (id - 3) > 0`.
   
   | Conjunct written after a non-deterministic or throwable one | Before | 
After |
   |---|---|---|
   | Can fail, e.g. `10L / (t.id - 3) > 0` | Moved | Kept in place |
   | Can not fail, over a column that can fail, e.g. `q > 0` | Moved | Kept in 
place |
   | Can not fail, over columns that can not fail, e.g. `t.id > 5` | Moved | 
Moved |
   
   The conjuncts kept in place stay in their written order. Before, a conjunct 
kept in the filter above an outer join, e.g. one over the right side of a left 
outer join, was placed ahead of the non-deterministic conjunct.
   
   Every case of the rule goes through `split`, so this covers:
   
   - A filter over a join of any type that the rule handles, and over an as-of 
join.
   - A filter over an inner join: a conjunct kept in place is not moved into 
the join condition either.
   - A join condition: a conjunct written after a throwable one stays in the 
condition. A join condition can not have a non-deterministic conjunct.
   - An external lookup join: `splitLookupJoin` decides it over all the 
conjuncts. A non-deterministic conjunct such as `rand() < 0.5` has no 
references, and it is set aside before `split` sees the others.
   
   ### Why are the changes needed?
   
   A query that should succeed fails. With ANSI mode, the default since Apache 
Spark 4.0.0, a division or a cast is enough.
   
   ```sql
   -- Issue 1: It should return no row, but fails with DIVIDE_BY_ZERO.
   SELECT * FROM range(0, 10, 1, 1) t JOIN range(0, 10, 1, 1) r ON t.id = r.id
   WHERE rand(10) * 10 > 100 AND 10L / (t.id - 3) > 0;
   
   -- Issue 2: It should return no row, but fails with DIVIDE_BY_ZERO.
   SELECT * FROM range(0, 10, 1, 1) t LEFT JOIN range(0, 10, 1, 1) r ON t.id = 
r.id
   WHERE rand(10) * 10 > 100 AND coalesce(10L / (r.id - 3), 1L) > 0;
   ```
   
   `rand(10) * 10 > 100` is false on every row, so the division should never 
run for `t.id = 3` or `r.id = 3`. In Issue 1, `PushPredicateThroughJoin` pushes 
`10L / (t.id - 3) > 0` into the left side, below the join. In Issue 2, it keeps 
the conjunct over the right side above the left outer join, but puts it ahead 
of `rand(10) * 10 > 100`. A throwable conjunct written first, such as a `CASE 
WHEN` with `raise_error`, causes the same failure.
   
   The other query reported in the review of #60631, with `q > 0` where `q` is 
an alias of `10L / (id - 3)` in the left side, still fails with the default 
configurations. There, `InferFiltersFromConstraints` adds `isnotnull(q)` ahead 
of `rand(10) * 10 > 100`, since `failableOutput` does not look through a join, 
and that filter is then pushed. This PR does not change `failableOutput`. With 
`spark.sql.constraintPropagation.enabled=false`, or with `q IS NULL` instead of 
`q > 0`, the query returns no row with this PR under whole-stage codegen.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. The queries above succeed. This is a long-standing issue since 
[v2.3.0](https://github.com/apache/spark/releases/tag/v2.3.0) (2018-02-22). I 
confirmed that both issues fail with `DIVIDE_BY_ZERO` 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) and [v4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0) 
(2026-10-27). It is easier to hit since 
[v4.0.0](https://github.com/apache/spark/releases/tag/v4.0.0) (2025-05-19), 
which enables ANSI mode by default.
   
   A conjunct written after a non-deterministic or throwable one is no longer 
pushed below a join or moved into its condition, unless it can not fail. For 
example, `rand() < 0.5 AND CAST(t.s AS INT) = 1` over a join keeps `CAST(t.s AS 
INT) = 1` above the join with `rand() < 0.5`, so it no longer prunes the 
partitions of `t`. Likewise, an equality over both sides of an inner join that 
can fail is no longer moved into the join condition. For example, `SELECT * 
FROM t, r WHERE rand() < 0.5 AND t.id + 1 = r.id` in ANSI mode is planned as a 
join without a condition and a filter above it, instead of an equi-join on 
`t.id + 1 = r.id`. A conjunct written before the non-deterministic or throwable 
one is not affected, nor is a condition written in the `ON` clause.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test cases. They fail without this PR.
   
   - `FilterPushdownSuite`: over an inner join, a division written after 
`rand(0) > 0.1`, and conditions over a column of either side that can fail, 
stay above the join. A condition over a column that can not fail, and a 
division written before `rand(0) > 0.1`, are pushed.
   - `PushPredicateThroughExternalLookupJoinSuite`: an ANSI addition over both 
sides written after a non-deterministic conjunct does not move into the 
condition of an inner lookup join. An existing case now writes its conjunct 
over both sides, an addition that can overflow in ANSI mode, before the 
throwing one, since it no longer moves when written after it.
   - `DataFrameSuite`: Issue 1, written with the DataFrame API, returns no row 
in ANSI mode instead of failing with `DIVIDE_BY_ZERO`.
   
   I also ran the catalyst optimizer suites, `DataFrameSuite`, the 
`PlanStabilitySuite`s, the external lookup join suites in `sql/core` and 
`SQLQueryTestSuite` locally. No golden files change. With a temporary suite 
(not included), I ran Issues 1 and 2 before and after this PR, along with right 
outer, left semi and as-of joins, a division over both sides of an inner join, 
and a throwable `CASE WHEN` with `raise_error` in the `WHERE` or the `ON` 
clause. I ran each with whole-stage codegen on and off. All of them fail before 
this PR and succeed after it. The query with `q IS NULL` above also fails 
before this PR, and returns no row after it under whole-stage codegen.
   
   ### 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]

Reply via email to