datafusion-spark 54.0.0

DataFusion expressions that emulate Apache Spark's behavior
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

//! Shared helpers for Spark-compatible hash functions.
//!
//! Generic helpers ([`hash_primitive_values`], [`hash_bytes_values`]) cover
//! the cases where a function-based abstraction is clean; less uniform paths
//! (booleans, decimals, list/map dispatch) remain as macros.
//!
//! Ported from Apache DataFusion Comet:
//! <https://github.com/apache/datafusion-comet/blob/main/native/spark-expr/src/hash_funcs/utils.rs>

use arrow::array::{Array, PrimitiveArray};
use arrow::datatypes::ArrowPrimitiveType;
use datafusion_common::Result;

/// Hash the values of a primitive array, calling `transform` to convert each
/// value into a byte slice before feeding it to `hash_method`.
#[inline]
pub(crate) fn hash_primitive_values<P, F, B, H>(
    array: &PrimitiveArray<P>,
    hashes: &mut [u64],
    transform: F,
    hash_method: H,
) where
    P: ArrowPrimitiveType,
    F: Fn(P::Native) -> B,
    B: AsRef<[u8]>,
    H: Fn(B, u64) -> u64,
{
    let values = array.values();
    if array.null_count() == 0 {
        // Fast path: no nulls, skip null checks
        for (i, hash) in hashes.iter_mut().enumerate() {
            *hash = hash_method(transform(values[i]), *hash);
        }
    } else {
        // Slow path: check each row for null
        for (i, hash) in hashes.iter_mut().enumerate() {
            if !array.is_null(i) {
                *hash = hash_method(transform(values[i]), *hash);
            }
        }
    }
}

/// Hash an array by calling `value_at(array, i)` to produce a byte slice for
/// each non-null row. Used for byte-slice arrays (Utf8/Binary/FixedSizeBinary
/// and their Large/View variants) and for arrays whose values need a small
/// transform (e.g. booleans widened to `i32`, decimals to `i128` bytes).
#[inline]
pub(crate) fn hash_bytes_values<A, B, H>(
    array: &A,
    hashes: &mut [u64],
    value_at: impl Fn(&A, usize) -> B,
    hash_method: H,
) where
    A: Array,
    B: AsRef<[u8]>,
    H: Fn(B, u64) -> u64,
{
    if array.null_count() == 0 {
        for (i, hash) in hashes.iter_mut().enumerate() {
            *hash = hash_method(value_at(array, i), *hash);
        }
    } else {
        for (i, hash) in hashes.iter_mut().enumerate() {
            if !array.is_null(i) {
                *hash = hash_method(value_at(array, i), *hash);
            }
        }
    }
}

/// Fallible variant of [`hash_bytes_values`]: `value_at` may return an error
/// (used by the small-decimal path, where values may not fit in `i64`).
#[inline]
pub(crate) fn try_hash_bytes_values<A, B, H>(
    array: &A,
    hashes: &mut [u64],
    value_at: impl Fn(&A, usize) -> Result<B>,
    hash_method: H,
) -> Result<()>
where
    A: Array,
    B: AsRef<[u8]>,
    H: Fn(B, u64) -> u64,
{
    if array.null_count() == 0 {
        for (i, hash) in hashes.iter_mut().enumerate() {
            *hash = hash_method(value_at(array, i)?, *hash);
        }
    } else {
        for (i, hash) in hashes.iter_mut().enumerate() {
            if !array.is_null(i) {
                *hash = hash_method(value_at(array, i)?, *hash);
            }
        }
    }
    Ok(())
}

/// Downcast `$column` to `$array_type`, panicking with a useful error if the
/// type does not match.
#[macro_export]
#[doc(hidden)]
macro_rules! downcast_array {
    ($column:ident, $array_type:ident) => {
        $column
            .as_any()
            .downcast_ref::<$array_type>()
            .unwrap_or_else(|| {
                panic!(
                    "Failed to downcast column to {}. Actual data type: {:?}.",
                    stringify!($array_type),
                    $column.data_type()
                )
            })
    };
}

/// Hash a byte-slice-accessor array (strings, binary, fixed-size binary, ...)
/// where `value(i)` returns the item directly.
///
/// Kept as a macro because `a.value(i)` returns a borrow whose lifetime
/// depends on the array, which Rust's closure inference does not propagate
/// through a `Fn(&A, usize) -> B` bound with a free type parameter `B`.
#[macro_export]
#[doc(hidden)]
macro_rules! hash_array {
    ($array_type:ident, $column:ident, $hashes:ident, $hash_method:ident) => {
        let array = $crate::downcast_array!($column, $array_type);
        if array.null_count() == 0 {
            for i in 0..$hashes.len() {
                $hashes[i] = $hash_method(&array.value(i), $hashes[i]);
            }
        } else {
            for i in 0..$hashes.len() {
                if !array.is_null(i) {
                    $hashes[i] = $hash_method(&array.value(i), $hashes[i]);
                }
            }
        }
    };
}

/// Hash a `BooleanArray`, widening each value to `$hash_input_type` first.
#[macro_export]
#[doc(hidden)]
macro_rules! hash_array_boolean {
    ($array_type:ident, $column:ident, $hash_input_type:ident, $hashes:ident, $hash_method:ident) => {
        let array = $crate::downcast_array!($column, $array_type);
        $crate::function::hash::utils::hash_bytes_values(
            array,
            $hashes,
            |a, i| $hash_input_type::from(a.value(i)).to_le_bytes(),
            $hash_method,
        );
    };
}

/// Hash a primitive array, widening each native value to `$ty` before hashing.
#[macro_export]
#[doc(hidden)]
macro_rules! hash_array_primitive {
    ($array_type:ident, $column:ident, $ty:ident, $hashes:ident, $hash_method:ident) => {
        let array = $crate::downcast_array!($column, $array_type);
        $crate::function::hash::utils::hash_primitive_values(
            array,
            $hashes,
            |v| (v as $ty).to_le_bytes(),
            $hash_method,
        );
    };
}

/// Hash a floating-point primitive array, normalizing `-0.0` to `0.0` per Spark.
#[macro_export]
#[doc(hidden)]
macro_rules! hash_array_primitive_float {
    ($array_type:ident, $column:ident, $ty:ident, $ty2:ident, $hashes:ident, $hash_method:ident) => {
        let array = $crate::downcast_array!($column, $array_type);
        $crate::function::hash::utils::hash_primitive_values(
            array,
            $hashes,
            |v| {
                // Spark uses 0 as hash for -0.0, see `Murmur3Hash` expression.
                if v == 0.0 && v.is_sign_negative() {
                    (0 as $ty2).to_le_bytes()
                } else {
                    (v as $ty).to_le_bytes()
                }
            },
            $hash_method,
        );
    };
}

/// Hash a small (precision <= 18) decimal array by reducing each value to `i64`.
/// Errors via `?` if a value does not fit in `i64`.
#[macro_export]
#[doc(hidden)]
macro_rules! hash_array_small_decimal {
    ($array_type:ident, $column:ident, $hashes:ident, $hash_method:ident) => {
        let array = $crate::downcast_array!($column, $array_type);
        $crate::function::hash::utils::try_hash_bytes_values(
            array,
            $hashes,
            |a, i| {
                i64::try_from(a.value(i))
                    .map(|v| v.to_le_bytes())
                    .map_err(|e| DataFusionError::Execution(e.to_string()))
            },
            $hash_method,
        )?;
    };
}

/// Hash a wide decimal array using the raw 128-bit byte representation.
#[macro_export]
#[doc(hidden)]
macro_rules! hash_array_decimal {
    ($array_type:ident, $column:ident, $hashes:ident, $hash_method:ident) => {
        let array = $crate::downcast_array!($column, $array_type);
        $crate::function::hash::utils::hash_bytes_values(
            array,
            $hashes,
            |a, i| a.value(i).to_le_bytes(),
            $hash_method,
        );
    };
}

/// Hash a list array with primitive elements by directly accessing the underlying buffer.
/// This avoids the overhead of slicing and recursive calls for common cases.
/// Supports both variable-length lists (with offsets) and fixed-size lists.
#[macro_export]
#[doc(hidden)]
macro_rules! hash_list_primitive {
    // Variable-length list variant (List/LargeList)
    (offsets: $offsets:expr, $list_array:ident, $elem_array:ident, $hashes:ident, $hash_method:ident, $value_transform:expr) => {
        if $list_array.null_count() == 0 && $elem_array.null_count() == 0 {
            for (row_idx, hash) in $hashes.iter_mut().enumerate() {
                let start = $offsets[row_idx] as usize;
                let end = $offsets[row_idx + 1] as usize;
                for elem_idx in start..end {
                    let value = $elem_array.value(elem_idx);
                    *hash = $hash_method($value_transform(value), *hash);
                }
            }
        } else {
            for (row_idx, hash) in $hashes.iter_mut().enumerate() {
                if !$list_array.is_null(row_idx) {
                    let start = $offsets[row_idx] as usize;
                    let end = $offsets[row_idx + 1] as usize;
                    for elem_idx in start..end {
                        if !$elem_array.is_null(elem_idx) {
                            let value = $elem_array.value(elem_idx);
                            *hash = $hash_method($value_transform(value), *hash);
                        }
                    }
                }
            }
        }
    };
    // Fixed-size list variant
    (fixed_size: $list_size:expr, $list_array:ident, $elem_array:ident, $hashes:ident, $hash_method:ident, $value_transform:expr) => {
        if $list_array.null_count() == 0 && $elem_array.null_count() == 0 {
            for (row_idx, hash) in $hashes.iter_mut().enumerate() {
                let start = row_idx * $list_size;
                for elem_idx in 0..$list_size {
                    let value = $elem_array.value(start + elem_idx);
                    *hash = $hash_method($value_transform(value), *hash);
                }
            }
        } else {
            for (row_idx, hash) in $hashes.iter_mut().enumerate() {
                if !$list_array.is_null(row_idx) {
                    let start = row_idx * $list_size;
                    for elem_idx in 0..$list_size {
                        if !$elem_array.is_null(start + elem_idx) {
                            let value = $elem_array.value(start + elem_idx);
                            *hash = $hash_method($value_transform(value), *hash);
                        }
                    }
                }
            }
        }
    };
}

/// Hash a list array by recursively hashing each element.
/// For each row, we hash all elements in the list.
/// Spark hashes arrays by recursively hashing each element, where each
/// element's hash is computed using the previous element's hash as the seed.
/// This creates a chain: hash(elem_n, hash(elem_n-1, ... hash(elem_0, seed)...))
/// Dispatches hash operations for List/LargeList/FixedSizeList arrays with primitive element types.
/// This macro eliminates duplication by handling the type-to-array mapping for all supported primitives.
#[macro_export]
#[doc(hidden)]
macro_rules! hash_list_with_primitive_elements {
    // Variant for List/LargeList with offsets
    (offsets: $list_array_type:ident, $list_array:ident, $values:ident, $offsets:ident, $field:expr, $hashes_buffer:ident, $hash_method:ident, $recursive_hash_method:ident, $fallback_offset_type:ty, $col:ident) => {
        match $field.data_type() {
            DataType::Int8 => {
                let elem_array = $values.as_any().downcast_ref::<Int8Array>().unwrap();
                $crate::hash_list_primitive!(offsets: $offsets, $list_array, elem_array, $hashes_buffer, $hash_method, |v: i8| (v as i32).to_le_bytes());
            }
            DataType::Int16 => {
                let elem_array = $values.as_any().downcast_ref::<Int16Array>().unwrap();
                $crate::hash_list_primitive!(offsets: $offsets, $list_array, elem_array, $hashes_buffer, $hash_method, |v: i16| (v as i32).to_le_bytes());
            }
            DataType::Int32 => {
                let elem_array = $values.as_any().downcast_ref::<Int32Array>().unwrap();
                $crate::hash_list_primitive!(offsets: $offsets, $list_array, elem_array, $hashes_buffer, $hash_method, |v: i32| v.to_le_bytes());
            }
            DataType::Int64 => {
                let elem_array = $values.as_any().downcast_ref::<Int64Array>().unwrap();
                $crate::hash_list_primitive!(offsets: $offsets, $list_array, elem_array, $hashes_buffer, $hash_method, |v: i64| v.to_le_bytes());
            }
            DataType::Float32 => {
                let elem_array = $values.as_any().downcast_ref::<Float32Array>().unwrap();
                $crate::hash_list_primitive!(offsets: $offsets, $list_array, elem_array, $hashes_buffer, $hash_method,
                    |v: f32| if v == 0.0 && v.is_sign_negative() { (0_i32).to_le_bytes() } else { v.to_le_bytes() });
            }
            DataType::Float64 => {
                let elem_array = $values.as_any().downcast_ref::<Float64Array>().unwrap();
                $crate::hash_list_primitive!(offsets: $offsets, $list_array, elem_array, $hashes_buffer, $hash_method,
                    |v: f64| if v == 0.0 && v.is_sign_negative() { (0_i64).to_le_bytes() } else { v.to_le_bytes() });
            }
            DataType::Boolean => {
                let elem_array = $values.as_any().downcast_ref::<BooleanArray>().unwrap();
                $crate::hash_list_primitive!(offsets: $offsets, $list_array, elem_array, $hashes_buffer, $hash_method, |v: bool| (i32::from(v)).to_le_bytes());
            }
            DataType::Utf8 => {
                let elem_array = $values.as_any().downcast_ref::<StringArray>().unwrap();
                if $list_array.null_count() == 0 && elem_array.null_count() == 0 {
                    for (row_idx, hash) in $hashes_buffer.iter_mut().enumerate() {
                        let start = $offsets[row_idx] as usize;
                        let end = $offsets[row_idx + 1] as usize;
                        for elem_idx in start..end {
                            *hash = $hash_method(elem_array.value(elem_idx), *hash);
                        }
                    }
                } else {
                    for (row_idx, hash) in $hashes_buffer.iter_mut().enumerate() {
                        if !$list_array.is_null(row_idx) {
                            let start = $offsets[row_idx] as usize;
                            let end = $offsets[row_idx + 1] as usize;
                            for elem_idx in start..end {
                                if !elem_array.is_null(elem_idx) {
                                    *hash = $hash_method(elem_array.value(elem_idx), *hash);
                                }
                            }
                        }
                    }
                }
            }
            DataType::Binary => {
                let elem_array = $values.as_any().downcast_ref::<BinaryArray>().unwrap();
                if $list_array.null_count() == 0 && elem_array.null_count() == 0 {
                    for (row_idx, hash) in $hashes_buffer.iter_mut().enumerate() {
                        let start = $offsets[row_idx] as usize;
                        let end = $offsets[row_idx + 1] as usize;
                        for elem_idx in start..end {
                            *hash = $hash_method(elem_array.value(elem_idx), *hash);
                        }
                    }
                } else {
                    for (row_idx, hash) in $hashes_buffer.iter_mut().enumerate() {
                        if !$list_array.is_null(row_idx) {
                            let start = $offsets[row_idx] as usize;
                            let end = $offsets[row_idx + 1] as usize;
                            for elem_idx in start..end {
                                if !elem_array.is_null(elem_idx) {
                                    *hash = $hash_method(elem_array.value(elem_idx), *hash);
                                }
                            }
                        }
                    }
                }
            }
            DataType::Date32 => {
                let elem_array = $values.as_any().downcast_ref::<Date32Array>().unwrap();
                $crate::hash_list_primitive!(offsets: $offsets, $list_array, elem_array, $hashes_buffer, $hash_method, |v: i32| v.to_le_bytes());
            }
            DataType::Timestamp(TimeUnit::Microsecond, _) => {
                let elem_array = $values.as_any().downcast_ref::<TimestampMicrosecondArray>().unwrap();
                $crate::hash_list_primitive!(offsets: $offsets, $list_array, elem_array, $hashes_buffer, $hash_method, |v: i64| v.to_le_bytes());
            }
            _ => {
                // Fall back to recursive approach for complex element types
                $crate::hash_list_array!($list_array_type, $fallback_offset_type, $col, $hashes_buffer, $recursive_hash_method);
            }
        }
    };
    // Variant for FixedSizeList with fixed size
    (fixed_size: $list_array:ident, $values:ident, $list_size:ident, $field:expr, $hashes_buffer:ident, $hash_method:ident, $recursive_hash_method:ident) => {
        match $field.data_type() {
            DataType::Int8 => {
                let elem_array = $values.as_any().downcast_ref::<Int8Array>().unwrap();
                $crate::hash_list_primitive!(fixed_size: $list_size, $list_array, elem_array, $hashes_buffer, $hash_method, |v: i8| (v as i32).to_le_bytes());
            }
            DataType::Int16 => {
                let elem_array = $values.as_any().downcast_ref::<Int16Array>().unwrap();
                $crate::hash_list_primitive!(fixed_size: $list_size, $list_array, elem_array, $hashes_buffer, $hash_method, |v: i16| (v as i32).to_le_bytes());
            }
            DataType::Int32 => {
                let elem_array = $values.as_any().downcast_ref::<Int32Array>().unwrap();
                $crate::hash_list_primitive!(fixed_size: $list_size, $list_array, elem_array, $hashes_buffer, $hash_method, |v: i32| v.to_le_bytes());
            }
            DataType::Int64 => {
                let elem_array = $values.as_any().downcast_ref::<Int64Array>().unwrap();
                $crate::hash_list_primitive!(fixed_size: $list_size, $list_array, elem_array, $hashes_buffer, $hash_method, |v: i64| v.to_le_bytes());
            }
            DataType::Float32 => {
                let elem_array = $values.as_any().downcast_ref::<Float32Array>().unwrap();
                $crate::hash_list_primitive!(fixed_size: $list_size, $list_array, elem_array, $hashes_buffer, $hash_method,
                    |v: f32| if v == 0.0 && v.is_sign_negative() { (0_i32).to_le_bytes() } else { v.to_le_bytes() });
            }
            DataType::Float64 => {
                let elem_array = $values.as_any().downcast_ref::<Float64Array>().unwrap();
                $crate::hash_list_primitive!(fixed_size: $list_size, $list_array, elem_array, $hashes_buffer, $hash_method,
                    |v: f64| if v == 0.0 && v.is_sign_negative() { (0_i64).to_le_bytes() } else { v.to_le_bytes() });
            }
            DataType::Boolean => {
                let elem_array = $values.as_any().downcast_ref::<BooleanArray>().unwrap();
                $crate::hash_list_primitive!(fixed_size: $list_size, $list_array, elem_array, $hashes_buffer, $hash_method, |v: bool| (i32::from(v)).to_le_bytes());
            }
            DataType::Utf8 => {
                let elem_array = $values.as_any().downcast_ref::<StringArray>().unwrap();
                if $list_array.null_count() == 0 && elem_array.null_count() == 0 {
                    for (row_idx, hash) in $hashes_buffer.iter_mut().enumerate() {
                        let start = row_idx * $list_size;
                        for elem_idx in 0..$list_size {
                            *hash = $hash_method(elem_array.value(start + elem_idx), *hash);
                        }
                    }
                } else {
                    for (row_idx, hash) in $hashes_buffer.iter_mut().enumerate() {
                        if !$list_array.is_null(row_idx) {
                            let start = row_idx * $list_size;
                            for elem_idx in 0..$list_size {
                                if !elem_array.is_null(start + elem_idx) {
                                    *hash = $hash_method(elem_array.value(start + elem_idx), *hash);
                                }
                            }
                        }
                    }
                }
            }
            DataType::Binary => {
                let elem_array = $values.as_any().downcast_ref::<BinaryArray>().unwrap();
                if $list_array.null_count() == 0 && elem_array.null_count() == 0 {
                    for (row_idx, hash) in $hashes_buffer.iter_mut().enumerate() {
                        let start = row_idx * $list_size;
                        for elem_idx in 0..$list_size {
                            *hash = $hash_method(elem_array.value(start + elem_idx), *hash);
                        }
                    }
                } else {
                    for (row_idx, hash) in $hashes_buffer.iter_mut().enumerate() {
                        if !$list_array.is_null(row_idx) {
                            let start = row_idx * $list_size;
                            for elem_idx in 0..$list_size {
                                if !elem_array.is_null(start + elem_idx) {
                                    *hash = $hash_method(elem_array.value(start + elem_idx), *hash);
                                }
                            }
                        }
                    }
                }
            }
            DataType::Date32 => {
                let elem_array = $values.as_any().downcast_ref::<Date32Array>().unwrap();
                $crate::hash_list_primitive!(fixed_size: $list_size, $list_array, elem_array, $hashes_buffer, $hash_method, |v: i32| v.to_le_bytes());
            }
            DataType::Timestamp(TimeUnit::Microsecond, _) => {
                let elem_array = $values.as_any().downcast_ref::<TimestampMicrosecondArray>().unwrap();
                $crate::hash_list_primitive!(fixed_size: $list_size, $list_array, elem_array, $hashes_buffer, $hash_method, |v: i64| v.to_le_bytes());
            }
            _ => {
                // Fall back to recursive approach for complex element types
                if $list_array.null_count() == 0 {
                    for (row_idx, hash) in $hashes_buffer.iter_mut().enumerate() {
                        let start = row_idx * $list_size;
                        for elem_idx in 0..$list_size {
                            let elem_array = $values.slice(start + elem_idx, 1);
                            let mut single_hash = [*hash];
                            $recursive_hash_method(&[elem_array], &mut single_hash)?;
                            *hash = single_hash[0];
                        }
                    }
                } else {
                    for (row_idx, hash) in $hashes_buffer.iter_mut().enumerate() {
                        if !$list_array.is_null(row_idx) {
                            let start = row_idx * $list_size;
                            for elem_idx in 0..$list_size {
                                let elem_array = $values.slice(start + elem_idx, 1);
                                let mut single_hash = [*hash];
                                $recursive_hash_method(&[elem_array], &mut single_hash)?;
                                *hash = single_hash[0];
                            }
                        }
                    }
                }
            }
        }
    };
}

/// Hash a map array by hashing its key/value entries in order.
///
/// Specializes for common key/value type combinations so the hot path stays
/// monomorphic; falls back to recursive hashing for arbitrary nested types.
#[macro_export]
#[doc(hidden)]
macro_rules! hash_map_array {
    // Specialized variant: typed key and value arrays, byte transforms.
    ($map_array:ident, $key_array:ident, $value_array:ident, $offsets:ident, $hashes_buffer:ident, $hash_method:ident, $key_transform:expr, $value_transform:expr) => {
        if $map_array.null_count() == 0
            && $key_array.null_count() == 0
            && $value_array.null_count() == 0
        {
            for (row_idx, hash) in $hashes_buffer.iter_mut().enumerate() {
                let start = $offsets[row_idx] as usize;
                let end = $offsets[row_idx + 1] as usize;
                for entry_idx in start..end {
                    *hash =
                        $hash_method($key_transform($key_array.value(entry_idx)), *hash);
                    *hash = $hash_method(
                        $value_transform($value_array.value(entry_idx)),
                        *hash,
                    );
                }
            }
        } else {
            for (row_idx, hash) in $hashes_buffer.iter_mut().enumerate() {
                if !$map_array.is_null(row_idx) {
                    let start = $offsets[row_idx] as usize;
                    let end = $offsets[row_idx + 1] as usize;
                    for entry_idx in start..end {
                        if !$key_array.is_null(entry_idx) {
                            *hash = $hash_method(
                                $key_transform($key_array.value(entry_idx)),
                                *hash,
                            );
                        }
                        if !$value_array.is_null(entry_idx) {
                            *hash = $hash_method(
                                $value_transform($value_array.value(entry_idx)),
                                *hash,
                            );
                        }
                    }
                }
            }
        }
    };
    // Fallback variant: recursively hash each key/value entry.
    (recursive: $map_array:ident, $keys:ident, $values:ident, $offsets:ident, $hashes_buffer:ident, $recursive_hash_method:ident) => {
        if $map_array.null_count() == 0 {
            for (row_idx, hash) in $hashes_buffer.iter_mut().enumerate() {
                let start = $offsets[row_idx] as usize;
                let end = $offsets[row_idx + 1] as usize;
                for entry_idx in start..end {
                    let key_array = $keys.slice(entry_idx, 1);
                    let mut single_hash = [*hash];
                    $recursive_hash_method(&[key_array], &mut single_hash)?;
                    *hash = single_hash[0];

                    let value_array = $values.slice(entry_idx, 1);
                    single_hash = [*hash];
                    $recursive_hash_method(&[value_array], &mut single_hash)?;
                    *hash = single_hash[0];
                }
            }
        } else {
            for (row_idx, hash) in $hashes_buffer.iter_mut().enumerate() {
                if !$map_array.is_null(row_idx) {
                    let start = $offsets[row_idx] as usize;
                    let end = $offsets[row_idx + 1] as usize;
                    for entry_idx in start..end {
                        let key_array = $keys.slice(entry_idx, 1);
                        let mut single_hash = [*hash];
                        $recursive_hash_method(&[key_array], &mut single_hash)?;
                        *hash = single_hash[0];

                        let value_array = $values.slice(entry_idx, 1);
                        single_hash = [*hash];
                        $recursive_hash_method(&[value_array], &mut single_hash)?;
                        *hash = single_hash[0];
                    }
                }
            }
        }
    };
}

/// Dispatch over a `MapArray`'s key/value types, calling [`hash_map_array!`]
/// with the right specialization (or the recursive fallback).
#[macro_export]
#[doc(hidden)]
macro_rules! hash_map_with_typed_entries {
    ($col:ident, $field:expr, $hashes_buffer:ident, $hash_method:ident, $recursive_hash_method:ident) => {
        let map_array = $col.as_any().downcast_ref::<MapArray>().unwrap();
        let keys = map_array.keys();
        let values = map_array.values();
        let offsets = map_array.offsets();

        if let DataType::Struct(fields) = $field.data_type() {
            let key_type = &fields[0].data_type();
            let value_type = &fields[1].data_type();

            match (key_type, value_type) {
                (DataType::Utf8, DataType::Int32) => {
                    let key_array = keys.as_any().downcast_ref::<StringArray>().unwrap();
                    let value_array = values.as_any().downcast_ref::<Int32Array>().unwrap();
                    $crate::hash_map_array!(
                        map_array, key_array, value_array, offsets, $hashes_buffer,
                        $hash_method, |v| v, |v: i32| v.to_le_bytes()
                    );
                }
                (DataType::Int32, DataType::Utf8) => {
                    let key_array = keys.as_any().downcast_ref::<Int32Array>().unwrap();
                    let value_array = values.as_any().downcast_ref::<StringArray>().unwrap();
                    $crate::hash_map_array!(
                        map_array, key_array, value_array, offsets, $hashes_buffer,
                        $hash_method, |v: i32| v.to_le_bytes(), |v| v
                    );
                }
                (DataType::Utf8, DataType::Utf8) => {
                    let key_array = keys.as_any().downcast_ref::<StringArray>().unwrap();
                    let value_array = values.as_any().downcast_ref::<StringArray>().unwrap();
                    $crate::hash_map_array!(
                        map_array, key_array, value_array, offsets, $hashes_buffer,
                        $hash_method, |v| v, |v| v
                    );
                }
                (DataType::Int32, DataType::Int32) => {
                    let key_array = keys.as_any().downcast_ref::<Int32Array>().unwrap();
                    let value_array = values.as_any().downcast_ref::<Int32Array>().unwrap();
                    $crate::hash_map_array!(
                        map_array, key_array, value_array, offsets, $hashes_buffer,
                        $hash_method, |v: i32| v.to_le_bytes(), |v: i32| v.to_le_bytes()
                    );
                }
                _ => {
                    $crate::hash_map_array!(
                        recursive: map_array, keys, values, offsets,
                        $hashes_buffer, $recursive_hash_method
                    );
                }
            }
        } else {
            return Err(DataFusionError::Internal(format!(
                "Map field type must be a struct, got: {}",
                $field.data_type()
            )));
        }
    };
}

#[macro_export]
#[doc(hidden)]
macro_rules! hash_list_array {
    ($array_type:ident, $offset_type:ty, $column: ident, $hashes: ident, $recursive_hash_method: ident) => {
        let list_array = $column
            .as_any()
            .downcast_ref::<$array_type>()
            .unwrap_or_else(|| {
                panic!(
                    "Failed to downcast column to {}. Actual data type: {:?}.",
                    stringify!($array_type),
                    $column.data_type()
                )
            });

        let values = list_array.values();
        let offsets = list_array.offsets();

        if list_array.null_count() == 0 {
            // Fast path: no nulls, skip null checks
            for (row_idx, hash) in $hashes.iter_mut().enumerate() {
                let start = offsets[row_idx] as usize;
                let end = offsets[row_idx + 1] as usize;
                let len = end - start;
                // Hash each element in sequence, chaining the hash values
                for elem_idx in 0..len {
                    let elem_array = values.slice(start + elem_idx, 1);
                    let mut single_hash = [*hash];
                    $recursive_hash_method(&[elem_array], &mut single_hash)?;
                    *hash = single_hash[0];
                }
            }
        } else {
            // Slow path: array has nulls, check each row
            for (row_idx, hash) in $hashes.iter_mut().enumerate() {
                if !list_array.is_null(row_idx) {
                    let start = offsets[row_idx] as usize;
                    let end = offsets[row_idx + 1] as usize;
                    let len = end - start;
                    // Hash each element in sequence, chaining the hash values
                    for elem_idx in 0..len {
                        let elem_array = values.slice(start + elem_idx, 1);
                        let mut single_hash = [*hash];
                        $recursive_hash_method(&[elem_array], &mut single_hash)?;
                        *hash = single_hash[0];
                    }
                }
            }
        }
    };
}

/// Creates hash values for every row, based on the values in the
/// columns.
///
/// The number of rows to hash is determined by `hashes_buffer.len()`.
/// `hashes_buffer` should be pre-sized appropriately
///
/// `hash_method` is the hash function to use.
/// `create_dictionary_hash_method` is the function to create hashes for dictionary arrays input.
/// `recursive_hash_method` is the function to call for recursive hashing of complex types.
#[macro_export]
#[doc(hidden)]
macro_rules! create_hashes_internal {
    ($arrays: ident, $hashes_buffer: ident, $hash_method: ident, $create_dictionary_hash_method: ident, $recursive_hash_method: ident) => {
        use arrow::array::{types::*, *};
        use arrow::datatypes::{DataType, TimeUnit};
        use datafusion_common::DataFusionError;

        for (i, col) in $arrays.iter().enumerate() {
            let first_col = i == 0;
            match col.data_type() {
                DataType::Boolean => {
                    $crate::hash_array_boolean!(
                        BooleanArray,
                        col,
                        i32,
                        $hashes_buffer,
                        $hash_method
                    );
                }
                DataType::Int8 => {
                    $crate::hash_array_primitive!(
                        Int8Array,
                        col,
                        i32,
                        $hashes_buffer,
                        $hash_method
                    );
                }
                DataType::Int16 => {
                    $crate::hash_array_primitive!(
                        Int16Array,
                        col,
                        i32,
                        $hashes_buffer,
                        $hash_method
                    );
                }
                DataType::Int32 => {
                    $crate::hash_array_primitive!(
                        Int32Array,
                        col,
                        i32,
                        $hashes_buffer,
                        $hash_method
                    );
                }
                DataType::Int64 => {
                    $crate::hash_array_primitive!(
                        Int64Array,
                        col,
                        i64,
                        $hashes_buffer,
                        $hash_method
                    );
                }
                DataType::Float32 => {
                    $crate::hash_array_primitive_float!(
                        Float32Array,
                        col,
                        f32,
                        i32,
                        $hashes_buffer,
                        $hash_method
                    );
                }
                DataType::Float64 => {
                    $crate::hash_array_primitive_float!(
                        Float64Array,
                        col,
                        f64,
                        i64,
                        $hashes_buffer,
                        $hash_method
                    );
                }
                DataType::Timestamp(TimeUnit::Second, _) => {
                    $crate::hash_array_primitive!(
                        TimestampSecondArray,
                        col,
                        i64,
                        $hashes_buffer,
                        $hash_method
                    );
                }
                DataType::Timestamp(TimeUnit::Millisecond, _) => {
                    $crate::hash_array_primitive!(
                        TimestampMillisecondArray,
                        col,
                        i64,
                        $hashes_buffer,
                        $hash_method
                    );
                }
                DataType::Timestamp(TimeUnit::Microsecond, _) => {
                    $crate::hash_array_primitive!(
                        TimestampMicrosecondArray,
                        col,
                        i64,
                        $hashes_buffer,
                        $hash_method
                    );
                }
                DataType::Timestamp(TimeUnit::Nanosecond, _) => {
                    $crate::hash_array_primitive!(
                        TimestampNanosecondArray,
                        col,
                        i64,
                        $hashes_buffer,
                        $hash_method
                    );
                }
                DataType::Date32 => {
                    $crate::hash_array_primitive!(
                        Date32Array,
                        col,
                        i32,
                        $hashes_buffer,
                        $hash_method
                    );
                }
                DataType::Date64 => {
                    $crate::hash_array_primitive!(
                        Date64Array,
                        col,
                        i64,
                        $hashes_buffer,
                        $hash_method
                    );
                }
                DataType::Utf8 => {
                    $crate::hash_array!(StringArray, col, $hashes_buffer, $hash_method);
                }
                DataType::LargeUtf8 => {
                    $crate::hash_array!(LargeStringArray, col, $hashes_buffer, $hash_method);
                }
                DataType::Utf8View => {
                    $crate::hash_array!(StringViewArray, col, $hashes_buffer, $hash_method);
                }
                DataType::Binary => {
                    $crate::hash_array!(BinaryArray, col, $hashes_buffer, $hash_method);
                }
                DataType::LargeBinary => {
                    $crate::hash_array!(LargeBinaryArray, col, $hashes_buffer, $hash_method);
                }
                DataType::BinaryView => {
                    $crate::hash_array!(BinaryViewArray, col, $hashes_buffer, $hash_method);
                }
                DataType::FixedSizeBinary(_) => {
                    $crate::hash_array!(FixedSizeBinaryArray, col, $hashes_buffer, $hash_method);
                }
                DataType::Null => {
                    // Nulls don't update the hash
                }
                // Apache Spark: if it's a small decimal, i.e. precision <= 18, turn it into long and hash it.
                // Else, turn it into bytes and hash it.
                DataType::Decimal128(precision, _) if *precision <= 18 => {
                    $crate::hash_array_small_decimal!(Decimal128Array, col, $hashes_buffer, $hash_method);
                }
                DataType::Decimal128(_, _) => {
                    $crate::hash_array_decimal!(Decimal128Array, col, $hashes_buffer, $hash_method);
                }
                DataType::Dictionary(index_type, _) => match **index_type {
                    DataType::Int8 => {
                        $create_dictionary_hash_method::<Int8Type>(col, $hashes_buffer, first_col)?;
                    }
                    DataType::Int16 => {
                        $create_dictionary_hash_method::<Int16Type>(
                            col,
                            $hashes_buffer,
                            first_col,
                        )?;
                    }
                    DataType::Int32 => {
                        $create_dictionary_hash_method::<Int32Type>(
                            col,
                            $hashes_buffer,
                            first_col,
                        )?;
                    }
                    DataType::Int64 => {
                        $create_dictionary_hash_method::<Int64Type>(
                            col,
                            $hashes_buffer,
                            first_col,
                        )?;
                    }
                    DataType::UInt8 => {
                        $create_dictionary_hash_method::<UInt8Type>(
                            col,
                            $hashes_buffer,
                            first_col,
                        )?;
                    }
                    DataType::UInt16 => {
                        $create_dictionary_hash_method::<UInt16Type>(
                            col,
                            $hashes_buffer,
                            first_col,
                        )?;
                    }
                    DataType::UInt32 => {
                        $create_dictionary_hash_method::<UInt32Type>(
                            col,
                            $hashes_buffer,
                            first_col,
                        )?;
                    }
                    DataType::UInt64 => {
                        $create_dictionary_hash_method::<UInt64Type>(
                            col,
                            $hashes_buffer,
                            first_col,
                        )?;
                    }
                    _ => {
                        return Err(DataFusionError::Internal(format!(
                            "Unsupported dictionary type in hasher hashing: {}",
                            col.data_type(),
                        )))
                    }
                },
                DataType::List(field) => {
                    let list_array = col.as_any().downcast_ref::<ListArray>().unwrap();
                    let values = list_array.values();
                    let offsets = list_array.offsets();

                    $crate::hash_list_with_primitive_elements!(offsets: ListArray, list_array, values, offsets, field, $hashes_buffer, $hash_method, $recursive_hash_method, i32, col);
                }
                DataType::LargeList(field) => {
                    let list_array = col.as_any().downcast_ref::<LargeListArray>().unwrap();
                    let values = list_array.values();
                    let offsets = list_array.offsets();

                    $crate::hash_list_with_primitive_elements!(offsets: LargeListArray, list_array, values, offsets, field, $hashes_buffer, $hash_method, $recursive_hash_method, i64, col);
                }
                DataType::FixedSizeList(field, size) => {
                    let list_array = col.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
                    let values = list_array.values();
                    let list_size = *size as usize;

                    $crate::hash_list_with_primitive_elements!(fixed_size: list_array, values, list_size, field, $hashes_buffer, $hash_method, $recursive_hash_method);
                }
                DataType::Struct(_) => {
                    let struct_array = col.as_any().downcast_ref::<StructArray>().unwrap();
                    // Hash each field of the struct - Spark hashes all fields recursively
                    let columns: Vec<ArrayRef> = struct_array.columns().to_vec();
                    if !columns.is_empty() {
                        $recursive_hash_method(&columns, $hashes_buffer)?;
                    }
                }
                DataType::Map(field, _) => {
                    $crate::hash_map_with_typed_entries!(
                        col, field, $hashes_buffer, $hash_method, $recursive_hash_method
                    );
                }
                _ => {
                    // This is internal because we should have caught this before.
                    return Err(DataFusionError::Internal(format!(
                        "Unsupported data type in hasher: {}",
                        col.data_type()
                    )));
                }
            }
        }
    };
}