Skip to content

fix: hash NaN the way Spark does in hash, xxhash64 and native shuffle - #6413

Draft
andygrove wants to merge 4 commits into
apache:mainfrom
andygrove:issue-6385-hash-nan
Draft

andygrove wants to merge 4 commits into
apache:mainfrom
andygrove:issue-6385-hash-nan

Conversation

@andygrove

Copy link
Copy Markdown
Member

Which issue does this PR close?

Part of #6385: the "canonicalize NaN in hash / xxhash64" step.

This is stacked on #6400, so until that merges this diff also contains #6400's commits. Only the last commit, 71d6e7b, is new here. I'll rebase once #6400 is in.

Rationale for this change

Spark hashes a float through doubleToLongBits or floatToIntBits. Both canonicalize NaN, so every NaN hashes alike. Comet hashed the raw bits. A NaN with the sign bit set therefore hashed differently from Spark. Negating a NaN produces one on every platform, and on x86-64 every NaN produced by arithmetic has the sign bit set:

Query Spark Comet before
SELECT hash(-d), xxhash64(-d) FROM t WHERE id = 3 -1281358385, -3127944061524951246 -1489914710, 9200374361256412029

The hash serde reports float arguments as compatible, so these queries did not fall back to Spark.

What changes are included in this PR?

  • float_semantics::hash_input now returns normalize_float: -0.0 hashes as 0.0, as before, and every NaN hashes as the canonical NaN. Since refactor: move native -0.0 and NaN handling into one float_semantics module #6400 every float hash path goes through hash_input. That covers hash, xxhash64, list, struct and dictionary elements, the native shuffle's hash partitioner, and approx_count_distinct.
  • At the default seed, xxhash64 handed "compatible" arguments to datafusion-spark's SparkXxhash64, which also hashes a NaN's raw bits. Floats, and types containing them, now stay on Comet's kernel. xxhash64_diff.rs lists the new divergence with a test, like its other known differences from upstream.
  • approx_count_distinct no longer runs a separate float normalization pass before hashing, because xxhash64 now gives the same hash on its own. Spark's HLL normalizes and then hashes, and gets the same result.

Effects beyond the two functions:

  • Native shuffle hash partitioning now sends a row with a non-canonical NaN key to the partition Spark picks. This matters when a Comet exchange has to agree with a Spark exchange. Joins were not affected, because Spark normalizes join keys before the exchange.
  • Runtime bloom filters hash their keys with xxhash64. A filter built by one engine and probed by the other now agrees on NaN keys.

The extra NaN check costs about 0.17 ns per value in the float hash loop. On an M3 Max a batch of 8192 doubles goes from 4.7 µs to 6.1 µs, still about 2x faster than before #6400.

datafusion-spark's SparkXxhash64 has the same gap, which I can report upstream.

How are these changes tested?

  • hash.sql gains NaN rows, compared against Spark. One query hashes d, -d, f and -f with hash and xxhash64. Another hashes arrays and structs of negated values. Against the old library the first query fails with the values in the table above. It passes now.
  • A new CometNativeShuffleSuite test compares spark_partition_id() per row against Spark for float keys, including negated NaNs, and asserts that the exchange ran natively. With the old hash, the NaN row went to partition 0, where Spark puts it in partition 5.
  • New unit tests, each of which fails under the old rule:
    • Spark's hash values for canonical, sign-bit and payload NaNs in the murmur3 and xxhash64 float tests.
    • Both hashes of non-canonical NaNs inside lists, large lists, fixed-size lists, structs and dictionaries.
    • approx_count_distinct counting the two zeros as one value and all NaNs as one.
    • The xxhash64 expression path keeping floats on Comet's kernel.
  • Results on macOS aarch64 with the default Spark 4.1 profile:
    • Unit tests: 1025 in spark-expr, 552 in core, 171 in shuffle.
    • CometSqlFileTestSuite: 578.
    • CometHashExpressionSuite and CometNativeShuffleSuite: 100.

…module

Part of apache#6385. Each native expression that follows one of Spark's float
rules carried its own copy of that rule. This moves them onto a new
`float_semantics` module in `spark-expr`, with no change in behavior.

- `normalize_float`, `canonicalize_nan`, `compare_floats` and `hash_input`
  are the per-value rules: `NormalizeNaNAndZero`, `Double.equals`,
  `compareDoubles`, and the `Murmur3Hash`/`XxHash64` input.
- `normalize_floats`, `normalize_nested_floats` and `has_float_leaf`
  normalize arrays. `NormalizeNaNAndZero` and `NormalizeNestedFloats`
  move into the module, and `NormalizeNaNAndZero::wrap_if_needed` takes
  over the planner's check in `create_normalized_key_expr`.
- `spark_comparator` replaces both `nested_equality` (nested `=` and
  `IN`) and the comparator local to `array_min`/`array_max`.

hll_plus_plus, max_min_by, mode, percentile, array_extrema,
array_position, arrays_overlap, nested_comparison, the float hash macros,
and the planner's sort keys and range partition bounds now call the
module.
- compare_floats uses the if-chain form again, so `.is_eq()` in
  array_position and percentile compiles to a single comparison.
- float_extrema calls new float_lt/float_gt predicates, which compile to
  the same instructions as the inline tests they replace. The three-way
  comparison was up to 9x slower in that loop.
- spark_equality gives nested `=` and `IN` back the length check that
  skips comparing the elements of lists of different lengths.
- float_semantics is a private module again, re-exporting
  normalize_floats, canonicalize_nan and the two expressions.
- The Parquet variant cast canonicalizes NaN with canonicalize_nan, and
  gains a test.
- Tests share the NaN constants and check the per-value rules in one
  table, and the max_by doc defers to the module docs.
…mparator

`check_types` compares logical types, so a `Dictionary(_, Float64)` passes
as `Float64` and then panicked when `float_comparator` downcast it. The
comparator now matches on both sides' types and returns the internal error.
`mismatched_types_are_rejected` covers a dictionary on either side, at the
top level and inside a list.
Part of apache#6385. Spark hashes a float through doubleToLongBits or
floatToIntBits, which canonicalize NaN, so every NaN hashes alike. Comet
hashed the raw bits, so `hash(-d)` and `xxhash64(-d)` differed from Spark
for a NaN, and on x86-64 so did the hash of every NaN that arithmetic
produces. `hash_input` now returns `normalize_float`, which covers `hash`,
`xxhash64`, list, struct and dictionary elements, the native shuffle's hash
partitioner and `approx_count_distinct`.

At the default seed, `xxhash64` handed float arguments to datafusion-spark's
`SparkXxhash64`, which also hashes the raw bits, so floats now stay on
Comet's kernel. `approx_count_distinct` no longer normalizes floats before
hashing, since `xxhash64` now does.
@github-actions github-actions Bot added bug Something isn't working area:aggregation Hash aggregates, aggregate expressions area:scan Parquet scan / data reading area:expressions Expression evaluation labels Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:aggregation Hash aggregates, aggregate expressions area:expressions Expression evaluation area:scan Parquet scan / data reading bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant