Repository navigation
Commit f56ae97
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
- sqllogictest/test_files/spark/hash
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
984 | 984 | | |
985 | 985 | | |
986 | 986 | | |
987 | | - | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
988 | 1001 | | |
989 | 1002 | | |
990 | 1003 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
423 | 423 | | |
424 | 424 | | |
425 | 425 | | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
426 | 454 | | |
427 | 455 | | |
428 | 456 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
0 commit comments