sarutak opened a new pull request, #60516:
URL: https://github.com/apache/spark/pull/60516

   ### What changes were proposed in this pull request?
   
   `ConstantPropagation` replaces an attribute with the literal of an equality 
conjunct, such as `a = 1`, in the other conjuncts of an `AND`. Since 
SPARK-60983 and SPARK-61033 it also moves the equalities in front when a 
conjunct that can fail would otherwise run on rows an equality drops, as an 
equality between an attribute and a literal can not fail.
   
   The rule judges the conjuncts as they are, though. A column that the 
projection below the filter computes, such as `i` for `cast(s as int) AS i`, is 
an attribute in the filter, so `i = 1` and `i > 0` look like they can not fail, 
while in ANSI mode they fail where `s` is not a number. The projection computes 
such a column only for the rows that reach a condition reading it, under 
whole-stage codegen, and `PushPredicateThroughNonJoin` pushes the conditions 
below the projection with the column's expression substituted. So moving `i = 
1` in front of the conjuncts written before it computes the cast on rows they 
filter out.
   
   This PR makes the rule collect the columns of the filter's child that can 
fail, through the projections, filters and samples at its top, and treat a 
conjunct that reads one as one that can fail. An equality over such a column is 
never moved. Its constant is still propagated where it is written, unless a 
conjunct that can fail comes between it and a rewritten conjunct before it, 
which could then run on rows that the written condition drops. Equalities over 
the other columns, including non-deterministic ones, which a projection 
computes on every row, are propagated and moved by the same rules as before, 
with a conjunct over a column that can fail counted as one that can fail.
   
   ### Why are the changes needed?
   
   With ANSI mode on, over a single Parquet file with the rows `(s, id) = ("1", 
7), ("x", 0)`:
   
   ```scala
   val df = spark.read.parquet(path)
     .select($"s", $"id", $"s".cast("int") as "i", rand(1) as "r")
   
   // `id > 5` and `id > 0` filter out the row ("x", 0), on which `i` fails.
   df.where($"id" > 5 && $"i" * $"id" > 0 && $"i" === 1).select($"s", $"id", 
$"r" >= 0)
   df.where($"id" > 5 && $"i" > 0 && lit(10) / ($"id" - 3) > 0 && $"i" === 1)
     .select($"s", $"id", $"r" >= 0)
   df.where($"id" > 0 && $"i" > 0 && $"id" === 7).select($"s", $"id", $"r" >= 0)
   ```
   
   All three fail with `CAST_INVALID_INPUT` on master with whole-stage codegen 
and return the row `("1", 7, true)` with this PR. The first two return it on 
4.1.4 and 4.3.0 too, so they are regressions from SPARK-60983 and SPARK-61033, 
which are only in master and branch-4.x; the third one fails there as well. 
Without `$"r" >= 0`, the projection goes away once the filter is pushed below 
it, and the first two still fail on master, with whole-stage codegen on or off.
   
   #59940 (SPARK-60730) keeps conditions after one over an expensive alias 
above the projection in their written order. There, with `f` a UDF, `f > 0 && i 
* id > 0 && i = 1` over the rows `("1", 1), ("x", 0)` fails with 
`CAST_INVALID_INPUT`, as the rule moves `i = 1` in front of `f > 0`. With this 
PR on top of #59940 it no longer fails with whole-stage codegen.
   
   This targets master and branch-4.x, where SPARK-60983 and SPARK-61033 are. 
#60337 (SPARK-61354) changes the same part of the rule; whichever goes in 
second is to adapt to the other.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. Queries like the ones above no longer fail with whole-stage codegen. 
Without it, an interpreted projection computes every column on every row, so 
they fail either way while the filter stays above the projection.
   
   As an equality over such a column is no longer moved, a query that fails as 
written now fails, where master moved the equality first and so skipped the 
failing rows: e.g. `10 / (id - i) > 0 && i = 1` over the rows `("1", 7), ("8", 
8)` fails with `DIVIDE_BY_ZERO`, while it succeeds on master and, as they 
replace `i` with 1 in the division, on 4.1.4 and 4.3.0. The rule can not tell 
on which rows moving the equality avoids an error and on which it raises one. 
The rule also no longer simplifies some conjuncts with such an equality's 
constant, and no longer rewrites an operand before one that reads such a 
column. As a conjunct over such a column counts as one that can fail, the other 
equalities are moved first in more cases, so a non-deterministic conjunct they 
move ahead of can see other rows, as on master (SPARK-61354). This applies in 
every mode to a column whose expression `ExprUtils.canEvaluateUnconditionally` 
rejects, such as `upper(s)` or a UDF.
   
   A column can still be computed ahead of the conditions before the first one 
reading it in other ways, unchanged by this PR: by the subexpression 
elimination path of `FilterExec` (SPARK-61377), by a filter that 
`InferFiltersFromConstraints` infers over it and puts in front (SPARK-61378), 
and, for an equality over a nullable column that the rule moves, on the rows 
where that equality is `NULL`, which an `AND` goes on with (SPARK-61438). On 
master, the equality over such a column, which was moved first as well, could 
stop such a row before it reached a conjunct that fails, e.g. for 
`(coalesce(id, 0) > 0 && 10 / x > 0 && i = 1 && id = 7) || id < 0` on a row 
where `id` is `NULL`, `i` is 2 and `x` is 0, which now fails with 
`DIVIDE_BY_ZERO`.
   
   ### How was this patch tested?
   
   New tests in `ConstantPropagationSuite`, which also check the order of the 
conjuncts: that an equality over such a column is not moved, and when it is 
propagated or not, also through other projections, filters and samples, and 
when a condition becomes `FALSE`; that a conjunct over such a column counts as 
one that can fail; that columns that can not fail are handled as before, also 
with ANSI mode off, and that a column read twice by each of 40 projections is 
not expanded.
   
   A new test in `DataFrameSuite` with the queries above, with the filter above 
the projection and pushed below it, and with queries that master simplifies so 
that no row computes the cast, which return the same results as on master. It 
fails on master. Mutants of the change are killed by the new tests.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Kiro CLI 3.1.0 (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