MaxGekk commented on code in PR #59984:
URL: https://github.com/apache/spark/pull/59984#discussion_r4466784140
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodegenSubexpressionEliminationSuite.scala:
##########
@@ -56,18 +56,81 @@ class CodegenSubexpressionEliminationSuite extends
SparkFunSuite {
}
}
+ test("SPARK-60332: GeneratePredicate evaluates a common subexpression of
later conjuncts " +
+ "after the conjuncts before it") {
+ // `Not(leaf)` is common to the last two conjuncts, and evaluated just
before the first of
+ // them, so the first conjunct decides whether `leaf` is evaluated at all:
a FALSE one stops
+ // the evaluation, while a TRUE or a NULL one does not, as `And.eval` does.
+ Seq(true, false).foreach { useSubexprElimination =>
+ Seq((false, false, false), (true, true, true), (null, true,
false)).foreach {
+ case (first, leafEvaluated, result) =>
+ // Distinct `id`s, so that the first conjunct and the leaf are not
the same expression.
+ val leaf = ExprWithEvaluatedState(id = 1)
+ val cond = And(And(ExprWithEvaluatedState(first, isNullable = first
== null),
+ Not(leaf)), Not(leaf))
+ val instance = GeneratePredicate.generate(cond,
useSubexprElimination)
+ instance.initialize(0)
+ withClue(s"useSubexprElimination = $useSubexprElimination, first =
$first: ") {
+ assert(instance.eval(null) === result)
+ assert(leaf.evaluated === leafEvaluated)
Review Comment:
Done in 358d156dde6: `ExprWithEvaluatedState` counts its evaluations, and
the tests assert exact counts, so a recomputation shows (once with the
elimination, twice without). The stale read in a branch is covered by the `(a +
c) + b` test over rows with different values.
--
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]