lance 8.0.0

A columnar data format that is 100x faster than Parquet for random access.
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors

//! In-memory BTree index for scalar fields.
//!
//! Provides O(log n) lookups and range queries. Used for primary key lookups
//! and scalar column filtering.
//!
//! Backed by [`super::arena_skiplist`] — a single-writer, lock-free-read
//! skiplist with no epoch reclamation. Reads (the point-lookup hot path and
//! scans) take no lock and no epoch pin; writes go through an (uncontended,
//! since the MemTable serializes them) `Mutex`.
//!
//! Three backends, chosen lazily by column type on first insert. The compact
//! backends store only a small comparable key + the row position (not the fat
//! value) so nodes are RocksDB-sized; the value is decoded from the key at
//! flush. Small nodes match RocksDB's cache behavior on the bottom-level walk.
//! - [`FixedIntBackend`] for fixed-width integers/dates: key is a compact
//!   [`FixedKey`] `{ order-preserving u64, position }` (~24B node).
//! - [`BytesBackend`] for strings / binary / `FixedSizeBinary` (UUID): key is
//!   [`BytesKey`] with the bytes stored inline for small values (UUID, short
//!   keys) and boxed only for long ones.
//! - [`ScalarBackend`] for everything else: the original `OrderableScalarValue`
//!   key (fat node, but handles arbitrary scalar types).

use std::sync::{Mutex, OnceLock};

use arrow_array::types::*;
use arrow_array::{Array, RecordBatch};
use arrow_schema::DataType;
use datafusion::common::ScalarValue;
use lance_core::{Error, Result};
use lance_index::scalar::btree::OrderableScalarValue;

use super::RowPosition;
use super::arena_skiplist::{SkipListReader, SkipListWriter, new_skiplist};

/// Composite key for the scalar (fallback) backend.
///
/// By combining (scalar_value, row_position), each entry is unique.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IndexKey {
    /// The indexed scalar value.
    pub value: OrderableScalarValue,
    /// Row position (makes the key unique for non-unique indexes).
    pub row_position: RowPosition,
}

impl PartialOrd for IndexKey {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for IndexKey {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // First compare by value, then by row_position
        match self.value.cmp(&other.value) {
            std::cmp::Ordering::Equal => self.row_position.cmp(&other.row_position),
            ord => ord,
        }
    }
}

/// Compact key for the fixed-width-integer backend: an order-preserving `u64`
/// encoding of the value plus the row position. Sorts by `(enc, position)` —
/// identical ordering to `(value, position)` because `enc` is order-preserving.
/// 16 bytes, so a node is ~24B (vs ~72B for the `OrderableScalarValue` node).
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct FixedKey {
    enc: u64,
    position: RowPosition,
}

/// Sign-flip a signed integer to an order-preserving unsigned key.
#[inline]
fn encode_signed(v: i64) -> u64 {
    (v as u64) ^ (1u64 << 63)
}

#[inline]
fn decode_signed(enc: u64) -> i64 {
    (enc ^ (1u64 << 63)) as i64
}

/// Whether `dt` is handled by the compact fixed-width-integer backend.
fn is_fixed_int(dt: &DataType) -> bool {
    matches!(
        dt,
        DataType::Int8
            | DataType::Int16
            | DataType::Int32
            | DataType::Int64
            | DataType::UInt8
            | DataType::UInt16
            | DataType::UInt32
            | DataType::UInt64
            | DataType::Date32
            | DataType::Date64
    )
}

/// Order-preserving `u64` encoding of a fixed-int `ScalarValue`, or `None` if
/// the value is null or not a fixed-int type.
fn encode_scalar(value: &ScalarValue) -> Option<u64> {
    Some(match value {
        ScalarValue::Int8(Some(v)) => encode_signed(*v as i64),
        ScalarValue::Int16(Some(v)) => encode_signed(*v as i64),
        ScalarValue::Int32(Some(v)) => encode_signed(*v as i64),
        ScalarValue::Int64(Some(v)) => encode_signed(*v),
        ScalarValue::UInt8(Some(v)) => *v as u64,
        ScalarValue::UInt16(Some(v)) => *v as u64,
        ScalarValue::UInt32(Some(v)) => *v as u64,
        ScalarValue::UInt64(Some(v)) => *v,
        ScalarValue::Date32(Some(v)) => encode_signed(*v as i64),
        ScalarValue::Date64(Some(v)) => encode_signed(*v),
        _ => return None,
    })
}

/// Decode a fixed-int `enc` back to its typed `ScalarValue` for `data_type`.
fn decode_enc(enc: u64, data_type: &DataType) -> ScalarValue {
    match data_type {
        DataType::Int8 => ScalarValue::Int8(Some(decode_signed(enc) as i8)),
        DataType::Int16 => ScalarValue::Int16(Some(decode_signed(enc) as i16)),
        DataType::Int32 => ScalarValue::Int32(Some(decode_signed(enc) as i32)),
        DataType::Int64 => ScalarValue::Int64(Some(decode_signed(enc))),
        DataType::UInt8 => ScalarValue::UInt8(Some(enc as u8)),
        DataType::UInt16 => ScalarValue::UInt16(Some(enc as u16)),
        DataType::UInt32 => ScalarValue::UInt32(Some(enc as u32)),
        DataType::UInt64 => ScalarValue::UInt64(Some(enc)),
        DataType::Date32 => ScalarValue::Date32(Some(decode_signed(enc) as i32)),
        DataType::Date64 => ScalarValue::Date64(Some(decode_signed(enc))),
        other => unreachable!("decode_enc on non-fixed-int type {other:?}"),
    }
}

/// The typed null `ScalarValue` for a fixed-int `data_type`.
fn null_scalar(data_type: &DataType) -> ScalarValue {
    match data_type {
        DataType::Int8 => ScalarValue::Int8(None),
        DataType::Int16 => ScalarValue::Int16(None),
        DataType::Int32 => ScalarValue::Int32(None),
        DataType::Int64 => ScalarValue::Int64(None),
        DataType::UInt8 => ScalarValue::UInt8(None),
        DataType::UInt16 => ScalarValue::UInt16(None),
        DataType::UInt32 => ScalarValue::UInt32(None),
        DataType::UInt64 => ScalarValue::UInt64(None),
        DataType::Date32 => ScalarValue::Date32(None),
        DataType::Date64 => ScalarValue::Date64(None),
        other => unreachable!("null_scalar on non-fixed-int type {other:?}"),
    }
}

/// Max key length stored inline in [`InlineBytes`]. 23 covers a 16-byte UUID
/// (`FixedSizeBinary(16)`) and short string/binary primary keys, keeping the
/// node a single allocation (no second cache miss on the seek).
const INLINE_CAP: usize = 23;

/// A byte key that lives inline in the node for small values and spills to the
/// heap only for long ones — so the common cases (UUID, short string PKs) get
/// the small-node win, and long keys still work (with the usual boxed penalty).
enum InlineBytes {
    Inline { len: u8, buf: [u8; INLINE_CAP] },
    Heap(Box<[u8]>),
}

impl InlineBytes {
    fn new(bytes: &[u8]) -> Self {
        if bytes.len() <= INLINE_CAP {
            let mut buf = [0u8; INLINE_CAP];
            buf[..bytes.len()].copy_from_slice(bytes);
            Self::Inline {
                len: bytes.len() as u8,
                buf,
            }
        } else {
            Self::Heap(bytes.into())
        }
    }

    #[inline]
    fn as_slice(&self) -> &[u8] {
        match self {
            Self::Inline { len, buf } => &buf[..*len as usize],
            Self::Heap(b) => b,
        }
    }
}

impl PartialEq for InlineBytes {
    fn eq(&self, other: &Self) -> bool {
        self.as_slice() == other.as_slice()
    }
}
impl Eq for InlineBytes {}
impl PartialOrd for InlineBytes {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for InlineBytes {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.as_slice().cmp(other.as_slice())
    }
}

/// Compact key for the byte backend: an order-preserving inline byte key plus
/// the row position. Sorts by `(bytes, position)` — lexicographic byte order is
/// the natural order for strings, binary, and `FixedSizeBinary`/UUID.
struct BytesKey {
    bytes: InlineBytes,
    position: RowPosition,
}

impl PartialEq for BytesKey {
    fn eq(&self, other: &Self) -> bool {
        self.position == other.position && self.bytes == other.bytes
    }
}
impl Eq for BytesKey {}
impl PartialOrd for BytesKey {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for BytesKey {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.bytes
            .cmp(&other.bytes)
            .then(self.position.cmp(&other.position))
    }
}

/// Whether `dt` is handled by the compact byte backend (strings / binary /
/// fixed-size binary, including UUID).
fn is_bytes_type(dt: &DataType) -> bool {
    matches!(
        dt,
        DataType::Utf8
            | DataType::LargeUtf8
            | DataType::Binary
            | DataType::LargeBinary
            | DataType::FixedSizeBinary(_)
    )
}

/// The order-preserving key bytes for a byte-typed `ScalarValue`, or `None` if
/// null or not a byte type. Strings encode as their UTF-8 bytes.
fn value_bytes(value: &ScalarValue) -> Option<&[u8]> {
    match value {
        ScalarValue::Utf8(Some(s)) | ScalarValue::LargeUtf8(Some(s)) => Some(s.as_bytes()),
        ScalarValue::Binary(Some(b))
        | ScalarValue::LargeBinary(Some(b))
        | ScalarValue::FixedSizeBinary(_, Some(b)) => Some(b.as_slice()),
        _ => None,
    }
}

/// Decode key bytes back to a typed `ScalarValue` for `data_type`.
fn decode_bytes(bytes: &[u8], data_type: &DataType) -> ScalarValue {
    match data_type {
        DataType::Utf8 => ScalarValue::Utf8(Some(String::from_utf8_lossy(bytes).into_owned())),
        DataType::LargeUtf8 => {
            ScalarValue::LargeUtf8(Some(String::from_utf8_lossy(bytes).into_owned()))
        }
        DataType::Binary => ScalarValue::Binary(Some(bytes.to_vec())),
        DataType::LargeBinary => ScalarValue::LargeBinary(Some(bytes.to_vec())),
        DataType::FixedSizeBinary(n) => ScalarValue::FixedSizeBinary(*n, Some(bytes.to_vec())),
        other => unreachable!("decode_bytes on non-byte type {other:?}"),
    }
}

/// The typed null `ScalarValue` for a byte `data_type`.
fn null_bytes_scalar(data_type: &DataType) -> ScalarValue {
    match data_type {
        DataType::Utf8 => ScalarValue::Utf8(None),
        DataType::LargeUtf8 => ScalarValue::LargeUtf8(None),
        DataType::Binary => ScalarValue::Binary(None),
        DataType::LargeBinary => ScalarValue::LargeBinary(None),
        DataType::FixedSizeBinary(n) => ScalarValue::FixedSizeBinary(*n, None),
        other => unreachable!("null_bytes_scalar on non-byte type {other:?}"),
    }
}

/// Compact backend for fixed-width integers / dates. The skiplist holds only
/// non-null entries as [`FixedKey`]; nulls are tracked separately (they never
/// appear in concrete point lookups and sort first at flush).
struct FixedIntBackend {
    reader: SkipListReader<FixedKey>,
    writer: Mutex<SkipListWriter<FixedKey>>,
    /// Row positions whose value is null (rare; not on the hot path).
    null_positions: Mutex<Vec<RowPosition>>,
    data_type: DataType,
}

impl FixedIntBackend {
    fn new(data_type: DataType) -> Self {
        let (writer, reader) = new_skiplist::<FixedKey>();
        Self {
            reader,
            writer: Mutex::new(writer),
            null_positions: Mutex::new(Vec::new()),
            data_type,
        }
    }

    fn insert_array(&self, array: &dyn Array, row_offset: u64) -> Result<()> {
        macro_rules! insert_int {
            ($array_type:ty, $to_i64:expr) => {{
                let typed = array
                    .as_any()
                    .downcast_ref::<arrow_array::PrimitiveArray<$array_type>>()
                    .unwrap();
                let mut writer = self.writer.lock().unwrap();
                let mut nulls: Vec<RowPosition> = Vec::new();
                for (row_idx, value) in typed.iter().enumerate() {
                    let position = row_offset + row_idx as u64;
                    match value {
                        Some(v) => writer.insert(FixedKey {
                            enc: $to_i64(v),
                            position,
                        }),
                        None => nulls.push(position),
                    }
                }
                drop(writer);
                if !nulls.is_empty() {
                    self.null_positions.lock().unwrap().extend(nulls);
                }
            }};
        }

        match array.data_type() {
            DataType::Int8 => insert_int!(Int8Type, |v: i8| encode_signed(v as i64)),
            DataType::Int16 => insert_int!(Int16Type, |v: i16| encode_signed(v as i64)),
            DataType::Int32 => insert_int!(Int32Type, |v: i32| encode_signed(v as i64)),
            DataType::Int64 => insert_int!(Int64Type, encode_signed),
            DataType::UInt8 => insert_int!(UInt8Type, |v: u8| v as u64),
            DataType::UInt16 => insert_int!(UInt16Type, |v: u16| v as u64),
            DataType::UInt32 => insert_int!(UInt32Type, |v: u32| v as u64),
            DataType::UInt64 => insert_int!(UInt64Type, |v: u64| v),
            DataType::Date32 => insert_int!(Date32Type, |v: i32| encode_signed(v as i64)),
            DataType::Date64 => insert_int!(Date64Type, encode_signed),
            other => {
                return Err(Error::invalid_input(format!(
                    "FixedIntBackend received non-fixed-int array {other:?}"
                )));
            }
        }
        Ok(())
    }

    fn get_newest_visible(
        &self,
        value: &ScalarValue,
        max_visible_row: RowPosition,
    ) -> Option<RowPosition> {
        // Concrete value lookups never hit nulls. A null query falls back to
        // the newest visible null position.
        let Some(enc) = encode_scalar(value) else {
            if value.is_null() {
                return self
                    .null_positions
                    .lock()
                    .unwrap()
                    .iter()
                    .copied()
                    .filter(|p| *p <= max_visible_row)
                    .max();
            }
            return None;
        };
        let target = FixedKey {
            enc,
            position: max_visible_row,
        };
        self.reader
            .upper_bound_with(&target, |key| (key.enc == enc).then_some(key.position))
            .flatten()
    }

    fn get(&self, value: &ScalarValue) -> Vec<RowPosition> {
        let Some(enc) = encode_scalar(value) else {
            if value.is_null() {
                return self.null_positions.lock().unwrap().clone();
            }
            return Vec::new();
        };
        let start = FixedKey { enc, position: 0 };
        let mut positions = Vec::new();
        for key in self.reader.range_from(&start) {
            if key.enc != enc {
                break;
            }
            positions.push(key.position);
        }
        positions
    }

    fn len(&self) -> usize {
        self.reader.len() + self.null_positions.lock().unwrap().len()
    }

    fn data_type(&self) -> DataType {
        self.data_type.clone()
    }

    fn snapshot(&self) -> Vec<(OrderableScalarValue, Vec<RowPosition>)> {
        let mut result: Vec<(OrderableScalarValue, Vec<RowPosition>)> = Vec::new();

        // Nulls sort first (None < Some), matching OrderableScalarValue order.
        let nulls = self.null_positions.lock().unwrap();
        if !nulls.is_empty() {
            let mut positions = nulls.clone();
            positions.sort_unstable();
            result.push((
                OrderableScalarValue(null_scalar(&self.data_type)),
                positions,
            ));
        }
        drop(nulls);

        let mut cur_enc: Option<u64> = None;
        for key in self.reader.iter() {
            if cur_enc == Some(key.enc) {
                result.last_mut().unwrap().1.push(key.position);
            } else {
                cur_enc = Some(key.enc);
                result.push((
                    OrderableScalarValue(decode_enc(key.enc, &self.data_type)),
                    vec![key.position],
                ));
            }
        }
        result
    }
}

/// Compact backend for byte-typed columns (strings / binary / `FixedSizeBinary`
/// / UUID). The skiplist holds non-null entries as [`BytesKey`] (key bytes
/// inline for small values); nulls are tracked separately and sort first.
struct BytesBackend {
    reader: SkipListReader<BytesKey>,
    writer: Mutex<SkipListWriter<BytesKey>>,
    null_positions: Mutex<Vec<RowPosition>>,
    data_type: DataType,
}

impl BytesBackend {
    fn new(data_type: DataType) -> Self {
        let (writer, reader) = new_skiplist::<BytesKey>();
        Self {
            reader,
            writer: Mutex::new(writer),
            null_positions: Mutex::new(Vec::new()),
            data_type,
        }
    }

    fn insert_array(&self, array: &dyn Array, row_offset: u64) -> Result<()> {
        // Append (position, key bytes) for each row; nulls go to the side list.
        // `$v => $to_bytes` extracts the key bytes from each non-null value
        // inline (no closure, so the borrow ties directly to the row value).
        macro_rules! insert_bytes {
            ($array_type:ty, $v:ident => $to_bytes:expr) => {{
                let typed = array.as_any().downcast_ref::<$array_type>().unwrap();
                let mut writer = self.writer.lock().unwrap();
                let mut nulls: Vec<RowPosition> = Vec::new();
                for row_idx in 0..typed.len() {
                    let position = row_offset + row_idx as u64;
                    if typed.is_null(row_idx) {
                        nulls.push(position);
                    } else {
                        let $v = typed.value(row_idx);
                        let bytes: &[u8] = $to_bytes;
                        writer.insert(BytesKey {
                            bytes: InlineBytes::new(bytes),
                            position,
                        });
                    }
                }
                drop(writer);
                if !nulls.is_empty() {
                    self.null_positions.lock().unwrap().extend(nulls);
                }
            }};
        }

        match array.data_type() {
            DataType::Utf8 => insert_bytes!(arrow_array::StringArray, v => v.as_bytes()),
            DataType::LargeUtf8 => insert_bytes!(arrow_array::LargeStringArray, v => v.as_bytes()),
            DataType::Binary => insert_bytes!(arrow_array::BinaryArray, v => v),
            DataType::LargeBinary => insert_bytes!(arrow_array::LargeBinaryArray, v => v),
            DataType::FixedSizeBinary(_) => {
                insert_bytes!(arrow_array::FixedSizeBinaryArray, v => v)
            }
            other => {
                return Err(Error::invalid_input(format!(
                    "BytesBackend received non-byte array {other:?}"
                )));
            }
        }
        Ok(())
    }

    fn get_newest_visible(
        &self,
        value: &ScalarValue,
        max_visible_row: RowPosition,
    ) -> Option<RowPosition> {
        let Some(bytes) = value_bytes(value) else {
            if value.is_null() {
                return self
                    .null_positions
                    .lock()
                    .unwrap()
                    .iter()
                    .copied()
                    .filter(|p| *p <= max_visible_row)
                    .max();
            }
            return None;
        };
        let target = BytesKey {
            bytes: InlineBytes::new(bytes),
            position: max_visible_row,
        };
        self.reader
            .upper_bound_with(&target, |key| {
                (key.bytes.as_slice() == bytes).then_some(key.position)
            })
            .flatten()
    }

    fn get(&self, value: &ScalarValue) -> Vec<RowPosition> {
        let Some(bytes) = value_bytes(value) else {
            if value.is_null() {
                return self.null_positions.lock().unwrap().clone();
            }
            return Vec::new();
        };
        let start = BytesKey {
            bytes: InlineBytes::new(bytes),
            position: 0,
        };
        let mut positions = Vec::new();
        for key in self.reader.range_from(&start) {
            if key.bytes.as_slice() != bytes {
                break;
            }
            positions.push(key.position);
        }
        positions
    }

    fn len(&self) -> usize {
        self.reader.len() + self.null_positions.lock().unwrap().len()
    }

    fn data_type(&self) -> DataType {
        self.data_type.clone()
    }

    fn snapshot(&self) -> Vec<(OrderableScalarValue, Vec<RowPosition>)> {
        let mut result: Vec<(OrderableScalarValue, Vec<RowPosition>)> = Vec::new();

        // Nulls sort first (None < Some).
        let nulls = self.null_positions.lock().unwrap();
        if !nulls.is_empty() {
            let mut positions = nulls.clone();
            positions.sort_unstable();
            result.push((
                OrderableScalarValue(null_bytes_scalar(&self.data_type)),
                positions,
            ));
        }
        drop(nulls);

        let mut cur: Option<Vec<u8>> = None;
        for key in self.reader.iter() {
            let bytes = key.bytes.as_slice();
            if cur.as_deref() == Some(bytes) {
                result.last_mut().unwrap().1.push(key.position);
            } else {
                cur = Some(bytes.to_vec());
                result.push((
                    OrderableScalarValue(decode_bytes(bytes, &self.data_type)),
                    vec![key.position],
                ));
            }
        }
        result
    }
}

/// Fallback backend for arbitrary scalar types, keyed by `OrderableScalarValue`.
struct ScalarBackend {
    reader: SkipListReader<IndexKey>,
    writer: Mutex<SkipListWriter<IndexKey>>,
}

impl ScalarBackend {
    fn new() -> Self {
        let (writer, reader) = new_skiplist::<IndexKey>();
        Self {
            reader,
            writer: Mutex::new(writer),
        }
    }

    fn add(&self, value: OrderableScalarValue, row_position: RowPosition) {
        self.writer.lock().unwrap().insert(IndexKey {
            value,
            row_position,
        });
    }

    fn insert_array(&self, array: &dyn Array, row_offset: u64) -> Result<()> {
        macro_rules! insert_primitive {
            ($array_type:ty, $scalar_variant:ident) => {{
                let typed_array = array
                    .as_any()
                    .downcast_ref::<arrow_array::PrimitiveArray<$array_type>>()
                    .unwrap();
                for (row_idx, value) in typed_array.iter().enumerate() {
                    let row_position = row_offset + row_idx as u64;
                    self.add(
                        OrderableScalarValue(ScalarValue::$scalar_variant(value)),
                        row_position,
                    );
                }
            }};
        }

        match array.data_type() {
            DataType::Int8 => insert_primitive!(Int8Type, Int8),
            DataType::Int16 => insert_primitive!(Int16Type, Int16),
            DataType::Int32 => insert_primitive!(Int32Type, Int32),
            DataType::Int64 => insert_primitive!(Int64Type, Int64),
            DataType::UInt8 => insert_primitive!(UInt8Type, UInt8),
            DataType::UInt16 => insert_primitive!(UInt16Type, UInt16),
            DataType::UInt32 => insert_primitive!(UInt32Type, UInt32),
            DataType::UInt64 => insert_primitive!(UInt64Type, UInt64),
            DataType::Float32 => insert_primitive!(Float32Type, Float32),
            DataType::Float64 => insert_primitive!(Float64Type, Float64),
            DataType::Date32 => insert_primitive!(Date32Type, Date32),
            DataType::Date64 => insert_primitive!(Date64Type, Date64),
            DataType::Utf8 => {
                let typed_array = array
                    .as_any()
                    .downcast_ref::<arrow_array::StringArray>()
                    .unwrap();
                for (row_idx, value) in typed_array.iter().enumerate() {
                    let row_position = row_offset + row_idx as u64;
                    self.add(
                        OrderableScalarValue(ScalarValue::Utf8(value.map(|s| s.to_string()))),
                        row_position,
                    );
                }
            }
            DataType::LargeUtf8 => {
                let typed_array = array
                    .as_any()
                    .downcast_ref::<arrow_array::LargeStringArray>()
                    .unwrap();
                for (row_idx, value) in typed_array.iter().enumerate() {
                    let row_position = row_offset + row_idx as u64;
                    self.add(
                        OrderableScalarValue(ScalarValue::LargeUtf8(value.map(|s| s.to_string()))),
                        row_position,
                    );
                }
            }
            DataType::Boolean => {
                let typed_array = array
                    .as_any()
                    .downcast_ref::<arrow_array::BooleanArray>()
                    .unwrap();
                for (row_idx, value) in typed_array.iter().enumerate() {
                    let row_position = row_offset + row_idx as u64;
                    self.add(
                        OrderableScalarValue(ScalarValue::Boolean(value)),
                        row_position,
                    );
                }
            }
            // Fallback for other types - use per-row extraction
            _ => {
                for row_idx in 0..array.len() {
                    let value = ScalarValue::try_from_array(array, row_idx)?;
                    let row_position = row_offset + row_idx as u64;
                    self.add(OrderableScalarValue(value), row_position);
                }
            }
        }
        Ok(())
    }

    fn get_newest_visible(
        &self,
        value: &ScalarValue,
        max_visible_row: RowPosition,
    ) -> Option<RowPosition> {
        let target = IndexKey {
            value: OrderableScalarValue(value.clone()),
            row_position: max_visible_row,
        };
        self.reader
            .upper_bound_with(&target, |key| {
                (key.value.0 == *value).then_some(key.row_position)
            })
            .flatten()
    }

    fn get(&self, value: &ScalarValue) -> Vec<RowPosition> {
        let start = IndexKey {
            value: OrderableScalarValue(value.clone()),
            row_position: 0,
        };
        let mut positions = Vec::new();
        for key in self.reader.range_from(&start) {
            if key.value.0 != *value {
                break;
            }
            positions.push(key.row_position);
        }
        positions
    }

    fn len(&self) -> usize {
        self.reader.len()
    }

    fn data_type(&self) -> Option<DataType> {
        self.reader.front_with(|key| key.value.0.data_type())
    }

    fn snapshot(&self) -> Vec<(OrderableScalarValue, Vec<RowPosition>)> {
        let mut result: Vec<(OrderableScalarValue, Vec<RowPosition>)> = Vec::new();
        for key in self.reader.iter() {
            if let Some(last) = result.last_mut()
                && last.0 == key.value
            {
                last.1.push(key.row_position);
                continue;
            }
            result.push((key.value.clone(), vec![key.row_position]));
        }
        result
    }
}

/// The chosen backend for a `BTreeMemIndex`, selected by column type.
enum Backend {
    FixedInt(FixedIntBackend),
    Bytes(BytesBackend),
    Scalar(ScalarBackend),
}

impl Backend {
    fn for_type(data_type: &DataType) -> Self {
        if is_fixed_int(data_type) {
            Self::FixedInt(FixedIntBackend::new(data_type.clone()))
        } else if is_bytes_type(data_type) {
            Self::Bytes(BytesBackend::new(data_type.clone()))
        } else {
            Self::Scalar(ScalarBackend::new())
        }
    }

    fn insert_array(&self, array: &dyn Array, row_offset: u64) -> Result<()> {
        match self {
            Self::FixedInt(b) => b.insert_array(array, row_offset),
            Self::Bytes(b) => b.insert_array(array, row_offset),
            Self::Scalar(b) => b.insert_array(array, row_offset),
        }
    }

    fn get_newest_visible(&self, value: &ScalarValue, max: RowPosition) -> Option<RowPosition> {
        match self {
            Self::FixedInt(b) => b.get_newest_visible(value, max),
            Self::Bytes(b) => b.get_newest_visible(value, max),
            Self::Scalar(b) => b.get_newest_visible(value, max),
        }
    }

    fn get(&self, value: &ScalarValue) -> Vec<RowPosition> {
        match self {
            Self::FixedInt(b) => b.get(value),
            Self::Bytes(b) => b.get(value),
            Self::Scalar(b) => b.get(value),
        }
    }

    fn len(&self) -> usize {
        match self {
            Self::FixedInt(b) => b.len(),
            Self::Bytes(b) => b.len(),
            Self::Scalar(b) => b.len(),
        }
    }

    fn data_type(&self) -> Option<DataType> {
        match self {
            Self::FixedInt(b) => Some(b.data_type()),
            Self::Bytes(b) => Some(b.data_type()),
            Self::Scalar(b) => b.data_type(),
        }
    }

    fn snapshot(&self) -> Vec<(OrderableScalarValue, Vec<RowPosition>)> {
        match self {
            Self::FixedInt(b) => b.snapshot(),
            Self::Bytes(b) => b.snapshot(),
            Self::Scalar(b) => b.snapshot(),
        }
    }
}

/// In-memory BTree index for scalar fields.
///
/// The backing `Backend` is selected lazily on first insert from the column's
/// Arrow type: a compact `FixedKey` for fixed-width integers, fat
/// `OrderableScalarValue` for everything else. Before the first insert the index
/// is empty (all reads return empty / `None`).
pub struct BTreeMemIndex {
    backend: OnceLock<Backend>,
    /// Field ID this index is built on.
    field_id: i32,
    /// Column name (for Arrow batch lookups).
    column_name: String,
}

impl std::fmt::Debug for BTreeMemIndex {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BTreeMemIndex")
            .field("field_id", &self.field_id)
            .field("column_name", &self.column_name)
            .field("len", &self.len())
            .finish()
    }
}

impl BTreeMemIndex {
    /// Create a new BTree index for the given field.
    pub fn new(field_id: i32, column_name: String) -> Self {
        Self {
            backend: OnceLock::new(),
            field_id,
            column_name,
        }
    }

    /// The newest row position for `value` that is visible at `max_visible_row`
    /// (inclusive), or `None` if the value has no visible row. A single
    /// **seek-and-stop** on the backing skiplist — no range collect, no
    /// allocation. This is the point-lookup hot path.
    pub fn get_newest_visible(
        &self,
        value: &ScalarValue,
        max_visible_row: RowPosition,
    ) -> Option<RowPosition> {
        self.backend
            .get()?
            .get_newest_visible(value, max_visible_row)
    }

    /// Get the field ID this index is built on.
    pub fn field_id(&self) -> i32 {
        self.field_id
    }

    /// Insert rows from a batch into the index.
    pub fn insert(&self, batch: &RecordBatch, row_offset: u64) -> Result<()> {
        let col_idx = batch
            .schema()
            .column_with_name(&self.column_name)
            .map(|(idx, _)| idx)
            .ok_or_else(|| {
                Error::invalid_input(format!("Column '{}' not found in batch", self.column_name))
            })?;

        let column = batch.column(col_idx);
        let backend = self
            .backend
            .get_or_init(|| Backend::for_type(column.data_type()));
        backend.insert_array(column.as_ref(), row_offset)
    }

    /// Look up row positions for an exact value.
    pub fn get(&self, value: &ScalarValue) -> Vec<RowPosition> {
        self.backend.get().map(|b| b.get(value)).unwrap_or_default()
    }

    /// Get the number of entries (not unique values).
    pub fn len(&self) -> usize {
        self.backend.get().map(|b| b.len()).unwrap_or(0)
    }

    /// Check if the index is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get the column name.
    pub fn column_name(&self) -> &str {
        &self.column_name
    }

    /// Get a snapshot of all entries grouped by value in sorted order.
    pub fn snapshot(&self) -> Vec<(OrderableScalarValue, Vec<RowPosition>)> {
        self.backend.get().map(|b| b.snapshot()).unwrap_or_default()
    }

    /// Get the data type of the indexed column.
    ///
    /// Returns None if the index is empty.
    pub fn data_type(&self) -> Option<arrow_schema::DataType> {
        self.backend.get().and_then(|b| b.data_type())
    }

    /// Export the index data as sorted RecordBatches for BTree index training.
    pub fn to_training_batches(&self, batch_size: usize) -> Result<Vec<RecordBatch>> {
        use arrow_schema::{DataType, Field, Schema};
        use lance_core::ROW_ID;
        use lance_index::scalar::registry::VALUE_COLUMN_NAME;
        use std::sync::Arc;

        let snapshot = self.snapshot();
        if snapshot.is_empty() {
            return Ok(vec![]);
        }

        let data_type = snapshot[0].0.0.data_type();
        let schema = Arc::new(Schema::new(vec![
            Field::new(VALUE_COLUMN_NAME, data_type, true),
            Field::new(ROW_ID, DataType::UInt64, false),
        ]));

        let mut batches = Vec::new();
        let mut values: Vec<ScalarValue> = Vec::with_capacity(batch_size);
        let mut row_ids: Vec<u64> = Vec::with_capacity(batch_size);

        // Expand each (value, [positions]) group into one row per position, in
        // sorted (value, position) order.
        for (value, positions) in &snapshot {
            for position in positions {
                values.push(value.0.clone());
                row_ids.push(*position);
                if values.len() >= batch_size {
                    batches.push(build_training_batch(&schema, &values, &row_ids)?);
                    values.clear();
                    row_ids.clear();
                }
            }
        }
        if !values.is_empty() {
            batches.push(build_training_batch(&schema, &values, &row_ids)?);
        }

        Ok(batches)
    }
}

/// Build a single training batch from values and row IDs.
fn build_training_batch(
    schema: &std::sync::Arc<arrow_schema::Schema>,
    values: &[ScalarValue],
    row_ids: &[u64],
) -> Result<RecordBatch> {
    use arrow_array::UInt64Array;
    use std::sync::Arc;

    let value_array = ScalarValue::iter_to_array(values.iter().cloned())?;
    let row_id_array = Arc::new(UInt64Array::from(row_ids.to_vec()));

    RecordBatch::try_new(schema.clone(), vec![value_array, row_id_array])
        .map_err(|e| Error::io(format!("Failed to create training batch: {}", e)))
}

/// Configuration for a BTree scalar index.
#[derive(Debug, Clone)]
pub struct BTreeIndexConfig {
    /// Index name.
    pub name: String,
    /// Field ID the index is built on.
    pub field_id: i32,
    /// Column name (for Arrow batch lookups).
    pub column: String,
}

#[cfg(test)]
mod tests {
    use super::*;
    use arrow_array::{Int32Array, Int64Array, StringArray, UInt32Array};
    use arrow_schema::{DataType, Field, Schema as ArrowSchema};
    use std::sync::Arc;

    fn create_test_schema() -> Arc<ArrowSchema> {
        Arc::new(ArrowSchema::new(vec![
            Field::new("id", DataType::Int32, false),
            Field::new("name", DataType::Utf8, true),
        ]))
    }

    fn create_test_batch(schema: &ArrowSchema, start_id: i32) -> RecordBatch {
        RecordBatch::try_new(
            Arc::new(schema.clone()),
            vec![
                Arc::new(Int32Array::from(vec![start_id, start_id + 1, start_id + 2])),
                Arc::new(StringArray::from(vec!["alice", "bob", "charlie"])),
            ],
        )
        .unwrap()
    }

    #[test]
    fn test_btree_index_insert_and_lookup() {
        let schema = create_test_schema();
        let index = BTreeMemIndex::new(0, "id".to_string());

        let batch = create_test_batch(&schema, 0);
        index.insert(&batch, 0).unwrap();

        assert_eq!(index.len(), 3);
        assert_eq!(index.get(&ScalarValue::Int32(Some(0))), vec![0]);
        assert_eq!(index.get(&ScalarValue::Int32(Some(1))), vec![1]);
    }

    #[test]
    fn test_btree_get_newest_visible_seek_and_stop() {
        let schema = create_test_schema();
        let index = BTreeMemIndex::new(0, "id".to_string());
        index.insert(&create_test_batch(&schema, 0), 0).unwrap();
        index.insert(&create_test_batch(&schema, 0), 3).unwrap();

        assert_eq!(
            index.get_newest_visible(&ScalarValue::Int32(Some(0)), 5),
            Some(3)
        );
        assert_eq!(
            index.get_newest_visible(&ScalarValue::Int32(Some(1)), 5),
            Some(4)
        );
        // Visibility watermark below the newest update.
        assert_eq!(
            index.get_newest_visible(&ScalarValue::Int32(Some(0)), 2),
            Some(0)
        );
        // Watermark below every version.
        assert_eq!(
            index.get_newest_visible(&ScalarValue::Int32(Some(1)), 0),
            None
        );
        // Absent key.
        assert_eq!(
            index.get_newest_visible(&ScalarValue::Int32(Some(999)), 5),
            None
        );
        let mut all = index.get(&ScalarValue::Int32(Some(0)));
        all.sort_unstable();
        assert_eq!(all, vec![0, 3]);
    }

    #[test]
    fn test_fixed_int_signed_ordering_negatives() {
        // Negative + positive i64 keys must sort correctly via the encoding.
        let schema = Arc::new(ArrowSchema::new(vec![Field::new(
            "k",
            DataType::Int64,
            true,
        )]));
        let index = BTreeMemIndex::new(0, "k".to_string());
        let batch = RecordBatch::try_new(
            schema,
            vec![Arc::new(Int64Array::from(vec![
                Some(5),
                Some(-3),
                Some(i64::MIN),
                Some(i64::MAX),
                Some(0),
            ]))],
        )
        .unwrap();
        index.insert(&batch, 0).unwrap();

        // snapshot is value-sorted; decode round-trips.
        let snap = index.snapshot();
        let values: Vec<i64> = snap
            .iter()
            .map(|(v, _)| match v.0 {
                ScalarValue::Int64(Some(x)) => x,
                _ => panic!("unexpected"),
            })
            .collect();
        assert_eq!(values, vec![i64::MIN, -3, 0, 5, i64::MAX]);
        assert_eq!(index.get(&ScalarValue::Int64(Some(-3))), vec![1]);
        assert_eq!(
            index.get_newest_visible(&ScalarValue::Int64(Some(i64::MIN)), 10),
            Some(2)
        );
    }

    #[test]
    fn test_fixed_int_unsigned() {
        let schema = Arc::new(ArrowSchema::new(vec![Field::new(
            "k",
            DataType::UInt32,
            false,
        )]));
        let index = BTreeMemIndex::new(0, "k".to_string());
        let batch = RecordBatch::try_new(
            schema,
            vec![Arc::new(UInt32Array::from(vec![10u32, 4_000_000_000, 1]))],
        )
        .unwrap();
        index.insert(&batch, 0).unwrap();
        let snap = index.snapshot();
        let values: Vec<u32> = snap
            .iter()
            .map(|(v, _)| match v.0 {
                ScalarValue::UInt32(Some(x)) => x,
                _ => panic!(),
            })
            .collect();
        assert_eq!(values, vec![1, 10, 4_000_000_000]);
        assert_eq!(
            index.get(&ScalarValue::UInt32(Some(4_000_000_000))),
            vec![1]
        );
    }

    #[test]
    fn test_fixed_int_nulls() {
        let schema = Arc::new(ArrowSchema::new(vec![Field::new(
            "k",
            DataType::Int32,
            true,
        )]));
        let index = BTreeMemIndex::new(0, "k".to_string());
        let batch = RecordBatch::try_new(
            schema,
            vec![Arc::new(Int32Array::from(vec![
                Some(7),
                None,
                Some(3),
                None,
            ]))],
        )
        .unwrap();
        index.insert(&batch, 0).unwrap();

        assert_eq!(index.len(), 4);
        // Nulls sort first.
        let snap = index.snapshot();
        assert_eq!(snap[0].0.0, ScalarValue::Int32(None));
        assert_eq!(snap[0].1, vec![1, 3]);
        assert_eq!(snap[1].0.0, ScalarValue::Int32(Some(3)));
        assert_eq!(snap[2].0.0, ScalarValue::Int32(Some(7)));
        // Null lookup returns null positions.
        let mut nulls = index.get(&ScalarValue::Int32(None));
        nulls.sort_unstable();
        assert_eq!(nulls, vec![1, 3]);
        assert_eq!(
            index.get_newest_visible(&ScalarValue::Int32(None), 10),
            Some(3)
        );
    }

    #[test]
    fn test_btree_index_multiple_batches() {
        let schema = create_test_schema();
        let index = BTreeMemIndex::new(0, "id".to_string());
        index.insert(&create_test_batch(&schema, 0), 0).unwrap();
        index.insert(&create_test_batch(&schema, 10), 3).unwrap();

        assert_eq!(index.len(), 6);
        assert_eq!(index.get(&ScalarValue::Int32(Some(10))), vec![3]);
    }

    #[test]
    fn test_btree_index_to_training_batches() {
        use lance_core::ROW_ID;
        use lance_index::scalar::registry::VALUE_COLUMN_NAME;

        let schema = create_test_schema();
        let index = BTreeMemIndex::new(0, "id".to_string());
        index.insert(&create_test_batch(&schema, 0), 0).unwrap();
        index.insert(&create_test_batch(&schema, 10), 3).unwrap();

        let batches = index.to_training_batches(100).unwrap();
        assert_eq!(batches.len(), 1);
        let batch = &batches[0];
        assert_eq!(batch.num_rows(), 6);
        assert_eq!(batch.schema().field(0).name(), VALUE_COLUMN_NAME);
        assert_eq!(batch.schema().field(1).name(), ROW_ID);

        let values = batch
            .column_by_name(VALUE_COLUMN_NAME)
            .unwrap()
            .as_any()
            .downcast_ref::<Int32Array>()
            .unwrap();
        assert_eq!(
            (0..6).map(|i| values.value(i)).collect::<Vec<_>>(),
            vec![0, 1, 2, 10, 11, 12]
        );
        let row_ids = batch
            .column_by_name(ROW_ID)
            .unwrap()
            .as_any()
            .downcast_ref::<arrow_array::UInt64Array>()
            .unwrap();
        assert_eq!(
            (0..6).map(|i| row_ids.value(i)).collect::<Vec<_>>(),
            vec![0, 1, 2, 3, 4, 5]
        );
    }

    #[test]
    fn test_btree_index_snapshot() {
        let schema = create_test_schema();
        let index = BTreeMemIndex::new(0, "id".to_string());
        index.insert(&create_test_batch(&schema, 0), 0).unwrap();

        let snapshot = index.snapshot();
        assert_eq!(snapshot.len(), 3);
        assert_eq!(snapshot[0].0.0, ScalarValue::Int32(Some(0)));
        assert_eq!(snapshot[1].0.0, ScalarValue::Int32(Some(1)));
        assert_eq!(snapshot[2].0.0, ScalarValue::Int32(Some(2)));
    }

    #[test]
    fn test_bytes_backend_strings() {
        let schema = Arc::new(ArrowSchema::new(vec![Field::new(
            "s",
            DataType::Utf8,
            true,
        )]));
        let index = BTreeMemIndex::new(0, "s".to_string());
        // Mix short (inline) and a long (> INLINE_CAP, heap) key; include a null.
        let long = "z".repeat(64);
        let batch = RecordBatch::try_new(
            schema,
            vec![Arc::new(StringArray::from(vec![
                Some("delta"),
                Some("alpha"),
                None,
                Some(long.as_str()),
                Some("alpha"), // duplicate value, newer position
            ]))],
        )
        .unwrap();
        index.insert(&batch, 0).unwrap();

        // Newest visible position for a duplicated value.
        assert_eq!(
            index.get_newest_visible(&ScalarValue::Utf8(Some("alpha".to_string())), 10),
            Some(4)
        );
        // Visibility watermark hides the newer duplicate.
        assert_eq!(
            index.get_newest_visible(&ScalarValue::Utf8(Some("alpha".to_string())), 3),
            Some(1)
        );
        // Long (heap) key round-trips.
        assert_eq!(
            index.get_newest_visible(&ScalarValue::Utf8(Some(long.clone())), 10),
            Some(3)
        );
        // snapshot: null first, then lexicographic order; decode round-trips.
        let snap = index.snapshot();
        assert_eq!(snap[0].0.0, ScalarValue::Utf8(None));
        assert_eq!(snap[0].1, vec![2]);
        assert_eq!(snap[1].0.0, ScalarValue::Utf8(Some("alpha".to_string())));
        assert_eq!(snap[1].1, vec![1, 4]);
        assert_eq!(snap[2].0.0, ScalarValue::Utf8(Some("delta".to_string())));
        assert_eq!(snap[3].0.0, ScalarValue::Utf8(Some(long)));
    }

    #[test]
    fn test_bytes_backend_fixed_size_binary_uuid() {
        // UUIDs are FixedSizeBinary(16); verify the compact byte backend.
        let schema = Arc::new(ArrowSchema::new(vec![Field::new(
            "id",
            DataType::FixedSizeBinary(16),
            false,
        )]));
        let index = BTreeMemIndex::new(0, "id".to_string());
        let a = [0x11u8; 16];
        let b = [0x22u8; 16];
        let c = [0xAAu8; 16];
        let values = vec![c.to_vec(), a.to_vec(), b.to_vec()];
        let arr = arrow_array::FixedSizeBinaryArray::try_from_iter(values.into_iter()).unwrap();
        let batch = RecordBatch::try_new(schema, vec![Arc::new(arr)]).unwrap();
        index.insert(&batch, 0).unwrap();

        // Point lookup by UUID bytes.
        assert_eq!(
            index.get_newest_visible(&ScalarValue::FixedSizeBinary(16, Some(a.to_vec())), 10),
            Some(1)
        );
        // snapshot is byte-sorted: a (0x11) < b (0x22) < c (0xAA).
        let snap = index.snapshot();
        let order: Vec<Vec<u8>> = snap
            .iter()
            .map(|(v, _)| match &v.0 {
                ScalarValue::FixedSizeBinary(16, Some(bytes)) => bytes.clone(),
                _ => panic!("unexpected"),
            })
            .collect();
        assert_eq!(order, vec![a.to_vec(), b.to_vec(), c.to_vec()]);
    }
}