oxidelake-device 0.3.0

OxideLake hardware abstraction: GpuBackend trait, CPU/CUDA/Metal backends, hardware detector
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
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
//! CPU reference implementations of the four v1 operators, over host
//! `RecordBatch`es. These define the semantics the GPU kernels must match:
//!
//! * filter: a comparison against `NULL` is `NULL`, and `NULL` predicates drop
//!   the row (SQL `WHERE` semantics); `Float64` compares as DataFusion's own
//!   filter does, in IEEE 754 totalOrder with `-0.0` read as `+0.0`;
//! * join: `NULL` keys never match;
//! * aggregate: `NULL` keys form their own group; `SUM/MIN/MAX` skip `NULL`s
//!   and are `NULL` for groups with no values; `COUNT` counts non-`NULL`s;
//!   `SUM` over `Int64` wraps on overflow, as stock DataFusion's does;
//!   `MIN`/`MAX` over `Float64` follow IEEE 754 totalOrder, the order Arrow's
//!   comparison and sort kernels use (`NaN` greatest, a `NaN` with the sign
//!   bit set least, `-0.0` below `0.0`); output is sorted by key with the
//!   `NULL` group last;
//! * vector distance: a `NULL` vector yields a `NULL` distance; cosine distance
//!   is `1 - cos(θ)` and is `NaN` when either norm is zero.

use std::collections::HashMap;
use std::sync::Arc;

use arrow::array::{
    Array, ArrayRef, BooleanArray, FixedSizeListArray, Float32Array, Float64Array, Int64Array,
    PrimitiveArray, RecordBatch, Scalar, UInt32Array,
};
use arrow::compute::kernels::cmp;
use arrow::compute::{TakeOptions, and_kleene, filter_record_batch, take};
use arrow::datatypes::{ArrowPrimitiveType, DataType, Field, Float64Type, Int64Type, Schema};
use oxidelake_core::EngineError;
use oxidelake_core::params::{
    AggregateFunction, AggregateSpec, Comparison, DistanceMetric, Literal, Predicate,
};
use rayon::prelude::*;

fn column(batch: &RecordBatch, index: usize) -> Result<&ArrayRef, EngineError> {
    batch.columns().get(index).ok_or_else(|| {
        EngineError::plan(format!(
            "column index {index} out of range for a batch with {} columns",
            batch.num_columns()
        ))
    })
}

fn int64_column<'a>(
    batch: &'a RecordBatch,
    index: usize,
    role: &str,
) -> Result<&'a Int64Array, EngineError> {
    let col = column(batch, index)?;
    col.as_any().downcast_ref::<Int64Array>().ok_or_else(|| {
        EngineError::unsupported(
            "cpu.int64_column",
            format!(
                "{role} column {index} has type {:?}; v1 supports Int64",
                col.data_type()
            ),
        )
    })
}

const NEGATIVE_ZERO_BITS: u64 = (-0.0f64).to_bits();

/// `-0.0` as `+0.0`; every other value, NaNs included, as it is.
fn positive_zero(v: f64) -> f64 {
    if v.to_bits() == NEGATIVE_ZERO_BITS {
        0.0
    } else {
        v
    }
}

/// A `Float64` column with its `-0.0`s read as `+0.0`; any other column, or
/// one holding no `-0.0`, as it is.
///
/// Arrow's `cmp` kernels order floats by IEEE 754 totalOrder, where
/// `-0.0 < +0.0`. DataFusion 55 rewrites `-0.0` to `+0.0` on both sides before
/// it calls them (`apply_cmp`), so its `FilterExec` has `-0.0 = 0.0` true and
/// `-0.0 < 0.0` false, and a GPU-placed filter has to select the rows that
/// filter selects (#28). Scanning first leaves the usual column uncopied, as
/// DataFusion's own normalization does.
fn negative_zeros_as_positive(col: &ArrayRef) -> ArrayRef {
    match col.as_any().downcast_ref::<Float64Array>() {
        Some(values)
            if values
                .values()
                .iter()
                .any(|v| v.to_bits() == NEGATIVE_ZERO_BITS) =>
        {
            Arc::new(values.unary::<_, Float64Type>(positive_zero))
        }
        _ => Arc::clone(col),
    }
}

/// Evaluates a predicate to a boolean mask (nulls where any operand is null).
///
/// `Float64` compares as DataFusion does: IEEE 754 totalOrder (a NaN sorts
/// above `+inf`, or below `-inf` when its sign bit is set, and two NaNs are
/// equal only bit for bit), with `-0.0` read as `+0.0`.
pub fn evaluate_predicate(
    batch: &RecordBatch,
    predicate: &Predicate,
) -> Result<BooleanArray, EngineError> {
    match predicate {
        Predicate::Compare {
            column: idx,
            op,
            literal,
        } => {
            let col = column(batch, *idx)?;
            if *col.data_type() != literal.data_type() {
                return Err(EngineError::unsupported(
                    "cpu.filter",
                    format!(
                        "column {idx} has type {:?} but the literal is {:?}",
                        col.data_type(),
                        literal.data_type()
                    ),
                ));
            }
            let scalar: ArrayRef = match literal {
                Literal::Int64(v) => Arc::new(Int64Array::from(vec![*v])),
                Literal::Float64(v) => Arc::new(Float64Array::from(vec![positive_zero(*v)])),
            };
            let scalar = Scalar::new(scalar);
            let col = &negative_zeros_as_positive(col);
            let mask = match op {
                Comparison::Eq => cmp::eq(col, &scalar),
                Comparison::Lt => cmp::lt(col, &scalar),
                Comparison::LtEq => cmp::lt_eq(col, &scalar),
                Comparison::Gt => cmp::gt(col, &scalar),
                Comparison::GtEq => cmp::gt_eq(col, &scalar),
            }?;
            Ok(mask)
        }
        Predicate::And(left, right) => {
            let l = evaluate_predicate(batch, left)?;
            let r = evaluate_predicate(batch, right)?;
            Ok(and_kleene(&l, &r)?)
        }
    }
}

/// Fused filter + projection.
pub fn filter_project(
    batch: &RecordBatch,
    predicate: &Predicate,
    projection: &[usize],
) -> Result<RecordBatch, EngineError> {
    let mask = evaluate_predicate(batch, predicate)?;
    let projected = batch.project(projection)?;
    Ok(filter_record_batch(&projected, &mask)?)
}

/// Hasher for `Int64` join keys: one multiply-xorshift round (SplitMix64's
/// finalizer) instead of SipHash. Keys are data, not attacker-chosen hash-flood
/// input at this layer — the build side is bounded by the query's own join.
#[derive(Default, Clone, Copy)]
struct KeyHasher(u64);

impl std::hash::Hasher for KeyHasher {
    fn finish(&self) -> u64 {
        let mut z = self.0;
        z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
        z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
        z ^ (z >> 31)
    }

    fn write(&mut self, bytes: &[u8]) {
        for chunk in bytes.chunks(8) {
            let mut word = [0u8; 8];
            word[..chunk.len()].copy_from_slice(chunk);
            self.0 = self.0.rotate_left(5) ^ u64::from_le_bytes(word);
        }
    }

    fn write_i64(&mut self, i: i64) {
        self.0 = self.0.rotate_left(5) ^ (i as u64);
    }
}

/// The build side of a hash join, hashed once: `key -> range` into a flat
/// row-index array grouped by key (no per-key allocation). Built once per
/// join and probed by every probe batch — see [`hash_join_probe`].
#[derive(Debug, Clone)]
pub struct JoinTable {
    ranges: HashMap<i64, (u32, u32), std::hash::BuildHasherDefault<KeyHasher>>,
    rows: Vec<u32>,
}

impl std::fmt::Debug for KeyHasher {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("KeyHasher")
    }
}

impl JoinTable {
    /// Hashes the `Int64` key column `key` of `build`; `NULL` keys are skipped
    /// (they never match).
    pub fn build(build: &RecordBatch, key: usize) -> Result<Self, EngineError> {
        let keys = int64_column(build, key, "build join key")?;
        // Pass 1: rows per key. Pass 2: prefix offsets. Pass 3: scatter rows.
        let mut counts: HashMap<i64, u32, std::hash::BuildHasherDefault<KeyHasher>> =
            HashMap::with_capacity_and_hasher(keys.len(), Default::default());
        for key in keys.iter().flatten() {
            *counts.entry(key).or_insert(0) += 1;
        }
        let mut ranges = HashMap::with_capacity_and_hasher(counts.len(), Default::default());
        let mut next: u32 = 0;
        for (key, count) in counts {
            ranges.insert(key, (next, count));
            next = next
                .checked_add(count)
                .ok_or_else(|| EngineError::execution("build side exceeds u32::MAX rows"))?;
        }
        let mut cursor: HashMap<i64, u32, std::hash::BuildHasherDefault<KeyHasher>> =
            ranges.iter().map(|(k, (start, _))| (*k, *start)).collect();
        let mut rows = vec![0u32; next as usize];
        for (j, key) in keys.iter().enumerate() {
            let Some(key) = key else { continue };
            if let Some(slot) = cursor.get_mut(&key) {
                rows[*slot as usize] = index_u32(j)?;
                *slot += 1;
            }
        }
        Ok(Self { ranges, rows })
    }

    /// Build rows whose key equals `key`, in build order.
    pub fn matches(&self, key: i64) -> &[u32] {
        match self.ranges.get(&key) {
            Some(&(start, len)) => &self.rows[start as usize..(start + len) as usize],
            None => &[],
        }
    }

    /// `(start, count)` of `key`'s run in [`Self::row_indices`].
    fn run(&self, key: i64) -> Option<(u32, u32)> {
        self.ranges.get(&key).copied()
    }

    /// Every distinct key with its run in [`Self::row_indices`], as
    /// `(key, start, count)`, in no particular order. A backend builds its
    /// device index from these rather than hashing the build side again
    /// (see `GpuBackend::prepare_join_build`).
    pub fn runs(&self) -> impl Iterator<Item = (i64, u32, u32)> + '_ {
        self.ranges
            .iter()
            .map(|(&key, &(start, count))| (key, start, count))
    }

    /// The build rows with a non-`NULL` key, grouped by key and in build
    /// order within a key: the array every run indexes.
    pub fn row_indices(&self) -> &[u32] {
        &self.rows
    }

    /// Number of distinct non-`NULL` keys.
    pub fn distinct_keys(&self) -> usize {
        self.ranges.len()
    }

    /// Number of build rows with a non-`NULL` key.
    pub fn rows(&self) -> usize {
        self.rows.len()
    }
}

/// Inner hash join on one `Int64` key per side; output = left columns then right columns.
/// Builds the table for `right` and probes once — see [`hash_join_probe`] for
/// the reusable form.
pub fn hash_join(
    left: &RecordBatch,
    right: &RecordBatch,
    left_key: usize,
    right_key: usize,
) -> Result<RecordBatch, EngineError> {
    let table = JoinTable::build(right, right_key)?;
    hash_join_probe(left, left_key, right, &table, None)
}

/// Probes `left` against a prebuilt [`JoinTable`] over `right` and returns
/// every match in one batch. The output is `left ++ right` columns, or the
/// `projection` subset (indices into that concatenation) in projection order
/// — columns outside it are never gathered.
///
/// One batch holds the whole product, however large: a key that repeats on
/// both sides makes it the product of the two counts. A caller that must
/// stay within a batch size probes with [`JoinMatches`] and gathers ranges
/// with [`hash_join_gather`].
pub fn hash_join_probe(
    left: &RecordBatch,
    left_key: usize,
    right: &RecordBatch,
    table: &JoinTable,
    projection: Option<&[usize]>,
) -> Result<RecordBatch, EngineError> {
    let matches = JoinMatches::probe(left, left_key, table)?;
    let rows = usize::try_from(matches.len())
        .map_err(|_| EngineError::execution("join output exceeds usize::MAX rows"))?;
    hash_join_gather(left, right, table, &matches, 0, rows, projection)
}

/// One probe batch's matches against a [`JoinTable`]: a sequence in
/// probe-row order, and in build order within a probe row, kept as each
/// row's run in the table and the running total of the matches before it.
/// Nothing is materialised: [`hash_join_gather`] turns any range of the
/// sequence into output rows, so a probe batch's output can be emitted in
/// batches of a bounded size however often its keys repeat (#95).
#[derive(Debug, Clone, Default)]
pub struct JoinMatches {
    /// `offsets[i]` is probe row `i`'s first match; one entry more than
    /// there are rows, the last being the total. `u64`: one batch's matches
    /// can pass `u32::MAX` however its output is cut.
    offsets: Vec<u64>,
    /// Where probe row `i`'s run starts in the table's row array (zero when
    /// its key matches nothing).
    starts: Vec<u32>,
}

impl JoinMatches {
    /// Looks every row of `left`'s `Int64` column `left_key` up in `table`.
    /// `NULL` keys match nothing.
    pub fn probe(
        left: &RecordBatch,
        left_key: usize,
        table: &JoinTable,
    ) -> Result<Self, EngineError> {
        let keys = int64_column(left, left_key, "left join key")?;
        // Output rows are gathered with `u32` probe-row indices.
        index_u32(keys.len())?;
        let mut offsets = Vec::with_capacity(keys.len() + 1);
        let mut starts = Vec::with_capacity(keys.len());
        let mut total = 0u64;
        for key in keys.iter() {
            offsets.push(total);
            let (start, count) = key.and_then(|k| table.run(k)).unwrap_or((0, 0));
            starts.push(start);
            total += u64::from(count);
        }
        offsets.push(total);
        Ok(Self { offsets, starts })
    }

    /// Total matches: the rows this probe batch joins to.
    pub fn len(&self) -> u64 {
        self.offsets.last().copied().unwrap_or(0)
    }

    /// `true` when no probe row matched.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Probe rows looked up.
    pub fn probe_rows(&self) -> usize {
        self.starts.len()
    }

    /// The probe row and the build row of matches `first..first + rows`.
    pub fn pairs(
        &self,
        table: &JoinTable,
        first: u64,
        rows: usize,
    ) -> Result<(UInt32Array, UInt32Array), EngineError> {
        let end = u64::try_from(rows)
            .ok()
            .and_then(|r| first.checked_add(r))
            .filter(|&end| end <= self.len())
            .ok_or_else(|| {
                EngineError::execution(format!(
                    "matches {first}..+{rows} are outside a probe with {} matches",
                    self.len()
                ))
            })?;
        let mut left = Vec::with_capacity(rows);
        let mut right = Vec::with_capacity(rows);
        // The probe row holding match `first` is the last one whose offset
        // is at most `first`: a row with no matches shares its offset with
        // the row after it, so it never wins.
        let mut row = self
            .offsets
            .partition_point(|&offset| offset <= first)
            .saturating_sub(1);
        let mut at = first;
        while at < end {
            // `at < end <= total == offsets[probe_rows]`, so `row` is a probe
            // row and `row + 1` an offset.
            let (lo, hi) = (self.offsets[row], self.offsets[row + 1]);
            let upto = hi.min(end);
            if upto > at {
                let start = self.starts[row] as usize;
                let run = table
                    .rows
                    .get(start + (at - lo) as usize..start + (upto - lo) as usize)
                    .ok_or_else(|| {
                        EngineError::execution("join matches were probed against another table")
                    })?;
                left.extend(std::iter::repeat_n(index_u32(row)?, run.len()));
                right.extend_from_slice(run);
                at = upto;
            }
            row += 1;
        }
        Ok((UInt32Array::from(left), UInt32Array::from(right)))
    }
}

/// Gathers matches `first..first + rows` of `matches` (probed from `left`
/// against `table`, built over `right`) as output rows. The output is
/// `left ++ right` columns, or the `projection` subset (indices into that
/// concatenation) in projection order — columns outside it are never
/// gathered.
pub fn hash_join_gather(
    left: &RecordBatch,
    right: &RecordBatch,
    table: &JoinTable,
    matches: &JoinMatches,
    first: u64,
    rows: usize,
    projection: Option<&[usize]>,
) -> Result<RecordBatch, EngineError> {
    if matches.probe_rows() != left.num_rows() {
        return Err(EngineError::execution(format!(
            "join matches cover {} probe rows but the probe batch has {}",
            matches.probe_rows(),
            left.num_rows()
        )));
    }
    let (left_idx, right_idx) = matches.pairs(table, first, rows)?;
    // A table built over another batch must not index past this one: checked
    // here, where it costs one pass over a chunk, rather than trusted.
    let checked = Some(TakeOptions { check_bounds: true });

    let all_fields: Vec<_> = left
        .schema()
        .fields()
        .iter()
        .chain(right.schema().fields().iter())
        .cloned()
        .collect();
    let width = left.num_columns();
    let selected: Vec<usize> = match projection {
        Some(indices) => indices.to_vec(),
        None => (0..all_fields.len()).collect(),
    };
    let mut fields = Vec::with_capacity(selected.len());
    let mut columns = Vec::with_capacity(selected.len());
    for &index in &selected {
        let field = all_fields.get(index).ok_or_else(|| {
            EngineError::plan(format!(
                "join projection index {index} out of range for {} columns",
                all_fields.len()
            ))
        })?;
        let array = if index < width {
            take(left.column(index).as_ref(), &left_idx, checked.clone())?
        } else {
            take(
                right.column(index - width).as_ref(),
                &right_idx,
                checked.clone(),
            )?
        };
        fields.push(Arc::clone(field));
        columns.push(array);
    }
    Ok(RecordBatch::try_new(
        Arc::new(Schema::new(fields)),
        columns,
    )?)
}

fn index_u32(i: usize) -> Result<u32, EngineError> {
    u32::try_from(i).map_err(|_| EngineError::execution("batch exceeds u32::MAX rows"))
}

/// One aggregate's per-group state: a slot per group, `None` until the group
/// sees a non-`NULL` value — which is what makes `SUM/MIN/MAX` `NULL` for a
/// group that has none.
#[derive(Debug)]
enum Accumulator {
    Count(Vec<i64>),
    SumI64(Vec<Option<i64>>),
    SumF64(Vec<Option<f64>>),
    MinI64(Vec<Option<i64>>),
    MaxI64(Vec<Option<i64>>),
    MinF64(Vec<Option<f64>>),
    MaxF64(Vec<Option<f64>>),
}

fn primitive<'a, T: ArrowPrimitiveType>(
    values: &'a ArrayRef,
    what: &str,
) -> Result<&'a PrimitiveArray<T>, EngineError> {
    values
        .as_any()
        .downcast_ref::<PrimitiveArray<T>>()
        .ok_or_else(|| {
            EngineError::execution(format!(
                "{what} input has type {:?}, expected {:?}",
                values.data_type(),
                T::DATA_TYPE
            ))
        })
}

/// Folds every valid `values[row]` into `slots[ids[row]]`: `first` starts a
/// group's value, `next` combines it with the value the group holds.
fn fold<T: ArrowPrimitiveType>(
    values: &PrimitiveArray<T>,
    ids: &[usize],
    slots: &mut [Option<T::Native>],
    first: impl Fn(T::Native) -> T::Native,
    next: impl Fn(T::Native, T::Native) -> T::Native,
) {
    for (row, &g) in ids.iter().enumerate() {
        if values.is_valid(row) {
            let v = values.value(row);
            slots[g] = Some(match slots[g] {
                Some(held) => next(held, v),
                None => first(v),
            });
        }
    }
}

/// `MIN` in IEEE 754 totalOrder (#101). `f64::min` is IEEE minNum: it skips
/// NaN, so an all-NaN group used to keep the +inf it started from, and it
/// leaves the sign of a zero result unspecified.
fn total_min(held: f64, v: f64) -> f64 {
    if v.total_cmp(&held).is_lt() { v } else { held }
}

/// `MAX` in IEEE 754 totalOrder (#101); see [`total_min`].
fn total_max(held: f64, v: f64) -> f64 {
    if v.total_cmp(&held).is_gt() { v } else { held }
}

impl Accumulator {
    fn new(func: AggregateFunction, input: &DataType, index: usize) -> Result<Self, EngineError> {
        Ok(match (func, input) {
            (AggregateFunction::Count, DataType::Int64 | DataType::Float64) => {
                Self::Count(Vec::new())
            }
            (AggregateFunction::Sum, DataType::Int64) => Self::SumI64(Vec::new()),
            (AggregateFunction::Sum, DataType::Float64) => Self::SumF64(Vec::new()),
            (AggregateFunction::Min, DataType::Int64) => Self::MinI64(Vec::new()),
            (AggregateFunction::Max, DataType::Int64) => Self::MaxI64(Vec::new()),
            (AggregateFunction::Min, DataType::Float64) => Self::MinF64(Vec::new()),
            (AggregateFunction::Max, DataType::Float64) => Self::MaxF64(Vec::new()),
            (_, other) => {
                return Err(EngineError::unsupported(
                    "cpu.aggregate",
                    format!(
                        "aggregate input column {index} has type {other:?}; v1 supports Int64 and Float64"
                    ),
                ));
            }
        })
    }

    fn grow(&mut self, groups: usize) {
        match self {
            Self::Count(v) => v.resize(groups, 0),
            Self::SumI64(v) | Self::MinI64(v) | Self::MaxI64(v) => v.resize(groups, None),
            Self::SumF64(v) | Self::MinF64(v) | Self::MaxF64(v) => v.resize(groups, None),
        }
    }

    /// Folds input values: `values[row]` belongs to group `ids[row]`.
    fn update(&mut self, values: &ArrayRef, ids: &[usize]) -> Result<(), EngineError> {
        match self {
            Self::Count(counts) => {
                for (row, &g) in ids.iter().enumerate() {
                    if values.is_valid(row) {
                        counts[g] += 1;
                    }
                }
            }
            // Wrapping, like every stock DataFusion `SUM` accumulator
            // (`add_wrapping`), rather than an error (#100).
            Self::SumI64(slots) => fold(
                primitive::<Int64Type>(values, "SUM")?,
                ids,
                slots,
                |v| v,
                i64::wrapping_add,
            ),
            // From 0.0, not from the first value, as DataFusion's accumulator
            // starts: the SUM of {-0.0} is 0.0.
            Self::SumF64(slots) => fold(
                primitive::<Float64Type>(values, "SUM")?,
                ids,
                slots,
                |v| 0.0 + v,
                |held, v| held + v,
            ),
            Self::MinI64(slots) => fold(
                primitive::<Int64Type>(values, "MIN")?,
                ids,
                slots,
                |v| v,
                i64::min,
            ),
            Self::MaxI64(slots) => fold(
                primitive::<Int64Type>(values, "MAX")?,
                ids,
                slots,
                |v| v,
                i64::max,
            ),
            Self::MinF64(slots) => fold(
                primitive::<Float64Type>(values, "MIN")?,
                ids,
                slots,
                |v| v,
                total_min,
            ),
            Self::MaxF64(slots) => fold(
                primitive::<Float64Type>(values, "MAX")?,
                ids,
                slots,
                |v| v,
                total_max,
            ),
        }
        Ok(())
    }

    /// Folds partial results — this accumulator's own output for other rows.
    /// Partial counts add up; the SUM, MIN or MAX of partial SUM, MIN or MAX
    /// values is the same function again, so those fold like input.
    fn merge(&mut self, partial: &ArrayRef, ids: &[usize]) -> Result<(), EngineError> {
        if let Self::Count(counts) = self {
            let partial = primitive::<Int64Type>(partial, "partial COUNT")?;
            for (row, &g) in ids.iter().enumerate() {
                if partial.is_valid(row) {
                    counts[g] = counts[g].wrapping_add(partial.value(row));
                }
            }
            return Ok(());
        }
        self.update(partial, ids)
    }

    /// The output column, groups in `order`.
    fn finish(&self, order: &[usize]) -> (DataType, ArrayRef) {
        fn pick<T: Copy>(slots: &[T], order: &[usize]) -> Vec<T> {
            order.iter().map(|&g| slots[g]).collect()
        }
        match self {
            Self::Count(counts) => (
                DataType::Int64,
                Arc::new(Int64Array::from(pick(counts, order))),
            ),
            Self::SumI64(slots) | Self::MinI64(slots) | Self::MaxI64(slots) => (
                DataType::Int64,
                Arc::new(Int64Array::from(pick(slots, order))),
            ),
            Self::SumF64(slots) | Self::MinF64(slots) | Self::MaxF64(slots) => (
                DataType::Float64,
                Arc::new(Float64Array::from(pick(slots, order))),
            ),
        }
    }
}

/// Grouped `SUM/COUNT/MIN/MAX` over one `Int64` key, fed a batch at a time.
///
/// [`Self::update`] folds input rows; [`Self::merge`] folds a batch laid out
/// as this aggregation's own output — what [`aggregate`] or a device kernel
/// returned for other rows — so a device result for one batch and the CPU
/// reference for the next accumulate into the same groups, and so does the
/// final half of a two-phase aggregate. State is one slot per group and
/// aggregate however many rows pass through (#29); [`Self::finish`] emits the
/// groups sorted by key, `NULL` last.
#[derive(Debug)]
pub struct GroupedAggregator {
    group_by: usize,
    key_name: String,
    inputs: Vec<usize>,
    names: Vec<String>,
    index: HashMap<i64, usize>,
    null_group: Option<usize>,
    keys: Vec<Option<i64>>,
    accumulators: Vec<Accumulator>,
}

impl GroupedAggregator {
    /// Accumulators for `spec` over batches of `schema`, with the key and
    /// input types checked up front.
    pub fn new(schema: &Schema, spec: &AggregateSpec) -> Result<Self, EngineError> {
        let field = |index: usize| {
            schema.fields().get(index).ok_or_else(|| {
                EngineError::plan(format!(
                    "column index {index} out of range for a batch with {} columns",
                    schema.fields().len()
                ))
            })
        };
        let key = field(spec.group_by)?;
        if key.data_type() != &DataType::Int64 {
            return Err(EngineError::unsupported(
                "cpu.int64_column",
                format!(
                    "group key column {} has type {:?}; v1 supports Int64",
                    spec.group_by,
                    key.data_type()
                ),
            ));
        }
        let mut accumulators = Vec::with_capacity(spec.aggregates.len());
        let mut names = Vec::with_capacity(spec.aggregates.len());
        for (func, index) in &spec.aggregates {
            let input = field(*index)?;
            accumulators.push(Accumulator::new(*func, input.data_type(), *index)?);
            names.push(format!("{}({})", func.name(), input.name()));
        }
        Ok(Self {
            group_by: spec.group_by,
            key_name: key.name().clone(),
            inputs: spec.aggregates.iter().map(|(_, index)| *index).collect(),
            names,
            index: HashMap::new(),
            null_group: None,
            keys: Vec::new(),
            accumulators,
        })
    }

    /// Groups seen so far.
    pub fn groups(&self) -> usize {
        self.keys.len()
    }

    /// The group of every row of `keys`, opening groups for keys not seen yet.
    fn group_ids(&mut self, keys: &Int64Array) -> Vec<usize> {
        let mut ids = Vec::with_capacity(keys.len());
        for row in 0..keys.len() {
            let next = self.keys.len();
            let id = if keys.is_valid(row) {
                let key = keys.value(row);
                let groups = &mut self.keys;
                *self.index.entry(key).or_insert_with(|| {
                    groups.push(Some(key));
                    next
                })
            } else if let Some(g) = self.null_group {
                g
            } else {
                self.keys.push(None);
                self.null_group = Some(next);
                next
            };
            ids.push(id);
        }
        for accumulator in &mut self.accumulators {
            accumulator.grow(self.keys.len());
        }
        ids
    }

    /// Folds the rows of one input batch.
    pub fn update(&mut self, batch: &RecordBatch) -> Result<(), EngineError> {
        let keys = int64_column(batch, self.group_by, "group key")?;
        let ids = self.group_ids(keys);
        for (accumulator, &input) in self.accumulators.iter_mut().zip(&self.inputs) {
            accumulator.update(column(batch, input)?, &ids)?;
        }
        Ok(())
    }

    /// Folds a partial result: a batch laid out as this aggregation's output
    /// (the key, then one column per aggregate) that covers other rows.
    pub fn merge(&mut self, partial: &RecordBatch) -> Result<(), EngineError> {
        if partial.num_columns() != 1 + self.accumulators.len() {
            return Err(EngineError::execution(format!(
                "a partial aggregate has {} columns; this aggregation's output has {}",
                partial.num_columns(),
                1 + self.accumulators.len()
            )));
        }
        let keys = int64_column(partial, 0, "partial group key")?;
        let ids = self.group_ids(keys);
        for (i, accumulator) in self.accumulators.iter_mut().enumerate() {
            accumulator.merge(partial.column(1 + i), &ids)?;
        }
        Ok(())
    }

    /// The groups, sorted by key with the `NULL` group last.
    pub fn finish(self) -> Result<RecordBatch, EngineError> {
        let mut order: Vec<usize> = (0..self.keys.len()).collect();
        // `None` sorts first as an `Option`; the reference puts it last.
        order.sort_unstable_by_key(|&g| (self.keys[g].is_none(), self.keys[g]));
        let mut fields = vec![Field::new(&self.key_name, DataType::Int64, true)];
        let mut columns: Vec<ArrayRef> = vec![Arc::new(Int64Array::from(
            order.iter().map(|&g| self.keys[g]).collect::<Vec<_>>(),
        ))];
        for (accumulator, name) in self.accumulators.iter().zip(&self.names) {
            let (data_type, array) = accumulator.finish(&order);
            fields.push(Field::new(name, data_type, true));
            columns.push(array);
        }
        Ok(RecordBatch::try_new(
            Arc::new(Schema::new(fields)),
            columns,
        )?)
    }
}

/// Grouped `SUM/COUNT/MIN/MAX` over one `Int64` key, in one pass over `batch`.
pub fn aggregate(batch: &RecordBatch, spec: &AggregateSpec) -> Result<RecordBatch, EngineError> {
    let mut groups = GroupedAggregator::new(&batch.schema(), spec)?;
    groups.update(batch)?;
    groups.finish()
}

/// `x` as a signed integer that orders like IEEE 754 totalOrder: comparing
/// two keys as `i64` is `f64::total_cmp` on the values. A device has 64-bit
/// integer atomic min/max but no totalOrder on doubles, so the CUDA kernel
/// reduces these keys (`oxide_total_order_key` in `aggregation.cu` is the
/// same transform) and the host decodes them with [`from_total_order_key`].
pub const fn total_order_key(x: f64) -> i64 {
    let bits = x.to_bits() as i64;
    // Flip every bit but the sign of a negative value: negative values then
    // order backwards by magnitude, below every positive one.
    bits ^ ((((bits >> 63) as u64) >> 1) as i64)
}

/// The value whose [`total_order_key`] is `key`; the transform is its own
/// inverse. `i64::MAX` decodes to the greatest value (a positive `NaN`) and
/// `i64::MIN` to the least (a negative one), which is what makes them the
/// device's starting values for `MIN` and `MAX`.
pub const fn from_total_order_key(key: i64) -> f64 {
    f64::from_bits((key ^ ((((key >> 63) as u64) >> 1) as i64)) as u64)
}

/// Appends a `Float32` distance column between `query` and each row's vector.
pub fn vector_distance(
    batch: &RecordBatch,
    column_index: usize,
    query: &[f32],
    metric: DistanceMetric,
    output_name: &str,
) -> Result<RecordBatch, EngineError> {
    let col = column(batch, column_index)?;
    let list = col
        .as_any()
        .downcast_ref::<FixedSizeListArray>()
        .ok_or_else(|| {
            EngineError::unsupported(
                "cpu.vector_distance",
                format!(
                    "column {column_index} has type {:?}; expected FixedSizeList<Float32>",
                    col.data_type()
                ),
            )
        })?;
    let values = list
        .values()
        .as_any()
        .downcast_ref::<Float32Array>()
        .ok_or_else(|| {
            EngineError::unsupported(
                "cpu.vector_distance",
                format!(
                    "vector element type is {:?}; expected Float32",
                    list.value_type()
                ),
            )
        })?;
    let dim = usize::try_from(list.value_length())
        .map_err(|_| EngineError::plan("negative list size"))?;
    if query.len() != dim {
        return Err(EngineError::plan(format!(
            "query vector has {} dimensions but the column has {dim}",
            query.len()
        )));
    }
    let raw = values.values();
    let distances: Vec<Option<f32>> = (0..batch.num_rows())
        .into_par_iter()
        .map(|row| {
            if list.is_null(row) {
                return None;
            }
            let start = usize::try_from(list.value_offset(row)).unwrap_or(0);
            let v = &raw[start..start + dim];
            Some(match metric {
                DistanceMetric::L2 => l2(v, query),
                DistanceMetric::Cosine => cosine(v, query),
            })
        })
        .collect();

    let mut fields: Vec<_> = batch.schema().fields().iter().cloned().collect();
    fields.push(Arc::new(Field::new(output_name, DataType::Float32, true)));
    let mut columns = batch.columns().to_vec();
    columns.push(Arc::new(Float32Array::from(distances)));
    Ok(RecordBatch::try_new(
        Arc::new(Schema::new(fields)),
        columns,
    )?)
}

/// Euclidean distance.
pub fn l2(a: &[f32], b: &[f32]) -> f32 {
    a.iter()
        .zip(b)
        .map(|(x, y)| (x - y) * (x - y))
        .sum::<f32>()
        .sqrt()
}

/// Cosine distance `1 - cos(θ)`; `NaN` when either norm is zero.
pub fn cosine(a: &[f32], b: &[f32]) -> f32 {
    let dot: f32 = a.iter().zip(b).map(|(x, y)| x * y).sum();
    let na: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let nb: f32 = b.iter().map(|y| y * y).sum::<f32>().sqrt();
    if na == 0.0 || nb == 0.0 {
        f32::NAN
    } else {
        1.0 - dot / (na * nb)
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use arrow::array::{Array, StringArray};
    use arrow::buffer::NullBuffer;
    use arrow::datatypes::SchemaRef;

    use super::*;

    fn schema() -> SchemaRef {
        Arc::new(Schema::new(vec![
            Field::new("k", DataType::Int64, true),
            Field::new("v", DataType::Float64, true),
            Field::new("s", DataType::Utf8, true),
        ]))
    }

    fn batch() -> RecordBatch {
        RecordBatch::try_new(
            schema(),
            vec![
                Arc::new(Int64Array::from(vec![
                    Some(1),
                    Some(2),
                    None,
                    Some(2),
                    Some(5),
                ])),
                Arc::new(Float64Array::from(vec![
                    Some(0.5),
                    None,
                    Some(2.5),
                    Some(3.5),
                    Some(-1.0),
                ])),
                Arc::new(StringArray::from(vec![
                    Some("a"),
                    Some("b"),
                    Some("c"),
                    None,
                    Some("e"),
                ])),
            ],
        )
        .unwrap()
    }

    #[test]
    fn filter_drops_null_predicates_and_projects() {
        let pred = Predicate::and(
            Predicate::compare(0, Comparison::GtEq, Literal::Int64(2)),
            Predicate::compare(1, Comparison::Lt, Literal::Float64(4.0)),
        );
        let out = filter_project(&batch(), &pred, &[2, 0]).unwrap();
        // row1: v is NULL → dropped; row2: k NULL → dropped; row3 kept; row4: v < 4 but k=5 ≥ 2 → kept
        assert_eq!(out.num_columns(), 2);
        assert_eq!(out.schema().field(0).name(), "s");
        let ks = out.column(1).as_any().downcast_ref::<Int64Array>().unwrap();
        assert_eq!(ks.values(), &[2, 5]);
    }

    /// The order DataFusion 55's own filter compares floats in (#28).
    #[test]
    fn float_comparisons_order_as_datafusion_does() {
        let values = [
            f64::NAN,
            -f64::NAN,
            f64::INFINITY,
            f64::NEG_INFINITY,
            0.0,
            -0.0,
            4.0,
        ];
        let batch = RecordBatch::try_new(
            Arc::new(Schema::new(vec![Field::new("v", DataType::Float64, false)])),
            vec![Arc::new(Float64Array::from(values.to_vec()))],
        )
        .unwrap();
        let selected = |op, x| -> Vec<usize> {
            let predicate = Predicate::compare(0, op, Literal::Float64(x));
            let mask = evaluate_predicate(&batch, &predicate).unwrap();
            (0..mask.len()).filter(|&row| mask.value(row)).collect()
        };
        // A NaN is above +inf, and below -inf when its sign bit is set.
        assert_eq!(selected(Comparison::Gt, 4.0), [0, 2]);
        assert_eq!(selected(Comparison::Lt, f64::NEG_INFINITY), [1]);
        assert_eq!(selected(Comparison::Eq, f64::NAN), [0]);
        // -0.0 and +0.0 are one value, in the column and in the literal.
        assert_eq!(selected(Comparison::Eq, 0.0), [4, 5]);
        assert_eq!(selected(Comparison::Eq, -0.0), [4, 5]);
        assert_eq!(selected(Comparison::Lt, 0.0), [1, 3]);
        assert_eq!(selected(Comparison::GtEq, -0.0), [0, 2, 4, 5, 6]);
    }

    #[test]
    fn filter_type_mismatch_is_unsupported() {
        let pred = Predicate::compare(1, Comparison::Eq, Literal::Int64(1));
        assert!(
            filter_project(&batch(), &pred, &[0])
                .unwrap_err()
                .is_unsupported()
        );
    }

    #[test]
    fn join_skips_null_keys_and_multiplies_matches() {
        let right = RecordBatch::try_new(
            Arc::new(Schema::new(vec![
                Field::new("rk", DataType::Int64, true),
                Field::new("name", DataType::Utf8, false),
            ])),
            vec![
                Arc::new(Int64Array::from(vec![Some(2), Some(2), None, Some(9)])),
                Arc::new(StringArray::from(vec!["two-a", "two-b", "null", "nine"])),
            ],
        )
        .unwrap();
        let out = hash_join(&batch(), &right, 0, 0).unwrap();
        assert_eq!(out.num_columns(), 5);
        // left rows with k=2 are rows 1 and 3; each matches two right rows → 4 rows
        assert_eq!(out.num_rows(), 4);
        let names = out
            .column(4)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        let mut got: Vec<&str> = names.iter().flatten().collect();
        got.sort_unstable();
        assert_eq!(got, vec!["two-a", "two-a", "two-b", "two-b"]);
    }

    /// Gathering a probe's matches in ranges of any size, cut inside a probe
    /// row's matches or across rows with none, yields exactly the one-batch
    /// result, in the same order (#95).
    #[test]
    fn join_matches_gather_in_ranges_equal_one_gather() {
        let right = RecordBatch::try_new(
            Arc::new(Schema::new(vec![
                Field::new("rk", DataType::Int64, true),
                Field::new("name", DataType::Utf8, false),
            ])),
            vec![
                Arc::new(Int64Array::from(vec![
                    Some(2),
                    Some(2),
                    None,
                    Some(9),
                    Some(2),
                    Some(5),
                ])),
                Arc::new(StringArray::from(vec![
                    "a", "b", "null", "nine", "c", "five",
                ])),
            ],
        )
        .unwrap();
        let left = batch();
        let table = JoinTable::build(&right, 0).unwrap();
        let matches = JoinMatches::probe(&left, 0, &table).unwrap();
        let whole = hash_join_probe(&left, 0, &right, &table, None).unwrap();
        // k = 1, 2, NULL, 2, 5 against three 2s and one 5.
        assert_eq!(matches.len(), 7);
        assert_eq!(whole.num_rows(), 7);
        let projection = [0, 1, 3, 4];
        for chunk in 1..=8 {
            let mut parts = Vec::new();
            let mut first = 0;
            while first < matches.len() {
                let rows = chunk.min(usize::try_from(matches.len() - first).unwrap());
                let part = hash_join_gather(
                    &left,
                    &right,
                    &table,
                    &matches,
                    first,
                    rows,
                    Some(&projection),
                )
                .unwrap();
                assert_eq!(part.num_rows(), rows);
                parts.push(part);
                first += rows as u64;
            }
            let joined = arrow::compute::concat_batches(&parts[0].schema(), &parts).unwrap();
            assert_eq!(joined, whole.project(&projection).unwrap(), "chunk {chunk}");
        }
        // Out of range, or probed against another table: an error, never a panic.
        assert!(matches.pairs(&table, 7, 1).is_err());
        assert!(matches.pairs(&table, 0, usize::MAX).is_err());
        let other = JoinTable::build(&right.slice(0, 2), 0).unwrap();
        assert!(hash_join_gather(&left, &right, &other, &matches, 0, 7, None).is_err());
        assert!(hash_join_gather(&left.slice(0, 2), &right, &table, &matches, 0, 1, None).is_err());
    }

    #[test]
    fn aggregate_groups_nulls_last_and_skips_null_values() {
        let spec = AggregateSpec {
            group_by: 0,
            aggregates: vec![
                (AggregateFunction::Sum, 1),
                (AggregateFunction::Count, 1),
                (AggregateFunction::Min, 1),
                (AggregateFunction::Max, 0),
            ],
        };
        let out = aggregate(&batch(), &spec).unwrap();
        let keys = out.column(0).as_any().downcast_ref::<Int64Array>().unwrap();
        assert_eq!(
            keys.iter().collect::<Vec<_>>(),
            vec![Some(1), Some(2), Some(5), None]
        );
        let sums = out
            .column(1)
            .as_any()
            .downcast_ref::<Float64Array>()
            .unwrap();
        assert_eq!(
            sums.iter().collect::<Vec<_>>(),
            vec![Some(0.5), Some(3.5), Some(-1.0), Some(2.5)]
        );
        let counts = out.column(2).as_any().downcast_ref::<Int64Array>().unwrap();
        assert_eq!(counts.values(), &[1, 1, 1, 1]);
        assert_eq!(out.schema().field(1).name(), "SUM(v)");
        assert_eq!(out.schema().field(4).name(), "MAX(k)");
        let maxk = out.column(4).as_any().downcast_ref::<Int64Array>().unwrap();
        assert_eq!(
            maxk.iter().collect::<Vec<_>>(),
            vec![Some(1), Some(2), Some(5), None]
        );
    }

    /// `k Int64?`, `i Int64?`, `f Float64?`.
    fn edge_batch(rows: &[(Option<i64>, Option<i64>, Option<f64>)]) -> RecordBatch {
        let schema = Arc::new(Schema::new(vec![
            Field::new("k", DataType::Int64, true),
            Field::new("i", DataType::Int64, true),
            Field::new("f", DataType::Float64, true),
        ]));
        RecordBatch::try_new(
            schema,
            vec![
                Arc::new(Int64Array::from(
                    rows.iter().map(|r| r.0).collect::<Vec<_>>(),
                )),
                Arc::new(Int64Array::from(
                    rows.iter().map(|r| r.1).collect::<Vec<_>>(),
                )),
                Arc::new(Float64Array::from(
                    rows.iter().map(|r| r.2).collect::<Vec<_>>(),
                )),
            ],
        )
        .unwrap()
    }

    /// Every column as bit patterns (`None` for `NULL`), so a comparison
    /// tells `-0.0` from `0.0` and a NaN's sign.
    fn bits(batch: &RecordBatch) -> Vec<Vec<Option<u64>>> {
        batch
            .columns()
            .iter()
            .map(|c| {
                if let Some(ints) = c.as_any().downcast_ref::<Int64Array>() {
                    ints.iter().map(|v| v.map(|v| v as u64)).collect()
                } else {
                    let floats = c.as_any().downcast_ref::<Float64Array>().unwrap();
                    floats.iter().map(|v| v.map(f64::to_bits)).collect()
                }
            })
            .collect()
    }

    /// Folding batch after batch, or merging each batch's own result in
    /// either order, gives exactly what one pass over all the rows gives —
    /// the streaming aggregate's whole correctness argument (#29). The rows
    /// carry every value the accumulators treat specially: `NULL` keys and
    /// values, a group with no values, Int64 sums that wrap, NaN of both
    /// signs, signed zeros and an infinity. (A float `SUM` over NaNs of both
    /// signs would depend on the order of addition, in DataFusion too, so no
    /// group mixes them.)
    #[test]
    fn batch_by_batch_and_merged_partials_equal_one_pass() {
        use AggregateFunction::{Count, Max, Min, Sum};
        let nan = f64::NAN;
        let rows = [
            (Some(1), Some(i64::MAX - 1), Some(1.0)),
            (Some(2), Some(5), Some(nan)),
            (None, Some(i64::MAX), Some(-0.0)),
            (Some(1), Some(i64::MAX - 1), Some(nan)),
            (Some(3), None, None),
            (Some(4), Some(i64::MIN), Some(-nan)),
            (None, Some(i64::MAX), Some(0.0)),
            (Some(1), Some(-3), Some(3.0)),
            (Some(4), Some(7), Some(f64::NEG_INFINITY)),
            (Some(2), Some(-1), Some(0.25)),
        ];
        let spec = AggregateSpec {
            group_by: 0,
            aggregates: vec![
                (Sum, 1),
                (Count, 1),
                (Min, 1),
                (Max, 1),
                (Sum, 2),
                (Count, 2),
                (Min, 2),
                (Max, 2),
            ],
        };
        let whole = aggregate(&edge_batch(&rows), &spec).unwrap();
        for split in 1..rows.len() {
            let (a, b) = rows.split_at(split);
            let (a, b) = (edge_batch(a), edge_batch(b));
            let mut streamed = GroupedAggregator::new(&a.schema(), &spec).unwrap();
            streamed.update(&a).unwrap();
            streamed.update(&b).unwrap();
            let mut merged = GroupedAggregator::new(&a.schema(), &spec).unwrap();
            merged.merge(&aggregate(&b, &spec).unwrap()).unwrap();
            merged.merge(&aggregate(&a, &spec).unwrap()).unwrap();
            assert_eq!(
                bits(&streamed.finish().unwrap()),
                bits(&whole),
                "update, split at {split}"
            );
            assert_eq!(
                bits(&merged.finish().unwrap()),
                bits(&whole),
                "merge, split at {split}"
            );
        }
        // And the answers themselves: wrapped sums, totalOrder extremes.
        let sums = whole
            .column(1)
            .as_any()
            .downcast_ref::<Int64Array>()
            .unwrap();
        assert_eq!(
            sums.iter().collect::<Vec<_>>(),
            vec![Some(-7), Some(4), None, Some(i64::MIN + 7), Some(-2)]
        );
        let float = |c: usize| bits(&whole)[c].clone();
        assert_eq!(
            float(7),
            [Some(1.0), Some(0.25), None, Some(-nan), Some(-0.0)]
                .map(|v| v.map(f64::to_bits))
                .to_vec()
        );
        assert_eq!(
            float(8),
            [
                Some(nan),
                Some(nan),
                None,
                Some(f64::NEG_INFINITY),
                Some(0.0)
            ]
            .map(|v| v.map(f64::to_bits))
            .to_vec()
        );
    }

    /// A partial result that is not laid out as this aggregation's output is
    /// refused, not folded into the wrong accumulators.
    #[test]
    fn merging_a_mislaid_partial_is_an_error() {
        let spec = AggregateSpec {
            group_by: 0,
            aggregates: vec![(AggregateFunction::Count, 1)],
        };
        let mut groups = GroupedAggregator::new(&schema(), &spec).unwrap();
        assert!(groups.merge(&batch()).is_err());
        assert_eq!(groups.groups(), 0);
    }

    #[test]
    fn aggregate_over_empty_batch_has_no_groups() {
        let empty = RecordBatch::new_empty(schema());
        let spec = AggregateSpec {
            group_by: 0,
            aggregates: vec![(AggregateFunction::Count, 1)],
        };
        let out = aggregate(&empty, &spec).unwrap();
        assert_eq!(out.num_rows(), 0);
        assert_eq!(out.num_columns(), 2);
    }

    /// The integer image the CUDA kernel reduces orders exactly as
    /// `f64::total_cmp` and decodes to the same bits (#101): signed zeros,
    /// subnormals, infinities and NaNs of either sign and any payload.
    #[test]
    fn total_order_keys_order_like_total_cmp_and_round_trip() {
        let values = [
            f64::from_bits(u64::MAX),
            -f64::NAN,
            f64::NEG_INFINITY,
            f64::MIN,
            -1.0,
            -f64::from_bits(1),
            -0.0,
            0.0,
            f64::from_bits(1),
            f64::MIN_POSITIVE,
            1.0,
            f64::MAX,
            f64::INFINITY,
            f64::from_bits(0x7FF0_0000_0000_0001),
            f64::NAN,
            f64::from_bits(0x7FFF_FFFF_FFFF_FFFF),
        ];
        for a in values {
            assert_eq!(
                from_total_order_key(total_order_key(a)).to_bits(),
                a.to_bits()
            );
            for b in values {
                assert_eq!(
                    total_order_key(a).cmp(&total_order_key(b)),
                    a.total_cmp(&b),
                    "{a:?} ({:#x}) vs {b:?} ({:#x})",
                    a.to_bits(),
                    b.to_bits()
                );
            }
        }
        assert_eq!(
            from_total_order_key(i64::MAX).to_bits(),
            0x7FFF_FFFF_FFFF_FFFF
        );
        assert_eq!(from_total_order_key(i64::MIN).to_bits(), u64::MAX);
    }

    #[test]
    fn vector_distances() {
        let field = Arc::new(Field::new("item", DataType::Float32, false));
        let vectors = FixedSizeListArray::try_new(
            field,
            2,
            Arc::new(Float32Array::from(vec![1.0, 0.0, 0.0, 1.0, 3.0, 4.0])),
            Some(NullBuffer::from(vec![true, false, true])),
        )
        .unwrap();
        let schema = Arc::new(Schema::new(vec![Field::new(
            "vec",
            vectors.data_type().clone(),
            true,
        )]));
        let batch = RecordBatch::try_new(schema, vec![Arc::new(vectors)]).unwrap();
        let out = vector_distance(&batch, 0, &[1.0, 0.0], DistanceMetric::L2, "d").unwrap();
        let d = out
            .column(1)
            .as_any()
            .downcast_ref::<Float32Array>()
            .unwrap();
        assert_eq!(
            d.iter().collect::<Vec<_>>(),
            vec![Some(0.0), None, Some(20f32.sqrt())]
        );
        let out = vector_distance(&batch, 0, &[1.0, 0.0], DistanceMetric::Cosine, "d").unwrap();
        let d = out
            .column(1)
            .as_any()
            .downcast_ref::<Float32Array>()
            .unwrap();
        assert!((d.value(0)).abs() < 1e-6);
        assert!((d.value(2) - (1.0 - 3.0 / 5.0)).abs() < 1e-6);
        assert!(vector_distance(&batch, 0, &[1.0], DistanceMetric::L2, "d").is_err());
        assert!(cosine(&[0.0, 0.0], &[1.0, 0.0]).is_nan());
    }
}