dongjoon-hyun opened a new pull request, #60631:
URL: https://github.com/apache/spark/pull/60631
### What changes were proposed in this pull request?
This PR fixes `CombineFilters` so that it does not move a condition over a
column that can fail ahead of a non-deterministic or throwable condition
written before it. An example is `q > 0`, where `q` is an alias of `10L / (id -
3)` in ANSI mode.
Since #60157 (SPARK-60980), `CombineFilters` combines a conjunct of the
outer filter written after a non-deterministic or throwable one with the filter
below only when `ExprUtils.canEvaluateUnconditionally` accepts it. That check
looks at the conjunct alone. `q > 0` compares a column with a literal, so it
passes. However, when `q` is a column of a `Project` below that is computed
with an expression that can fail, the conjunct can fail as that expression does:
- `PushPredicateThroughNonJoin` pushes the combined filter below the
`Project` with `q` replaced by `10L / (id - 3)`.
- When the filter is not pushed, e.g. because the `Project` has a
non-deterministic column, whole-stage codegen still computes `q` for a row only
when a filter above first reads it. So the combined filter computes `q` ahead
of the non-deterministic conjunct.
This PR also requires such a conjunct not to read a column that can fail.
The columns come from `PredicateHelper.failableOutput`, which #60574
(SPARK-61823) added for `InferFiltersFromConstraints`, applied to the child of
the filter below:
- A deterministic column of a `Project` whose expression
`ExprUtils.canEvaluateUnconditionally` rejects, or that reads such a column of
the `Project`'s child.
- `Filter` and `Sample` pass the columns of their child through. Other
operators are not looked through.
- A non-deterministic column is not counted. `ProjectExec` computes it on
every row, and no filter is pushed through a `Project` with one.
The columns are collected only for a conjunct written after a
non-deterministic or throwable one that `ExprUtils.canEvaluateUnconditionally`
accepts.
| Outer conjunct written after a non-deterministic or throwable one | Before
| After |
|---|---|---|
| Can fail, e.g. `10L / (id - 3) > 0` | Kept above | Kept above |
| Can not fail, over columns that can not fail, e.g. `id = 1` | Combined |
Combined |
| Can not fail, over a column that can fail, e.g. `q > 0` | Combined | Kept
above |
### 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
-- It should return no row, but fails with DIVIDE_BY_ZERO.
SELECT id, q FROM (
SELECT * FROM (SELECT id, 10L / (id - 3) AS q FROM range(0, 10, 1, 1))
WHERE id >= 0)
WHERE rand(10) * 10 > 100 AND q > 0;
```
`rand(10) * 10 > 100` is false on every row, so `q` should never be computed
for `id = 3`. With the inner `WHERE id >= 0`, `CombineFilters` moves `q > 0`
into the inner filter, ahead of `rand(10) * 10 > 100`. The combined filter is
then pushed below the projection as `10L / (id - 3) > 0`.
### Does this PR introduce _any_ user-facing change?
Yes. The query above succeeds. This is a long-standing issue since
[v3.2.0](https://github.com/apache/spark/releases/tag/v3.2.0) (2021-10-06). 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. SPARK-60980, which is not released yet,
fixed the same failure for a conjunct that can fail by itself, but not for one
over a column that can fail.
A conjunct over a column that can fail is no longer combined with the filter
below when a non-deterministic or throwable conjunct is written before it. For
example, with `p` an alias of `CAST(s AS INT)` in ANSI mode, `rand() < 0.5 AND
p = 1` over another filter keeps `p = 1` above with `rand() < 0.5`. A query
with a single filter is not affected.
### How was this patch tested?
Pass the CIs with the newly added test cases. Both fail without this PR.
- `FilterPushdownSuite`: a conjunct over a column that can fail stays above
`rand(0) > 0.1`. The column is an ANSI cast in a `Project`, which also reaches
the conjunct through a `Sample`, another `Project` and a `Filter`. A conjunct
over a non-deterministic column is still combined.
- `DataFrameSuite`: the query above, 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` and the
`PlanStabilitySuite`s locally. No golden files change. With a temporary suite
(not included), I ran the query above in SQL and with the DataFrame API, before
and after this PR, with whole-stage codegen on and off. I also ran a similar
query over a `Project` with a non-deterministic column and an ANSI cast,
`rand(10) * 10 > 100 AND i > 0` with `i` the cast. Without this PR,
`CombineFilters` moves `i > 0` ahead of the non-deterministic conjunct, and the
query fails with `CAST_INVALID_INPUT` under whole-stage codegen. With this PR,
it returns no row.
### 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]