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

   ### What changes were proposed in this pull request?
   
   This PR extends `PredicateHelper.failableOutput` so that it also reports a 
column that can fail, e.g. one that a projection computes with an ANSI cast or 
division, through a `Union`, a `Window`, an `EventTimeWatermark`, the operators 
of `PushPredicateThroughNonJoin.canPushThrough`, such as `Repartition`, 
`RepartitionByExpression`, `Generate` and `Expand`, and an `Aggregate` that 
groups by it.
   
   `failableOutput` returns the output columns of a plan that can fail to 
compute. Its callers trust it:
   
   - `PushPredicateThroughJoin`, `ReorderJoin` and `CombineFilters`, through 
`splitMovable`, keep a condition over such a column in place when a 
non-deterministic or throwable condition is written before it.
   - `InferFiltersFromConstraints` puts no inferred filter over such a column 
ahead of the condition of a `Filter`.
   
   Before this PR, the helper reported no column through these operators, on 
the ground that they compute every column of their input rows. That holds where 
the operator itself reads the column, but a condition moved below one of them 
keeps moving: `PushPredicateThroughNonJoin` pushes it below the operator, 
through the projection that computes the column with the expression 
substituted, and into one side of a join below, where it is evaluated on the 
rows that the join drops.
   
   The helper now reports these columns too:
   
   | Operator | Columns reported |
   |---|---|
   | `Union` | An output column whose column at the same position is reported 
for some child, by position as `ConstantPropagation.failableColumnsOf` does. |
   | `Window`, `EventTimeWatermark` and the operators of 
`PushPredicateThroughNonJoin.canPushThrough` | The columns reported for the 
child. |
   | `Aggregate` | Besides the result columns it reported before, a result 
column whose expression, with each aggregate function taken as its value, is 
deterministic and reads a column reported for the child, i.e. a grouping key or 
an alias of one. |
   
   The scaladoc now says why a condition moved below these operators can still 
move on. The existing cases (`Project`, `Filter`, `Sample`, the limits, 
`Offset`, `Sort`, `Join` and the aggregate result columns) are unchanged, and 
other operators, e.g. `Deduplicate`, are still not looked through. Since the 
`canPushThrough` case calls `PushPredicateThroughNonJoin.canPushThrough` 
itself, an operator added there is covered without another change.
   
   This gap was found in the review of #60766 (SPARK-62144), whose 
`PushPredicateThroughNonJoin` cases also read `failableOutput`, e.g. of a 
`Union`. With both, a condition over such a column also stays above a union, a 
window or an aggregation that it reaches directly, without a join in between.
   
   ### Why are the changes needed?
   
   A query that should succeed fails. With ANSI mode, the default since Apache 
Spark 4.0.0, a cast or a division is enough. In each query below, the inner 
join keeps the ids 0 to 2 of `l`, where neither `CAST(l.id * 1000000000 AS 
INT)` nor `10L / (l.id - 3)` fails.
   
   ```sql
   -- It should return no row, but fails with CAST_OVERFLOW.
   SELECT * FROM (
     SELECT k, count(*) OVER (PARTITION BY k) AS w FROM (
       SELECT CAST(l.id * 1000000000 AS INT) AS k
       FROM range(10) AS l JOIN range(3) AS r ON l.id = r.id))
   CROSS JOIN range(1)
   WHERE rand(10) * 10 > 100 AND k > 0;
   
   -- It should return no row, but fails with CAST_OVERFLOW.
   SELECT * FROM (
     SELECT CAST(l.id * 1000000000 AS INT) AS k
     FROM range(10) AS l JOIN range(3) AS r ON l.id = r.id
     UNION ALL
     SELECT CAST(id AS INT) AS k FROM range(10))
   CROSS JOIN range(1)
   WHERE rand(10) * 10 > 100 AND k > 0;
   
   -- It should return no row, but fails with CAST_OVERFLOW.
   SELECT * FROM (
     SELECT k, count(*) AS c FROM (
       SELECT CAST(l.id * 1000000000 AS INT) AS k
       FROM range(10) AS l JOIN range(3) AS r ON l.id = r.id)
     GROUP BY k)
   CROSS JOIN range(1)
   WHERE rand(10) * 10 > 100 AND k > 0;
   
   -- It should return no row, but fails with CAST_OVERFLOW.
   SELECT * FROM (
     SELECT * FROM (
       SELECT CAST(l.id * 1000000000 AS INT) AS k
       FROM range(10) AS l JOIN range(3) AS r ON l.id = r.id)
     DISTRIBUTE BY k)
   CROSS JOIN range(1)
   WHERE rand(10) * 10 > 100 AND k > 0;
   
   -- It should return no row, but fails with DIVIDE_BY_ZERO.
   SELECT * FROM (
     SELECT q, d, count(*) OVER (PARTITION BY q) AS w FROM (
       SELECT 10L / (l.id - 3) AS q, CAST(l.id AS DOUBLE) AS d
       FROM range(10) AS l JOIN range(3) AS r ON l.id = r.id))
   WHERE w > 5 AND q = d AND d > 0;
   ```
   
   In the first four queries, `rand(10) * 10 > 100` is false on every row, so 
`k > 0`, written after it, should never be evaluated. 
`PushPredicateThroughJoin` still pushes `k > 0` into the left side of the cross 
join, as `failableOutput` reports no column through the window, the union, the 
aggregation or the repartition. `PushPredicateThroughNonJoin` then pushes it 
below that operator, through the projection as `CAST(l.id * 1000000000 AS INT) 
> 0` and into `l`, where the cast overflows from id 3. For the first query, the 
optimized plan on the master branch is:
   
   ```
   Filter ((rand(10) * 10.0) > 100.0)
   +- Join Cross
      :- Window [count(1) windowspecdefinition(k#0, 
specifiedwindowframe(RowFrame, unboundedpreceding$(), unboundedfollowing$())) 
AS w#1L], [k#0]
      :  +- Project [cast((id#2L * 1000000000) as int) AS k#0]
      :     +- Join Inner, (id#2L = id#3L)
      :        :- Filter (cast((id#2L * 1000000000) as int) > 0)
      :        :  +- Range (0, 10, step=1)
      :        +- Range (0, 3, step=1)
      +- Range (0, 1, step=1)
   ```
   
   In the last query, `w > 5` is false on every row, and the written condition 
stays above the window. But `InferFiltersFromConstraints` adds `q > 0` and 
`isnotnull(q)`, inferred from `q = d AND d > 0`, ahead of it, as 
`failableOutput` reports no column through the window. They are pushed below 
the window and into `l`, where the division is by zero on id 3. The query 
succeeds with `spark.sql.constraintPropagation.enabled=false`. The optimized 
plan on the master branch is:
   
   ```
   Filter (((w#8L > 5) AND (q#6 = d#7)) AND (d#7 > 0.0))
   +- Window [count(1) windowspecdefinition(q#6, specifiedwindowframe(RowFrame, 
unboundedpreceding$(), unboundedfollowing$())) AS w#8L], 
[knownfloatingpointnormalized(normalizenanandzero(q#6))]
      +- Project [(10.0 / cast((id#9L - 3) as double)) AS q#6, cast(id#9L as 
double) AS d#7]
         +- Join Inner, (id#9L = id#10L)
            :- Filter (((10.0 / cast((id#9L - 3) as double)) > 0.0) AND 
isnotnull((10.0 / cast((id#9L - 3) as double))))
            :  +- Range (0, 10, step=1)
            +- Range (0, 3, step=1)
   ```
   
   With this PR, `k > 0` stays above the cross join in the first four queries, 
e.g. `Filter (((rand(10) * 10.0) > 100.0) AND (k#0 > 0))` above `Join Cross` in 
the first one, and no filter is inferred in the last query, so `l` gets no 
`Filter` in either plan.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. The queries above succeed. Each of them fails 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) and 
[v4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0) (2026-10-27). The 
last one succeeds there with `spark.sql.constraintPropagation.enabled=false`.
   
   Through these operators, a condition over a column that can fail is no 
longer moved ahead of a non-deterministic or throwable condition written before 
it, and no filter over such a column is inferred ahead of the condition of a 
`Filter`. No golden file of the `PlanStabilitySuite`s changes.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test cases. Each of them fails without 
this PR.
   
   - `FilterPushdownSuite`: after `rand(0) > 0.1`, `PushPredicateThroughJoin` 
keeps a condition over an ANSI division column above the join when that side is 
a union whose second child computes the column, a window partitioned by it, a 
watermark, a repartition by it, or an aggregation grouped by it, also as an 
alias. A condition over a plain column is still pushed.
   - `InferFiltersFromConstraintsSuite`: no constraint over an ANSI cast column 
is inferred through a window partitioned by it or an aggregation grouped by it, 
while one over a plain column is.
   - `DataFrameSuite`: the queries above, written with the DataFrame API, 
return no row in ANSI mode with whole-stage codegen on and off.
   
   Each new case of `failableOutput` is covered: with any one of them disabled 
(an aggregate result reading a reported column, a grouping key, the union, the 
union mapped from its first child only, the window, the watermark, the 
`canPushThrough` operators), a new test fails. I also ran the catalyst 
optimizer suites, `DataFrameSuite`, `JoinSuite` and the `PlanStabilitySuite`s 
locally. They pass, and no golden file changes. With a temporary suite (not 
included), the queries above return no row in ANSI mode with whole-stage 
codegen on and off.
   
   ### 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