dongjoon-hyun opened a new pull request, #60765:
URL: https://github.com/apache/spark/pull/60765
### What changes were proposed in this pull request?
This PR fixes `PushPredicateThroughNonJoin` so that it does not push a
filter condition that can fail, such as a division or a cast in ANSI mode,
below a `Generate` without `outer` or a `ScriptTransformation`.
These operators can output no row for an input row, e.g. `explode` of an
empty array. Below them, a pushed condition is evaluated on the input rows that
they drop, which it never sees above them. #59944 (SPARK-60633) already keeps a
**throwable** condition, such as `raise_error` or `assert_true`, above these
operators, even when it is written first, with the `keepThrowableUp` flag of
`pushDownPredicate`. But `Expression.throwable` is opt-in metadata, and a
division, a cast and most other expressions that can fail in ANSI mode do not
set it, so they were still pushed.
This PR renames the flag to `keepFailableUp` and, under it, pushes only the
conditions that `ExprUtils.canEvaluateUnconditionally` accepts. This is the
check that `CombineFilters` and `PushPredicateThroughJoin` use for a condition
that can fail. It rejects a throwable condition too, so the behavior of
SPARK-60633 is kept. A condition that can not fail, such as `id > 5`, is still
pushed.
| Filter condition above a `Generate` without `outer` or a
`ScriptTransformation` | Before | After |
|---|---|---|
| Throwable, e.g. `assert_true(id != 3) IS NULL` | Kept above (SPARK-60633)
| Same |
| Can fail, e.g. `10L / (id - 3) > 0` or `CAST(s AS INT) > 0` | Pushed below
| Kept above |
| Can not fail, e.g. `id > 5` | Pushed below | Same, unless the conservative
check can not tell (see below) |
The other operators that this rule pushes a filter through generically, and
a `Generate` with `outer`, output at least one row for each input row, so they
are 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 or a cast is enough.
```sql
SET spark.sql.ansi.enabled=true;
-- Issue 1: It should return 6 rows (id 4 to 9), but fails with
DIVIDE_BY_ZERO.
SELECT * FROM (SELECT id, explode(if(id = 3, array(), array(1))) x FROM
range(0, 10, 1, 1))
WHERE 10L / (id - 3) > 0;
-- Issue 2: It should return 9 rows, but fails with CAST_INVALID_INPUT.
SELECT * FROM (
SELECT id, s, explode(if(id = 3, array(), array(1))) x
FROM (SELECT id, if(id = 3, 'a', '1') s FROM range(0, 10, 1, 1)))
WHERE CAST(s AS INT) > 0;
-- Issue 3: It should return no row, but fails with DIVIDE_BY_ZERO.
SELECT * FROM (
SELECT TRANSFORM(id) USING 'sed -n /NOMATCH/p' AS (x STRING) FROM range(0,
10, 1, 1))
WHERE 10L / (SELECT min(id) FROM range(0, 1)) > 0;
```
In Issues 1 and 2, `explode` outputs no row for `id = 3`, and in Issue 3,
the script outputs no row at all, so the condition should never run on those
rows. Before this PR, the condition is pushed below the operator, e.g. for
Issue 1:
```
*(1) Generate explode(if ((id#17L = 3)) [] else [1]), [id#17L], false, [x#18]
+- *(1) Filter ((10.0 / cast((id#17L - 3) as double)) > 0.0)
+- *(1) Range (0, 10, step=1, splits=1)
```
### Does this PR introduce _any_ user-facing change?
Yes. The queries above succeed. I confirmed that all three issues fail 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), and
on `master`. These releases do not have SPARK-60633, so there a throwable
condition is pushed below these operators too.
A filter condition that can fail is no longer pushed below a `Generate`
without `outer` or a `ScriptTransformation`, e.g. into a data source scan below
an `explode`. Most conditions that can not fail, such as `id > 5`, are not
affected. But the check of whether a condition can fail
(`ExprUtils.canEvaluateUnconditionally`) is conservative, so some of them also
stay above these operators now, such as `upper(s) = 'X'` over a `STRING` column
`s`.
### How was this patch tested?
Pass the CIs with the newly added test cases. They fail without this PR.
- `FilterPushdownSuite`: above a `Generate` without `outer`, an ANSI
division stays above it, while a comparison written after it is still pushed
below it.
- `DataFrameSuite`: Issue 1, written with the DataFrame API over `range`,
returns the rows that the generate keeps instead of failing with
`DIVIDE_BY_ZERO`, with whole-stage codegen on and off.
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 the three issues with whole-stage codegen on and off. All
of them fail before this PR and succeed after it, while `id > 5` above the same
generate is still pushed below 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]