vibesql-storage 0.2.0

Storage engine for vibesql SQL database
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
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
use vibesql_types::SqlValue;

use crate::database::{
    indexes::{IndexData, IndexManager},
    DatabaseConfig, SpillPolicy,
};

#[test]
fn test_range_scan_preserves_index_order() {
    // Create index data with rows that are NOT in order by row index
    // but ARE in order by indexed value
    let mut data = std::collections::BTreeMap::new();

    // col0 values: row 1 has 50, row 2 has 60, row 0 has 70
    // Index should be sorted by value: 50, 60, 70
    // Keys are now normalized to Double for consistent comparison
    data.insert(vec![SqlValue::Double(50.0)], vec![1]);
    data.insert(vec![SqlValue::Double(60.0)], vec![2]);
    data.insert(vec![SqlValue::Double(70.0)], vec![0]);

    let index_data = IndexData::InMemory { data };

    // Query: col0 > 55 should return rows in index order: [2, 0] (values 60, 70)
    let result = index_data.range_scan(
        Some(&SqlValue::Integer(55)),
        None,
        false, // exclusive start
        false,
    );

    // Result should be [2, 0] NOT [0, 2]
    // This preserves the index ordering (60 comes before 70)
    assert_eq!(
        result,
        vec![2, 0],
        "range_scan should return rows in index order (by value), not row index order"
    );
}

#[test]
fn test_range_scan_between_preserves_order() {
    // Test BETWEEN queries maintain index order
    let mut data = std::collections::BTreeMap::new();

    // Values out of row-index order
    // Keys are now normalized to Double for consistent comparison
    data.insert(vec![SqlValue::Double(40.0)], vec![5]);
    data.insert(vec![SqlValue::Double(50.0)], vec![1]);
    data.insert(vec![SqlValue::Double(60.0)], vec![2]);
    data.insert(vec![SqlValue::Double(70.0)], vec![0]);

    let index_data = IndexData::InMemory { data };

    // Query: col0 BETWEEN 45 AND 65 (i.e., col0 >= 45 AND col0 <= 65)
    let result = index_data.range_scan(
        Some(&SqlValue::Integer(45)),
        Some(&SqlValue::Integer(65)),
        true, // inclusive start
        true, // inclusive end
    );

    // Should return [1, 2] (values 50, 60) in that order
    assert_eq!(result, vec![1, 2], "BETWEEN should return rows in index order");
}

#[test]
fn test_range_scan_with_duplicate_values() {
    // Test case: multiple rows with the same indexed value
    let mut data = std::collections::BTreeMap::new();

    // Multiple rows with value 60: rows 3, 7, 2 (in insertion order)
    // Keys are now normalized to Double for consistent comparison
    data.insert(vec![SqlValue::Double(50.0)], vec![1]);
    data.insert(vec![SqlValue::Double(60.0)], vec![3, 7, 2]); // duplicates
    data.insert(vec![SqlValue::Double(70.0)], vec![0]);

    let index_data = IndexData::InMemory { data };

    // Query: col0 >= 60 should return [3, 7, 2, 0]
    // Rows with value 60 maintain insertion order, then row 0 with value 70
    let result = index_data.range_scan(
        Some(&SqlValue::Integer(60)),
        None,
        true, // inclusive start
        false,
    );

    assert_eq!(
        result,
        vec![3, 7, 2, 0],
        "Duplicate values should maintain insertion order within the same key"
    );
}

#[test]
fn test_multi_lookup_with_duplicate_values() {
    // Test case: multi_lookup with duplicate indexed values
    let mut data = std::collections::BTreeMap::new();

    // Multiple rows with value 60: rows 3, 7, 2 (in insertion order)
    // Keys are now normalized to Double for consistent comparison
    data.insert(vec![SqlValue::Double(50.0)], vec![1]);
    data.insert(vec![SqlValue::Double(60.0)], vec![3, 7, 2]); // duplicates
    data.insert(vec![SqlValue::Double(70.0)], vec![0]);

    let index_data = IndexData::InMemory { data };

    // Query: col0 IN (60, 70) should return [3, 7, 2, 0]
    // Rows with value 60 maintain insertion order, then row 0 with value 70
    let result = index_data.multi_lookup(&[SqlValue::Integer(60), SqlValue::Integer(70)]);

    assert_eq!(
        result,
        vec![3, 7, 2, 0],
        "multi_lookup with duplicate values should maintain insertion order within the same key"
    );
}

// Note: This test cannot verify DiskBacked variant in test builds because
// DISK_BACKED_THRESHOLD is set to usize::MAX when cfg(test) is active.
// The test still verifies that large index creation succeeds.
#[test]
fn test_disk_backed_index_creation_with_bulk_load() {
    // Test that large indexes can be created successfully.
    // Note: In test builds, DISK_BACKED_THRESHOLD is usize::MAX, so this will
    // create an InMemory index. The DiskBacked path is tested via benchmarks.
    use vibesql_ast::OrderDirection;
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    use crate::Row;

    // Use unique temp directory to avoid conflicts with other tests
    let temp_dir = tempfile::tempdir().expect("Failed to create temp directory");

    // Create a table schema with one integer column
    let columns = vec![ColumnSchema::new("id".to_string(), DataType::Integer, false)];
    let table_schema = TableSchema::new("test_table".to_string(), columns);

    // Create 100,500 rows - this would exceed DISK_BACKED_THRESHOLD in production
    let num_rows = 100_500;
    let table_rows: Vec<Row> =
        (0..num_rows).map(|i| Row::from_vec(vec![SqlValue::Integer(i as i64)])).collect();

    let mut index_manager = IndexManager::new();
    index_manager.set_database_path(temp_dir.path().to_path_buf());

    // Create index - will be InMemory in test builds, DiskBacked in production
    let result = index_manager.create_index(
        "idx_id".to_string(),
        "test_table".to_string(),
        "main",
        &table_schema,
        &table_rows,
        false, // non-unique
        vec![vibesql_ast::IndexColumn::Column {
            column_name: "id".to_string(),
            direction: OrderDirection::Asc,
            prefix_length: None,
        }],
        None,
        None,
    );

    assert!(result.is_ok(), "Large index creation should succeed");

    // Verify index was created
    assert!(index_manager.index_exists("idx_id"));

    // Verify index data exists (don't assert on variant type due to cfg(test) threshold)
    let index_data = index_manager.get_index_data("idx_id");
    assert!(index_data.is_some(), "Index data should be retrievable");
}

#[test]
fn test_in_memory_index_for_small_tables() {
    // Test that in-memory indexes are still used for small tables
    use vibesql_ast::OrderDirection;
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    use crate::Row;

    let columns = vec![ColumnSchema::new("value".to_string(), DataType::Integer, false)];
    let table_schema = TableSchema::new("small_table".to_string(), columns);

    // Create small number of rows (well below threshold)
    let table_rows: Vec<Row> =
        (0..100).map(|i| Row::from_vec(vec![SqlValue::Integer(i as i64)])).collect();

    let mut index_manager = IndexManager::new();

    let result = index_manager.create_index(
        "idx_value".to_string(),
        "small_table".to_string(),
        "main",
        &table_schema,
        &table_rows,
        false,
        vec![vibesql_ast::IndexColumn::Column {
            column_name: "value".to_string(),
            direction: OrderDirection::Asc,
            prefix_length: None,
        }],
        None,
        None,
    );

    assert!(result.is_ok());
    assert!(index_manager.index_exists("idx_value"));

    // Verify it's using InMemory variant
    let index_data = index_manager.get_index_data("idx_value");
    assert!(index_data.is_some());
    match index_data.unwrap() {
        IndexData::InMemory { .. } => {
            // Success - in-memory was used for small table
        }
        IndexData::DiskBacked { .. } => {
            panic!("Expected InMemory variant for small table, got DiskBacked");
        }
        IndexData::IVFFlat { .. } => {
            panic!("Expected InMemory variant for small table, got IVFFlat");
        }
        IndexData::Hnsw { .. } => {
            panic!("Expected InMemory variant for small table, got Hnsw");
        }
    }
}

#[test]
fn test_budget_enforcement_with_spill_policy() {
    // Test that memory budget is enforced with SpillToDisk policy
    use vibesql_ast::OrderDirection;
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    use crate::Row;

    // Use unique temp directory to avoid conflicts with other tests
    let temp_dir = tempfile::tempdir().expect("Failed to create temp directory");

    let columns = vec![ColumnSchema::new("value".to_string(), DataType::Integer, false)];
    let table_schema = TableSchema::new("test_table".to_string(), columns);

    // Create small rows for in-memory indexes
    let table_rows: Vec<Row> =
        (0..100).map(|i| Row::from_vec(vec![SqlValue::Integer(i as i64)])).collect();

    let mut index_manager = IndexManager::new();
    index_manager.set_database_path(temp_dir.path().to_path_buf());

    // Set a very small memory budget to force eviction
    let config = DatabaseConfig {
        memory_budget: 1000,            // 1KB - very small to force eviction
        disk_budget: 100 * 1024 * 1024, // 100MB disk
        spill_policy: SpillPolicy::SpillToDisk,
        sql_mode: vibesql_types::SqlMode::default(),
        columnar_cache_budget: 1024 * 1024, // 1MB columnar cache
    };
    index_manager.set_config(config);

    // Create first index - should succeed and be in-memory
    let result1 = index_manager.create_index(
        "idx_1".to_string(),
        "test_table".to_string(),
        "main",
        &table_schema,
        &table_rows,
        false,
        vec![vibesql_ast::IndexColumn::Column {
            column_name: "value".to_string(),
            direction: OrderDirection::Asc,
            prefix_length: None,
        }],
        None,
        None,
    );
    assert!(result1.is_ok());

    // Creating a second index should trigger eviction of the first one
    let result2 = index_manager.create_index(
        "idx_2".to_string(),
        "test_table".to_string(),
        "main",
        &table_schema,
        &table_rows,
        false,
        vec![vibesql_ast::IndexColumn::Column {
            column_name: "value".to_string(),
            direction: OrderDirection::Asc,
            prefix_length: None,
        }],
        None,
        None,
    );
    assert!(result2.is_ok());

    // Both indexes should exist (one in memory, one spilled to disk)
    assert!(index_manager.index_exists("idx_1"));
    assert!(index_manager.index_exists("idx_2"));
}

#[test]
fn test_lru_eviction_order() {
    // Test that LRU eviction selects the coldest (least recently used) index
    use std::thread;

    use instant::Duration;
    use vibesql_ast::OrderDirection;
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    use crate::Row;

    // Use unique temp directory to avoid conflicts with other tests
    let temp_dir = tempfile::tempdir().expect("Failed to create temp directory");

    let columns = vec![ColumnSchema::new("value".to_string(), DataType::Integer, false)];
    let table_schema = TableSchema::new("test_table".to_string(), columns);

    let table_rows: Vec<Row> =
        (0..50).map(|i| Row::from_vec(vec![SqlValue::Integer(i as i64)])).collect();

    let mut index_manager = IndexManager::new();
    index_manager.set_database_path(temp_dir.path().to_path_buf());

    // Small budget to trigger eviction
    let config = DatabaseConfig {
        memory_budget: 2000, // 2KB
        disk_budget: 100 * 1024 * 1024,
        spill_policy: SpillPolicy::SpillToDisk,
        sql_mode: vibesql_types::SqlMode::default(),
        columnar_cache_budget: 1024 * 1024, // 1MB columnar cache
    };
    index_manager.set_config(config);

    // Create idx_1
    index_manager
        .create_index(
            "idx_1".to_string(),
            "test_table".to_string(),
            "main",
            &table_schema,
            &table_rows,
            false,
            vec![vibesql_ast::IndexColumn::Column {
                column_name: "value".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            }],
            None,
            None,
        )
        .unwrap();

    thread::sleep(Duration::from_millis(10));

    // Create idx_2
    index_manager
        .create_index(
            "idx_2".to_string(),
            "test_table".to_string(),
            "main",
            &table_schema,
            &table_rows,
            false,
            vec![vibesql_ast::IndexColumn::Column {
                column_name: "value".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            }],
            None,
            None,
        )
        .unwrap();

    thread::sleep(Duration::from_millis(10));

    // Access idx_1 to make it "hot" (more recently used than idx_2)
    let _ = index_manager.get_index_data("idx_1");

    thread::sleep(Duration::from_millis(10));

    // Create idx_3 - should evict idx_2 (coldest), not idx_1
    index_manager
        .create_index(
            "idx_3".to_string(),
            "test_table".to_string(),
            "main",
            &table_schema,
            &table_rows,
            false,
            vec![vibesql_ast::IndexColumn::Column {
                column_name: "value".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            }],
            None,
            None,
        )
        .unwrap();

    // All indexes should still exist
    assert!(index_manager.index_exists("idx_1"));
    assert!(index_manager.index_exists("idx_2"));
    assert!(index_manager.index_exists("idx_3"));

    // idx_2 should have been evicted to disk (coldest)
    // idx_1 and idx_3 should still be in memory (hot)
    let _backend_1 = index_manager.resource_tracker.get_backend("idx_1");
    let backend_2 = index_manager.resource_tracker.get_backend("idx_2");
    let _backend_3 = index_manager.resource_tracker.get_backend("idx_3");

    // Note: Exact behavior depends on memory sizes, but idx_2 should be coldest
    assert!(backend_2.is_some());
}

#[test]
fn test_access_tracking() {
    // Test that index accesses are tracked for LRU
    use vibesql_ast::OrderDirection;
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    use crate::Row;

    let columns = vec![ColumnSchema::new("value".to_string(), DataType::Integer, false)];
    let table_schema = TableSchema::new("test_table".to_string(), columns);

    let table_rows: Vec<Row> =
        (0..10).map(|i| Row::from_vec(vec![SqlValue::Integer(i as i64)])).collect();

    let mut index_manager = IndexManager::new();

    index_manager
        .create_index(
            "idx_test".to_string(),
            "test_table".to_string(),
            "main",
            &table_schema,
            &table_rows,
            false,
            vec![vibesql_ast::IndexColumn::Column {
                column_name: "value".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            }],
            None,
            None,
        )
        .unwrap();

    // Initial access count should be 0 (creation doesn't count as access)
    let stats = index_manager.resource_tracker.get_index_stats("IDX_TEST");
    assert!(stats.is_some());
    let initial_count = stats.unwrap().get_access_count();

    // Access the index a few times
    let _ = index_manager.get_index_data("IDX_TEST");
    let _ = index_manager.get_index_data("IDX_TEST");
    let _ = index_manager.get_index_data("IDX_TEST");

    // Access count should have increased
    let stats = index_manager.resource_tracker.get_index_stats("IDX_TEST");
    assert!(stats.is_some());
    let final_count = stats.unwrap().get_access_count();

    assert!(
        final_count > initial_count,
        "Access count should increase after index accesses (initial: {}, final: {})",
        initial_count,
        final_count
    );
}

#[test]
fn test_resource_cleanup_on_drop() {
    // Test that resources are freed when indexes are dropped
    use vibesql_ast::OrderDirection;
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    use crate::Row;

    let columns = vec![ColumnSchema::new("value".to_string(), DataType::Integer, false)];
    let table_schema = TableSchema::new("test_table".to_string(), columns);

    let table_rows: Vec<Row> =
        (0..100).map(|i| Row::from_vec(vec![SqlValue::Integer(i as i64)])).collect();

    let mut index_manager = IndexManager::new();

    // Create an index
    index_manager
        .create_index(
            "idx_test".to_string(),
            "test_table".to_string(),
            "main",
            &table_schema,
            &table_rows,
            false,
            vec![vibesql_ast::IndexColumn::Column {
                column_name: "value".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            }],
            None,
            None,
        )
        .unwrap();

    // Memory should be in use
    let memory_before = index_manager.resource_tracker.memory_used();
    assert!(memory_before > 0);

    // Drop the index
    index_manager.drop_index("idx_test").unwrap();

    // Memory should be freed
    let memory_after = index_manager.resource_tracker.memory_used();
    assert_eq!(memory_after, 0, "Memory should be freed after dropping index");
}

#[test]
fn test_database_config_presets() {
    // Test that preset configurations have expected values
    let browser_config = DatabaseConfig::browser_default();
    assert_eq!(browser_config.memory_budget, 512 * 1024 * 1024); // 512MB
    assert_eq!(browser_config.disk_budget, 2 * 1024 * 1024 * 1024); // 2GB
    assert_eq!(browser_config.spill_policy, SpillPolicy::SpillToDisk);

    let server_config = DatabaseConfig::server_default();
    assert_eq!(server_config.memory_budget, 16 * 1024 * 1024 * 1024); // 16GB
    assert_eq!(server_config.disk_budget, 1024 * 1024 * 1024 * 1024); // 1TB
    assert_eq!(server_config.spill_policy, SpillPolicy::BestEffort);

    let test_config = DatabaseConfig::test_default();
    assert_eq!(test_config.memory_budget, 10 * 1024 * 1024); // 10MB
    assert_eq!(test_config.disk_budget, 100 * 1024 * 1024); // 100MB
    assert_eq!(test_config.spill_policy, SpillPolicy::SpillToDisk);
}

#[test]
fn test_index_scan_after_database_reset() {
    // Reproduces issue #1618: Index scans returning 0 rows after Database::reset()
    // This simulates the sqllogictest runner's database pooling behavior
    use vibesql_ast::{IndexColumn, OrderDirection};
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    use crate::{Database, Row};

    // Helper function to run a complete test cycle
    fn run_test_cycle(db: &mut Database, cycle_num: usize) -> Result<(), String> {
        eprintln!("\n=== Test Cycle {} ===", cycle_num);

        // Create table schema
        let columns = vec![
            ColumnSchema::new("pk".to_string(), DataType::Integer, false),
            ColumnSchema::new("col0".to_string(), DataType::Integer, false),
        ];
        let mut table_schema = TableSchema::new("tab1".to_string(), columns);
        table_schema.primary_key = Some(vec!["pk".to_string()]);

        // Create table (simulates: CREATE TABLE tab1...)
        db.create_table(table_schema.clone()).unwrap();
        eprintln!("  Created table 'tab1'");

        // Insert rows into table (simulates: INSERT INTO tab1 VALUES...)
        let rows = vec![
            Row::from_vec(vec![SqlValue::Integer(1), SqlValue::Integer(100)]),
            Row::from_vec(vec![SqlValue::Integer(2), SqlValue::Integer(200)]),
            Row::from_vec(vec![SqlValue::Integer(3), SqlValue::Integer(300)]),
            Row::from_vec(vec![SqlValue::Integer(4), SqlValue::Integer(400)]),
            Row::from_vec(vec![SqlValue::Integer(5), SqlValue::Integer(500)]),
        ];

        // Get the table and insert rows
        let table = db.get_table_mut("tab1").unwrap();
        for row in &rows {
            table.insert(row.clone()).unwrap();
        }
        eprintln!("  Inserted {} rows", rows.len());

        // Create index on col0 (simulates: CREATE INDEX idx_col0 ON tab1(col0))
        db.create_index(
            "idx_col0".to_string(),
            "tab1".to_string(),
            false,
            vec![IndexColumn::Column {
                column_name: "col0".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            }],
        )
        .unwrap();
        eprintln!("  Created index 'idx_col0'");

        // Verify index was created and populated
        assert!(db.index_exists("idx_col0"));
        let index_data = db.get_index_data("idx_col0").expect("Index should exist");

        // Check that index contains row indices
        let all_indices: Vec<usize> = match &index_data {
            crate::database::indexes::IndexData::InMemory { data, .. } => {
                data.values().flatten().copied().collect()
            }
            crate::database::indexes::IndexData::DiskBacked { .. } => {
                panic!("Expected in-memory index for small table");
            }
            crate::database::indexes::IndexData::IVFFlat { .. } => {
                panic!("Expected in-memory index for small table, got IVFFlat");
            }
            crate::database::indexes::IndexData::Hnsw { .. } => {
                panic!("Expected in-memory index for small table, got Hnsw");
            }
        };
        eprintln!("  Index contains {} row references", all_indices.len());
        assert_eq!(all_indices.len(), 5, "Index should contain 5 row references");

        // Now perform index scan (simulates: SELECT pk FROM tab1 WHERE col0 > 250)
        // Get table for index scan
        let table = db.get_table("tab1").expect("Table should exist");

        // Perform range scan on index
        let matching_row_indices = index_data.range_scan(
            Some(&SqlValue::Integer(250)), // col0 > 250
            None,
            false, // exclusive start
            false,
        );
        eprintln!("  Index range scan returned {} row indices", matching_row_indices.len());

        // This should return indices for rows 3, 4, 5 (col0 = 300, 400, 500)
        if matching_row_indices.len() != 3 {
            return Err(format!(
                "Cycle {}: Index scan should find 3 rows with col0 > 250, but found {}",
                cycle_num,
                matching_row_indices.len()
            ));
        }

        // Fetch actual rows using the indices
        let all_rows = table.scan();
        eprintln!("  Table has {} total rows", all_rows.len());

        let fetched_rows: Vec<Row> =
            matching_row_indices.into_iter().filter_map(|idx| all_rows.get(idx).cloned()).collect();
        eprintln!("  Fetched {} rows from table using index", fetched_rows.len());

        // This is the CRITICAL assertion that fails in sqllogictest after reset
        if fetched_rows.len() != 3 {
            return Err(format!(
                "Cycle {}: Should fetch 3 rows from table using index scan, but got {} rows. \
                 Table has {} total rows. This reproduces issue #1618!",
                cycle_num,
                fetched_rows.len(),
                all_rows.len()
            ));
        }

        // Verify the correct rows were returned
        assert_eq!(fetched_rows[0].values[1], SqlValue::Integer(300));
        assert_eq!(fetched_rows[1].values[1], SqlValue::Integer(400));
        assert_eq!(fetched_rows[2].values[1], SqlValue::Integer(500));

        eprintln!("  ✓ Cycle {} PASSED", cycle_num);
        Ok(())
    }

    // Simulate sqllogictest thread-local database pooling behavior:
    // - First test file: uses Database::new()
    // - Subsequent test files: reuse database after reset()

    let mut db = Database::new();

    // CYCLE 1: First test file (no reset) - this should PASS
    eprintln!("\n🔄 Running first test file (fresh database)...");
    run_test_cycle(&mut db, 1).expect("Cycle 1 should pass (fresh database)");

    // CYCLE 2: Second test file (after reset) - this should FAIL and reproduce issue #1618
    eprintln!("\n🔄 Resetting database (simulating pooling)...");
    db.reset();
    eprintln!("🔄 Running second test file (pooled database after reset)...");

    match run_test_cycle(&mut db, 2) {
        Ok(()) => {
            eprintln!("\n⚠️  WARNING: Cycle 2 passed! The bug may have been fixed.");
            eprintln!("    If this test now passes, issue #1618 is resolved.");
        }
        Err(e) => {
            panic!("\n❌ REPRODUCED ISSUE #1618:\n{}\n\nThis demonstrates the database pooling bug where index scans return 0 rows after Database::reset()", e);
        }
    }
}

#[test]
fn test_thread_local_pool_pattern() {
    // Test that mimics the EXACT thread-local pooling pattern from db_adapter.rs
    // This may better reproduce the sqllogictest runner bug
    use std::cell::RefCell;

    use vibesql_ast::{IndexColumn, OrderDirection};
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::{DataType, SqlValue};

    use crate::{Database, Row};

    thread_local! {
        static TEST_DB_POOL: RefCell<Option<Database>> = const { RefCell::new(None) };
    }

    fn get_pooled_database() -> Database {
        TEST_DB_POOL.with(|pool| {
            let mut pool_ref = pool.borrow_mut();
            match pool_ref.take() {
                Some(mut db) => {
                    eprintln!("  [POOL] Reusing pooled database after reset");
                    db.reset();
                    db
                }
                None => {
                    eprintln!("  [POOL] Creating fresh database");
                    Database::new()
                }
            }
        })
    }

    fn return_to_pool(db: Database) {
        TEST_DB_POOL.with(|pool| {
            let mut pool_ref = pool.borrow_mut();
            if pool_ref.is_none() {
                eprintln!("  [POOL] Returning database to pool");
                *pool_ref = Some(db);
            }
        });
    }

    // Helper to run a test cycle
    fn run_cycle(cycle_num: usize) {
        eprintln!("\n=== Cycle {} ===", cycle_num);

        // Get database from pool (mimics VibeSqlDB::new())
        let mut db = get_pooled_database();

        // Create table
        let columns = vec![
            ColumnSchema::new("pk".to_string(), DataType::Integer, false),
            ColumnSchema::new("col0".to_string(), DataType::Integer, false),
        ];
        let mut table_schema = TableSchema::new("tab1".to_string(), columns);
        table_schema.primary_key = Some(vec!["pk".to_string()]);
        db.create_table(table_schema).unwrap();

        // Insert rows
        let table = db.get_table_mut("tab1").unwrap();
        for i in 1..=5 {
            table
                .insert(Row::from_vec(vec![SqlValue::Integer(i), SqlValue::Integer(i * 100)]))
                .unwrap();
        }
        eprintln!("  Inserted 5 rows");

        // Create index
        db.create_index(
            "idx_col0".to_string(),
            "tab1".to_string(),
            false,
            vec![IndexColumn::Column {
                column_name: "col0".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            }],
        )
        .unwrap();
        eprintln!("  Created index");

        // Query using index
        let index_data = db.get_index_data("idx_col0").unwrap();
        let matching_indices =
            index_data.range_scan(Some(&SqlValue::Integer(250)), None, false, false);
        eprintln!("  Index scan returned {} indices", matching_indices.len());

        let table = db.get_table("tab1").unwrap();
        let all_rows = table.scan();
        let fetched_rows: Vec<Row> =
            matching_indices.into_iter().filter_map(|idx| all_rows.get(idx).cloned()).collect();

        eprintln!("  Fetched {} rows from table", fetched_rows.len());
        assert_eq!(
            fetched_rows.len(),
            3,
            "Cycle {}: Expected 3 rows, got {}",
            cycle_num,
            fetched_rows.len()
        );
        eprintln!("  ✓ Cycle {} PASSED", cycle_num);

        // Return to pool (mimics Drop trait)
        return_to_pool(db);
    }

    // Run multiple cycles using the pool
    run_cycle(1);
    run_cycle(2);
    run_cycle(3);
}

// ============================================================================
// Direct Index Lookup API Tests (Issue #3140)
// ============================================================================

#[test]
fn test_lookup_by_index_single_column() {
    // Test direct index lookup with single-column index
    use vibesql_ast::{IndexColumn, OrderDirection};
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::{DataType, SqlValue};

    use crate::{Database, Row};

    let mut db = Database::new();

    // Create table
    let columns = vec![
        ColumnSchema::new("id".to_string(), DataType::Integer, false),
        ColumnSchema::new("name".to_string(), DataType::Varchar { max_length: Some(50) }, false),
    ];
    let mut table_schema = TableSchema::new("users".to_string(), columns);
    table_schema.primary_key = Some(vec!["id".to_string()]);
    db.create_table(table_schema).unwrap();

    // Insert rows
    let table = db.get_table_mut("users").unwrap();
    table
        .insert(Row::from_vec(vec![SqlValue::Integer(1), SqlValue::Varchar("Alice".into())]))
        .unwrap();
    table
        .insert(Row::from_vec(vec![SqlValue::Integer(2), SqlValue::Varchar("Bob".into())]))
        .unwrap();
    table
        .insert(Row::from_vec(vec![SqlValue::Integer(3), SqlValue::Varchar("Charlie".into())]))
        .unwrap();

    // Create index on id column
    db.create_index(
        "idx_users_id".to_string(),
        "users".to_string(),
        true, // unique
        vec![IndexColumn::Column {
            column_name: "id".to_string(),
            direction: OrderDirection::Asc,
            prefix_length: None,
        }],
    )
    .unwrap();

    // Test lookup_by_index - existing key
    let result = db.lookup_by_index("idx_users_id", &[SqlValue::Integer(2)]).unwrap();
    assert!(result.is_some());
    let rows = result.unwrap();
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].values[1], SqlValue::Varchar("Bob".into()));

    // Test lookup_by_index - non-existing key
    let result = db.lookup_by_index("idx_users_id", &[SqlValue::Integer(999)]).unwrap();
    assert!(result.is_none());
}

#[test]
fn test_lookup_one_by_index() {
    // Test lookup_one_by_index which returns just the first row
    use vibesql_ast::{IndexColumn, OrderDirection};
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::{DataType, SqlValue};

    use crate::{Database, Row};

    let mut db = Database::new();

    // Create table
    let columns = vec![
        ColumnSchema::new("id".to_string(), DataType::Integer, false),
        ColumnSchema::new("value".to_string(), DataType::Integer, false),
    ];
    let mut table_schema = TableSchema::new("items".to_string(), columns);
    table_schema.primary_key = Some(vec!["id".to_string()]);
    db.create_table(table_schema).unwrap();

    // Insert rows
    let table = db.get_table_mut("items").unwrap();
    table.insert(Row::from_vec(vec![SqlValue::Integer(1), SqlValue::Integer(100)])).unwrap();
    table.insert(Row::from_vec(vec![SqlValue::Integer(2), SqlValue::Integer(200)])).unwrap();

    // Create index
    db.create_index(
        "idx_items_pk".to_string(),
        "items".to_string(),
        true,
        vec![IndexColumn::Column {
            column_name: "id".to_string(),
            direction: OrderDirection::Asc,
            prefix_length: None,
        }],
    )
    .unwrap();

    // Test lookup_one_by_index
    let row = db.lookup_one_by_index("idx_items_pk", &[SqlValue::Integer(1)]).unwrap();
    assert!(row.is_some());
    assert_eq!(row.unwrap().values[1], SqlValue::Integer(100));

    // Test non-existing key
    let row = db.lookup_one_by_index("idx_items_pk", &[SqlValue::Integer(999)]).unwrap();
    assert!(row.is_none());
}

#[test]
fn test_lookup_by_index_composite_key() {
    // Test direct index lookup with composite (multi-column) key
    use vibesql_ast::{IndexColumn, OrderDirection};
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::{DataType, SqlValue};

    use crate::{Database, Row};

    let mut db = Database::new();

    // Create table with composite key (warehouse_id, district_id, order_id)
    let columns = vec![
        ColumnSchema::new("w_id".to_string(), DataType::Integer, false),
        ColumnSchema::new("d_id".to_string(), DataType::Integer, false),
        ColumnSchema::new("o_id".to_string(), DataType::Integer, false),
        ColumnSchema::new("amount".to_string(), DataType::DoublePrecision, false),
    ];
    let table_schema = TableSchema::new("orders".to_string(), columns);
    db.create_table(table_schema).unwrap();

    // Insert rows
    let table = db.get_table_mut("orders").unwrap();
    table
        .insert(Row::from_vec(vec![
            SqlValue::Integer(1),
            SqlValue::Integer(1),
            SqlValue::Integer(1),
            SqlValue::Double(100.0),
        ]))
        .unwrap();
    table
        .insert(Row::from_vec(vec![
            SqlValue::Integer(1),
            SqlValue::Integer(1),
            SqlValue::Integer(2),
            SqlValue::Double(200.0),
        ]))
        .unwrap();
    table
        .insert(Row::from_vec(vec![
            SqlValue::Integer(1),
            SqlValue::Integer(2),
            SqlValue::Integer(1),
            SqlValue::Double(300.0),
        ]))
        .unwrap();
    table
        .insert(Row::from_vec(vec![
            SqlValue::Integer(2),
            SqlValue::Integer(1),
            SqlValue::Integer(1),
            SqlValue::Double(400.0),
        ]))
        .unwrap();

    // Create composite index
    db.create_index(
        "idx_orders_pk".to_string(),
        "orders".to_string(),
        true,
        vec![
            IndexColumn::Column {
                column_name: "w_id".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
            IndexColumn::Column {
                column_name: "d_id".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
            IndexColumn::Column {
                column_name: "o_id".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
        ],
    )
    .unwrap();

    // Test lookup with full composite key
    let result = db
        .lookup_by_index(
            "idx_orders_pk",
            &[SqlValue::Integer(1), SqlValue::Integer(2), SqlValue::Integer(1)],
        )
        .unwrap();
    assert!(result.is_some());
    let rows = result.unwrap();
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].values[3], SqlValue::Double(300.0));

    // Test non-existing composite key
    let result = db
        .lookup_by_index(
            "idx_orders_pk",
            &[SqlValue::Integer(9), SqlValue::Integer(9), SqlValue::Integer(9)],
        )
        .unwrap();
    assert!(result.is_none());
}

#[test]
fn test_lookup_by_index_batch() {
    // Test batch lookup with multiple keys
    use vibesql_ast::{IndexColumn, OrderDirection};
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::{DataType, SqlValue};

    use crate::{Database, Row};

    let mut db = Database::new();

    // Create table
    let columns = vec![
        ColumnSchema::new("id".to_string(), DataType::Integer, false),
        ColumnSchema::new("name".to_string(), DataType::Varchar { max_length: Some(50) }, false),
    ];
    let table_schema = TableSchema::new("products".to_string(), columns);
    db.create_table(table_schema).unwrap();

    // Insert rows
    let table = db.get_table_mut("products").unwrap();
    for i in 1..=10 {
        table
            .insert(Row::from_vec(vec![
                SqlValue::Integer(i),
                SqlValue::Varchar(arcstr::ArcStr::from(format!("Product{}", i))),
            ]))
            .unwrap();
    }

    // Create index
    db.create_index(
        "idx_products_pk".to_string(),
        "products".to_string(),
        true,
        vec![IndexColumn::Column {
            column_name: "id".to_string(),
            direction: OrderDirection::Asc,
            prefix_length: None,
        }],
    )
    .unwrap();

    // Batch lookup - some exist, some don't
    let keys = vec![
        vec![SqlValue::Integer(2)],
        vec![SqlValue::Integer(5)],
        vec![SqlValue::Integer(999)], // doesn't exist
        vec![SqlValue::Integer(8)],
    ];
    let results = db.lookup_by_index_batch("idx_products_pk", &keys).unwrap();

    assert_eq!(results.len(), 4);
    assert!(results[0].is_some()); // id=2
    assert!(results[1].is_some()); // id=5
    assert!(results[2].is_none()); // id=999 doesn't exist
    assert!(results[3].is_some()); // id=8

    // Verify values
    assert_eq!(results[0].as_ref().unwrap()[0].values[1], SqlValue::Varchar("Product2".into()));
    assert_eq!(results[1].as_ref().unwrap()[0].values[1], SqlValue::Varchar("Product5".into()));
    assert_eq!(results[3].as_ref().unwrap()[0].values[1], SqlValue::Varchar("Product8".into()));
}

#[test]
fn test_lookup_one_by_index_batch() {
    // Test batch lookup returning single rows (for unique indexes)
    use vibesql_ast::{IndexColumn, OrderDirection};
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::{DataType, SqlValue};

    use crate::{Database, Row};

    let mut db = Database::new();

    // Create table
    let columns = vec![
        ColumnSchema::new("id".to_string(), DataType::Integer, false),
        ColumnSchema::new("price".to_string(), DataType::DoublePrecision, false),
    ];
    let table_schema = TableSchema::new("items".to_string(), columns);
    db.create_table(table_schema).unwrap();

    // Insert rows
    let table = db.get_table_mut("items").unwrap();
    for i in 1..=5 {
        table
            .insert(Row::from_vec(vec![SqlValue::Integer(i), SqlValue::Double(i as f64 * 10.0)]))
            .unwrap();
    }

    // Create index
    db.create_index(
        "idx_items_id".to_string(),
        "items".to_string(),
        true,
        vec![IndexColumn::Column {
            column_name: "id".to_string(),
            direction: OrderDirection::Asc,
            prefix_length: None,
        }],
    )
    .unwrap();

    // Batch lookup single rows
    let keys = vec![
        vec![SqlValue::Integer(1)],
        vec![SqlValue::Integer(3)],
        vec![SqlValue::Integer(99)], // doesn't exist
    ];
    let results = db.lookup_one_by_index_batch("idx_items_id", &keys).unwrap();

    assert_eq!(results.len(), 3);
    assert!(results[0].is_some());
    assert!(results[1].is_some());
    assert!(results[2].is_none());

    assert_eq!(results[0].unwrap().values[1], SqlValue::Double(10.0));
    assert_eq!(results[1].unwrap().values[1], SqlValue::Double(30.0));
}

#[test]
fn test_lookup_by_index_error_cases() {
    // Test error handling for invalid index names
    use crate::Database;

    let db = Database::new();

    // Test lookup on non-existent index
    let result = db.lookup_by_index("nonexistent_index", &[SqlValue::Integer(1)]);
    assert!(result.is_err());

    let result = db.lookup_one_by_index("nonexistent_index", &[SqlValue::Integer(1)]);
    assert!(result.is_err());

    let result = db.lookup_by_index_batch("nonexistent_index", &[vec![SqlValue::Integer(1)]]);
    assert!(result.is_err());
}

// ============================================================================
// Prefix Index Lookup API Tests (Issue #3195)
// ============================================================================

#[test]
fn test_lookup_by_index_prefix_basic() {
    // Test basic prefix lookup on a multi-column index
    use vibesql_ast::{IndexColumn, OrderDirection};
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::{DataType, SqlValue};

    use crate::{Database, Row};

    let mut db = Database::new();

    // Create table with 3-column composite key (like TPC-C NEW_ORDER)
    let columns = vec![
        ColumnSchema::new("w_id".to_string(), DataType::Integer, false),
        ColumnSchema::new("d_id".to_string(), DataType::Integer, false),
        ColumnSchema::new("o_id".to_string(), DataType::Integer, false),
        ColumnSchema::new("carrier_id".to_string(), DataType::Integer, true),
    ];
    let table_schema = TableSchema::new("new_order".to_string(), columns);
    db.create_table(table_schema).unwrap();

    // Insert rows: warehouse 1, districts 1-3, varying orders
    let table = db.get_table_mut("new_order").unwrap();
    // District 1: orders 101, 102
    table
        .insert(Row::from_vec(vec![
            SqlValue::Integer(1),
            SqlValue::Integer(1),
            SqlValue::Integer(101),
            SqlValue::Null,
        ]))
        .unwrap();
    table
        .insert(Row::from_vec(vec![
            SqlValue::Integer(1),
            SqlValue::Integer(1),
            SqlValue::Integer(102),
            SqlValue::Null,
        ]))
        .unwrap();
    // District 2: orders 201, 202, 203
    table
        .insert(Row::from_vec(vec![
            SqlValue::Integer(1),
            SqlValue::Integer(2),
            SqlValue::Integer(201),
            SqlValue::Null,
        ]))
        .unwrap();
    table
        .insert(Row::from_vec(vec![
            SqlValue::Integer(1),
            SqlValue::Integer(2),
            SqlValue::Integer(202),
            SqlValue::Null,
        ]))
        .unwrap();
    table
        .insert(Row::from_vec(vec![
            SqlValue::Integer(1),
            SqlValue::Integer(2),
            SqlValue::Integer(203),
            SqlValue::Null,
        ]))
        .unwrap();
    // District 3: order 301
    table
        .insert(Row::from_vec(vec![
            SqlValue::Integer(1),
            SqlValue::Integer(3),
            SqlValue::Integer(301),
            SqlValue::Null,
        ]))
        .unwrap();

    // Create composite index on (w_id, d_id, o_id)
    db.create_index(
        "idx_no_pk".to_string(),
        "new_order".to_string(),
        true,
        vec![
            IndexColumn::Column {
                column_name: "w_id".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
            IndexColumn::Column {
                column_name: "d_id".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
            IndexColumn::Column {
                column_name: "o_id".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
        ],
    )
    .unwrap();

    // Test prefix lookup: find all orders for warehouse 1, district 2
    let rows = db
        .lookup_by_index_prefix("idx_no_pk", &[SqlValue::Integer(1), SqlValue::Integer(2)])
        .unwrap();

    // Should find 3 rows (orders 201, 202, 203)
    assert_eq!(rows.len(), 3, "Should find 3 orders for district 2");

    // Verify the order IDs
    let order_ids: Vec<i64> = rows
        .iter()
        .map(|r| match &r.values[2] {
            SqlValue::Integer(v) => *v,
            _ => 0,
        })
        .collect();
    assert!(order_ids.contains(&201));
    assert!(order_ids.contains(&202));
    assert!(order_ids.contains(&203));
}

#[test]
fn test_lookup_by_index_prefix_single_column() {
    // Test prefix lookup with single-column prefix
    use vibesql_ast::{IndexColumn, OrderDirection};
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::{DataType, SqlValue};

    use crate::{Database, Row};

    let mut db = Database::new();

    // Create table
    let columns = vec![
        ColumnSchema::new("warehouse".to_string(), DataType::Integer, false),
        ColumnSchema::new("district".to_string(), DataType::Integer, false),
        ColumnSchema::new("value".to_string(), DataType::Integer, false),
    ];
    let table_schema = TableSchema::new("test_data".to_string(), columns);
    db.create_table(table_schema).unwrap();

    // Insert rows
    let table = db.get_table_mut("test_data").unwrap();
    // Warehouse 1: 3 rows
    table
        .insert(Row::from_vec(vec![
            SqlValue::Integer(1),
            SqlValue::Integer(1),
            SqlValue::Integer(100),
        ]))
        .unwrap();
    table
        .insert(Row::from_vec(vec![
            SqlValue::Integer(1),
            SqlValue::Integer(2),
            SqlValue::Integer(200),
        ]))
        .unwrap();
    table
        .insert(Row::from_vec(vec![
            SqlValue::Integer(1),
            SqlValue::Integer(3),
            SqlValue::Integer(300),
        ]))
        .unwrap();
    // Warehouse 2: 2 rows
    table
        .insert(Row::from_vec(vec![
            SqlValue::Integer(2),
            SqlValue::Integer(1),
            SqlValue::Integer(400),
        ]))
        .unwrap();
    table
        .insert(Row::from_vec(vec![
            SqlValue::Integer(2),
            SqlValue::Integer(2),
            SqlValue::Integer(500),
        ]))
        .unwrap();

    // Create index on (warehouse, district)
    db.create_index(
        "idx_wd".to_string(),
        "test_data".to_string(),
        false,
        vec![
            IndexColumn::Column {
                column_name: "warehouse".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
            IndexColumn::Column {
                column_name: "district".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
        ],
    )
    .unwrap();

    // Test single-column prefix: find all rows for warehouse 1
    let rows = db.lookup_by_index_prefix("idx_wd", &[SqlValue::Integer(1)]).unwrap();
    assert_eq!(rows.len(), 3, "Should find 3 rows for warehouse 1");

    // Test single-column prefix: find all rows for warehouse 2
    let rows = db.lookup_by_index_prefix("idx_wd", &[SqlValue::Integer(2)]).unwrap();
    assert_eq!(rows.len(), 2, "Should find 2 rows for warehouse 2");
}

#[test]
fn test_lookup_by_index_prefix_no_match() {
    // Test prefix lookup when no rows match
    use vibesql_ast::{IndexColumn, OrderDirection};
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::{DataType, SqlValue};

    use crate::{Database, Row};

    let mut db = Database::new();

    // Create table
    let columns = vec![
        ColumnSchema::new("a".to_string(), DataType::Integer, false),
        ColumnSchema::new("b".to_string(), DataType::Integer, false),
    ];
    let table_schema = TableSchema::new("test_table".to_string(), columns);
    db.create_table(table_schema).unwrap();

    // Insert rows
    let table = db.get_table_mut("test_table").unwrap();
    table.insert(Row::from_vec(vec![SqlValue::Integer(1), SqlValue::Integer(10)])).unwrap();
    table.insert(Row::from_vec(vec![SqlValue::Integer(2), SqlValue::Integer(20)])).unwrap();

    // Create index
    db.create_index(
        "idx_ab".to_string(),
        "test_table".to_string(),
        false,
        vec![
            IndexColumn::Column {
                column_name: "a".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
            IndexColumn::Column {
                column_name: "b".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
        ],
    )
    .unwrap();

    // Test prefix that doesn't match any rows
    let rows = db.lookup_by_index_prefix("idx_ab", &[SqlValue::Integer(999)]).unwrap();
    assert!(rows.is_empty(), "Should find no rows for non-existent prefix");
}

#[test]
fn test_lookup_by_index_prefix_batch_basic() {
    // Test batch prefix lookup on a multi-column index
    use vibesql_ast::{IndexColumn, OrderDirection};
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::{DataType, SqlValue};

    use crate::{Database, Row};

    let mut db = Database::new();

    // Create table (like TPC-C NEW_ORDER)
    let columns = vec![
        ColumnSchema::new("w_id".to_string(), DataType::Integer, false),
        ColumnSchema::new("d_id".to_string(), DataType::Integer, false),
        ColumnSchema::new("o_id".to_string(), DataType::Integer, false),
    ];
    let table_schema = TableSchema::new("new_order".to_string(), columns);
    db.create_table(table_schema).unwrap();

    // Insert rows for warehouse 1, all 10 districts
    let table = db.get_table_mut("new_order").unwrap();
    for d_id in 1..=10 {
        // Each district has d_id orders (district 1 has 1 order, district 2 has 2, etc.)
        for o_id in 1..=d_id {
            table
                .insert(Row::from_vec(vec![
                    SqlValue::Integer(1),
                    SqlValue::Integer(d_id as i64),
                    SqlValue::Integer(o_id as i64 * 100),
                ]))
                .unwrap();
        }
    }

    // Create composite index
    db.create_index(
        "idx_no_pk".to_string(),
        "new_order".to_string(),
        true,
        vec![
            IndexColumn::Column {
                column_name: "w_id".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
            IndexColumn::Column {
                column_name: "d_id".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
            IndexColumn::Column {
                column_name: "o_id".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
        ],
    )
    .unwrap();

    // Batch prefix lookup: find all orders for districts 1, 5, and 10
    let prefixes = vec![
        vec![SqlValue::Integer(1), SqlValue::Integer(1)], // district 1
        vec![SqlValue::Integer(1), SqlValue::Integer(5)], // district 5
        vec![SqlValue::Integer(1), SqlValue::Integer(10)], // district 10
    ];
    let results = db.lookup_by_index_prefix_batch("idx_no_pk", &prefixes).unwrap();

    assert_eq!(results.len(), 3);
    assert_eq!(results[0].len(), 1, "District 1 should have 1 order");
    assert_eq!(results[1].len(), 5, "District 5 should have 5 orders");
    assert_eq!(results[2].len(), 10, "District 10 should have 10 orders");
}

#[test]
fn test_lookup_by_index_prefix_batch_tpcc_delivery() {
    // Test the exact TPC-C Delivery transaction pattern:
    // Look up all new orders for all 10 districts in a single batch
    use vibesql_ast::{IndexColumn, OrderDirection};
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::{DataType, SqlValue};

    use crate::{Database, Row};

    let mut db = Database::new();

    // Create NEW_ORDER table
    let columns = vec![
        ColumnSchema::new("no_w_id".to_string(), DataType::Integer, false),
        ColumnSchema::new("no_d_id".to_string(), DataType::Integer, false),
        ColumnSchema::new("no_o_id".to_string(), DataType::Integer, false),
    ];
    let table_schema = TableSchema::new("new_order".to_string(), columns);
    db.create_table(table_schema).unwrap();

    // Insert new orders for warehouse 1
    let table = db.get_table_mut("new_order").unwrap();
    let w_id = 1;
    // Each district has some new orders (simulating pending orders)
    // District 1 has 1 order, district 2 has 2, ..., district 10 has 10
    for d_id in 1..=10 {
        for o_id in 2100..(2100 + d_id) {
            table
                .insert(Row::from_vec(vec![
                    SqlValue::Integer(w_id),
                    SqlValue::Integer(d_id as i64),
                    SqlValue::Integer(o_id as i64),
                ]))
                .unwrap();
        }
    }

    // Create composite index (primary key)
    db.create_index(
        "idx_new_order_pk".to_string(),
        "new_order".to_string(),
        true,
        vec![
            IndexColumn::Column {
                column_name: "no_w_id".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
            IndexColumn::Column {
                column_name: "no_d_id".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
            IndexColumn::Column {
                column_name: "no_o_id".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
        ],
    )
    .unwrap();

    // TPC-C Delivery transaction: batch prefix lookup for all 10 districts
    let prefixes: Vec<Vec<SqlValue>> =
        (1..=10).map(|d| vec![SqlValue::Integer(w_id), SqlValue::Integer(d)]).collect();

    let results = db.lookup_by_index_prefix_batch("idx_new_order_pk", &prefixes).unwrap();

    // Verify we got results for all 10 districts
    assert_eq!(results.len(), 10);

    // Verify each district has the expected number of orders
    // District 1 has 1 order (2100..2101), district 2 has 2, ..., district 10 has 10
    for (d_idx, rows) in results.iter().enumerate() {
        let d_id = d_idx + 1;
        let expected_orders = d_id; // District N has N orders
        assert_eq!(
            rows.len(),
            expected_orders,
            "District {} should have {} new orders",
            d_id,
            expected_orders
        );
    }
}

#[test]
fn test_lookup_by_index_prefix_error_cases() {
    // Test error handling for prefix lookup
    use crate::Database;

    let db = Database::new();

    // Test prefix lookup on non-existent index
    let result = db.lookup_by_index_prefix("nonexistent_index", &[SqlValue::Integer(1)]);
    assert!(result.is_err());

    // Test batch prefix lookup on non-existent index
    let result =
        db.lookup_by_index_prefix_batch("nonexistent_index", &[vec![SqlValue::Integer(1)]]);
    assert!(result.is_err());
}

#[test]
fn test_lookup_by_index_prefix_empty_prefix() {
    // Test prefix lookup with empty prefix
    use vibesql_ast::{IndexColumn, OrderDirection};
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::{DataType, SqlValue};

    use crate::{Database, Row};

    let mut db = Database::new();

    // Create table
    let columns = vec![
        ColumnSchema::new("a".to_string(), DataType::Integer, false),
        ColumnSchema::new("b".to_string(), DataType::Integer, false),
    ];
    let table_schema = TableSchema::new("test_table".to_string(), columns);
    db.create_table(table_schema).unwrap();

    // Insert rows
    let table = db.get_table_mut("test_table").unwrap();
    table.insert(Row::from_vec(vec![SqlValue::Integer(1), SqlValue::Integer(10)])).unwrap();

    // Create index
    db.create_index(
        "idx_ab".to_string(),
        "test_table".to_string(),
        false,
        vec![
            IndexColumn::Column {
                column_name: "a".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
            IndexColumn::Column {
                column_name: "b".to_string(),
                direction: OrderDirection::Asc,
                prefix_length: None,
            },
        ],
    )
    .unwrap();

    // Empty prefix matches everything - returns all rows
    let rows = db.lookup_by_index_prefix("idx_ab", &[]).unwrap();
    assert_eq!(rows.len(), 1, "Empty prefix should return all rows");
}

// ============================================================================
// range_scan_limit tests (#3638)
// ============================================================================

#[test]
fn test_range_scan_limit_basic() {
    // Test that range_scan_limit stops after collecting limit rows
    let mut data = std::collections::BTreeMap::new();

    // Insert 10 rows with values 10, 20, ..., 100
    for i in 1..=10 {
        data.insert(vec![SqlValue::Double((i * 10) as f64)], vec![i as usize]);
    }

    let index_data = IndexData::InMemory { data };

    // Query all rows with limit 3
    let result = index_data.range_scan_limit(None, None, false, false, Some(3));
    assert_eq!(result.len(), 3, "Should return exactly 3 rows");

    // Query with BETWEEN and limit
    let result = index_data.range_scan_limit(
        Some(&SqlValue::Integer(25)),
        Some(&SqlValue::Integer(85)),
        true,
        true,
        Some(2),
    );
    // Values 30, 40, 50, 60, 70, 80 match, but limit is 2
    assert_eq!(result.len(), 2, "Should return exactly 2 rows with limit");
    // Should be rows with values 30 and 40 (row indices 3 and 4)
    assert_eq!(result, vec![3, 4]);
}

#[test]
fn test_range_scan_limit_no_limit() {
    // Test that range_scan_limit with None limit returns all rows
    let mut data = std::collections::BTreeMap::new();

    for i in 1..=5 {
        data.insert(vec![SqlValue::Double((i * 10) as f64)], vec![i as usize]);
    }

    let index_data = IndexData::InMemory { data };

    // Query with no limit - should return all
    let result = index_data.range_scan_limit(None, None, false, false, None);
    assert_eq!(result.len(), 5, "Should return all rows when limit is None");
}

#[test]
fn test_range_scan_limit_zero() {
    // Test that range_scan_limit with limit=0 returns empty
    let mut data = std::collections::BTreeMap::new();

    for i in 1..=5 {
        data.insert(vec![SqlValue::Double((i * 10) as f64)], vec![i as usize]);
    }

    let index_data = IndexData::InMemory { data };

    let result = index_data.range_scan_limit(None, None, false, false, Some(0));
    assert!(result.is_empty(), "Should return empty when limit is 0");
}

#[test]
fn test_range_scan_limit_larger_than_result() {
    // Test that limit larger than result set returns all matches
    let mut data = std::collections::BTreeMap::new();

    for i in 1..=3 {
        data.insert(vec![SqlValue::Double((i * 10) as f64)], vec![i as usize]);
    }

    let index_data = IndexData::InMemory { data };

    // Ask for 100 rows but only 3 exist
    let result = index_data.range_scan_limit(None, None, false, false, Some(100));
    assert_eq!(result.len(), 3, "Should return all 3 rows when limit > result size");
}