fix: canonicalize NaN in flat arrays_overlap float keys - #5376
Open
sunchao wants to merge 2 commits into
Open
Conversation
sunchao
marked this pull request as ready for review
August 17, 2026 19:05
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why are the changes needed?
arrays_overlapanswers a simple question: do two arrays contain at least one common value? For floating-point arrays, Spark treats every representation ofNaNas the same value, even when their underlying IEEE 754 sign bits or payloads differ. Comet's native implementation compared those raw bit patterns instead. As a result, enabling Comet could silently change the answer to an otherwise ordinary Spark SQL query.Consider a Parquet table containing a
NaN:For the first row,
-xis still aNaN, but negating it at execution time changes its sign bit. Spark correctly considers that value equal to the originalNaN. On Spark 3.4–4.1, before this change, Comet incorrectly treated the two representations as different:xNaNtruefalsetrue+0.0falsefalsefalse1.0falsefalsefalseThe
+0.0row is also a compatibility control:-xbecomes-0.0, and Spark 3.4–4.1 considers the two zeros different for flat arrays. FixingNaNcomparisons must not change that result.Spark 4.2 and later intentionally behave differently. Under SPARK-54918, Spark's optimizer normalizes both floating-point
arrays_overlapoperands before evaluation. Consequently,+0.0and-0.0overlap, and both Spark and Comet returntrue. The native comparison must still preserve distinct zero keys for earlier Spark versions; on Spark 4.2+, the operands have already been normalized before those keys are constructed.On Spark 3.4–4.1, the mismatch also affects SQL null semantics. If the first array additionally contains
NULL, Spark returnstruebecause the twoNaNvalues overlap, while the previous Comet implementation returnsNULLbecause it misses that definite match:Runtime negation in these examples is intentional. Parquet canonicalizes floating-point
NaNvalues when writing them, so storing a negative or custom-payloadNaNin the input file is not enough to reproduce the bug. A distinct representation must be produced after the scan.The signed-zero boundary is therefore version-specific:
A general-purpose floating-point normalizer that also merges signed zero inside Comet's comparison key would fix the NaN mismatch but introduce a Spark 3.4–4.1 compatibility regression.
Closes #5270.
What changes were proposed in this PR?
The change gives Comet's existing flat-array comparison the same floating-point equality contract as Spark. When Comet derives a comparison key for a
FLOATorDOUBLE, everyNaNrepresentation is mapped to one canonical key. All other values retain their original bit patterns. This preserves distinct signed zeros when Spark 3.4–4.1 passes them through unchanged, while respecting Spark 4.2's earlier normalization of the operands.Applying the correction at the shared comparison-key boundary fixes both ways the existing implementation checks for overlap: direct comparisons for small arrays and hash-based lookups for larger arrays. The execution plan, fast-path structure, null propagation, and behavior for non-floating-point values are otherwise unchanged.
This PR intentionally does not change nested-array comparisons. Spark uses a different equality contract for nested values, and the separate nested signed-zero issue is addressed in #5235.
How was this PR tested?
The native Rust regressions construct positive, negative, and signaling
NaNbit patterns directly for both floating-point widths. They verify that those values overlap, that signed zeros remain distinct, that null handling remains correct, and that the same behavior holds on both sides of the small-array/hash-lookup threshold. All 23 focusedarrays_overlaptests pass.The Spark regression exercises the actual native execution path against a Parquet-backed table. It first confirms that Parquet canonicalized the stored
NaNvalues, then uses runtime negation to produce a noncanonical representation after the scan. Spark and Comet results are compared forFLOATandDOUBLE, both argument orders, nullable arrays, signed-zero controls, and arrays large enough to use hash lookup.The explicit signed-zero expectations follow each Spark version's actual semantics: the two zeros remain distinct through Spark 4.1 and compare equal after Spark 4.2's operand normalization. The focused regression passes locally on Spark 3.5, Spark 4.0, and Spark 4.2.
Native formatting, compilation, and focused tests:
Spark 3.5, from the repository root with JDK 17 configured:
Spark 4.0, using a clean reactor build to avoid mixing Scala versions:
Spark 4.2, including its version-aware signed-zero expectations:
Maven Spotless checks pass for all three locally tested Spark profiles.