rapidgithub opened a new pull request, #60621:
URL: https://github.com/apache/spark/pull/60621
### What changes were proposed in this pull request?
This PR fixes a float/double normalization gap in `NOT IN` subqueries on
non-nullable columns where `-0.0` and `0.0` (as well as `NaN` values) are
erroneously treated as distinct values during join execution.
Specifically:
1. In `RewritePredicateSubquery` (`subquery.scala`), when rewriting
`Not(InSubquery)` into a `LeftAnti` join (and into an `ExistenceJoin` for
nested predicates), we avoid unconditionally wrapping each equi-condition in
`Or(c, IsNull(c))`. If `c` is non-nullable (`!c.nullable`), `IsNull(c)` is
statically `false`, so we emit `c` directly.
2. In `Optimizer` (`Optimizer.scala`), we add `BooleanSimplification` to the
`RewriteSubquery` batch immediately after `NullPropagation`. This guarantees
that if `NullPropagation` (or any other rule in the batch) reduces null checks
to boolean literals (`true`/`false`), they are simplified to canonical forms
before downstream rules and before the `NormalizeFloatingNumbers` batch runs.
### Why are the changes needed?
In SPARK-54972, `NullPropagation` was added to the `RewriteSubquery` batch
so that non-nullable `NOT IN` subqueries could be simplified into standard
equi-joins. However:
1. `RewritePredicateSubquery` expanded `NOT IN` into `(x = y) OR isnull(x =
y)`.
2. For non-nullable columns, `NullPropagation` replaced `isnull(x = y)` with
`false`, leaving `(x = y) OR false`.
3. Because `BooleanSimplification` was not in the `RewriteSubquery` batch,
the join condition remained `(x = y) OR false` when the subsequent
`NormalizeFloatingNumbers` batch executed.
4. Neither `ExtractEquiJoinKeys` (which requires equi-join conjuncts) nor
`ExtractSingleColumnNullAwareAntiJoin` (which requires `Or(..., IsNull(...))`)
matched `(x = y) OR false`. Consequently, `NormalizeFloatingNumbers` skipped
the join and left the keys un-normalized.
5. `BooleanSimplification` ran much later in `SparkOptimizer`, reducing `(x
= y) OR false` to `x = y`.
6. At physical execution, `BroadcastHashJoinExec` / `ShuffledHashJoinExec`
compared the raw binary representations of the keys (`0.0` as
`0x0000000000000000L` vs `-0.0` as `0x8000000000000000L`), treating them as
unequal. As a result, outer rows matching inner rows on `0.0 = -0.0` were
erroneously returned by the `LeftAnti` join.
Example reproduction:
```python
spark.createDataFrame([(0.0,)], "x DOUBLE NOT
NULL").createOrReplaceTempView("t")
spark.createDataFrame([(-0.0,)], "y DOUBLE NOT
NULL").createOrReplaceTempView("u")
spark.sql("SELECT * FROM t WHERE x NOT IN (SELECT y FROM u)").collect()
# Expected: []
# Previous: [Row(x=0.0)]
```
### Does this PR introduce _any_ user-facing change?
Yes. Queries using `NOT IN` subqueries on non-nullable `FLOAT` or `DOUBLE`
columns comparing `0.0` and `-0.0` (or `NaN` and `NaN`) now correctly return
empty results instead of erroneously returning rows that match the subquery.
### How was this patch tested?
- Added an optimizer plan test in `RewriteSubquerySuite.scala`
(`"SPARK-60473: NOT IN subquery on non-nullable column rewrites without
redundant IsNull"`) verifying that non-nullable `NOT IN` rewrites directly to
`EqualTo` without redundant `IsNull`.
- Added end-to-end SQL query tests in `SubquerySuite.scala` (`"SPARK-60473:
NOT IN subquery on non-nullable float/double columns with -0.0/0.0 and NaNs"`)
covering:
- Single-column `NOT IN` comparing `0.0D` vs `-0.0D`
- Single-column `NOT IN` comparing `NaN` vs `NaN`
- Multi-column `NOT IN` with non-nullable `(DOUBLE NOT NULL, FLOAT NOT
NULL)` columns
- Disjunctive/nested `NOT IN` condition evaluated via `ExistenceJoin`
### Was this patch authored or co-authored using generative AI tooling?
Yes
Google Antigravity (Gemini 3.8 Flash)
--
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]