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

   ### What changes were proposed in this pull request?
   
   This PR fixes `PushPredicateThroughNonJoin` so that it does not push a 
condition that can fail, such as a division in ANSI mode, below an operator 
ahead of a non-deterministic or throwable condition written before it.
   
   For a filter over an operator, `PushPredicateThroughNonJoin` pushes the 
conjuncts that the operator lets through below it, where they are evaluated 
before the filter. Since #20069 (SPARK-22895), it pushes every such 
deterministic conjunct, no matter where it is written. #59944 (SPARK-60633) 
added `splitPushable`, which keeps a throwable conjunct above when a conjunct 
written before it stays above, but it still pushes the other ones. So a 
conjunct written after a non-deterministic or throwable one that stays above 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) and #60631 (SPARK-61886) 
fixed for `CombineFilters`, and #60646 (SPARK-61924) for 
`PushPredicateThroughJoin`, here reached through the other operators.
   
   This PR changes `splitPushable`. Once a non-deterministic or throwable 
conjunct stays above, a conjunct written after it is pushed only when it can 
not fail:
   
   - `ExprUtils.canEvaluateUnconditionally` accepts it in the form it is 
evaluated below the operator. Below a `Project` or an `Aggregate`, that is with 
the aliases substituted, so `q > 0`, where `q` is an alias of `10L / (id - 3)`, 
can fail.
   - It reads no column of the child that `PredicateHelper.failableOutput` 
(#60574, SPARK-61823 and #60643, SPARK-61921) reports.
   
   This is the check that `PredicateHelper.splitMovable` makes for 
`PushPredicateThroughJoin` and, since #60692 (SPARK-62049), for 
`CombineFilters`.
   
   | Conjunct written after a non-deterministic or throwable one that stays 
above | Before | After |
   |---|---|---|
   | Can fail, e.g. `10L / (id - 3) > 0` | Pushed | Kept above |
   | Can not fail, over a column that can fail | Pushed | Kept above |
   | Can not fail, over columns that can not fail, e.g. `id > 3` | Pushed | 
Pushed |
   
   The conjuncts kept above stay in their written order. A conjunct written 
after a deterministic one that stays above, such as `c > 1` over the result of 
an aggregate, is still pushed unless it is throwable, as before. That follows 
`CombineFilters` and `PushPredicateThroughJoin`, which also keep the order only 
after a non-deterministic or throwable conjunct.
   
   `splitPushable` is used for a filter over a `Project`, an `Aggregate`, a 
`Window`, a `Union`, an `EventTimeWatermark` and the other operators of 
`canPushThrough`, so this covers all of them. Over a `Project`, the conjuncts 
kept above go to the projection split of #58403 (SPARK-55014), which could 
place a cheap one in its lowest layer, that is below the projection all the 
same. A cheap conjunct is not split off now, since it is only kept above for 
its order. Before this PR, the only cheap conjuncts kept above were throwable 
ones, which were not split off already.
   
   ### 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 (SELECT id, count(*) c FROM range(0, 10, 1, 1) GROUP BY id)
   WHERE rand(10) * 10 > 100 AND 10L / (id - 3) > 0;
   
   -- Issue 2: It should return no row, but fails with DIVIDE_BY_ZERO.
   SELECT * FROM (SELECT id FROM range(0, 10, 1, 1) UNION ALL SELECT id FROM 
range(0, 10, 1, 1))
   WHERE rand(10) * 10 > 100 AND 10L / (id - 3) > 0;
   ```
   
   `rand(10) * 10 > 100` is false on every row, so the division should never 
run for `id = 3`. `rand(10) * 10 > 100` stays above the `Aggregate` and the 
`Union`, as it is non-deterministic, but `PushPredicateThroughNonJoin` pushes 
`10L / (id - 3) > 0` below the `Aggregate` in Issue 1, and into both children 
of the `Union` in Issue 2. A `Window` partitioned by `id` and an `explode` fail 
the same way. So does a throwable conjunct written first, such as `CASE WHEN c 
> 100 THEN raise_error('boom') ELSE false END` over the result of the aggregate.
   
   This PR does not change how a column that can fail is computed. A `Union` 
and a `Generate` compute every column of their child on every row, so a query 
that reads such a column still fails, although the conjunct now stays above. An 
example is `q > 0` written after `rand(10) * 10 > 100`, where a child of a 
`Union` computes `q` as `10L / (id - 3)`.
   
   ### 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), [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), with whole-stage codegen on and off. 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 that stays 
above is no longer pushed below the operator, unless it can not fail. So such a 
conjunct no longer prunes partitions or is pushed down to the data source. For 
example, `rand(10) < 0.5 AND CAST(s AS INT) = 1` over an aggregate grouped by a 
string column `s` keeps `CAST(s AS INT) = 1` above the aggregate in ANSI mode. 
Then `rand(10) < 0.5` is evaluated on every group again, as written, instead of 
only on the group that the cast kept, so the rows it keeps can differ. A 
conjunct written before the non-deterministic or throwable one is not affected.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test cases. They fail without this PR.
   
   - `FilterPushdownSuite`:
     - Over an `Aggregate`, a condition over an alias of a division, written 
after `rand(10) > 0.5` or a throwable condition, stays above, and a condition 
over a grouping key is still pushed. A condition over a grouping key that the 
projection below computes with a division stays above too. After a 
deterministic condition that stays above, a condition over an alias of a 
division is still pushed.
     - Over a `Union`, a division written after `rand(10) > 0.5` stays above, 
and a condition over a column is still pushed into both children.
     - Over a `Project` with an expensive alias, a cheap division written after 
a non-deterministic condition over that alias is neither pushed nor split off 
into a lower layer, and a condition over a column is still pushed.
   - `DataFrameSuite`: Issues 1 and 2, written with the DataFrame API, return 
no row in ANSI mode with whole-stage codegen on and off, instead of failing 
with `DIVIDE_BY_ZERO`.
   
   I also ran the catalyst optimizer suites, `DataFrameSuite` and the 
`PlanStabilitySuite`s locally. No golden files change. With a temporary suite 
(not included), I ran Issues 1 and 2 before and after this PR, along with a 
`Window`, an `explode`, a `Project` with an expensive alias and the throwable 
conjunct above, with whole-stage codegen on and off. All of them fail before 
this PR and succeed 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]

Reply via email to