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

   ### What changes were proposed in this pull request?
   
   This PR fixes `PushPredicateThroughJoin` so that, for a filter over an inner 
join, it does not evaluate a filter condition that can fail, such as a division 
in ANSI mode, ahead of a throwable condition of the join, such as a `CASE WHEN` 
with `raise_error`.
   
   An inner join evaluates its condition before the filter above it, so the 
filter's conditions are written after the join's, as if at the end of its `ON` 
clause. #60646 (SPARK-61924) keeps a condition that can fail in place when a 
non-deterministic or throwable conjunct is written before it, in the same 
filter or in the same `ON` clause. But for a filter over an inner join, the 
rule split the filter's conditions without looking at the join condition:
   
   - A condition over one side was pushed below the join.
   - A condition over both sides was moved into the join condition, ahead of 
the join's own conjuncts (`newJoinConditions ++ joinCondition`).
   
   Either way, the condition was evaluated on rows that the throwable conjunct 
of the join condition rejects.
   
   This PR adds an `afterBarrier` parameter to `PredicateHelper.splitMovable`. 
When it is true, all the conjuncts are treated as written after a 
non-deterministic or throwable one, so only the ones that can not fail may 
move. `PushPredicateThroughJoin` sets it for a filter over an inner join whose 
condition has a non-deterministic or throwable conjunct. The filter's 
conditions that can fail then stay in the filter above the join.
   
   | Filter condition over an inner join with a throwable condition | Before | 
After |
   |---|---|---|
   | Can fail, over both sides, e.g. `10 / (t1.v - t2.g) < 100` | Moved into 
the join condition, ahead of it | Kept above the join |
   | Can fail, over one side, e.g. `10 / (t1.v - 9) < 100` | Pushed below the 
join | Kept above the join |
   | Can not fail, e.g. `t1.v > 9` or `t1.v = t2.g` | Pushed, or moved into the 
join condition | Same |
   
   The other join types are not changed. A conjunct that moves may still go 
ahead of the join condition, since with a throwable join condition only the 
ones that can not fail move.
   
   ### 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
   CREATE TABLE tc1(k INT, v INT, f INT) USING parquet;
   CREATE TABLE tc2(k INT, g INT) USING parquet;
   INSERT INTO tc1 VALUES (1, 9, 5);
   INSERT INTO tc2 VALUES (1, 9);
   
   -- Issue 1: It should return no row, but fails with DIVIDE_BY_ZERO.
   SELECT t1.k FROM tc1 t1 JOIN tc2 t2
     ON t1.k = t2.k AND CASE WHEN t1.f < 0 THEN raise_error('neg') ELSE t1.f > 
t2.g END
   WHERE 10 / (t1.v - t2.g) < 100;
   
   -- Issue 2: It should return no row, but fails with DIVIDE_BY_ZERO.
   SELECT t1.k FROM tc1 t1 JOIN tc2 t2
     ON t1.k = t2.k AND CASE WHEN t1.f < 0 THEN raise_error('neg') ELSE t1.f > 
t2.g END
   WHERE 10 / (t1.v - 9) < 100;
   ```
   
   The `CASE WHEN` is false on the only pair, since `5 > 9` is false, so the 
division by zero should never run. On `master`, the same conditions written in 
the `ON` clause, or in the `WHERE` clause of a comma join, return no row since 
#60646. In Issue 1, the optimized join condition is `((10 / (v - g) < 100) AND 
(k = k)) AND CASE WHEN ...`. In Issue 2, `10 / (v - 9) < 100` is pushed into 
the filter on `tc1`, below the join.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. The queries above succeed. I confirmed that both issues fail with 
`DIVIDE_BY_ZERO` in ANSI mode, with whole-stage codegen on and off, 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). There, the same conditions in the `ON` clause fail too, which 
#60646 fixes in the unreleased branches.
   
   A filter condition that can fail is no longer pushed below an inner join, or 
moved into its condition, when the join condition has a non-deterministic or 
throwable conjunct. For example, `10 / (t1.v - 9) < 100` in Issue 2 no longer 
filters `tc1` before the join. A filter condition that can not fail, and a join 
without such a conjunct, are not affected.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test cases. They fail without this PR.
   
   - `FilterPushdownSuite`: over an inner join whose condition has a throwable 
conjunct, an ANSI division over both sides and one over one side stay above the 
join. A comparison over one side is still pushed, and an equality over both 
sides is still moved into the join condition.
   - `DataFrameSuite`: both issues, written with the DataFrame API over 
`range`, return the rows the `CASE WHEN` keeps instead of failing with 
`DIVIDE_BY_ZERO`.
   
   I also ran the catalyst optimizer suites, `DataFrameSuite`, `JoinSuite` and 
the `PlanStabilitySuite`s locally. No golden files change. With a temporary 
suite (not included), I ran both issues, the same filter condition over the 
right side, the issue in a subquery, and a left outer join that 
`EliminateOuterJoin` makes inner, with whole-stage codegen on and off. All of 
them fail before this PR and succeed after it. The same conditions in the `ON` 
clause or with a comma join succeed before and after this PR.
   
   ### 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