Skip to content

Commit f56ae97

Browse files
mrhard9090Moltrax8
andauthored
fix: xxhash64 of a NULL struct ignores the struct fields (apache#25774)
## Which issue does this PR close? - Closes apache#25505. ## Rationale for this change Spark's `xxhash64` treats a NULL value by leaving the running hash unchanged, so `xxhash64` of a NULL struct should be the seed, `42`. When a struct is NULL but its child arrays still hold values, for example after `nullif(s, s)`, DataFusion hashed those child values anyway: ```sql SELECT s IS NULL, xxhash64(s), xxhash64(arrow_cast(NULL, 'Struct("a": Int64, "b": Utf8)')) FROM (SELECT nullif(s, s) AS s FROM (SELECT named_struct('a', 1, 'b', 'x') AS s)); -- true 6158280145716684778 42 ``` The same NULL gets two different hashes depending on how it was produced. ## What changes are included in this PR? In the `DataType::Struct` branch of `create_hashes_internal!`, when the struct array has NULLs, the hashes are saved before the fields are hashed, and the saved value is put back for every NULL row. Without NULLs the branch works as before and copies nothing. ## What is the testing strategy for this PR? - New unit test `test_xxhash64_null_struct_ignores_fields`: a struct whose second row is NULL but whose fields still hold values. The NULL row keeps the seed and the valid row is hashed. - `spark/hash/xxhash64.slt` gets the query from the issue, which now returns `true 42 42`. Without the change it returns `6158280145716684778` for the masked NULL, so the test fails. - `cargo test -p datafusion-spark --lib xxhash64` passes, and so do the `xxhash64` sqllogictest, `cargo fmt --all` and `cargo clippy -p datafusion-spark --no-deps --all-targets --all-features -- -D warnings`. ## Are there any user-facing changes? `xxhash64` of a NULL struct now returns the input hash (`42` for a single argument), matching Spark. There are no API changes. This PR was written with help from Claude Opus 5.5. I reviewed the change and ran the tests above myself. --------- Co-authored-by: mrhard9090 <[email protected]>
1 parent 884465c commit f56ae97

3 files changed

Lines changed: 58 additions & 1 deletion

File tree

‎datafusion/spark/src/function/hash/utils.rs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,20 @@ macro_rules! create_hashes_internal {
984984
// Hash each field of the struct - Spark hashes all fields recursively
985985
let columns: Vec<ArrayRef> = struct_array.columns().to_vec();
986986
if !columns.is_empty() {
987-
$recursive_hash_method(&columns, $hashes_buffer)?;
987+
match struct_array.nulls().filter(|nulls| nulls.null_count() > 0) {
988+
// A NULL struct leaves the hash unchanged like any other
989+
// NULL, whatever its fields still hold.
990+
Some(nulls) => {
991+
let before = $hashes_buffer.to_vec();
992+
$recursive_hash_method(&columns, $hashes_buffer)?;
993+
for (i, hash) in $hashes_buffer.iter_mut().enumerate() {
994+
if nulls.is_null(i) {
995+
*hash = before[i];
996+
}
997+
}
998+
}
999+
None => $recursive_hash_method(&columns, $hashes_buffer)?,
1000+
}
9881001
}
9891002
}
9901003
DataType::Map(field, _) => {

‎datafusion/spark/src/function/hash/xxhash64.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,34 @@ mod tests {
423423
assert_ne!(hashes[1], hashes[2]);
424424
}
425425

426+
#[test]
427+
fn test_xxhash64_null_struct_ignores_fields() {
428+
use arrow::array::StructArray;
429+
use arrow::buffer::NullBuffer;
430+
use arrow::datatypes::{Field, Fields};
431+
432+
// Row 1 is NULL but its fields still hold values.
433+
let fields = Fields::from(vec![
434+
Field::new("a", DataType::Int32, false),
435+
Field::new("b", DataType::Utf8, false),
436+
]);
437+
let struct_array = StructArray::new(
438+
fields,
439+
vec![
440+
Arc::new(Int32Array::from(vec![1, 1])) as ArrayRef,
441+
Arc::new(StringArray::from(vec!["x", "x"])) as ArrayRef,
442+
],
443+
Some(NullBuffer::from(vec![true, false])),
444+
);
445+
let array_ref: ArrayRef = Arc::new(struct_array);
446+
447+
let mut hashes = vec![DEFAULT_SEED; 2];
448+
create_xxhash64_hashes(&[array_ref], &mut hashes).unwrap();
449+
450+
assert_ne!(hashes[0], DEFAULT_SEED);
451+
assert_eq!(hashes[1], DEFAULT_SEED);
452+
}
453+
426454
#[test]
427455
fn test_xxhash64_list() {
428456
use arrow::array::ListArray;

‎datafusion/sqllogictest/test_files/spark/hash/xxhash64.slt‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,19 @@ SELECT id, xxhash64(id, name) FROM xxhash_test ORDER BY id;
195195

196196
statement ok
197197
DROP TABLE xxhash_test;
198+
199+
# A NULL struct hashes like any other NULL, whatever its fields still hold
200+
query BII
201+
SELECT s IS NULL,
202+
xxhash64(s),
203+
xxhash64(arrow_cast(NULL, 'Struct("a": Int64, "b": Utf8)'))
204+
FROM (SELECT nullif(s, s) AS s FROM (SELECT named_struct('a', 1, 'b', 'x') AS s));
205+
----
206+
true 42 42
207+
208+
# A NULL struct in a later argument leaves the incoming hash unchanged
209+
query II
210+
SELECT xxhash64(7, nullif(s, s)), xxhash64(7)
211+
FROM (SELECT named_struct('a', 1, 'b', 'x') AS s);
212+
----
213+
-2864211556195259663 -2864211556195259663

0 commit comments

Comments
 (0)