sarutak opened a new pull request, #60498:
URL: https://github.com/apache/spark/pull/60498
### What changes were proposed in this pull request?
`ConstantPropagation` no longer propagates an `EqualTo` between an attribute
and a NULL literal under a `NOT`.
Under a `NOT`, a NULL result is not the same as FALSE, so the rule only
propagates an equality over an attribute that is not nullable (`safeToReplace`,
SPARK-30447), on the premise that such an equality is never NULL. An `EqualTo`
with a NULL literal is NULL on every row whatever the attribute is, though, so
the `AND` under the `NOT` is NULL or FALSE, and a row passes the `NOT` only
where another conjunct is FALSE. Replacing the attribute with NULL changes the
other conjuncts: one that is not FALSE can become FALSE, which makes the `NOT`
TRUE where it is NULL, and one that is FALSE can become NULL or TRUE, which
makes the `NOT` NULL where it is TRUE.
Outside a `NOT`, where a NULL result counts as FALSE, the `AND` is never
TRUE whatever the attribute is replaced with, so such an equality is still
propagated there, as before. An `EqualNullSafe` with a NULL literal is never
NULL and is still propagated in both cases (under a `NOT`, as before, only over
an attribute that is not nullable).
### Why are the changes needed?
With `NullPropagation` excluded, over `spark.range(4, 7, 1, 1)`:
```scala
spark.conf.set("spark.sql.optimizer.excludedRules",
"org.apache.spark.sql.catalyst.optimizer.NullPropagation")
val n = typedLit[java.lang.Long](null)
val df = spark.range(4, 7, 1, 1)
df.where(!($"id" === n && ($"id" + 0 <=> 5))).collect() // should be 4, 6
df.where(!($"id" === n && $"id" > 5)).collect() // should be 4, 5
```
The first condition is NULL for id 5, so that row is not returned, but
`ConstantPropagation` replaces `id` with NULL in `id + 0 <=> 5`, which is
folded to FALSE, so the condition becomes TRUE and the filter is pruned: all
three rows are returned. In the second, `id > 5` becomes NULL, so the condition
is NULL also for the ids 4 and 5, where it is TRUE, and no row is returned.
With ANSI mode on or off, the first returns all three rows on master and on
3.5.9, 4.0.0, 4.0.5, 4.1.4, 4.2.0, 4.2.1 and 4.3.0, and the second returns no
row on master and on those from 4.0.0; 3.5.9 returns 4 and 5, as the rule
rewrites comparisons other than `EqualTo` and `EqualNullSafe` only since
SPARK-42500 (4.0.0). The NULL can also come from a projection below:
`df.select($"id", n as "n").where(!($"id" === $"n" && ($"id" + 0 <=> 5)))`
returns all three rows too.
With the default rules, `NullPropagation` turns such an equality into a NULL
literal earlier in the same batch, so these queries return the right rows. With
`lit(null).cast("bigint")` or `CAST(NULL AS BIGINT)` instead of `typedLit`, the
first returns the right rows also with `NullPropagation` excluded, and so does
`!($"id" === lit(null).cast("bigint") && ($"id" * 2 <=> 10))`.
### Does this PR introduce _any_ user-facing change?
Yes, but only with `NullPropagation` excluded through
`spark.sql.optimizer.excludedRules`, for a filter with an `EqualTo` between an
attribute that is not nullable and a NULL literal under a `NOT`: it returns the
rows its condition gives, e.g. the ids 4 and 6, and 4 and 5, above, instead of
all three and none. A conjunct next to such an equality is no longer folded
with the NULL, so it runs where the condition runs it: with ANSI mode on,
`!($"id" === n && lit(10L) / ($"id" - 4) > 0)` now fails with `DIVIDE_BY_ZERO`
on id 4, as it does with the default rules. Before, with `NullPropagation`
excluded, the division was folded away with the NULL and no row was returned.
With the default rules, nothing changes.
### How was this patch tested?
A new test in `ConstantPropagationSuite`, in ANSI mode on and off: an
`EqualTo` with a NULL literal on either side under a `NOT`, also below an `OR`
there, is not propagated, while one with another literal still is; outside a
`NOT`, it still is; an `EqualNullSafe` with a NULL literal on either side still
is, also under a `NOT` over an attribute that is not nullable. A new test in
`DataFrameSuite`, with `NullPropagation` excluded, runs the two queries above
and the one with the NULL from a projection, in ANSI mode on and off, the
division under a `NOT` in ANSI mode, and `10 / (id - 4) > 0 AND id = NULL`,
outside a `NOT`, which returns no row without failing, as before.
Each of these mutants of the change fails at least one of the new tests: no
guard (as on master); no guard on one side, for each side; a guard that also
applies outside a `NOT`, on both sides or on either one; a guard that rejects
every `EqualTo` under a `NOT`, on both sides or on the literal-left one; and a
guard on an `EqualNullSafe`, everywhere or under a `NOT` on either side.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Kiro CLI 3.2.1 (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]