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

   ### What changes were proposed in this pull request?
   
   With whole-stage codegen, the subexpression elimination (CSE) branch of 
`FilterExec.doConsume` evaluated every input column that the conditions read at 
the top of the row loop (`inputVarsEvalCode`), before any condition. An input 
column can be a computation, e.g. an alias that a `ProjectExec` in the same 
codegen stage computes lazily, so it was computed on rows that an earlier 
condition drops, and an error it raised failed the query. The non-CSE branch 
(`generatePredicateCode`) computes each column just before the first condition 
that reads it.
   
   This PR makes the CSE branch do the same:
   
   - The CSE analysis is run with the code of the input columns hidden 
(`input(i).code` set to `EmptyBlock`), then the code is put back. The code 
generation of a common subexpression copies the code of each column it reads 
into the subexpression's own code, and the analysis then clears that code in 
`input`; without hiding it, a condition before the first use of the 
subexpression would read a column that is not declared yet, and two 
subexpressions reading the same column would declare it twice. That is why the 
branch evaluated all columns up front.
   - With the code put back, each column is emitted by 
`evaluateRequiredVariables` just before the first check that reads it, the same 
helper and position the non-CSE branch uses: before an `IsNotNull` check, or 
before a condition together with the common subexpressions it first references. 
A common subexpression first referenced by a condition is part of that 
condition, and the subexpressions it contains read a subset of the condition's 
columns, so the condition's columns cover the ones they read.
   
   The common subexpressions themselves, and where each is evaluated, do not 
change (SPARK-56032, SPARK-60332): only the input columns move from the top of 
the loop to the first check that reads them. The CSE gate (take the CSE branch 
only with a non-cheap common subexpression) is kept.
   
   The comments, the doc of the internal config 
`spark.sql.subexpressionElimination.filterExec.enabled`, and the test comments 
that justified the CSE gate by the eager evaluation are updated to drop that 
rationale.
   
   ### Why are the changes needed?
   
   With whole-stage codegen and the config on, a filter whose conditions share 
a non-cheap common subexpression must not fail where the same filter with the 
config off returns rows. Reproducer:
   
   ```scala
   import org.apache.spark.sql.functions._
   val f = udf((x: Long) => x)
   val r = udf((x: Long) => { if (x <= 5) throw new 
IllegalStateException(s"r($x)"); x })
   spark.conf.set("spark.sql.optimizer.splitProjectionForExpensiveFilters", 
"false")
   spark.range(10).select(f($"id").as("f"), r($"id").as("r"))
     .where("f >= 0 AND f + 1 > 6 AND f + 1 < 100 AND r > 0").collect()
   // before: [FAILED_EXECUTE_UDF] ... java.lang.IllegalStateException: r(5)
   // after:  6, 7, 8, 9
   // before, with spark.sql.subexpressionElimination.filterExec.enabled=false: 
6, 7, 8, 9
   ```
   
   The filter stays above the projection because its conditions read the UDF 
aliases, and the common `f + 1` makes it take the CSE branch, which computed 
`r` up front, before `f + 1 > 6` dropped the rows with `id <= 5`.
   
   The eager evaluation was added by SPARK-56032 (4.2.0). On master the filter 
is reached this way when 
`spark.sql.optimizer.splitProjectionForExpensiveFilters` is off; with it on 
(the default), `r` is computed in a projection above the filter on `f`. Spark 
4.2, 4.3 and `branch-4.x` have no such split, so the reproducer fails there 
with the default configs (without the `spark.conf.set` line); this was measured 
on the 4.2.0 and 4.3.0 releases. This is also a prerequisite of SPARK-60730 
(#59940), which keeps conditions that can fail above a projection in the order 
they were written.
   
   With whole-stage codegen off, `ProjectExec` computes all its columns before 
the filter, so the reproducer still fails there, as before and with the config 
off; this PR changes only the codegen path. Spark does not guarantee the 
evaluation order between a projection and the filter above it, so computing a 
column only where a condition first reads it is a valid semantics the non-CSE 
codegen path already has.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. With whole-stage codegen and 
`spark.sql.subexpressionElimination.filterExec.enabled` on (both default), a 
filter whose conditions share a non-cheap common subexpression now computes 
each input column just before the first condition that reads it, as the non-CSE 
code already does, instead of up front. For a column that is a computation over 
an alias the child projection evaluates lazily:
   
   - A query like the one above returns rows instead of failing, when the 
column raised on a row an earlier condition drops. A query that still fails can 
raise a different error, the one it raises with the config off.
   - A UDF or other computation behind such a column is no longer invoked, and 
such a column is no longer decoded, on rows an earlier condition drops.
   
   A query that succeeds both before and after returns the same rows; only a 
before-failure (now a success, or a different error) changes.
   
   ### How was this patch tested?
   
   New test in `WholeStageCodegenSuite`: `SPARK-61377: FilterExec CSE computes 
an input column just before the first conjunct that reads it`. It filters a 
projection of UDF aliases that throw for the rows the earlier conditions drop, 
over four condition shapes: a non-nullable column (no inferred `IsNotNull`) 
first read by a plain condition, a nullable column first read by its 
`IsNotNull` check, a column first read by a common subexpression referenced 
after a dropping condition, and a column read only by a leftover `IsNotNull` 
plus columns the filter does not read. It asserts the filter over the 
projection is in one codegen stage, that the CSE branch is taken (its generated 
code differs from the non-CSE path), and that the config on and off return the 
same rows. It runs under both `spark.sql.codegen.methodSplitThreshold` 1 and 
`Int.MaxValue`, to exercise the common subexpressions split into functions and 
kept inline. It fails without the fix. The existing `SPARK-56032` and 
`SPARK-6033
 2` tests in `WholeStageCodegenSuite` pass.
   
   ### 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