dongjoon-hyun opened a new pull request, #60772:
URL: https://github.com/apache/spark/pull/60772
### What changes were proposed in this pull request?
This PR fixes the projection split of `PushPredicateThroughNonJoin` (#58403,
SPARK-55014) so that it does not filter on a condition in a lower layer ahead
of a non-deterministic or throwable condition written before it.
For a filter over a projection, `PushPredicateThroughNonJoin` keeps the
conditions over expensive aliases, such as UDFs, above the projection.
`splitProjectionForExpensiveConditions` then stacks the deterministic,
non-throwable ones in lower layers by cost, each over a projection that
computes the aliases it needs, so that an expensive alias only runs on the rows
that the layers below kept. A non-deterministic condition stays in the top
filter, and so does a throwable one since #59848 (SPARK-60602). But the split
still places the conditions written after it in lower layers, so they are
evaluated ahead of it, on rows that the `AND` short-circuit would have kept
from them. Each of these conditions reads an expensive alias, such as a Scala
or Python UDF, `reflect` or a regex whose pattern comes from the data, and each
of them can fail.
This PR stops the split at the first non-deterministic or throwable
condition. It and all the conditions written after it stay in the top filter,
in their written order. The conditions written before it are split as before.
| Condition over an expensive alias | Before | After |
|---|---|---|
| Written before the first non-deterministic or throwable condition that
stays above | Split into a lower layer by cost | Same |
| Written after it | Split into a lower layer by cost, ahead of it | Kept in
the top filter, after it |
This matches `CombineFilters` and `PushPredicateThroughJoin`
(`PredicateHelper.splitMovable`), which move a condition written after a
non-deterministic or throwable one only when
`ExprUtils.canEvaluateUnconditionally` accepts it. That check rejects every
expensive expression, UDFs and regexes included, so no condition that reaches
the split would pass it, and this PR keeps them all in place instead of
checking each one. A condition written after a deterministic one is still split
ahead of it by cost, as before, like the conditions that `CombineFilters` and
`PushPredicateThroughJoin` move.
### Why are the changes needed?
A query that should succeed fails. With ANSI mode, the default since Apache
Spark 4.0.0, a cast is enough.
```scala
val notX = udf((e: String) => e != "x")
val length = udf((e: String) => e.length)
val parse = udf((e: String) => e.toInt)
val df = spark.range(0, 10, 1, 1).selectExpr("IF(id = 3, 'x', CAST(id AS
STRING)) AS e")
// Issue 1: It should return 9 rows, but fails with CAST_INVALID_INPUT.
df.select($"e", notX($"e") as "f", length($"e") as "n")
.where((rand(10) * 10 > 100 || $"f") && $"e".cast("int") + $"n" > 0)
.collect()
// Issue 2: It should return 8 rows, but fails with FAILED_EXECUTE_UDF.
df.select($"e", notX($"e") as "f", parse($"e") as "g")
.where((rand(10) * 10 > 100 || $"f") && $"g" > 0 && $"g" < 100)
.collect()
```
`rand(10) * 10 > 100` is false on every row, so the first conjunct is false
only for `'x'`, and the cast in Issue 1 and `parse` in Issue 2 should never run
on it. The first conjunct stays in the top filter, as it is non-deterministic,
but the split filters on the second one in a lower layer, e.g. for Issue 1:
```
Filter (((rand(10) * 10.0) > 100.0) OR f#78)
+- Project [e#75, UDF(e#75) AS f#78, n#81]
+- Filter ((cast(e#75 as int) + n#81) > 0)
+- Project [e#75, UDF(e#75) AS n#81]
+- Project [if ((id#74L = 3)) x else cast(id#74L as string) AS e#75]
+- Range (0, 10, step=1, splits=Some(1))
```
A throwable condition that can filter out rows, such as `f OR (e = 'zzz' AND
raise_error('boom') IS NULL)`, fails Issue 1 the same way, and so does a regex
whose pattern comes from the data, such as `e RLIKE concat(e, s)` over an alias
`s`, on a row whose pattern is invalid.
### Does this PR introduce _any_ user-facing change?
No for the released versions. The projection split of SPARK-55014 is only in
`master` (4.4.0), not in
[v4.2.1](https://github.com/apache/spark/releases/tag/v4.2.1) (2026-10-21) or
[v4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0) (2026-10-27).
In `master`, the queries above succeed. Issue 1 succeeds with whole-stage
codegen on and off. Issue 2 succeeds with whole-stage codegen on. With it off,
Issue 2 still fails, as it does without the split: the top projection computes
every alias for every row it gets, including `parse` on `'x'`, before the
filter. A condition over an expensive alias written after a non-deterministic
or throwable one that stays above is no longer split off, so its aliases are
computed in the top projection instead of a lower layer. Whole-stage codegen
computes such an alias only when the filter reads it, but an interpreted
projection or a Python UDF computes it on every row that reaches the top
projection, including the rows that a lower layer used to filter out.
### How was this patch tested?
Pass the CIs with the newly added and updated test cases. The new ones fail
without this PR.
- `FilterPushdownSuite`:
- A condition over an expensive alias, written after a non-deterministic
condition or after a throwable one that can filter out rows, is not split off,
which leaves nothing worth splitting. This replaces `SPARK-60602: a throwable
condition does not keep later conditions from being split off`, which expected
the condition to be split off ahead of the throwable one.
- `SPARK-55014: a non-deterministic condition is not split off into a
lower layer` now writes the condition over two aliases before the
non-deterministic one, so it still checks that the split puts it in the layer
that the non-deterministic condition would have taken on cost alone.
- `DataFrameSuite`: Issue 1 returns its 9 rows in ANSI mode with whole-stage
codegen on and off, instead of failing with `CAST_INVALID_INPUT`.
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, the throwable condition and the regex
above, with whole-stage codegen on and off. All of them fail before this PR.
After it, all of them succeed, except Issue 2 with whole-stage codegen off, as
described above.
### 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]