fix: canonicalize NaN in flat arrays_overlap float keys - #5376
Draft
sunchao wants to merge 1 commit into
Draft
Conversation
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. Before this change, Comet incorrectly treated the two representations as different:xNaNtruefalsetrue+0.0falsefalsefalse1.0falsefalsefalseThe
+0.0row doubles as an important compatibility control:-xbecomes-0.0, and Spark deliberately considers the two zeros different for flat arrays. FixingNaNcomparisons must not change that result.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.There is one important boundary: Spark's flat-array equality considers all
NaNrepresentations equal, but still treats positive and negative zero as different values:A general-purpose floating-point normalizer that also merges signed zero would therefore replace the original bug with a different Spark compatibility regression.
Closes #5270.
What changes were proposed in this PR?
The change aligns Comet's existing flat-array comparison with the equality contract Spark already uses for boxed floating-point values. When Comet derives a comparison key for a
FLOATorDOUBLE, everyNaNrepresentation is mapped to one canonical key. All other values retain their original bit patterns, so+0.0and-0.0remain distinct.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 focused regression passes on both Spark 3.5 and Spark 4.0.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:
Maven Spotless checks also pass for both Spark profiles.