dongjoon-hyun opened a new pull request, #60643:
URL: https://github.com/apache/spark/pull/60643
### What changes were proposed in this pull request?
This PR extends `PredicateHelper.failableOutput`, which #60574 (SPARK-61823)
added, so that it also finds the columns that can fail when they reach a
`Filter` through a `LIMIT`, an `OFFSET` or a join, or when they are the result
of an aggregation. `InferFiltersFromConstraints` then stops putting an inferred
filter over such a column, e.g. `q > 0` from `q = k AND k > 0`, ahead of the
condition of the `Filter`.
`failableOutput` returns the columns of a plan that can fail to compute.
Before this PR, these were the deterministic columns of a `Project` whose
expression `ExprUtils.canEvaluateUnconditionally` rejects, looking through
`Filter` and `Sample` only. Every other operator returned no column. Under
whole-stage codegen, however, the limits and the streamed side of a hash join
hand a column of the `Project` below them on without computing it, and an
aggregation computes a deterministic result column only when a filter above
first reads it. So an inferred filter over such a column still computes it on
rows that a conjunct of the condition drops.
This PR adds the following to the helper:
- `LocalLimit`, `GlobalLimit` and `Offset` pass the columns of their child
through, as `Filter` and `Sample` do.
- `Join` passes the columns of both sides through. Only the streamed side of
a hash join hands them on lazily; a build side or a sort-merge join gets every
column computed. The logical plan does not tell them apart, so this is
conservative there.
- `Aggregate` returns a result column whose expression, with each aggregate
function taken as its value, is deterministic and rejected by
`ExprUtils.canEvaluateUnconditionally`, e.g. `10L / (max(id) - 3)`. The
aggregation computes its grouping keys and aggregate functions on every row,
and a non-deterministic result for every group, so they do not count.
The other operators are still not looked through. I checked each of them on
the master branch with a query of the same shape, in ANSI mode with whole-stage
codegen:
| Operator | Why it is not looked through |
|---|---|
| `SubqueryAlias`, `ResolvedHint` | `FinishAnalysis` removes them before
`InferFiltersFromConstraints` runs. |
| `Repartition`, `RepartitionByExpression`, `Sort`, `Window` | The filter is
pushed below them. Otherwise, an exchange, a sort or `WindowExec` gets every
column of every row computed. |
| `Union`, `Generate`, `Deduplicate` | The query fails with
`spark.sql.constraintPropagation.enabled=false` too, so an inferred filter does
not cause the failure. |
| `Tail` | Only at the top of a plan, from `Dataset.tail`. |
The helper is also used by #60631 (SPARK-61886), which is not merged yet,
for `CombineFilters`. That rule gets the same coverage once both are merged,
e.g. for `Filter(rand() > 0.1 AND q > 0, Filter(c, Limit(Project(10L / (id - 3)
AS q, ...))))`.
### 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 FROM (
SELECT id, 10L / (id - 3) AS q, CAST(id AS DOUBLE) AS k FROM range(0, 10,
1, 1) LIMIT 8)
WHERE id > 5 AND q = k AND k > 0;
-- It should return no row, but fails with DIVIDE_BY_ZERO.
SELECT id FROM (
SELECT id, max(id) AS m, 10L / (max(id) - 3) AS q, CAST(max(id) AS DOUBLE)
AS k
FROM range(0, 10, 1, 1) GROUP BY id)
WHERE m > 5 AND q = k AND k > 0;
-- It should return no row, but fails with DIVIDE_BY_ZERO.
SELECT id, r >= 0 FROM (
SELECT /*+ BROADCAST(b) */ id, q, k, rand(2) AS r
FROM (SELECT id, 10L / (id - 3) AS q, CAST(id AS DOUBLE) AS k FROM
range(0, 10, 1, 1)) a
JOIN (SELECT id AS j FROM range(0, 10, 1, 1)) b ON id = j)
WHERE id > 5 AND q = k AND k > 0;
```
In each query, the condition written first (`id > 5` or `m > 5`) is false
for the row or group where `q` divides by zero, so `q` should never be computed
there. The `LIMIT`, the aggregation, and `rand(2) AS r` above the join keep the
condition above the column. For the first query, the optimized plan on the
master branch is:
```
Project [id#2L]
+- Filter ((((cast(id#2L as double) <=> q#0) AND (q#0 > 0.0)) AND
isnotnull(q#0)) AND (((id#2L > 5) AND (q#0 = k#1)) AND (k#1 > 0.0)))
+- GlobalLimit 8
+- LocalLimit 8
+- Project [id#2L, (10.0 / cast((id#2L - 3) as double)) AS q#0,
cast(id#2L as double) AS k#1]
+- Range (0, 10, step=1, splits=Some(1))
```
With this PR, the `Filter` keeps only the written condition. Each query also
succeeds with `spark.sql.constraintPropagation.enabled=false`.
### Does this PR introduce _any_ user-facing change?
Yes. The queries above succeed. I confirmed that each of them fails 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) and
[v4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0) (2026-10-27), and
succeeds there with `spark.sql.constraintPropagation.enabled=false`.
When a `Filter` reads such a column through one of these operators, it no
longer gets an inferred filter over that column, e.g. `isnotnull(q)`.
### How was this patch tested?
Pass the CIs with the newly added test cases. Both fail without this PR.
- `InferFiltersFromConstraintsSuite`: no constraint over an ANSI cast column
is inferred when the column reaches the `Filter` through a `LIMIT`, an `OFFSET`
and the left side of a join, nor over an aggregate result on the right side
whose expression can fail. Constraints over a plain column, an aggregate
function result, and a non-deterministic aggregate result are still inferred.
- `DataFrameSuite`: the three queries above, written with the DataFrame API,
return no row in ANSI mode with whole-stage codegen 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 queries above with whole-stage codegen on and off.
Without whole-stage codegen they still fail, before and after this PR, because
the interpreted `ProjectExec` and aggregation compute every column of a 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]