dongjoon-hyun commented on code in PR #60498:
URL: https://github.com/apache/spark/pull/60498#discussion_r4589068756
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -176,11 +178,18 @@ object ConstantPropagation extends Rule[LogicalPlan] with
PredicateHelper {
condition match {
case _ if !condition.containsAllPatterns(LITERAL, BINARY_COMPARISON) =>
(None, AttributeMap.empty)
+ // An `EqualTo` with a NULL literal is NULL on every row, also when the
attribute is not
+ // nullable, so under a `NOT`, where `safeToReplace` relies on the
equalities not being NULL,
+ // replacing the attribute could change another conjunct, e.g. from TRUE
to FALSE or from
+ // FALSE to NULL, and so the `NOT` from NULL to TRUE or from TRUE to
NULL. Where a NULL counts
+ // as FALSE, such an `AND` is never TRUE whatever the attribute is
replaced with, so it is
+ // still propagated there. An `EqualNullSafe` with a NULL literal is
never NULL, so it is
+ // propagated in both cases, under a `NOT` only over an attribute that
is not nullable.
case e @ EqualTo(left: AttributeReference, right: Literal)
- if safeToReplace(left, nullIsFalse) =>
+ if safeToReplace(left, nullIsFalse) && (nullIsFalse || right.value !=
null) =>
Review Comment:
**[1] The new guard is the same as `nullIsFalse || !e.nullable`, so
`safeToReplace` could check the equality itself.**
`EqualTo` keeps `BinaryExpression.nullable`, which is `left.nullable ||
right.nullable`, and `Literal.nullable` is `value == null`. So for `e @
EqualTo(attr, lit)`, `safeToReplace(attr, nullIsFalse) && (nullIsFalse ||
lit.value != null)` equals `nullIsFalse || !e.nullable` for every combination
of the three flags. That is the premise the new comment names: under a `NOT`,
an equality is propagated only if it is never NULL.
Checking the equality itself would keep the two `EqualTo` cases identical,
instead of `right.value` in one and `left.value` in the other (the description
lists a mutant for each side), and `safeToReplace` would no longer need the
added sentence about a check it does not do. For example, keeping the behavior
of the `EqualNullSafe` cases:
```scala
// Under a NOT, a NULL is not FALSE, so an equality is propagated there
only if it is never
// NULL: not one over a nullable attribute, nor an `EqualTo` with a NULL
literal.
private def safeToReplace(e: BinaryComparison, ar: AttributeReference,
nullIsFalse: Boolean) =
nullIsFalse || !(ar.nullable || e.nullable)
```
with `if safeToReplace(e, left, nullIsFalse) =>` and so on in all four
cases. Dropping `ar.nullable` there would also propagate `a <=> lit` over a
nullable `a` under a `NOT`, which looks safe as well since `<=>` is never NULL,
but that changes the behavior, so it is not for this PR.
##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala:
##########
@@ -3032,6 +3032,43 @@ class DataFrameSuite extends SharedSparkSession
}
}
+ test("SPARK-61704: ConstantPropagation does not propagate an EqualTo with a
NULL literal " +
+ "under a NOT") {
+ // `NullPropagation` would turn `id = NULL` into a NULL literal first, so
it is excluded. Under
+ // the `NOT`, `id = NULL` is NULL on every row, so a row passes only where
the other conjunct is
+ // FALSE. Each condition gives the result that it gives with the default
rules. The NULL is a
+ // `typedLit`: with `lit(null).cast("bigint")`, the first query returns
the right rows also
+ // without this fix.
+ val df = spark.range(4, 7, 1, 1).toDF()
+ val nullLong = typedLit[java.lang.Long](null)
+ Seq("true", "false").foreach { ansi =>
Review Comment:
**[2] nit, the ANSI loop runs the three queries twice without changing any
outcome.**
With the fix, nothing is propagated under the `NOT`, so these plans do not
depend on `spark.sql.ansi.enabled`. Without it, both modes return the same
wrong rows: on the 4.3.0 release with `NullPropagation` excluded, the three
queries return `[4, 5, 6]`, `[]` and `[4, 5, 6]` with ANSI mode on and off. So
each mutant in the description passes or fails here the same way in both modes,
while `checkAnswer` plans and runs every query twice. The block at L3058 also
repeats the same two confs. How about dropping the loop and moving these checks
into that block?
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/ConstantPropagationSuite.scala:
##########
@@ -324,4 +324,50 @@ class ConstantPropagationSuite extends PlanTest {
val lengthOfTrimmed = trimmed.where($"s" === "a" && Length($"s") ===
2).analyze
comparePlans(Optimize.execute(lengthOfTrimmed), lengthOfTrimmed)
}
+
+ test("SPARK-61704: Do not propagate an EqualTo with a NULL literal under a
NOT") {
+ val nullInt = Literal(null, IntegerType)
+ Seq(true, false).foreach { ansi =>
Review Comment:
**[4] nit, the ANSI loop changes no assertion here either.**
The guard does not read the conf, and with the fix `replaceConstants` is not
called for the conditions under a `NOT` at L336-L339, since nothing is
propagated there. ANSI mode only changes the eval mode of the `Add` in `d + 0`
and `a + 0`, i.e. whether `replaceConstants` maps its children or folds it, and
both give `Filter(false)` after `Optimize` at L359-L363. So every assertion and
every mutant in the description gives the same result in both modes. If both
paths are meant to be covered, the SPARK-60882 test at L290 already pins them
with an explicit `EvalMode`. Could the loop go?
##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala:
##########
@@ -3032,6 +3032,43 @@ class DataFrameSuite extends SharedSparkSession
}
}
+ test("SPARK-61704: ConstantPropagation does not propagate an EqualTo with a
NULL literal " +
+ "under a NOT") {
+ // `NullPropagation` would turn `id = NULL` into a NULL literal first, so
it is excluded. Under
+ // the `NOT`, `id = NULL` is NULL on every row, so a row passes only where
the other conjunct is
+ // FALSE. Each condition gives the result that it gives with the default
rules. The NULL is a
+ // `typedLit`: with `lit(null).cast("bigint")`, the first query returns
the right rows also
+ // without this fix.
+ val df = spark.range(4, 7, 1, 1).toDF()
+ val nullLong = typedLit[java.lang.Long](null)
+ Seq("true", "false").foreach { ansi =>
+ withSQLConf(SQLConf.ANSI_ENABLED.key -> ansi,
+ SQLConf.OPTIMIZER_EXCLUDED_RULES.key -> NullPropagation.ruleName) {
+ // Propagating the NULL made the condition TRUE for id 5 ...
+ checkAnswer(df.where(!($"id" === nullLong && ($"id" + 0 <=> 5))),
Seq(Row(4L), Row(6L)))
+ // ... also from a projection below ...
+ checkAnswer(
+ df.select($"id", nullLong as "n").where(!($"id" === $"n" && ($"id" +
0 <=> 5)))
+ .select($"id"),
+ Seq(Row(4L), Row(6L)))
+ // ... and NULL for the ids 4 and 5.
+ checkAnswer(df.where(!($"id" === nullLong && $"id" > 5)), Seq(Row(4L),
Row(5L)))
+ }
+ }
+ withSQLConf(SQLConf.ANSI_ENABLED.key -> "true",
+ SQLConf.OPTIMIZER_EXCLUDED_RULES.key -> NullPropagation.ruleName) {
+ // The division runs on id 4 and fails, as it does with the default
rules. Propagating the
+ // NULL folded it away, so that no row was returned.
+ val e = intercept[SparkArithmeticException] {
+ df.where(!($"id" === nullLong && lit(10L) / ($"id" - 4) > 0)).collect()
+ }
+ assert(e.getCondition === "DIVIDE_BY_ZERO")
+ // Outside a `NOT` it is still propagated, so the division over `id - 4`
is folded with NULL
+ // and does not run, as before.
+ checkAnswer(df.where(lit(10L) / ($"id" - 4) > 0 && $"id" === nullLong),
Seq.empty)
Review Comment:
**[6] nit, this check covers behavior the PR does not change.**
Outside a `NOT`, the equality is propagated as before, and
`ConstantPropagationSuite.scala` L352-L356 already fails if the guard also
applies there, on either side, while this query only notices it on the
attribute-left side. It does show the contrast with the `NOT` case at run time,
so please keep it if that is the intent.
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/ConstantPropagationSuite.scala:
##########
@@ -324,4 +324,50 @@ class ConstantPropagationSuite extends PlanTest {
val lengthOfTrimmed = trimmed.where($"s" === "a" && Length($"s") ===
2).analyze
comparePlans(Optimize.execute(lengthOfTrimmed), lengthOfTrimmed)
}
+
+ test("SPARK-61704: Do not propagate an EqualTo with a NULL literal under a
NOT") {
+ val nullInt = Literal(null, IntegerType)
+ Seq(true, false).foreach { ansi =>
+ withSQLConf(SQLConf.ANSI_ENABLED.key -> ansi.toString) {
+ // `d = NULL` is NULL on every row, so the condition under the `NOT`
is NULL or FALSE.
+ // Replacing `d` with NULL would make `d + 0 <=> 5` FALSE, and the
condition FALSE where it
+ // is NULL, or `d > b` NULL, and the condition NULL where it is FALSE.
Also below an `OR`.
+ Seq(
+ Not(columnD === nullInt && (columnD + 0 <=> 5)),
+ Not(nullInt === columnD && (columnD + 0 <=> 5)),
+ Not(columnD === nullInt && columnD > columnB),
Review Comment:
**[5] nit, L338 and the `columnD === Literal(1)` case in L345 are already
covered.**
L338 goes through the same attribute-left branch as L336, and its `And` is
the left side of L339, which fails for every mutant L338 catches. An
attribute-left `EqualTo` with a non-NULL literal under a `NOT` is what the
SPARK-30447 test checks at L184 (`!(d = 1 AND d + 1 = 1)` becomes `true`), so
only `Literal(1) === columnD` is new in L345. L339 is worth keeping: it is the
only assertion with an `Or` under the `NOT`, so the only one that fails if the
`Or` case stops passing `nullIsFalse` down. Would you drop L338 and `columnD
=== Literal(1)`?
##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala:
##########
@@ -3032,6 +3032,43 @@ class DataFrameSuite extends SharedSparkSession
}
}
+ test("SPARK-61704: ConstantPropagation does not propagate an EqualTo with a
NULL literal " +
+ "under a NOT") {
+ // `NullPropagation` would turn `id = NULL` into a NULL literal first, so
it is excluded. Under
+ // the `NOT`, `id = NULL` is NULL on every row, so a row passes only where
the other conjunct is
+ // FALSE. Each condition gives the result that it gives with the default
rules. The NULL is a
+ // `typedLit`: with `lit(null).cast("bigint")`, the first query returns
the right rows also
+ // without this fix.
+ val df = spark.range(4, 7, 1, 1).toDF()
+ val nullLong = typedLit[java.lang.Long](null)
+ Seq("true", "false").foreach { ansi =>
+ withSQLConf(SQLConf.ANSI_ENABLED.key -> ansi,
+ SQLConf.OPTIMIZER_EXCLUDED_RULES.key -> NullPropagation.ruleName) {
+ // Propagating the NULL made the condition TRUE for id 5 ...
+ checkAnswer(df.where(!($"id" === nullLong && ($"id" + 0 <=> 5))),
Seq(Row(4L), Row(6L)))
+ // ... also from a projection below ...
+ checkAnswer(
+ df.select($"id", nullLong as "n").where(!($"id" === $"n" && ($"id" +
0 <=> 5)))
Review Comment:
**[3] nit, this query reaches `ConstantPropagation` as the same `Filter` as
the first one.**
`PushDownPredicates` runs before `ConstantPropagation` in the same iteration
of the batch, and pushing the filter through the `Project` replaces `n` with
its NULL literal. So the rule sees `NOT(id = NULL AND (id + 0) <=> 5)` over the
`Range` here too, the same branch as L3048. It documents the "NULL from a
projection below" case of the description, so would a comment on the first
query, or running this one in one mode only, be enough?
--
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]