sarutak commented on code in PR #60557:
URL: https://github.com/apache/spark/pull/60557#discussion_r4594146069


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/WholeStageCodegenSuite.scala:
##########
@@ -2154,32 +2174,37 @@ class WholeStageCodegenSuite extends SharedSparkSession
         SQLConf.SPLIT_PROJECTION_FOR_EXPENSIVE_FILTERS.key -> "false") {
         val df = spark.range(10).select(
           f($"id").as("f"), r($"id").as("r"), rb($"id").as("rb"),
-          rc($"id").as("rc"), u($"id").as("u"))
-        Seq(
-          // `r` (non-nullable, no IsNotNull) first read by `r > 0`, after the 
dropping `f + 1 > 6`.
-          "f >= 0 AND f + 1 > 6 AND f + 1 < 100 AND r > 0",
-          // `rb` (nullable) first read by its IsNotNull check, just before 
`rb > 0`.
-          "f + 1 > 6 AND f + 1 < 100 AND rb IS NOT NULL AND rb > 0",
-          // `r` first read by the common subexpression `r + 1`, referenced 
after `f + 1 > 6`.
-          "f + 1 > 6 AND r + 1 > 0 AND r + 1 < 100 AND f + 1 < 100",
-          // `rc` read only by a leftover IsNotNull; `rb` and `u` are not read 
by the filter.
-          "f + 1 > 6 AND f + 1 < 100 AND r > 0 AND rc IS NOT NULL"
-        ).foreach { predicate =>
-          val filtered = df.where(predicate)
-          firstFilterOverProject(filtered)
-          checkAnswer(filtered,
-            (6L to 9L).map(i => Row(i, i, i, i, i)))
-          // With the config off the same query returns the same rows (non-CSE 
path), and the
-          // generated code differs, so the shape above does take the CSE path.
-          val cseOn = codegenString(filtered.queryExecution.executedPlan)
-          val cseOff = withSQLConf(
-              SQLConf.SUBEXPRESSION_ELIMINATION_FILTER_EXEC_ENABLED.key -> 
"false") {
-            val off = df.where(predicate)
-            checkAnswer(off, (6L to 9L).map(i => Row(i, i, i, i, i)))
-            codegenString(off.queryExecution.executedPlan)
+          rc($"id").as("rc"), u($"id").as("u"), n($"id").as("n"))
+        shapes.foreach { case Shape(predicate, inferredNotNullN) =>
+          // Names the shape and the threshold in a failure, also when the 
query raises.
+          withClue(s"methodSplitThreshold $threshold, $predicate: ") {
+            try {
+              val filtered = df.where(predicate)
+              val fe = firstFilterOverProject(filtered)
+              if (inferredNotNullN) {
+                assert(fe.condition.exists {
+                  case IsNotNull(a: Attribute) => a.name == "n"
+                  case _ => false
+                }, s"IsNotNull(n) should be inferred: ${fe.condition}")
+              }
+              checkAnswer(filtered,
+                (6L to 9L).map(i => Row(i, i, i, i, i, i)))
+              // With the config off the same query returns the same rows 
(non-CSE path), and
+              // the generated code differs, so the shape above does take the 
CSE path.
+              val cseOn = codegenString(filtered.queryExecution.executedPlan)
+              val cseOff = withSQLConf(
+                  SQLConf.SUBEXPRESSION_ELIMINATION_FILTER_EXEC_ENABLED.key -> 
"false") {
+                val off = df.where(predicate)
+                checkAnswer(off, (6L to 9L).map(i => Row(i, i, i, i, i, i)))
+                codegenString(off.queryExecution.executedPlan)
+              }
+              assert(normalize(cseOn) != normalize(cseOff),
+                "each shape should take the CSE path (codegen differs on/off)")
+            } catch {
+              case e: TestFailedException => throw e
+              case e: Exception => fail("the query failed", e)

Review Comment:
   Thanks, done in 609378540a5: it now catches `NonFatal(e)`. A 
`TestFailedException` (a failed ScalaTest assertion) is still rethrown first, 
so it keeps its own message. With `methodSplitThreshold` fixed at 1, a change 
that makes the split states hoist a column now fails with `methodSplitThreshold 
1, <shape>: the query failed`, caused by the `AssertionError` of the assertion 
added in `subExprStatesToEliminate`.
   



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/WholeStageCodegenSuite.scala:
##########
@@ -2138,8 +2143,23 @@ class WholeStageCodegenSuite extends SharedSparkSession
       fe.get
     }
     // Each `select` mints fresh exprIds that appear in the codegen dump's 
plan header but not in
-    // the generated Java; normalize them away to compare the generated code.
-    def normalize(code: String): String = code.replaceAll("#\\d+", "#")
+    // the generated Java; normalize them, and whitespace, away to compare the 
generated code.
+    def normalize(code: String): String = code.replaceAll("#\\d+", 
"#").replaceAll("\\s+", " ")

Review Comment:
   Thanks, you are right. Done in 609378540a5: `normalize` now also drops the 
`/* NNN */` line numbers before collapsing whitespace.
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -215,21 +215,21 @@ trait GeneratePredicateHelper extends PredicateHelper {
    *
    * So the states are generated with `ctx.currentVars` swapped for copies 
whose code is blank;
    * `input` keeps its code, and the states read only the columns' variables.
-   * `exprCodesNeedEvaluate` is then empty (nothing to hoist); its (empty) 
evaluation code is
-   * returned with the states and emitted by the caller anyway, so a future 
change that does hoist
-   * a column fails no stage rather than dropping a declaration. This hiding 
is the precondition
-   * `generatePredicateCode` documents for `subExprStates`, done here so a 
caller need not repeat
-   * it.
+   * `exprCodesNeedEvaluate` is then empty, and is not emitted, unlike in 
`ProjectExec` and
+   * `ExpandExec`, which evaluate their states up front: 
`generatePredicateCode` loads each
+   * column, and a split state takes the columns it reads as parameters. 
Copies that keep the
+   * code would not do: kept inline, a state would declare a column that 
`generatePredicateCode`
+   * declares too. This hiding is the precondition `generatePredicateCode` 
documents for
+   * `subExprStates`, done here so a caller need not repeat it.
    */
   protected def subExprStatesToEliminate(
       ctx: CodegenContext,
       equivalentExpressions: EquivalentExpressions,
-      input: Seq[ExprCode]): (Map[ExpressionEquals, SubExprEliminationState], 
String) = {
+      input: Seq[ExprCode]): Map[ExpressionEquals, SubExprEliminationState] = {
     val savedVars = ctx.currentVars
     ctx.currentVars = input.map(_.copy(code = EmptyBlock)).toIndexedSeq
     try {
-      val subExprs = 
ctx.subexpressionEliminationForWholeStageCodegen(equivalentExpressions)
-      (subExprs.states, evaluateVariables(subExprs.exprCodesNeedEvaluate))
+      
ctx.subexpressionEliminationForWholeStageCodegen(equivalentExpressions).states

Review Comment:
   Thanks, added in 609378540a5, with a message, and the Scaladoc mentions it. 
Only the split fills `exprCodesNeedEvaluate`, so this checks the split path: a 
change that leaves the code of the columns in the copies (which passed with the 
states split on the previous head) or that generates the states over `input` 
itself now fails here. Kept inline, both still fail at Janino (a redefinition, 
or a column read before its declaration).
   



-- 
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