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

   ### What changes were proposed in this pull request?
   
   This PR fixes `InferFiltersFromConstraints` so that it does not add a filter 
over a column that can fail ahead of the condition of a `Filter`. An example is 
`q > 0`, inferred from `q = k AND k > 0`, where `q` is an alias of `10L / (id - 
3)` in ANSI mode.
   
   The `Filter` case of `InferFiltersFromConstraints` puts the filters it 
infers ahead of the condition: `Filter(newFilters AND condition, child)`. Since 
#60162 (SPARK-60985), it infers only a constraint that 
`ExprUtils.canEvaluateUnconditionally` accepts. That check looks at the 
constraint 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 filter can fail as that expression does. 
Whole-stage codegen computes a deterministic column of a `Project` for a row 
only when a filter above first reads it. So the inferred `q > 0` computes `q` 
on rows that a conjunct of the condition, e.g. `id > 5`, drops.
   
   This PR also requires an inferred filter of the `Filter` case not to read a 
column that can fail. A new helper, `PredicateHelper.failableOutput`, collects 
these columns from the child of the `Filter`:
   
   - 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 helper runs only when a constraint passes the existing checks. It has 
the same semantics as `FailableColumns` in #60516, so other rules can share it.
   
   | Inferred constraint of a `Filter` | Before | After |
   |---|---|---|
   | Can fail, e.g. `10L / (id - 3) > 0` | Not added | Not added |
   | Can not fail, over columns that can not fail, e.g. `isnotnull(id)` | Added 
| Added |
   | Can not fail, over a column that can fail, e.g. `q > 0` or `isnotnull(q)` 
| Added | Not added |
   
   No filter that could be pushed down safely is lost. If the `Filter` is 
pushed through the `Project` later, a filter over the column becomes one over 
its expression, which `ExprUtils.canEvaluateUnconditionally` rejects. 
Otherwise, e.g. when the `Project` has a non-deterministic column or a `Sample` 
is in between, the filter stays above the `Project` and only moves the 
computation of the column ahead of the condition.
   
   The other cases of the rule, which infer filters for the inputs of a join, 
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
   -- It should return no row, but fails with DIVIDE_BY_ZERO.
   SELECT id, r >= 0 FROM (
     SELECT id, 10L / (id - 3) AS q, CAST(id AS DOUBLE) AS k, rand(2) AS r FROM 
range(0, 10, 1, 1))
   WHERE id > 5 AND q = k AND k > 0;
   ```
   
   `id > 5` is false for `id = 3`, so `q` should never be computed for that 
row. `rand(2) AS r` keeps the filter above the projection. From `q = k AND k > 
0` and the projection, `InferFiltersFromConstraints` infers three filters over 
`q` and puts them first. The optimized plan on the master branch is:
   
   ```
   Project [id#3L, (r#2 >= 0.0) AS (r >= 0)#4]
   +- Filter ((((cast(id#3L as double) <=> q#0) AND (q#0 > 0.0)) AND 
isnotnull(q#0)) AND (((id#3L > 5) AND (q#0 = k#1)) AND (k#1 > 0.0)))
      +- Project [id#3L, (10.0 / cast((id#3L - 3) as double)) AS q#0, 
cast(id#3L as double) AS k#1, rand(2) AS r#2]
         +- Range (0, 10, step=1, splits=Some(1))
   ```
   
   With this PR, the `Filter` keeps only the written condition, `((id#3L > 5) 
AND (q#0 = k#1)) AND (k#1 > 0.0)`. The query also succeeds with 
`spark.sql.constraintPropagation.enabled=false`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. The query above succeeds. I confirmed that it 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). 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-60985, which is not released yet, 
fixed the same failure for a constraint that can fail by itself, but not for 
one over a column that can fail.
   
   When a `Filter` stays above a `Project` that computes a column that can 
fail, 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 a column that can 
fail is inferred, e.g. `i > 0` from `i = k AND k > 0`. The column is an ANSI 
cast in a `Project`, which also reaches the `Filter` through a `Sample`, 
another `Project` and a `Filter`. A constraint over a non-deterministic column 
is still inferred.
   - `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 with whole-stage codegen on and off. 
Without whole-stage codegen it still fails, before and after this PR, because 
the interpreted `ProjectExec` computes 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]

Reply via email to