Conversation
…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.
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.
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
doubleToLongBitsorfloatToIntBits. 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:SELECT hash(-d), xxhash64(-d) FROM t WHERE id = 3-1281358385, -3127944061524951246-1489914710, 9200374361256412029The 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_inputnow returnsnormalize_float:-0.0hashes as0.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 throughhash_input. That covershash,xxhash64, list, struct and dictionary elements, the native shuffle's hash partitioner, andapprox_count_distinct.xxhash64handed "compatible" arguments to datafusion-spark'sSparkXxhash64, which also hashes a NaN's raw bits. Floats, and types containing them, now stay on Comet's kernel.xxhash64_diff.rslists the new divergence with a test, like its other known differences from upstream.approx_count_distinctno longer runs a separate float normalization pass before hashing, becausexxhash64now gives the same hash on its own. Spark's HLL normalizes and then hashes, and gets the same result.Effects beyond the two functions:
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
SparkXxhash64has the same gap, which I can report upstream.How are these changes tested?
hash.sqlgains NaN rows, compared against Spark. One query hashesd,-d,fand-fwithhashandxxhash64. 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.CometNativeShuffleSuitetest comparesspark_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.murmur3andxxhash64float tests.approx_count_distinctcounting the two zeros as one value and all NaNs as one.xxhash64expression path keeping floats on Comet's kernel.CometSqlFileTestSuite: 578.CometHashExpressionSuiteandCometNativeShuffleSuite: 100.