fix: canonicalize NaN in flat arrays_overlap float keys - #5376
Merged
sunchao merged 3 commits intoAug 19, 2026
Merged
Conversation
sunchao
marked this pull request as ready for review
August 17, 2026 19:05
Member
Author
|
Thanks @comphead! Moved the behavioral cases into the existing I kept a small Scala raw-bit test because SQL equality cannot distinguish NaN representations. It verifies that runtime negation produces noncanonical NaNs after Parquet has canonicalized the stored values. Both focused integration tests pass locally on Spark 3.5, 4.0, and 4.2, and all 23 targeted Rust tests pass. |
comphead
approved these changes
Aug 19, 2026
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.
Mixed scalar/array argument broadcasting is another pre-existing issue, tracked separately in #5269. This change does not alter that argument-dispatch behavior.
How was this PR tested?
The native Rust regressions construct positive, negative, and signaling
NaNbit patterns directly for both floating-point widths. They verify NaN equivalence, distinct signed-zero keys, SQL null handling, and both sides of the small-array/hash-lookup threshold. All 23 focusedarrays_overlaptests pass.Most integration coverage now lives in the existing
expressions/array/arrays_overlap.sqlfixture, as requested in review. Its Parquet-backed queries generate noncanonical NaNs after the scan and exerciseFLOATandDOUBLE, reversed operands, nullable arrays, signed-zero controls, and arrays large enough to use hash lookup. The SQL-file harness compares Comet with the running Spark version and checks native execution, so it also covers Spark 4.2's different signed-zero behavior without duplicating version-specific expected results.A small Scala test remains to inspect the actual floating-point bits. It confirms that Parquet canonicalizes stored NaNs and that native runtime negation produces noncanonical NaNs after the scan—something SQL equality alone cannot establish.
Both focused integration tests pass locally on Spark 3.5, Spark 4.0, and Spark 4.2. Maven Spotless checks also pass for all three profiles.
Native formatting, compilation, and focused tests, from
native/:cargo fmt --all -- --check cargo build cargo test -p datafusion-comet-spark-expr --lib \ array_funcs::arrays_overlap::tests -- --nocaptureThe focused Spark commands were run from the repository root with JDK 17 configured. Clean reactor builds were used when switching profiles: