dongjoon-hyun commented on code in PR #60557:
URL: https://github.com/apache/spark/pull/60557#discussion_r4591467583
##########
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:
Since `exprCodesNeedEvaluate` is no longer emitted, what about guarding the
invariant here?
```scala
val subExprs =
ctx.subexpressionEliminationForWholeStageCodegen(equivalentExpressions)
assert(subExprs.exprCodesNeedEvaluate.isEmpty)
subExprs.states
```
Then, a future change which hoists a column fails here instead of silently
dropping the declaration and surfacing as an unrelated Janino compile error.
##########
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:
Scala's `assert` (e.g., the ones in `CodeGenerator` on the split path)
throws `java.lang.AssertionError`, which is an `Error`, not an `Exception`. It
escapes this `catch` (and `QueryTest.checkAnswer` too, because
`materializedRdd.count()` is not guarded), and `withClue` does not decorate it,
so the failure would not show the shape and the threshold. How about `case
NonFatal(e) => fail("the query failed", e)` with `import
scala.util.control.NonFatal`?
##########
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:
`codegenString` formats the code via `CodeFormatter.format`, which
re-indents each line and prefixes it with a `/* NNN */` line number. So `\\s+`
only collapses intra-line spaces; a single extra blank line still shifts all
following line numbers and makes `normalize(cseOn) != normalize(cseOff)` hold.
If the intention is to ignore whitespace-only differences, we may need to strip
the line-number comments as well, e.g., `.replaceAll("/\\* \\d+ \\*/", "")`.
--
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]