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

   ### What changes were proposed in this pull request?
   
   This is a follow-up of #60363. It corrects a comment and a Scaladoc that 
#60363 added, stops emitting the code that evaluates `exprCodesNeedEvaluate`, 
which #60363 left always empty, and adds a column and a condition shape to the 
test it added:
   
   - The comment on the gate in `FilterExec.doConsume` said that eliminating a 
common subexpression adds a local variable per state. Kept inline, a state 
computes the repeated expression once in place of each occurrence, so it adds 
no local variable. The comment now names only the cost of the split: when the 
code of the states together exceeds `spark.sql.codegen.methodSplitThreshold` 
and the parameters of each fit in a Java method, each common subexpression 
becomes a method with up to two mutable fields, which is known only once the 
states are generated, after the gate.
   - `GeneratePredicateHelper.subExprStatesToEliminate` generates the states 
with the code of the input columns hidden, so `exprCodesNeedEvaluate` is always 
empty, but it still returned the code that evaluates it, and 
`FilterExec.doConsume` emitted that code at the top of the row loop. Its 
Scaladoc said this was so "a future change that does hoist a column fails no 
stage rather than dropping a declaration". Neither of the two plain ways such a 
change can go behaved that way:
     - A change that generates the states over `input` itself, without the 
hiding: with the states inline, the column was declared only inside the states 
that read it, so a check or condition before the first of them read it 
undeclared and the generated code did not compile. With the states split, the 
emitted code kept the stage compiling only by declaring the column at the top 
of the loop, ahead of the conditions that drop rows, which brings back what 
#60363 fixed: the `SPARK-61377` test fails as the UDF behind the column throws.
     - A change that leaves the code of the columns in the copies declared the 
column twice: with the states split, by the emitted code and by 
`generatePredicateCode`; with them inline, by the state and by 
`generatePredicateCode`, which this PR does not change.
   
     The helper now returns only the states, and `doConsume` no longer emits 
that code: `generatePredicateCode` loads each column, and a split state takes 
the columns it reads as parameters. The Scaladoc now says this, and why the 
copies must have their code blanked.
   - `SPARK-61377: FilterExec CSE computes an input column just before the 
first conjunct that reads it` in `WholeStageCodegenSuite` gets a sixth column 
`n`, NULL for the rows `r` throws on, and the shape `n + r > 0 AND f + 1 > 0 
AND f + 1 < 100`. The inferred `IsNotNull(n)`, generated among the checks of `n 
+ r > 0`, the first condition that reads `r`, drops those rows, so `r` must be 
loaded after it. The test asserts that the check is inferred, and a failure, 
including an error the query raises or a compile error of the generated code, 
now names the shape and the split threshold. The comparison of the generated 
code with and without elimination now also ignores whitespace.
   
   ### Why are the changes needed?
   
   The comment and the Scaladoc do not match the code, and the code that 
evaluates `exprCodesNeedEvaluate` was either empty or, if a column ever reached 
it, an early or a second declaration of the column. Without the new shape, no 
test in `WholeStageCodegenSuite` fails when `generatePredicateCode` loads the 
columns of a conjunct before the `IsNotNull` checks generated for it (see 
below).
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. The code no longer emitted is always empty, so the generated Java 
changes only in whitespace.
   
   ### How was this patch tested?
   
   `WholeStageCodegenSuite` passes, and the `SPARK-61377` test also passes with 
`SPARK_ANSI_SQL_MODE=false`.
   
   These mutants were run on master and with this PR against all of 
`WholeStageCodegenSuite`, and (c) and (d) also against one test at a fixed 
`spark.sql.codegen.methodSplitThreshold`, 1 (split) or `Int.MaxValue` (inline): 
(c) against the `SPARK-61377` test, and (d) against `SPARK-56032: subexpression 
elimination in FilterExec codegen`.
   
   | Mutant | master | this PR |
   |---|---|---|
   | (a) `generatePredicateCode` loads the columns of a conjunct before all the 
checks generated for it | the suite passes | the `SPARK-61377` test fails, also 
with `SPARK_ANSI_SQL_MODE=false` |
   | (b) ... after the checks that `precedingNotNullChecks` generates for it 
with the NULL-conjunct flag, but before its other checks | the suite passes | 
the `SPARK-61377` test fails, also with `SPARK_ANSI_SQL_MODE=false` |
   | (c) `subExprStatesToEliminate` generates the states over `input` itself, 
without hiding the code | split: fails as `r` throws (`r(0)`); inline: 
`Expression "project_value_1" is not an rvalue` | split and inline: `Expression 
"project_value_1" is not an rvalue` |
   | (d) the copies keep the code of the columns | split and inline: 
`Redefinition of local variable` | split: passes; inline: `Redefinition of 
local variable` |
   
   The suite fails for (c) and (d) on both, with compile errors of the same 
kinds.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Kiro CLI 3.3.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