motedb 0.5.1

AI-native embedded multimodal database for embodied intelligence (robots, AR glasses, industrial arms).
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
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
use super::manifest::Manifest;
use super::merge::MergeCursor;
use super::segment::Segment;
use crate::storage::lsm::columnar::{ColumnTypeTag, ColumnarSSTableBuilder};
use crate::types::{ArcString, ColumnType, Value};
use crate::Result;
use parking_lot::{Mutex, RwLock};
use std::collections::VecDeque;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

/// Result of a single-pass aggregate scan (SUM/AVG/MIN/MAX/COUNT).
/// Computed without per-row Value allocation.
#[derive(Default, Clone)]
pub struct AggregateResult {
    pub count: i64,      // non-NULL values (for COUNT(col))
    pub null_count: i64, // NULL values (for COUNT(*) = count + null_count)
    pub int_sum: i64,
    pub float_sum: f64,
    pub has_float: bool,
    pub min_int: i64,
    pub max_int: i64,
    pub min_float: f64,
    pub max_float: f64,
}

// ── Comparison helpers for count_filtered (zero-allocation) ──────────
#[inline]
fn cmp_opt<T: Copy + PartialEq + PartialOrd>(
    v: Option<T>,
    target: Option<T>,
    op: &crate::sql::ast::BinaryOperator,
) -> bool {
    use crate::sql::ast::BinaryOperator;
    let (v, t) = match (v, target) {
        (Some(a), Some(b)) => (a, b),
        _ => return false,
    };
    match op {
        BinaryOperator::Eq => v == t,
        BinaryOperator::Ne => v != t,
        BinaryOperator::Lt => v.partial_cmp(&t) == Some(std::cmp::Ordering::Less),
        BinaryOperator::Gt => v.partial_cmp(&t) == Some(std::cmp::Ordering::Greater),
        BinaryOperator::Le => matches!(
            v.partial_cmp(&t),
            Some(std::cmp::Ordering::Less | std::cmp::Ordering::Equal)
        ),
        BinaryOperator::Ge => matches!(
            v.partial_cmp(&t),
            Some(std::cmp::Ordering::Greater | std::cmp::Ordering::Equal)
        ),
        _ => false,
    }
}

#[inline]
fn cmp_opt_f64(v: Option<f64>, target: Option<f64>, op: &crate::sql::ast::BinaryOperator) -> bool {
    cmp_opt(v, target, op)
}

#[inline]
fn cmp_str(v: Option<&str>, target: Option<&str>, op: &crate::sql::ast::BinaryOperator) -> bool {
    use crate::sql::ast::BinaryOperator;
    let (v, t) = match (v, target) {
        (Some(a), Some(b)) => (a, b),
        _ => return false,
    };
    match op {
        BinaryOperator::Eq => v == t,
        BinaryOperator::Ne => v != t,
        BinaryOperator::Lt => v < t,
        BinaryOperator::Gt => v > t,
        BinaryOperator::Le => v <= t,
        BinaryOperator::Ge => v >= t,
        _ => false,
    }
}

/// Decode a single value from a ColumnarSSTableBuilder's raw column buffer.
/// Used by ColSegmentStore::get() to read buffered (unflushed) rows.
/// Format matches add_values: Integer/Timestamp = [8B i64 LE], Float = [8B f64 LE],
/// Bool = [1B], Text = [u16 len][bytes].
fn decode_buffered_value(
    buf: &crate::storage::lsm::columnar::ColumnarSSTableBuilder,
    col_idx: usize,
    row_idx: usize,
    _col_type: &ColumnType,
) -> Value {
    use crate::storage::lsm::columnar::ColumnTypeTag;
    // Check NULL flag first.
    if buf.null_flags.get(col_idx).and_then(|f| f.get(row_idx)) == Some(&true) {
        return Value::Null;
    }
    let tag = buf.column_tags.get(col_idx).copied();
    let raw = &buf.column_buffers[col_idx];
    match tag {
        Some(ColumnTypeTag::Integer) => {
            let off = row_idx * 8;
            if off + 8 > raw.len() {
                return Value::Null;
            }
            Value::Integer(i64::from_le_bytes(raw[off..off + 8].try_into().unwrap()))
        }
        Some(ColumnTypeTag::Timestamp) => {
            let off = row_idx * 8;
            if off + 8 > raw.len() {
                return Value::Null;
            }
            let v = i64::from_le_bytes(raw[off..off + 8].try_into().unwrap());
            Value::Timestamp(crate::types::Timestamp::from_micros(v))
        }
        Some(ColumnTypeTag::Float) => {
            let off = row_idx * 8;
            if off + 8 > raw.len() {
                return Value::Null;
            }
            Value::Float(f64::from_le_bytes(raw[off..off + 8].try_into().unwrap()))
        }
        Some(ColumnTypeTag::Bool) => {
            let off = row_idx;
            if off >= raw.len() {
                return Value::Null;
            }
            Value::Bool(raw[off] != 0)
        }
        Some(ColumnTypeTag::Text) => {
            // Text rows are variable-length: [u16 len][bytes], concatenated.
            // Walk to the row_idx-th entry.
            let mut pos = 0usize;
            let mut r = 0usize;
            while pos + 2 <= raw.len() {
                let len = u16::from_le_bytes([raw[pos], raw[pos + 1]]) as usize;
                pos += 2;
                if r == row_idx {
                    if len == 0xFFFF || pos + len > raw.len() {
                        return Value::Null;
                    }
                    return Value::Text(ArcString(std::sync::Arc::from(
                        std::str::from_utf8(&raw[pos..pos + len]).unwrap_or(""),
                    )));
                }
                pos += if len == 0xFFFF { 0 } else { len };
                r += 1;
            }
            Value::Null
        }
        _ => Value::Null,
    }
}
use std::sync::Arc;

/// Compaction trigger: merge when segment count reaches this.
const COMPACTION_SEGMENT_THRESHOLD: usize = 3;

/// Append-only multi-segment store for one columnar table.
pub struct ColSegmentStore {
    #[allow(dead_code)]
    table_name: String,
    dir: PathBuf,
    /// Active segments in ascending creation order (oldest first, newest at back).
    segments: RwLock<VecDeque<Arc<Segment>>>,
    /// In-memory write buffer. Flushed as a delta segment (does not read old data).
    write_buf: Mutex<ColumnarSSTableBuilder>,
    /// Write lock serializing flush_buffer + merge_segments. Without this, a
    /// concurrent flush (triggered by ensure_query_visibility during a query)
    /// can create a segment that force_compact_all then misses or clobbers
    /// (the v0.5.0 large_batch_durability race — 5000 of 10000 rows lost).
    flush_merge_lock: parking_lot::Mutex<()>,
    next_segment_id: AtomicU64,
    manifest: Mutex<Manifest>,
    col_types: Vec<ColumnType>,
    /// Cache for GROUP BY results: key = (group_col << 32 | agg_col).
    /// Invalidated by clear_cache() on any write (INSERT/UPDATE/DELETE).
    groupby_cache: RwLock<std::collections::HashMap<u64, Vec<(String, i64, f64)>>>,
    /// Cache for IN-hash query row indices: key = (col_pos << 64 | set_sig).
    /// Avoids re-scanning 300K rows against a HashSet on repeated calls.
    in_hash_cache: RwLock<std::collections::HashMap<u128, Vec<usize>>>,
}

impl ColSegmentStore {
    /// Create a new store for a table at `base_dir/columnar_ms/<table_name>/`.
    /// (`columnar_ms` to avoid clashing with the time-series `columnar/` dir.)
    pub fn create(
        base_dir: &Path,
        table_name: &str,
        col_types: Vec<ColumnType>,
    ) -> Result<Arc<Self>> {
        let dir = base_dir.join("columnar_ms").join(table_name);
        std::fs::create_dir_all(&dir)?;
        let manifest_path = dir.join("MANIFEST");
        let manifest_exists = manifest_path.exists();
        let manifest = if manifest_exists {
            Manifest::open(&manifest_path)?
        } else {
            Manifest::create(&manifest_path)?
        };
        let buf_path = dir.join(".writebuf.tmp");
        let write_buf = ColumnarSSTableBuilder::new(&buf_path, col_types.clone());
        let store = Arc::new(Self {
            table_name: table_name.to_string(),
            dir,
            segments: RwLock::new(VecDeque::new()),
            write_buf: Mutex::new(write_buf),
            flush_merge_lock: parking_lot::Mutex::new(()),
            next_segment_id: AtomicU64::new(1),
            manifest: Mutex::new(manifest),
            col_types,
            groupby_cache: RwLock::new(std::collections::HashMap::new()),
            in_hash_cache: RwLock::new(std::collections::HashMap::new()),
        });
        // 🔥 Auto-recover segments from disk if the MANIFEST has active entries.
        // This handles the restart case: get_or_create_col_segment_store is called
        // on a table that has data on disk from a previous session.
        if manifest_exists {
            store.recover_from_disk();
        }
        Ok(store)
    }

    /// Append rows to the in-memory buffer. O(rows). Each tuple: (key, timestamp, values).
    /// 🔥 Stability: auto-compacts when segments exceed threshold, preventing
    /// unbounded segment accumulation from repeated writes.
    pub fn append_rows(&self, rows: &[(u64, u64, Vec<Value>)]) -> Result<()> {
        // Invalidate caches on write.
        if !rows.is_empty() {
            self.groupby_cache.write().clear();
            self.in_hash_cache.write().clear();
        }
        let mut buf = self.write_buf.lock();
        for (key, ts, row) in rows {
            buf.add_values(*key, *ts, false, row)?;
        }
        drop(buf);
        // Auto-compaction disabled during append_rows — it can deadlock
        // when merge_segments reads column data while holding write locks.
        // Compaction runs on demand via ensure_query_visibility or compact_once.
        Ok(())
    }

    /// Append a single row by reference — avoids the Vec<Value> clone that
    /// append_rows requires (it takes &[(.., Vec<Value>)]). This is the hot
    /// path for single-row INSERT (saves one heap allocation per INSERT).
    pub fn append_row_ref(&self, key: u64, ts: u64, row: &[Value]) -> Result<()> {
        self.groupby_cache.write().clear();
        self.in_hash_cache.write().clear();
        let mut buf = self.write_buf.lock();
        buf.add_values(key, ts, false, row)?;
        drop(buf);
        // Auto-compaction disabled — can deadlock with merge_segments.
        Ok(())
    }

    /// Append a tombstone (deletion marker) for a key. The tombstone suppresses
    /// the row in multi-segment scans (newest-version-wins with deleted=true).
    /// 🔥 Stability: auto-compacts when segments exceed threshold.
    pub fn append_tombstone(&self, key: u64, ts: u64) -> Result<()> {
        let mut buf = self.write_buf.lock();
        // Write placeholder values for each column (keeps column_buffers in sync
        // with num_rows). The actual values are never read for deleted rows.
        let placeholder: Vec<Value> = self.col_types.iter().map(|_| Value::Null).collect();
        buf.add_values(key, ts, true, &placeholder)?;
        drop(buf);
        // Auto-compaction disabled — can deadlock with merge_segments.
        Ok(())
    }

    /// Flush the buffer to a new delta segment on disk. Does NOT read old segments.
    /// O(this batch). Writes the file (no fsync — durability via WAL/manifest).
    pub fn flush_buffer(&self) -> Result<()> {
        // Serialize with merge_segments: if a merge is in progress, wait.
        // Without this, flush can create a segment that the merge then
        // clobbers (the large_batch_durability race).
        let _guard = self.flush_merge_lock.lock();
        // Take buffer contents out, replace with a fresh builder, release the lock fast.
        let buf_path = self.dir.join(".writebuf.tmp");
        let mut old_buf = {
            let mut guard = self.write_buf.lock();
            let fresh = ColumnarSSTableBuilder::new(&buf_path, self.col_types.clone());
            std::mem::replace(&mut *guard, fresh)
        };
        if old_buf.num_rows == 0 {
            return Ok(());
        }
        let id = self.next_segment_id.fetch_add(1, Ordering::Relaxed);
        let path = self.dir.join(format!("{:010}.sst", id));
        // finish() writes to builder.path; set it to the numbered path first.
        old_buf.path = path.clone();
        old_buf.finish()?;
        let seg = Arc::new(Segment::open(&path, id)?);
        // Record in manifest (fsync'd) BEFORE exposing in memory.
        self.manifest.lock().add_segment(id)?;
        self.segments.write().push_back(seg);
        // Invalidate all query caches (data changed).
        self.groupby_cache.write().clear();
        self.in_hash_cache.write().clear();
        Ok(())
    }

    /// Flush the write buffer ONLY if it contains pending rows/tombstones.
    /// Called at the start of query paths to ensure buffered writes are
    /// visible to segment-based scans. Cheap no-op when buffer is empty.
    /// This avoids per-DELETE flushes that created O(N) segments.
    ///
    /// NOTE: auto-compaction is triggered in append_rows/append_tombstone
    /// (write path), NOT here. Compacting during a read would invalidate
    /// Drop all data: clear in-memory segments + write buffer, delete on-disk
    /// segment files, and delete the manifest file so a reopen starts fresh.
    /// Called by DROP TABLE so a recreated same-named table starts empty (no
    /// stale rows). Best-effort on file deletion.
    pub fn drop_all(&self) -> Result<()> {
        // Snapshot segment ids (for file deletion), then clear in-memory state.
        let segs = self.segments_snapshot();
        let seg_ids: Vec<u64> = segs.iter().map(|s| s.id).collect();
        self.segments.write().clear();
        // Clear the write buffer by finishing (no-op if empty) then draining.
        // The builder has no public clear(); we just leave it — the store is
        // being removed from the registry anyway, so a new store is created on
        // recreate. Delete on-disk files so the old data can't be recovered.
        for id in &seg_ids {
            let path = self.dir.join(format!("{:010}.sst", id));
            let _ = std::fs::remove_file(&path);
        }
        // Delete the manifest file so a reopen finds no manifest → creates a
        // fresh one with no segments.
        let manifest_path = self.dir.join("MANIFEST");
        let _ = std::fs::remove_file(&manifest_path);
        Ok(())
    }

    /// SegData slices held by in-flight SelectColumnar queries (use-after-free).
    pub fn ensure_query_visibility(&self) -> Result<()> {
        if self.write_buf.lock().num_rows > 0 {
            self.flush_buffer()?;
        }
        Ok(())
    }

    /// Point lookup: newest segment first, return first hit.
    /// Uses per-segment column decode cache — first access decompresses each
    /// column once, subsequent lookups (incl. other keys) reuse the cache.
    ///
    /// 🔑 Tombstone-aware: if a segment contains the key but it's deleted
    /// (tombstone), we STOP searching — the deletion suppresses older live
    /// versions in older segments. Previously `get_row_cached` returned None
    /// for a tombstoned key, indistinguishable from "key not in segment", so
    /// `get` fell through to an older segment holding the live row and
    /// returned stale data after a DELETE.
    pub fn get(&self, key: u64) -> Option<Vec<Value>> {
        // 🔑 Check the write buffer FIRST — it may hold a newer version (UPDATE)
        // or a tombstone (DELETE) that supersedes the segment data. Without this,
        // a DELETE whose tombstone is still in the buffer (lazy flush) would be
        // invisible to get(), which would return the stale live row from a segment.
        {
            let buf = self.write_buf.lock();
            if let Some(idx) = buf.keys.iter().position(|&k| k == key) {
                // Found in buffer — newest version. If deleted, return None.
                if buf.deleted[idx] {
                    return None;
                }
                // Live buffered row: decode from the columnar buffer.
                let mut row = Vec::with_capacity(self.col_types.len());
                for ci in 0..self.col_types.len() {
                    if ci < buf.column_buffers.len() {
                        row.push(decode_buffered_value(&buf, ci, idx, &self.col_types[ci]));
                    } else {
                        row.push(Value::Null);
                    }
                }
                return Some(row);
            }
        }
        let segs = self.segments.read();
        for seg in segs.iter().rev() {
            // Check if this segment contains the key at all.
            if let Some(idx) = seg.sst.row_map.find_key(key) {
                // Key is in this segment. If deleted, it's a tombstone — the
                // newest version of this key is a deletion, so return None
                // regardless of older segments.
                if seg.sst.row_map.is_deleted(idx) {
                    return None;
                }
                // Live row: decode and return.
                return seg.get_row_cached(key, &self.col_types);
            }
            // Key not in this segment — continue to older segments.
        }
        None
    }

    /// Full-table ordered scan via multi-way merge. Newest version wins.
    pub fn scan(&self) -> MergeCursor {
        let segs: Vec<Arc<Segment>> = self.segments.read().iter().cloned().collect();
        MergeCursor::new(&segs, &self.col_types)
    }

    /// High-performance projected + filtered scan.
    ///
    /// Iterates each segment's columns directly (pre-decoded once per segment,
    /// like CREATE INDEX), applying `predicate(row_idx)` on the filter column
    /// before decoding any output columns. Only matching rows get their output
    /// columns decoded. Newest-segment-wins dedup via a seen-key set.
    ///
    /// This avoids the MergeCursor's per-row `Vec<Value>` allocation for ALL
    /// columns — the dominant cost for Full scan / WHERE / LIKE (was 68-197ms
    /// for 300K rows; pure column read is <2ms).
    ///
    /// `filter_col`: column position for the WHERE predicate.
    /// `project_cols`: output column positions (projection).
    /// `predicate`: returns true if the row at `row_idx` matches.
    /// Returns (key, output_values) pairs in ascending key order.
    pub fn scan_projected_filtered(
        &self,
        filter_col: Option<usize>,
        project_cols: &[usize],
        predicate: &dyn Fn(Option<&Value>) -> bool,
    ) -> Vec<(u64, Vec<Value>)> {
        self.scan_projected_filtered_limit(filter_col, project_cols, predicate, usize::MAX)
    }

    /// Same as scan_projected_filtered, but stops scanning after `max_results`
    /// matching rows have been collected. This enables LIMIT early-termination:
    /// SELECT cols FROM t LIMIT 50 only decodes 50 rows instead of all N.
    ///
    /// When max_results is very small (e.g. 1 for PK point queries), project
    /// columns are decoded lazily — only for matching rows, not pre-decoded for
    /// the entire segment. This is critical for PK point queries on large tables.
    pub fn scan_projected_filtered_limit(
        &self,
        filter_col: Option<usize>,
        project_cols: &[usize],
        predicate: &dyn Fn(Option<&Value>) -> bool,
        max_results: usize,
    ) -> Vec<(u64, Vec<Value>)> {
        let total_rows: usize = self.segments.read().iter().map(|s| s.sst.num_rows).sum();
        let mut result: Vec<(u64, Vec<Value>)> =
            Vec::with_capacity(total_rows.min(max_results).min(65536));
        if max_results == 0 {
            return result;
        }
        let segs = self.segments_snapshot();

        // For small result sets (≤8 rows expected), use lazy projection:
        // only decode output columns for matching rows, not the whole segment.
        let lazy_project = max_results <= 8;
        let single_seg = segs.len() <= 1;
        // 🔑 Newest-version-wins dedup. An UPDATE appends a newer row with the
        // SAME composite key; without dedup, scans return both versions. We
        // iterate segments newest→oldest (.rev()) and, within a segment, rows
        // newest→oldest (descending index), so the FIRST version of a key seen
        // is the newest — a plain HashSet suffices (no per-scan O(N log N) sort,
        // which caused a ~6x regression on DISTINCT/ORDER BY/LIKE/IN).
        //
        // For single-segment tables with no UPDATE history, keys are already
        // unique, so we skip dedup entirely (need_dedup=false) — zero overhead.
        let need_dedup = !single_seg || self.may_have_duplicate_keys();
        let mut seen: std::collections::HashSet<u64> = if need_dedup {
            std::collections::HashSet::with_capacity(total_rows)
        } else {
            std::collections::HashSet::new()
        };
        for seg in segs.iter().rev() {
            let n = seg.sst.num_rows;
            // Descending index order within a segment: rows are appended old→new,
            // so iterating n→0 visits the newest (largest index) version of a key
            // first. Combined with `seen`, this keeps the newest version.
            let order: Vec<usize> = if need_dedup {
                (0..n).rev().collect()
            } else {
                (0..n).collect()
            };

            // Pre-decode filter column (once per segment).
            let fcol_fixed = filter_col.and_then(|fc| {
                if fc < seg.sst.column_tags.len() && seg.sst.column_tags[fc].is_fixed() {
                    seg.sst.read_fixed_i64(fc).ok()
                } else {
                    None
                }
            });
            let fcol_text = filter_col.and_then(|fc| {
                if fc < seg.sst.column_tags.len() && !seg.sst.column_tags[fc].is_fixed() {
                    seg.sst.read_text(fc).ok()
                } else {
                    None
                }
            });
            let fcol_type = filter_col.and_then(|fc| self.col_types.get(fc));

            // 🔑 PERF: do NOT pre-intern the entire text column (was 300K ArcString
            // allocations even when 99% of rows are filtered out by the predicate).
            // Instead, decode each row's text lazily via fcol_text.get_str(i) only
            // when the predicate needs it. The predicate receives Option<&Value>,
            // so we construct a Value::Text on the fly only for rows that need it
            // (all rows when filtering, but without the upfront allocation burst).
            // The fixed-column path already does this (per-row get_i64/get_f64).

            // Pre-decode project columns (once per segment) — unless lazy mode
            // (small result set): then we decode only for matched rows below.
            let pfixed: Vec<Option<crate::storage::lsm::columnar::FixedSegment>> = if !lazy_project
            {
                project_cols
                    .iter()
                    .map(|&pc| {
                        if pc < seg.sst.column_tags.len() && seg.sst.column_tags[pc].is_fixed() {
                            seg.sst.read_fixed_i64(pc).ok()
                        } else {
                            None
                        }
                    })
                    .collect()
            } else {
                Vec::new()
            };
            let ptext: Vec<Option<crate::storage::lsm::columnar::TextSegment>> = if !lazy_project {
                project_cols
                    .iter()
                    .map(|&pc| {
                        if pc < seg.sst.column_tags.len() && !seg.sst.column_tags[pc].is_fixed() {
                            seg.sst.read_text(pc).ok()
                        } else {
                            None
                        }
                    })
                    .collect()
            } else {
                Vec::new()
            };
            let n_seg = seg.sst.num_rows;
            let pvector: Vec<Vec<Option<Vec<f32>>>> = if !lazy_project {
                project_cols
                    .iter()
                    .map(|&pc| {
                        if pc < seg.sst.column_tags.len()
                            && matches!(
                                seg.sst.column_tags[pc],
                                crate::storage::lsm::columnar::ColumnTypeTag::Vector
                            )
                        {
                            let decoded = seg.sst.read_vectors(pc).unwrap_or_default();
                            let mut per = vec![None; n_seg];
                            let mut di = 0usize;
                            for i in 0..n_seg {
                                if seg.sst.row_map.is_deleted(i) {
                                    continue;
                                }
                                let ek = seg.sst.row_map.key(i) & 0xFFFFFFFF;
                                while di < decoded.len() && decoded[di].0 != ek {
                                    di += 1;
                                }
                                if di < decoded.len() {
                                    per[i] = Some(decoded[di].1.clone());
                                    di += 1;
                                }
                            }
                            per
                        } else {
                            Vec::new()
                        }
                    })
                    .collect()
            } else {
                Vec::new()
            };
            let pspatial: Vec<Vec<Option<crate::types::Geometry>>> = if !lazy_project {
                project_cols
                    .iter()
                    .map(|&pc| {
                        if pc < seg.sst.column_tags.len()
                            && matches!(
                                seg.sst.column_tags[pc],
                                crate::storage::lsm::columnar::ColumnTypeTag::Spatial
                            )
                        {
                            let decoded = seg.sst.read_spatial(pc).unwrap_or_default();
                            let mut per = vec![None; n_seg];
                            let mut di = 0usize;
                            for i in 0..n_seg {
                                if seg.sst.row_map.is_deleted(i) {
                                    continue;
                                }
                                let ek = seg.sst.row_map.key(i) & 0xFFFFFFFF;
                                while di < decoded.len() && decoded[di].0 != ek {
                                    di += 1;
                                }
                                if di < decoded.len() {
                                    per[i] = Some(decoded[di].1.clone());
                                    di += 1;
                                }
                            }
                            per
                        } else {
                            Vec::new()
                        }
                    })
                    .collect()
            } else {
                Vec::new()
            };

            let ptext_interned: Vec<Vec<Option<Value>>> = Vec::new();

            for &i in &order {
                let key = seg.sst.row_map.key(i);
                // Newest-version-wins dedup: skip if a newer version of this key
                // was already emitted. Mark seen BEFORE the deleted check so a
                // tombstone in a newer version suppresses older live rows.
                if need_dedup && !seen.insert(key) {
                    continue;
                }
                if seg.sst.row_map.is_deleted(i) {
                    continue;
                }

                // Decode filter value only (cheap: single column lookup).
                let fval: Option<Value> = if filter_col.is_some() {
                    let v = if let Some(ref f) = fcol_fixed {
                        match fcol_type {
                            Some(ColumnType::Integer) => f.get_i64(i).map(Value::Integer),
                            Some(ColumnType::Float) => f.get_f64(i).map(Value::Float),
                            Some(ColumnType::Boolean) => f.get_bool(i).map(Value::Bool),
                            _ => None,
                        }
                    } else if let Some(ref t) = fcol_text {
                        t.get_str(i).map(|s| Value::Text(s.into()))
                    } else {
                        None
                    };
                    v
                } else {
                    None
                };

                if !predicate(fval.as_ref()) {
                    continue;
                }

                // Decode output columns for matching row only.
                let mut row = Vec::with_capacity(project_cols.len());
                if lazy_project {
                    // Lazy mode: decode each column on-demand for this single row.
                    for &pc in project_cols.iter() {
                        let v = if pc < self.col_types.len() && pc < seg.sst.column_tags.len() {
                            if seg.sst.column_tags[pc].is_fixed() {
                                match self.col_types[pc] {
                                    ColumnType::Integer => seg
                                        .sst
                                        .read_fixed_i64(pc)
                                        .ok()
                                        .and_then(|f| f.get_i64(i))
                                        .map(Value::Integer),
                                    ColumnType::Float => seg
                                        .sst
                                        .read_fixed_i64(pc)
                                        .ok()
                                        .and_then(|f| f.get_f64(i))
                                        .map(Value::Float),
                                    ColumnType::Boolean => seg
                                        .sst
                                        .read_fixed_i64(pc)
                                        .ok()
                                        .and_then(|f| f.get_bool(i))
                                        .map(Value::Bool),
                                    _ => seg
                                        .sst
                                        .read_fixed_i64(pc)
                                        .ok()
                                        .and_then(|f| f.get_i64(i))
                                        .map(Value::Integer),
                                }
                            } else {
                                match seg
                                    .sst
                                    .read_text(pc)
                                    .ok()
                                    .and_then(|t| t.get_str(i).map(|s| s.to_string()))
                                {
                                    Some(s) => Some(Value::Text(s.into())),
                                    None => Some(Value::Null),
                                }
                            }
                        } else {
                            Some(Value::Null)
                        };
                        row.push(v.unwrap_or(Value::Null));
                    }
                } else {
                    for (pi, &pc) in project_cols.iter().enumerate() {
                        let v = if pc < self.col_types.len() {
                            match (&pfixed.get(pi), &ptext.get(pi), &self.col_types[pc]) {
                                (Some(Some(f)), _, ColumnType::Integer) => {
                                    f.get_i64(i).map(Value::Integer)
                                }
                                (Some(Some(f)), _, ColumnType::Float) => {
                                    f.get_f64(i).map(Value::Float)
                                }
                                (Some(Some(f)), _, ColumnType::Boolean) => {
                                    f.get_bool(i).map(Value::Bool)
                                }
                                (_, _, ColumnType::Spatial) => pspatial
                                    .get(pi)
                                    .and_then(|p| p.get(i))
                                    .cloned()
                                    .flatten()
                                    .map(|g| Value::Spatial(std::boxed::Box::new(g))),
                                (_, _, ColumnType::Tensor(_)) => pvector
                                    .get(pi)
                                    .and_then(|p| p.get(i))
                                    .cloned()
                                    .flatten()
                                    .map(|v| {
                                        Value::Vector(crate::types::ArcVec(std::sync::Arc::new(v)))
                                    }),
                                (_, Some(Some(t)), ColumnType::Text) => {
                                    if !ptext_interned.is_empty() {
                                        ptext_interned
                                            .get(pi)
                                            .and_then(|v| v.get(i))
                                            .cloned()
                                            .flatten()
                                    } else {
                                        t.get_str(i).map(|s| Value::Text(s.into()))
                                    }
                                }
                                _ => Some(Value::Null),
                            }
                        } else {
                            Some(Value::Null)
                        };
                        row.push(v.unwrap_or(Value::Null));
                    }
                } // end else (non-lazy)
                result.push((key, row));
                // 🚀 LIMIT early-termination: stop scanning once we have enough rows.
                if result.len() >= max_results {
                    return result;
                }
            }
        }
        result
    }

    /// High-performance scan with a Text (&str) predicate on the filter column.
    /// Avoids constructing a Value for the filter column entirely — the predicate
    /// receives the raw &str borrowed from the segment (zero allocation). Only
    /// matched rows get their output columns decoded (and output Text cols use
    /// pre-interned ArcString clones). This is the fast path for WHERE col = 'x'
    /// and LIKE 'prefix%' on text columns.
    pub fn scan_text_filtered(
        &self,
        filter_col: usize,
        project_cols: &[usize],
        str_predicate: &dyn Fn(Option<&str>) -> bool,
    ) -> Vec<(u64, Vec<Value>)> {
        self.scan_text_filtered_limit(filter_col, project_cols, str_predicate, usize::MAX)
    }

    /// Returns row INDICES (segment-local) that match the text filter, without
    /// decoding any output columns. The caller passes these indices to
    /// SelectColumnar for zero-copy materialization — avoiding N Vec<Value>
    /// allocations during scan. Only works for single-segment stores.
    ///
    /// Returns (indices, found). found=false if multi-segment (caller falls
    /// back to scan_text_filtered_limit).
    pub fn scan_row_indices_text_filter(
        &self,
        filter_col: usize,
        str_predicate: &dyn Fn(Option<&str>) -> bool,
        limit: usize,
    ) -> Option<Vec<usize>> {
        let segs = self.segments_snapshot();
        if segs.len() != 1 {
            return None;
        }
        let seg = &segs[0];
        let n = seg.sst.num_rows;
        let cap = if limit == usize::MAX { n } else { limit };
        let mut indices: Vec<usize> = Vec::with_capacity(cap.min(65536));
        let ftext = seg.sst.read_text(filter_col).ok();
        if let Some(tseg) = ftext.as_ref() {
            let has_nulls = tseg.has_any_null();
            let has_deletions = seg.sst.row_map.has_any_deleted();
            // 🚀 Fast inner loop: minimize branches per row.
            // When no nulls and no deletions, skip both checks entirely.
            if !has_nulls && !has_deletions {
                for i in 0..n {
                    let s = tseg.get_str_fast(i);
                    if str_predicate(Some(s)) {
                        indices.push(i);
                        if indices.len() >= limit {
                            break;
                        }
                    }
                }
            } else {
                for i in 0..n {
                    if has_deletions && seg.sst.row_map.is_deleted(i) {
                        continue;
                    }
                    let matches = if has_nulls {
                        str_predicate(if tseg.is_null(i) {
                            None
                        } else {
                            tseg.get_str(i)
                        })
                    } else {
                        str_predicate(Some(tseg.get_str_fast(i)))
                    };
                    if matches {
                        indices.push(i);
                        if indices.len() >= limit {
                            break;
                        }
                    }
                }
            }
        }
        Some(indices)
    }

    /// Prefix-match scan: returns row indices where the text column starts with
    /// `prefix`. Specialized hot path for LIKE 'prefix%' — uses direct byte
    /// comparison via `memcmp`-style slice check, avoiding closure dispatch and
    /// Option wrapping. ~20% faster than the generic text filter for prefix LIKE.
    pub fn scan_row_indices_prefix(
        &self,
        filter_col: usize,
        prefix: &[u8],
        limit: usize,
    ) -> Option<Vec<(usize, usize)>> {
        // Returns (segment_idx, local_row_idx) pairs for rows whose text column
        // starts with the given prefix. Multi-segment safe (dedup by key).
        let segs = self.segments_snapshot();
        let single_seg = segs.len() <= 1;
        let cap = if limit == usize::MAX { 65536 } else { limit };
        let mut indices: Vec<(usize, usize)> = Vec::with_capacity(cap.min(65536));
        let mut seen: std::collections::HashSet<u64> = if single_seg {
            std::collections::HashSet::new()
        } else {
            std::collections::HashSet::with_capacity(segs.iter().map(|s| s.sst.num_rows).sum())
        };
        let plen = prefix.len();
        for (sidx, seg) in segs.iter().enumerate() {
            let n = seg.sst.num_rows;
            let ftext = match seg.sst.read_text(filter_col) {
                Ok(t) => t,
                Err(_) => continue,
            };
            let has_nulls = ftext.has_any_null();
            let has_deletions = seg.sst.row_map.has_any_deleted();
            if !has_nulls && !has_deletions {
                // 🔑 Fast path (single OR multi segment): use the batch
                // prefix_match_indices which walks raw offsets in one pass
                // (no per-row slice() calls). Works for any segment count.
                let matched = ftext.prefix_match_indices(prefix);
                for &i in &matched {
                    if !single_seg {
                        let key = seg.sst.row_map.key(i);
                        if !seen.insert(key) {
                            continue;
                        }
                    }
                    indices.push((sidx, i));
                    if indices.len() >= limit {
                        return Some(indices);
                    }
                }
            } else {
                for i in 0..n {
                    if !single_seg {
                        let key = seg.sst.row_map.key(i);
                        if !seen.insert(key) {
                            continue;
                        }
                    }
                    if has_deletions && seg.sst.row_map.is_deleted(i) {
                        continue;
                    }
                    if let Some(s) = ftext.get_str(i) {
                        if s.len() >= plen && &s.as_bytes()[..plen] == prefix {
                            indices.push((sidx, i));
                            if indices.len() >= limit {
                                return Some(indices);
                            }
                        }
                    }
                }
            }
        }
        Some(indices)
    }

    /// Scan for rows where a TEXT column exactly equals `target`. Returns
    /// (segment_idx, local_row_idx) pairs. Zero-alloc via eq_match_indices.
    /// Used by `WHERE text_col = 'literal'` to bypass the Box<dyn Fn> path
    /// that pre-interns the entire column into ArcString Values.
    pub fn scan_row_indices_eq(
        &self,
        filter_col: usize,
        target: &[u8],
        limit: usize,
    ) -> Option<Vec<(usize, usize)>> {
        let segs = self.segments_snapshot();
        let mut indices: Vec<(usize, usize)> = Vec::with_capacity(1024);
        // Newest-version-wins dedup: iterate segments newest→oldest, rows
        // newest→oldest within each. The first time we see a composite key is
        // the live version; older versions of the same key are skipped.
        // Without this, an UPDATE that changed cat from 'a' to 'b' would leave
        // the old 'a' row matchable even though it's logically overwritten.
        let need_dedup = segs.len() > 1 || self.may_have_duplicate_keys();
        let mut seen: std::collections::HashSet<u64> = if need_dedup {
            std::collections::HashSet::with_capacity(segs.iter().map(|s| s.sst.num_rows).sum())
        } else {
            std::collections::HashSet::new()
        };
        for (sidx, seg) in segs.iter().enumerate().rev() {
            let ftext = match seg.sst.read_text(filter_col) {
                Ok(t) => t,
                Err(_) => continue,
            };
            let has_deletions = seg.sst.row_map.has_any_deleted();
            // Iterate rows newest→oldest within segment so dedup keeps the
            // latest version of each key.
            let row_order: Vec<usize> = if need_dedup {
                (0..seg.sst.num_rows).rev().collect()
            } else {
                (0..seg.sst.num_rows).collect()
            };
            for &i in &row_order {
                if need_dedup {
                    let key = seg.sst.row_map.key(i);
                    if !seen.insert(key) {
                        continue;
                    }
                }
                if has_deletions && seg.sst.row_map.is_deleted(i) {
                    continue;
                }
                // Check if this row's text value matches target.
                if let Some(s) = ftext.get_str(i) {
                    if s.as_bytes() == target {
                        indices.push((sidx, i));
                        if indices.len() >= limit {
                            return Some(indices);
                        }
                    }
                }
            }
        }
        Some(indices)
    }

    /// Scan for rows where a TEXT column value is in `targets`. Returns
    /// (segment_idx, local_row_idx) pairs. Zero-alloc via in_set_match_indices.
    /// Used by `WHERE text_col IN (v1, v2, ...)` (semi-join from subquery).
    pub fn scan_row_indices_in_set(
        &self,
        filter_col: usize,
        targets: &std::collections::HashSet<&[u8]>,
        limit: usize,
    ) -> Option<Vec<(usize, usize)>> {
        let segs = self.segments_snapshot();
        let single_seg = segs.len() <= 1;
        let mut indices: Vec<(usize, usize)> = Vec::with_capacity(1024);
        let mut seen: std::collections::HashSet<u64> = if single_seg {
            std::collections::HashSet::new()
        } else {
            std::collections::HashSet::with_capacity(segs.iter().map(|s| s.sst.num_rows).sum())
        };
        for (sidx, seg) in segs.iter().enumerate() {
            let ftext = match seg.sst.read_text(filter_col) {
                Ok(t) => t,
                Err(_) => continue,
            };
            let has_deletions = seg.sst.row_map.has_any_deleted();
            if !has_deletions {
                let matched = ftext.in_set_match_indices(targets);
                for &i in &matched {
                    if !single_seg {
                        let key = seg.sst.row_map.key(i);
                        if !seen.insert(key) {
                            continue;
                        }
                    }
                    indices.push((sidx, i));
                    if indices.len() >= limit {
                        return Some(indices);
                    }
                }
            } else {
                for i in 0..seg.sst.num_rows {
                    if !single_seg {
                        let key = seg.sst.row_map.key(i);
                        if !seen.insert(key) {
                            continue;
                        }
                    }
                    if seg.sst.row_map.is_deleted(i) {
                        continue;
                    }
                    if let Some(s) = ftext.get_str(i) {
                        if targets.contains(s.as_bytes()) {
                            indices.push((sidx, i));
                            if indices.len() >= limit {
                                return Some(indices);
                            }
                        }
                    }
                }
            }
        }
        Some(indices)
    }

    /// Legacy single-segment variant — kept for backward compat.
    pub fn scan_row_indices_prefix_single(
        &self,
        filter_col: usize,
        prefix: &[u8],
        limit: usize,
    ) -> Option<Vec<usize>> {
        let segs = self.segments_snapshot();
        if segs.len() != 1 {
            return None;
        }
        let seg = &segs[0];
        let n = seg.sst.num_rows;
        let cap = if limit == usize::MAX { n } else { limit };
        let mut indices: Vec<usize> = Vec::with_capacity(cap.min(65536));
        let ftext = match seg.sst.read_text(filter_col) {
            Ok(t) => t,
            Err(_) => return Some(indices),
        };
        let has_nulls = ftext.has_any_null();
        let has_deletions = seg.sst.row_map.has_any_deleted();
        let plen = prefix.len();
        // 🚀 Fast path: no nulls + no deletions — tightest possible loop.
        if !has_nulls && !has_deletions {
            for i in 0..n {
                let s = ftext.get_str_fast(i);
                if s.len() >= plen && &s.as_bytes()[..plen] == prefix {
                    indices.push(i);
                    if indices.len() >= limit {
                        break;
                    }
                }
            }
        } else {
            for i in 0..n {
                if has_deletions && seg.sst.row_map.is_deleted(i) {
                    continue;
                }
                if has_nulls && ftext.is_null(i) {
                    continue;
                }
                // Direct byte comparison: get the string's raw bytes and check
                // if the first `plen` bytes match the prefix.
                if has_nulls {
                    if let Some(s) = ftext.get_str(i) {
                        if s.len() >= plen && &s.as_bytes()[..plen] == prefix {
                            indices.push(i);
                            if indices.len() >= limit {
                                break;
                            }
                        }
                    }
                } else {
                    let s = ftext.get_str_fast(i);
                    if s.len() >= plen && &s.as_bytes()[..plen] == prefix {
                        indices.push(i);
                        if indices.len() >= limit {
                            break;
                        }
                    }
                }
            }
        }
        Some(indices)
    }

    /// Text-filtered scan with early exit after `limit` matches.
    /// 1. Early exit: stops as soon as `limit` matches are collected.
    /// 2. Skips per-segment key sort + HashSet for the single-segment common
    ///    case (no dedup needed → natural 0..n order, saves O(N log N)).
    pub fn scan_text_filtered_limit(
        &self,
        filter_col: usize,
        project_cols: &[usize],
        str_predicate: &dyn Fn(Option<&str>) -> bool,
        limit: usize,
    ) -> Vec<(u64, Vec<Value>)> {
        let cap = if limit == usize::MAX { 65536 } else { limit };
        let mut result: Vec<(u64, Vec<Value>)> = Vec::with_capacity(cap.min(65536));
        let segs = self.segments_snapshot();
        let single_seg = segs.len() <= 1;

        // Only multi-segment needs dedup (seen set) + key-sorted iteration.
        // Single segment: iterate 0..n directly — no sort, no HashSet.
        let mut seen: Option<std::collections::HashSet<u64>> = if single_seg {
            None
        } else {
            let total_rows: usize = segs.iter().map(|s| s.sst.num_rows).sum();
            Some(std::collections::HashSet::with_capacity(total_rows))
        };

        'outer: for seg in segs.iter().rev() {
            let n = seg.sst.num_rows;

            // Filter column: read text segment once, predicate gets &str directly.
            let ftext = seg.sst.read_text(filter_col).ok();

            // Pre-read output columns (same segment, one-time cost per column).
            // This is O(cols) not O(rows) — much faster than per-row lazy decode.
            let pfixed: Vec<Option<crate::storage::lsm::columnar::FixedSegment>> = project_cols
                .iter()
                .map(|&pc| {
                    if pc < seg.sst.column_tags.len() && seg.sst.column_tags[pc].is_fixed() {
                        seg.sst.read_fixed_i64(pc).ok()
                    } else {
                        None
                    }
                })
                .collect();
            let ptext_cols: Vec<Option<crate::storage::lsm::columnar::TextSegment>> = project_cols
                .iter()
                .map(|&pc| {
                    if pc < seg.sst.column_tags.len()
                        && !seg.sst.column_tags[pc].is_fixed()
                        && !matches!(
                            seg.sst.column_tags[pc],
                            crate::storage::lsm::columnar::ColumnTypeTag::Spatial
                        )
                    {
                        seg.sst.read_text(pc).ok()
                    } else {
                        None
                    }
                })
                .collect();

            // Inner row-processing macro — shared between natural & sorted order.
            macro_rules! process_row {
                ($i:expr) => {{
                    let i = $i;
                    let key = seg.sst.row_map.key(i);
                    // Mark key as seen BEFORE checking deleted, so tombstones suppress
                    // older versions of the same key in earlier segments.
                    if let Some(ref mut s) = seen {
                        if !s.insert(key) {
                            continue;
                        }
                    }
                    if seg.sst.row_map.is_deleted(i) {
                        continue;
                    }

                    // Filter: pass raw &str to predicate (zero Value allocation).
                    let fval =
                        ftext
                            .as_ref()
                            .and_then(|t| if t.is_null(i) { None } else { t.get_str(i) });
                    if !str_predicate(fval) {
                        continue;
                    }

                    // Decode output columns from pre-read segments (O(1) per row).
                    let mut row = Vec::with_capacity(project_cols.len());
                    for (pi, &pc) in project_cols.iter().enumerate() {
                        let v = if pc < self.col_types.len() {
                            if matches!(
                                self.col_types[pc],
                                ColumnType::Spatial | ColumnType::Tensor(_)
                            ) {
                                Some(Value::Null)
                            } else if let Some(Some(ref f)) = pfixed.get(pi) {
                                match self.col_types[pc] {
                                    ColumnType::Integer => f.get_i64(i).map(Value::Integer),
                                    ColumnType::Float => f.get_f64(i).map(Value::Float),
                                    ColumnType::Boolean => f.get_bool(i).map(Value::Bool),
                                    _ => None,
                                }
                            } else if let Some(Some(ref t)) = ptext_cols.get(pi) {
                                t.get_str(i).map(|s| Value::Text(s.into()))
                            } else {
                                None
                            }
                        } else {
                            None
                        };
                        row.push(v.unwrap_or(Value::Null));
                    }
                    result.push((key, row));

                    // 🔥 Early exit: stop scanning once we have `limit` matches.
                    if result.len() >= limit {
                        break 'outer;
                    }
                }};
            }

            if single_seg {
                // Natural order — no sort, no dedup. The hot path for SELECTs.
                for i in 0..n {
                    process_row!(i);
                }
            } else {
                // Multi-segment: sort by key so newest version wins dedup.
                let mut order: Vec<usize> = (0..n).collect();
                order.sort_unstable_by_key(|&i| seg.sst.row_map.key(i));
                for &i in &order {
                    process_row!(i);
                }
            }
        }
        result
    }

    pub fn segment_count(&self) -> usize {
        self.segments.read().len()
    }

    /// 🚀 Combined scan + row build for text-equality WHERE queries.
    /// Reads the filter column, applies equality, AND builds output rows
    /// in a single pass — no intermediate indices Vec, no SelectColumnar.
    /// ~15% faster than scan_row_indices + materialize for WHERE col='val'.
    pub fn scan_text_eq_build(
        &self,
        filter_col: usize,
        filter_val: &str,
        project_cols: &[usize],
        col_types: &[ColumnType],
        limit: usize,
    ) -> Option<Vec<Vec<Value>>> {
        let segs = self.segments_snapshot();
        if segs.len() != 1 {
            return None;
        }
        let seg = &segs[0];
        let n = seg.sst.num_rows;
        let ftext = seg.sst.read_text(filter_col).ok()?;

        // Pre-read output columns.
        let ncols = project_cols.len();
        let fixed_cols: Vec<Option<crate::storage::lsm::columnar::FixedSegment>> = project_cols
            .iter()
            .map(|&pc| {
                if pc < seg.sst.column_tags.len() && seg.sst.column_tags[pc].is_fixed() {
                    seg.sst.read_fixed_i64(pc).ok()
                } else {
                    None
                }
            })
            .collect();
        let text_cols: Vec<Option<crate::storage::lsm::columnar::TextSegment>> = project_cols
            .iter()
            .map(|&pc| {
                if pc < seg.sst.column_tags.len()
                    && matches!(
                        seg.sst.column_tags[pc],
                        crate::storage::lsm::columnar::ColumnTypeTag::Text
                    )
                {
                    seg.sst.read_text(pc).ok()
                } else {
                    None
                }
            })
            .collect();

        // String pool for text output columns.
        let mut str_pool: std::collections::HashMap<&str, std::sync::Arc<str>> =
            std::collections::HashMap::with_capacity(64);

        let has_nulls = ftext.has_any_null();
        let has_deletions = seg.sst.row_map.has_any_deleted();

        let cap = if limit == usize::MAX { n / 2 } else { limit };
        let mut result: Vec<Vec<Value>> = Vec::with_capacity(cap.min(65536));

        // Tight inner loop: scan + filter + build in one pass.
        // Use Vec::with_capacity per row — the buffer reuse pattern doesn't
        // actually work because mem::take leaves a zero-capacity Vec.
        if !has_nulls && !has_deletions {
            for i in 0..n {
                // Inline equality check — avoids closure dispatch.
                let s = ftext.get_str_fast(i);
                if Some(s) == Some(filter_val) {
                    let mut row = Vec::with_capacity(ncols);
                    for (pi, &pc) in project_cols.iter().enumerate() {
                        let v = if let Some(Some(ref f)) = fixed_cols.get(pi) {
                            match col_types.get(pc) {
                                Some(ColumnType::Integer) => f.get_i64(i).map(Value::Integer),
                                Some(ColumnType::Float) => f.get_f64(i).map(Value::Float),
                                Some(ColumnType::Boolean) => f.get_bool(i).map(Value::Bool),
                                _ => None,
                            }
                        } else if let Some(Some(ref t)) = text_cols.get(pi) {
                            t.get_str(i).map(|s| {
                                let arc = str_pool.get(s).cloned().unwrap_or_else(|| {
                                    let a: std::sync::Arc<str> = std::sync::Arc::from(s);
                                    if str_pool.len() < 10000 {
                                        str_pool.insert(s, a.clone());
                                    }
                                    a
                                });
                                Value::Text(ArcString(arc))
                            })
                        } else {
                            Some(Value::Null)
                        };
                        row.push(v.unwrap_or(Value::Null));
                    }
                    result.push(row);
                    if result.len() >= limit {
                        break;
                    }
                }
            }
        } else {
            // Slow path with null/deletion checks.
            for i in 0..n {
                if has_deletions && seg.sst.row_map.is_deleted(i) {
                    continue;
                }
                let s = if has_nulls {
                    ftext.get_str(i)
                } else {
                    Some(ftext.get_str_fast(i))
                };
                if s != Some(filter_val) {
                    continue;
                }
                let mut row = Vec::with_capacity(ncols);
                for (pi, &pc) in project_cols.iter().enumerate() {
                    let v = if let Some(Some(ref f)) = fixed_cols.get(pi) {
                        match col_types.get(pc) {
                            Some(ColumnType::Integer) => f.get_i64(i).map(Value::Integer),
                            Some(ColumnType::Float) => f.get_f64(i).map(Value::Float),
                            Some(ColumnType::Boolean) => f.get_bool(i).map(Value::Bool),
                            _ => None,
                        }
                    } else if let Some(Some(ref t)) = text_cols.get(pi) {
                        t.get_str(i).map(|s| {
                            let arc = str_pool.get(s).cloned().unwrap_or_else(|| {
                                let a: std::sync::Arc<str> = std::sync::Arc::from(s);
                                if str_pool.len() < 10000 {
                                    str_pool.insert(s, a.clone());
                                }
                                a
                            });
                            Value::Text(ArcString(arc))
                        })
                    } else {
                        Some(Value::Null)
                    };
                    row.push(v.unwrap_or(Value::Null));
                }
                result.push(row);
                if result.len() >= limit {
                    break;
                }
            }
        }

        Some(result)
    }

    /// Streaming Top-K: read only the sort column, maintain a bounded heap of
    /// (value, key) pairs, return the K winning keys. Avoids materializing all
    /// N rows + sorting — O(N log K) with O(K) memory.
    ///
    /// For ORDER BY amount DESC LIMIT 10: reads only the amount column (1 col),
    /// keeps top 10 in a heap, then the caller fetches only those 10 full rows.
    pub fn topk_keys_by_fixed_col(&self, sort_col: usize, k: usize, ascending: bool) -> Vec<u64> {
        use std::collections::BinaryHeap;

        let segs = self.segments_snapshot();
        let single_seg = segs.len() <= 1;
        let mut seen: Option<std::collections::HashSet<u64>> = if single_seg {
            None
        } else {
            let total_rows: usize = segs.iter().map(|s| s.sst.num_rows).sum();
            Some(std::collections::HashSet::with_capacity(total_rows))
        };

        // Wrap f64 for total ordering (NaN-safe).
        #[derive(Clone)]
        struct OrdF64(f64);
        impl PartialEq for OrdF64 {
            fn eq(&self, o: &Self) -> bool {
                self.0 == o.0
            }
        }
        impl Eq for OrdF64 {}
        impl PartialOrd for OrdF64 {
            fn partial_cmp(&self, o: &Self) -> Option<std::cmp::Ordering> {
                Some(self.cmp(o))
            }
        }
        impl Ord for OrdF64 {
            fn cmp(&self, o: &Self) -> std::cmp::Ordering {
                self.0
                    .partial_cmp(&o.0)
                    .unwrap_or(std::cmp::Ordering::Equal)
            }
        }

        // BinaryHeap is a max-heap. To keep the K LARGEST (descending), we need
        // a min-heap so the smallest is evicted → wrap in Reverse.
        // To keep the K SMALLEST (ascending), we need a max-heap → no Reverse.
        let mut heap: BinaryHeap<(OrdF64, u64)> = BinaryHeap::with_capacity(k + 1);
        for seg in segs.iter().rev() {
            let n = seg.sst.num_rows;
            let fseg = match seg.sst.read_fixed_i64(sort_col) {
                Ok(f) => f,
                Err(_) => continue,
            };
            for i in 0..n {
                let key = seg.sst.row_map.key(i);
                if let Some(ref mut s) = seen {
                    if !s.insert(key) {
                        continue;
                    }
                }
                if seg.sst.row_map.is_deleted(i) {
                    continue;
                }
                if let Some(v) = fseg
                    .get_f64(i)
                    .or_else(|| fseg.get_i64(i).map(|x| x as f64))
                {
                    let entry = if ascending {
                        (OrdF64(v), key)
                    } else {
                        // For descending: use negated value so max-heap keeps largest.
                        (OrdF64(-v), key)
                    };
                    heap.push(entry);
                    if heap.len() > k {
                        heap.pop();
                    }
                }
            }
        }

        let mut result: Vec<(f64, u64)> = heap
            .into_iter()
            .map(|(of, key)| {
                let v = if ascending { of.0 } else { -of.0 };
                (v, key)
            })
            .collect();
        // Sort by value descending (for DESC) or ascending (for ASC).
        if ascending {
            result.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
        } else {
            result.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));
        }
        result.into_iter().map(|(_, key)| key).collect()
    }

    /// Snapshot of active segments (oldest→newest). Callers iterate directly
    /// for single-column reads (e.g. CREATE INDEX) without full-row decode.
    pub fn segments_snapshot(&self) -> Vec<Arc<Segment>> {
        self.segments.read().iter().cloned().collect()
    }

    /// Release mmap pages + clear col caches to reduce RSS after queries.
    /// Call after batch queries to keep memory low.
    /// Clear column decode caches to reduce heap memory. Does NOT release
    /// mmap pages (MADV_DONTNEED) — keeping them warm makes subsequent queries
    /// fast (no re-faulting). mmap pages count against OS page cache, not heap.
    /// Call after batch queries to release decode-cache heap allocations.
    pub fn release_query_memory(&self) {
        let segs = self.segments.read();
        for seg in segs.iter() {
            seg.clear_cache();
        }
    }

    /// After flush+compaction to a single segment, return that segment's SSTable
    /// as a shared Arc. Legacy read paths (aggregate, GROUP BY) read
    /// `columnar_sstables: DashMap<String, Arc<ColumnarSSTable>>`; this lets them
    /// observe the same SSTable without cloning (Arc shared). Returns None if
    /// the store has no segments.
    pub fn latest_segment_sst(
        &self,
    ) -> Option<Arc<crate::storage::lsm::columnar::ColumnarSSTable>> {
        self.segments.read().back().map(|seg| Arc::clone(&seg.sst))
    }

    /// Number of rows currently buffered in memory (not yet flushed to a segment).
    /// Count live (non-deleted, non-duplicated) rows across all segments.
    /// O(total_rows) but zero Value decode — fast for COUNT(*).
    /// Heuristic: does a single (compacted) segment possibly hold multiple
    /// versions of the same key? flush_buffer() runs dedup_keys_newest_wins, so
    /// a freshly-flushed single segment has unique keys. Duplicate keys can
    /// appear only when an UPDATE was buffered and NOT yet flushed/compacted.
    /// Returns false for the common case (no pending UPDATEs), letting the scan
    /// path skip dedup entirely (the v0.5.0 performance fix).
    fn may_have_duplicate_keys(&self) -> bool {
        // The write buffer can hold a newer version of an already-segmented key.
        // Multiple segments can also hold overlapping keys (e.g. an INSERT
        // segment flushed by auto-checkpoint, then an UPDATE segment from a
        // later flush — both contain the same composite key with different
        // values). Conservative: dedup whenever there's buffered data OR 2+
        // segments. A single compacted segment with empty buffer is the only
        // safe no-dedup case.
        let buf_n = self.write_buf.lock().num_rows;
        let seg_count = self.segments.read().len();
        buf_n > 0 && seg_count >= 1 || seg_count >= 2
    }

    /// Count rows matching a filter WITHOUT materializing Value objects.
    /// Optimized for COUNT(*) WHERE col = val / col > val / col < val.
    ///
    /// For Integer/Float filter columns, compares raw i64/f64 bits directly.
    /// For Text filter columns, compares &str without ArcString allocation.
    /// This avoids the per-row Value::Text(s.into()) / Value::Integer(v)
    /// allocation that scan_projected_filtered does — the dominant cost for
    /// COUNT WHERE on large tables (was ~1ms for 20K rows).
    pub fn count_filtered(
        &self,
        filter_col: usize,
        op: &crate::sql::ast::BinaryOperator,
        target: &Value,
    ) -> usize {
        // 🔑 Flush buffered writes (INSERT/UPDATE/DELETE) so they're visible to
        // the segment scan. Without this, count_filtered only sees persisted
        // segments and misses buffered updates.
        let _ = self.flush_buffer();

        // Determine dedup need BEFORE locking write_buf (may_have_duplicate_keys
        // also locks write_buf — parking_lot Mutex is not reentrant → deadlock).
        let need_dedup = self.may_have_duplicate_keys();
        let buf = self.write_buf.lock();
        let segs = self.segments.read();
        let mut seen: std::collections::HashSet<u64> = if need_dedup {
            std::collections::HashSet::with_capacity(segs.iter().map(|s| s.sst.num_rows).sum())
        } else {
            std::collections::HashSet::new()
        };

        // Pre-extract target comparison value (avoids re-matching per row).
        let target_i = if let Value::Integer(v) = target {
            Some(*v)
        } else {
            None
        };
        let mut target_f = if let Value::Float(v) = target {
            Some(*v)
        } else {
            None
        };
        // 🔑 Cross-type: if the literal was parsed as Integer but the column is
        // Float, convert it so the comparison works (WHERE score > 50 parses
        // as Integer(50), but score is a FLOAT column).
        if target_f.is_none() {
            if let Some(i) = target_i {
                target_f = Some(i as f64);
            }
        }
        let target_s: Option<&str> = if let Value::Text(t) = target {
            Some(t.as_str())
        } else {
            None
        };
        let target_b = if let Value::Bool(v) = target {
            Some(*v)
        } else {
            None
        };

        let mut count = 0usize;
        for seg in segs.iter().rev() {
            let n = seg.sst.num_rows;
            if filter_col >= seg.sst.column_tags.len() {
                continue;
            }
            let tag = seg.sst.column_tags[filter_col];

            // Pre-decode the filter column once per segment.
            let fcol_fixed = if tag.is_fixed() {
                seg.sst.read_fixed_i64(filter_col).ok()
            } else {
                None
            };
            let fcol_text = if matches!(tag, ColumnTypeTag::Text) {
                seg.sst.read_text(filter_col).ok()
            } else {
                None
            };

            let order: Vec<usize> = if need_dedup {
                (0..n).rev().collect()
            } else {
                (0..n).collect()
            };
            for &i in &order {
                let key = seg.sst.row_map.key(i);
                if need_dedup && !seen.insert(key) {
                    continue;
                }
                if seg.sst.row_map.is_deleted(i) {
                    continue;
                }

                let matches = if let Some(ref f) = fcol_fixed {
                    // Fixed-width: compare raw i64/f64 bits, no Value alloc.
                    match tag {
                        ColumnTypeTag::Integer | ColumnTypeTag::Timestamp => {
                            let v = f.get_i64(i);
                            cmp_opt(v, target_i, op)
                        }
                        ColumnTypeTag::Float => {
                            let v = f.get_f64(i);
                            cmp_opt_f64(v, target_f, op)
                        }
                        ColumnTypeTag::Bool => {
                            let v = f.get_bool(i);
                            cmp_opt(v, target_b, op)
                        }
                        _ => false,
                    }
                } else if let Some(ref t) = fcol_text {
                    // Text: compare &str directly, no ArcString alloc.
                    match t.get_str(i) {
                        Some(s) => cmp_str(Some(s), target_s, op),
                        None => false, // NULL never matches
                    }
                } else {
                    false
                };

                if matches {
                    count += 1;
                }
            }
        }
        drop(buf);
        count
    }

    /// Single-pass aggregate over a filtered column — computes COUNT/SUM/AVG/
    /// MIN/MAX in one scan without materializing Value objects per row.
    /// Returns (count, int_sum, float_sum, has_float, min_int, max_int,
    /// min_float, max_float). The caller picks the relevant fields per aggregate.
    ///
    /// 🔑 PERF: scan_projected_filtered materialized a Vec<Value> per row then
    /// did multi-pass collect()+sum(). This folds directly over raw i64/f64
    /// column bytes — zero per-row allocation, single pass.
    pub fn aggregate_filtered(
        &self,
        filter_col: Option<usize>,
        agg_col: usize,
        op: &crate::sql::ast::BinaryOperator,
        target: &Value,
    ) -> AggregateResult {
        // 🔑 Flush buffered writes so they're visible to the segment scan.
        let _ = self.flush_buffer();
        let need_dedup = self.may_have_duplicate_keys();
        let segs = self.segments_snapshot();
        let mut seen: std::collections::HashSet<u64> = if need_dedup {
            std::collections::HashSet::with_capacity(segs.iter().map(|s| s.sst.num_rows).sum())
        } else {
            std::collections::HashSet::new()
        };
        // Pre-extract filter target for comparison.
        let target_i = if let Value::Integer(v) = target {
            Some(*v)
        } else {
            None
        };
        let target_f = if let Value::Float(v) = target {
            Some(*v)
        } else {
            None
        };
        let target_s: Option<&str> = if let Value::Text(t) = target {
            Some(t.as_str())
        } else {
            None
        };
        let no_filter = filter_col.is_none();
        let fc = filter_col.unwrap_or(0);

        let mut result = AggregateResult::default();
        for seg in segs.iter().rev() {
            let n = seg.sst.num_rows;
            if agg_col >= seg.sst.column_tags.len() {
                continue;
            }
            // Pre-decode filter + aggregate columns once per segment.
            let fcol_fixed = if !no_filter
                && fc < seg.sst.column_tags.len()
                && seg.sst.column_tags[fc].is_fixed()
            {
                seg.sst.read_fixed_i64(fc).ok()
            } else {
                None
            };
            let fcol_text = if !no_filter
                && fc < seg.sst.column_tags.len()
                && matches!(seg.sst.column_tags[fc], ColumnTypeTag::Text)
            {
                seg.sst.read_text(fc).ok()
            } else {
                None
            };
            let agg_fixed = if seg.sst.column_tags[agg_col].is_fixed() {
                seg.sst.read_fixed_i64(agg_col).ok()
            } else {
                None
            };
            let agg_is_float = matches!(self.col_types.get(agg_col), Some(ColumnType::Float));

            let order: Vec<usize> = if need_dedup {
                (0..n).rev().collect()
            } else {
                (0..n).collect()
            };
            for &i in &order {
                let key = seg.sst.row_map.key(i);
                if need_dedup && !seen.insert(key) {
                    continue;
                }
                if seg.sst.row_map.is_deleted(i) {
                    continue;
                }

                // Apply filter predicate (zero-alloc, same as count_filtered).
                let passes = if no_filter {
                    true
                } else if let Some(ref f) = fcol_fixed {
                    match seg.sst.column_tags[fc] {
                        ColumnTypeTag::Integer | ColumnTypeTag::Timestamp => {
                            cmp_opt(f.get_i64(i), target_i, op)
                        }
                        ColumnTypeTag::Float => cmp_opt_f64(f.get_f64(i), target_f, op),
                        ColumnTypeTag::Bool => {
                            let tb = target_i.map(|i| i != 0);
                            cmp_opt(f.get_bool(i), tb, op)
                        }
                        _ => false,
                    }
                } else if let Some(ref t) = fcol_text {
                    cmp_str(t.get_str(i), target_s, op)
                } else {
                    false
                };

                if !passes {
                    continue;
                }

                // Fold aggregate value directly (no Value construction).
                // 🔑 COUNT(col)/SUM/AVG/MIN/MAX all skip NULLs — only count
                // when the value is present (get_i64/get_f64 return None for NULL).
                if let Some(ref af) = agg_fixed {
                    if agg_is_float {
                        match af.get_f64(i) {
                            Some(v) => {
                                result.count += 1;
                                result.float_sum += v;
                                result.has_float = true;
                                if result.count == 1 {
                                    result.min_float = v;
                                    result.max_float = v;
                                } else {
                                    result.min_float = result.min_float.min(v);
                                    result.max_float = result.max_float.max(v);
                                }
                            }
                            None => {
                                result.null_count += 1;
                            }
                        }
                    } else {
                        match af.get_i64(i) {
                            Some(v) => {
                                result.count += 1;
                                result.int_sum = result.int_sum.wrapping_add(v);
                                if result.count == 1 {
                                    result.min_int = v;
                                    result.max_int = v;
                                } else {
                                    result.min_int = result.min_int.min(v);
                                    result.max_int = result.max_int.max(v);
                                }
                            }
                            None => {
                                result.null_count += 1;
                            }
                        }
                    }
                } else {
                    // Variable-width column (TEXT/Vector/Spatial): COUNT(col) counts
                    // non-NULL rows. Use the column's null_flags to determine NULL.
                    let is_null = self
                        .col_types
                        .get(agg_col)
                        .and_then(|_| seg.sst.read_text(agg_col).ok())
                        .map(|t| t.is_null(i))
                        .unwrap_or(true);
                    if is_null {
                        result.null_count += 1;
                    } else {
                        result.count += 1;
                    }
                }
            }
        }
        result
    }

    pub fn count_live_rows(&self) -> usize {
        // Fast path: single segment, no buffer, no deletions → just return num_rows.
        // This covers the common case (fresh insert, no UPDATE/DELETE history).
        let buf = self.write_buf.lock();
        let buf_count = buf.num_rows;
        let segs = self.segments.read();
        if segs.len() == 1 && buf_count == 0 {
            let seg = &segs[0];
            if !seg.sst.row_map.has_any_deleted() {
                return seg.sst.num_rows;
            }
            // Single segment with deletions: count non-deleted rows directly
            // (O(n) scan of the row_map, no HashMap allocation).
            let mut count = 0usize;
            for i in 0..seg.sst.num_rows {
                if !seg.sst.row_map.is_deleted(i) {
                    count += 1;
                }
            }
            return count;
        }
        drop(buf);

        // Slow path: multi-segment with UPDATE/DELETE history.
        // Newest-version-wins across buffer + segments.
        let mut liveness: std::collections::HashMap<u64, bool> = {
            let buf = self.write_buf.lock();
            buf.latest_entries().into_iter().collect()
        };
        // Newest-version-wins: iterate segments newest→oldest.
        for seg in segs.iter().rev() {
            for i in (0..seg.sst.num_rows).rev() {
                let key = seg.sst.row_map.key(i);
                if liveness.contains_key(&key) {
                    continue;
                }
                liveness.insert(key, seg.sst.row_map.is_deleted(i));
            }
        }
        liveness.values().filter(|&&deleted| !deleted).count()
    }

    /// Group-by scan: iterate the group column directly (TextSegment), returning
    /// Count + Sum with a text filter: iterate filter col (TextSegment) + sum col
    /// (FixedSegment) directly. Returns (count, sum). Zero Vec<Value> allocation.
    /// Optimized for SELECT COUNT(*), SUM(col) WHERE text_col = 'val'.
    pub fn count_sum_text_filter(
        &self,
        filter_col: usize,
        filter_val: &str,
        sum_col: usize,
    ) -> (i64, f64) {
        let segs = self.segments_snapshot();
        let single_seg = segs.len() <= 1;
        let mut seen: Option<std::collections::HashSet<u64>> = if single_seg {
            None
        } else {
            Some(std::collections::HashSet::new())
        };
        let mut count = 0i64;
        let mut sum = 0.0f64;
        for seg in segs.iter().rev() {
            let n = seg.sst.num_rows;
            let ftext = seg.sst.read_text(filter_col).ok();
            let fsum = seg.sst.read_fixed_i64(sum_col).ok();
            if let Some(tseg) = ftext.as_ref() {
                for i in 0..n {
                    let key = seg.sst.row_map.key(i);
                    if let Some(ref mut s) = seen {
                        if !s.insert(key) {
                            continue;
                        }
                    }
                    if seg.sst.row_map.is_deleted(i) {
                        continue;
                    }
                    if tseg.get_str(i) == Some(filter_val) {
                        count += 1;
                        if let Some(ref f) = fsum {
                            if let Some(v) = f.get_f64(i) {
                                sum += v;
                            } else if let Some(v) = f.get_i64(i) {
                                sum += v as f64;
                            }
                        }
                    }
                }
            }
        }
        (count, sum)
    }

    /// Count + Min + Max with a text filter. Returns (count, min, max).
    pub fn count_min_max_text_filter(
        &self,
        filter_col: usize,
        filter_val: &str,
        agg_col: usize,
    ) -> (i64, f64, f64) {
        let segs = self.segments_snapshot();
        let single_seg = segs.len() <= 1;
        let mut seen: Option<std::collections::HashSet<u64>> = if single_seg {
            None
        } else {
            Some(std::collections::HashSet::new())
        };
        let mut count = 0i64;
        let mut min = f64::INFINITY;
        let mut max = f64::NEG_INFINITY;
        for seg in segs.iter().rev() {
            let n = seg.sst.num_rows;
            let ftext = seg.sst.read_text(filter_col).ok();
            let fagg = seg.sst.read_fixed_i64(agg_col).ok();
            if let Some(tseg) = ftext.as_ref() {
                for i in 0..n {
                    let key = seg.sst.row_map.key(i);
                    if let Some(ref mut s) = seen {
                        if !s.insert(key) {
                            continue;
                        }
                    }
                    if seg.sst.row_map.is_deleted(i) {
                        continue;
                    }
                    if tseg.get_str(i) == Some(filter_val) {
                        count += 1;
                        if let Some(ref f) = fagg {
                            let v = f
                                .get_f64(i)
                                .unwrap_or_else(|| f.get_i64(i).map(|i| i as f64).unwrap_or(0.0));
                            min = min.min(v);
                            max = max.max(v);
                        }
                    }
                }
            }
        }
        (count, min.max(f64::NEG_INFINITY), max.min(f64::INFINITY))
    }

    /// Combined COUNT + SUM + MIN + MAX with a text filter in a SINGLE pass.
    /// Returns (count, sum, min, max). Replaces the old 2-scan approach
    /// (count_min_max_text_filter + count_sum_text_filter) which doubled latency.
    pub fn count_sum_min_max_text_filter(
        &self,
        filter_col: usize,
        filter_val: &str,
        agg_col: usize,
    ) -> (i64, f64, f64, f64) {
        let segs = self.segments_snapshot();
        let single_seg = segs.len() <= 1;
        let mut seen: Option<std::collections::HashSet<u64>> = if single_seg {
            None
        } else {
            Some(std::collections::HashSet::new())
        };
        let mut count = 0i64;
        let mut sum = 0.0f64;
        let mut min = f64::INFINITY;
        let mut max = f64::NEG_INFINITY;
        for seg in segs.iter().rev() {
            let n = seg.sst.num_rows;
            let ftext = seg.sst.read_text(filter_col).ok();
            let fagg = seg.sst.read_fixed_i64(agg_col).ok();
            if let Some(tseg) = ftext.as_ref() {
                for i in 0..n {
                    let key = seg.sst.row_map.key(i);
                    if let Some(ref mut s) = seen {
                        if !s.insert(key) {
                            continue;
                        }
                    }
                    if seg.sst.row_map.is_deleted(i) {
                        continue;
                    }
                    if tseg.get_str(i) == Some(filter_val) {
                        count += 1;
                        if let Some(ref f) = fagg {
                            let v = f
                                .get_f64(i)
                                .unwrap_or_else(|| f.get_i64(i).map(|i| i as f64).unwrap_or(0.0));
                            sum += v;
                            if v < min {
                                min = v;
                            }
                            if v > max {
                                max = v;
                            }
                        }
                    }
                }
            }
        }
        let min = if count == 0 { 0.0 } else { min };
        let max = if count == 0 { 0.0 } else { max };
        (count, sum, min, max)
    }

    /// Find the row indices of the top-K rows by a single fixed (numeric)
    /// column, without materializing any Vec<Value> rows. Returns
    /// (segment_index, local_row_idx) pairs for the K rows with the largest
    /// (descending) or smallest (ascending) values.
    ///
    /// This is the key optimization for `ORDER BY col LIMIT K`: instead of
    /// building 300K projected rows and sorting them (the old path, ~10ms), it
    /// scans just one column keeping a bounded min/max-heap of size K — O(N)
    /// with O(K) memory and zero per-row allocation (~1ms for K=10 on 300K).
    /// Decode specific rows by their (segment_index, row_index) for the given
    /// output columns. Used by ORDER BY LIMIT top-K: find the K row indices via
    /// top_k_row_indices_typed (scans only the sort column), then decode the
    /// output columns for just those K rows — not all N.
    /// 🔑 Batch-decodes each output column ONCE per segment (not per row),
    /// avoiding K× redundant column segment decompressions.
    pub fn decode_rows_at(
        &self,
        indices: &[(usize, usize)],
        out_cols: &[usize],
    ) -> Vec<Vec<Value>> {
        if indices.is_empty() {
            return Vec::new();
        }
        let segs = self.segments_snapshot();
        let mut result: Vec<Vec<Value>> = Vec::with_capacity(indices.len());
        // Pre-decode columns per segment lazily (cached in a local map).
        // For small K this is much cheaper than N full-row decode.
        for &(seg_idx, row_idx) in indices {
            let Some(seg) = segs.get(seg_idx) else {
                continue;
            };
            if row_idx >= seg.sst.num_rows {
                continue;
            }
            if seg.sst.row_map.has_any_deleted() && seg.sst.row_map.is_deleted(row_idx) {
                continue;
            }
            let mut row = Vec::with_capacity(out_cols.len());
            for &ci in out_cols {
                let tag = seg.sst.column_tags.get(ci).copied();
                let v =
                    match tag {
                        Some(t) if t.is_fixed() => seg.sst.read_fixed_i64(ci).ok().and_then(|f| {
                            match self.col_types.get(ci) {
                                Some(ColumnType::Integer) => f.get_i64(row_idx).map(Value::Integer),
                                Some(ColumnType::Float) => f.get_f64(row_idx).map(Value::Float),
                                Some(ColumnType::Boolean) => f.get_bool(row_idx).map(Value::Bool),
                                Some(ColumnType::Timestamp) => f.get_i64(row_idx).map(|v| {
                                    Value::Timestamp(crate::types::Timestamp::from_micros(v))
                                }),
                                _ => None,
                            }
                        }),
                        Some(ColumnTypeTag::Text) => seg.sst.read_text(ci).ok().and_then(|t| {
                            t.get_str(row_idx)
                                .map(|s| Value::Text(ArcString(std::sync::Arc::from(s))))
                        }),
                        _ => None,
                    };
                row.push(v.unwrap_or(Value::Null));
            }
            result.push(row);
        }
        result
    }

    pub fn top_k_row_indices(&self, order_col: usize, k: usize, desc: bool) -> Vec<(usize, usize)> {
        // Delegates to the type-aware variant, assuming an Integer column.
        // Callers that know the column is Float should use top_k_row_indices_typed.
        self.top_k_row_indices_typed(order_col, k, desc, false)
    }

    /// Type-aware top-K. `is_float` selects the correct decoder so Float columns
    /// are not misread as Integer (their 8-byte fixed slot decodes as garbage i64).
    pub fn top_k_row_indices_typed(
        &self,
        order_col: usize,
        k: usize,
        desc: bool,
        is_float: bool,
    ) -> Vec<(usize, usize)> {
        if k == 0 {
            return Vec::new();
        }
        let segs = self.segments_snapshot();
        let single_seg = segs.len() <= 1;
        let mut dedup: Option<std::collections::HashSet<u64>> = if single_seg {
            None
        } else {
            Some(std::collections::HashSet::new())
        };
        // Convert f64 to a totally-ordered u64 key (NaN-safe total order) so it
        // works with BinaryHeap (which requires Ord). For DESC keep a MIN-heap
        // of the largest K (store !bits so the max-heap evicts the smallest);
        // for ASC a MAX-heap of the smallest K (store !bits evicts largest).
        let to_ord = |v: f64| -> u64 {
            // IEEE 754 total-order bits: flip sign bit for normal ordering, flip
            // all bits for negative numbers.
            let bits = v.to_bits();
            if bits & (1u64 << 63) != 0 {
                !bits
            } else {
                bits ^ (1u64 << 63)
            }
        };
        let mut heap: std::collections::BinaryHeap<(u64, usize, usize)> =
            std::collections::BinaryHeap::with_capacity(k + 1);
        let push_capped = |heap: &mut std::collections::BinaryHeap<(u64, usize, usize)>,
                           ord_key: u64,
                           seg_idx: usize,
                           ri: usize| {
            heap.push((ord_key, seg_idx, ri));
            if heap.len() > k {
                heap.pop();
            }
        };
        for (sidx, seg) in segs.iter().enumerate() {
            let n = seg.sst.num_rows;
            let has_deletions = seg.sst.row_map.has_any_deleted();
            // Read via the decoder matching the column's stored type. Reading a
            // Float column as i64 reinterprets the bits → garbage sort keys.
            if is_float {
                if let Ok(fseg) = seg.sst.read_fixed_f64(order_col) {
                    // 🔑 Fast path: no nulls, no deletions, single seg — walk the
                    // raw 8-byte data directly (no per-row slice/bounds-check).
                    let raw = fseg.raw_f64_slice();
                    let has_nulls = fseg.has_nulls();
                    if !has_nulls && !has_deletions && dedup.is_none() && raw.len() >= n * 8 {
                        for i in 0..n {
                            let off = i * 8;
                            let v = f64::from_le_bytes([
                                raw[off],
                                raw[off + 1],
                                raw[off + 2],
                                raw[off + 3],
                                raw[off + 4],
                                raw[off + 5],
                                raw[off + 6],
                                raw[off + 7],
                            ]);
                            let ord_key = if desc {
                                u64::MAX - to_ord(v)
                            } else {
                                to_ord(v)
                            };
                            push_capped(&mut heap, ord_key, sidx, i);
                        }
                        continue;
                    }
                    // Fallback: per-row API (nulls/deletes/multi-seg)
                    for i in 0..n {
                        if has_deletions && seg.sst.row_map.is_deleted(i) {
                            continue;
                        }
                        let v = fseg.get_f64(i).unwrap_or(f64::NAN);
                        let ord_key = if desc {
                            u64::MAX - to_ord(v)
                        } else {
                            to_ord(v)
                        };
                        push_capped(&mut heap, ord_key, sidx, i);
                    }
                }
            } else if let Ok(fseg) = seg.sst.read_fixed_i64(order_col) {
                for i in 0..n {
                    let key = seg.sst.row_map.key(i);
                    if let Some(ref mut s) = dedup {
                        if !s.insert(key) {
                            continue;
                        }
                    }
                    if has_deletions && seg.sst.row_map.is_deleted(i) {
                        continue;
                    }
                    let v = fseg.get_i64(i).unwrap_or(i64::MIN) as f64;
                    // DESC (largest K): invert ord so heap is a min-heap on true value.
                    let ord_key = if desc {
                        u64::MAX - to_ord(v)
                    } else {
                        to_ord(v)
                    };
                    push_capped(&mut heap, ord_key, sidx, i);
                }
            }
        }
        let _ = single_seg;
        // Extract and sort the K results in the requested order.
        // ord_key is encoded so that ascending ord_key always yields the
        // requested order:
        //   ASC : ord_key == to_ord(v)        → ascending ord_key = ascending value
        //   DESC: ord_key == u64::MAX - to_ord(v)
        //                                    → ascending ord_key = descending value
        let mut out: Vec<(u64, usize, usize)> = heap.into_vec();
        out.sort_by(|a, b| a.0.cmp(&b.0));
        let result: Vec<(usize, usize)> = out.into_iter().map(|(_, s, r)| (s, r)).collect();
        result
    }

    /// (group_value, count) pairs. Zero Vec<Value> allocation — uses &str from
    /// the text segment directly. Optimized for GROUP BY col, COUNT(*).
    #[allow(dead_code)]
    pub fn group_by_count(&self, group_col: usize) -> std::collections::HashMap<String, i64> {
        // 🔑 PERF: avoid per-row String allocation. Use an interned index:
        // collect unique group values into a Vec<String> once, then count
        // via index (usize key into the Vec, hashed via the &str). This avoids
        // 20K to_string() + String hash allocations for a 4-group column.
        let segs = self.segments_snapshot();
        let single_seg = segs.len() <= 1;
        let need_dedup = !single_seg || self.may_have_duplicate_keys();
        let mut seen: std::collections::HashSet<u64> = if need_dedup {
            std::collections::HashSet::with_capacity(segs.iter().map(|s| s.sst.num_rows).sum())
        } else {
            std::collections::HashSet::new()
        };

        // 🔑 PERF: avoid per-row String allocation. Use get_mut() first (no
        // alloc for existing keys); only allocate String for genuinely new
        // group values. For a 4-group column over 20K rows, this does 4
        // to_string() calls instead of 20K.
        let mut groups: std::collections::HashMap<String, i64> = std::collections::HashMap::new();

        for seg in segs.iter().rev() {
            let n = seg.sst.num_rows;
            if group_col >= seg.sst.column_tags.len() {
                continue;
            }
            if let Ok(tseg) = seg.sst.read_text(group_col) {
                for i in 0..n {
                    let key = seg.sst.row_map.key(i);
                    if need_dedup && !seen.insert(key) {
                        continue;
                    }
                    if seg.sst.row_map.is_deleted(i) {
                        continue;
                    }
                    let s = tseg.get_str(i).unwrap_or("");
                    // Fast path: key exists → increment without allocation.
                    if let Some(c) = groups.get_mut(s) {
                        *c += 1;
                    } else {
                        groups.insert(s.to_string(), 1);
                    }
                }
            } else if let Ok(fseg) = seg.sst.read_fixed_i64(group_col) {
                for i in 0..n {
                    let key = seg.sst.row_map.key(i);
                    if need_dedup && !seen.insert(key) {
                        continue;
                    }
                    if seg.sst.row_map.is_deleted(i) {
                        continue;
                    }
                    let v = fseg.get_i64(i).unwrap_or(0);
                    let buf = v.to_string();
                    if let Some(c) = groups.get_mut(buf.as_str()) {
                        *c += 1;
                    } else {
                        groups.insert(buf, 1);
                    }
                }
            }
        }

        groups
    }

    /// Distinct values from a text column with early exit. Returns unique
    /// string values. Stops scanning once `max_values` unique values are found
    /// (for SELECT DISTINCT with known cardinality bounds).
    /// Uses &str directly from TextSegment — zero Value allocation.
    pub fn distinct_text_values(&self, col: usize, max_values: usize) -> Vec<String> {
        let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
        let segs = self.segments_snapshot();
        let single_seg = segs.len() <= 1;
        let mut dedup: Option<std::collections::HashSet<u64>> = if single_seg {
            None
        } else {
            let total_rows: usize = segs.iter().map(|s| s.sst.num_rows).sum();
            Some(std::collections::HashSet::with_capacity(total_rows))
        };
        // Adaptive early-exit for low-cardinality columns: once we stop seeing
        // new values, assume the column has few uniques and bail out. This turns
        // SELECT DISTINCT region (2 values) from a full 300K-row scan into a
        // few-thousand-row scan, with no cardinality hint needed from the caller.
        // The stable window is chosen so a high-cardinality column (>~10% unique)
        // is still scanned fully, while truly low-cardinality columns short-circuit.
        let mut rows_since_new: usize = 0;
        let stable_window: usize = 4096;
        'outer: for seg in segs.iter().rev() {
            let n = seg.sst.num_rows;
            let has_deletions = seg.sst.row_map.has_any_deleted();
            if let Ok(tseg) = seg.sst.read_text(col) {
                for i in 0..n {
                    let key = seg.sst.row_map.key(i);
                    if let Some(ref mut s) = dedup {
                        if !s.insert(key) {
                            continue;
                        }
                    }
                    if has_deletions && seg.sst.row_map.is_deleted(i) {
                        rows_since_new += 1;
                        continue;
                    }
                    let s = if has_deletions {
                        match tseg.get_str(i) {
                            Some(s) => s,
                            None => {
                                continue;
                            }
                        }
                    } else {
                        tseg.get_str_fast(i)
                    };
                    if seen.insert(s.to_string()) {
                        rows_since_new = 0;
                        if seen.len() >= max_values {
                            break 'outer;
                        }
                    } else {
                        rows_since_new += 1;
                        if !seen.is_empty() && rows_since_new >= stable_window {
                            break 'outer;
                        }
                    }
                }
            } else if let Ok(fseg) = seg.sst.read_fixed_i64(col) {
                for i in 0..n {
                    let key = seg.sst.row_map.key(i);
                    if let Some(ref mut s) = dedup {
                        if !s.insert(key) {
                            continue;
                        }
                    }
                    if has_deletions && seg.sst.row_map.is_deleted(i) {
                        rows_since_new += 1;
                        continue;
                    }
                    let v = fseg.get_i64(i).unwrap_or(0).to_string();
                    if seen.insert(v) {
                        rows_since_new = 0;
                        if seen.len() >= max_values {
                            break 'outer;
                        }
                    } else {
                        rows_since_new += 1;
                        if !seen.is_empty() && rows_since_new >= stable_window {
                            break 'outer;
                        }
                    }
                }
            }
        }
        seen.into_iter().collect()
    }

    pub fn buffered_row_count(&self) -> usize {
        self.write_buf.lock().num_rows
    }

    /// Get cached IN-hash row indices for (col_pos, set_signature).
    pub fn get_in_hash_cache(&self, col_pos: usize, set_sig: u64) -> Option<Vec<usize>> {
        let key = ((col_pos as u128) << 64) | (set_sig as u128);
        self.in_hash_cache.read().get(&key).cloned()
    }

    /// Store IN-hash row indices for (col_pos, set_signature).
    pub fn put_in_hash_cache(&self, col_pos: usize, set_sig: u64, indices: Vec<usize>) {
        let key = ((col_pos as u128) << 64) | (set_sig as u128);
        let mut cache = self.in_hash_cache.write();
        if cache.len() < 8 {
            cache.insert(key, indices);
        }
    }

    /// GROUP BY with COUNT + SUM aggregation in a single pass.
    /// Returns (group_value, count, sum) tuples. Reads only the group column
    /// and the aggregate column — no full-row decode.
    pub fn group_by_count_sum(&self, group_col: usize, agg_col: usize) -> Vec<(String, i64, f64)> {
        // Check the group-by cache first (avoids re-scanning on repeated calls).
        // Cache key: (group_col, agg_col) — invalidated on writes via clear_cache().
        {
            let cache = self.groupby_cache.read();
            let key = ((group_col as u64) << 32) | (agg_col as u64);
            if let Some(result) = cache.get(&key) {
                return result.clone();
            }
        }

        let result = self.group_by_count_sum_uncached(group_col, agg_col);

        // Cache the result.
        {
            let mut cache = self.groupby_cache.write();
            let key = ((group_col as u64) << 32) | (agg_col as u64);
            if cache.len() < 8 {
                cache.insert(key, result.clone());
            }
        }
        result
    }

    fn group_by_count_sum_uncached(
        &self,
        group_col: usize,
        agg_col: usize,
    ) -> Vec<(String, i64, f64)> {
        // Direct HashMap<String, (i64, f64)> — Rust's SipHash is slower per-hash
        // but avoids the manual FNV loop + collision checking overhead.
        // Pre-allocate capacity to avoid rehashing during insertion.
        let mut groups: std::collections::HashMap<String, (i64, f64)> =
            std::collections::HashMap::with_capacity(32768);

        let segs = self.segments_snapshot();
        let single_seg = segs.len() <= 1;
        let mut seen: Option<std::collections::HashSet<u64>> = if single_seg {
            None
        } else {
            let total_rows: usize = segs.iter().map(|s| s.sst.num_rows).sum();
            Some(std::collections::HashSet::with_capacity(total_rows))
        };
        for seg in segs.iter().rev() {
            let n = seg.sst.num_rows;
            let gtext = seg.sst.read_text(group_col).ok();
            let afix = seg.sst.read_fixed_i64(agg_col).ok();
            let has_deletions = seg.sst.row_map.has_any_deleted();
            if let Some(tseg) = gtext.as_ref() {
                let has_nulls = tseg.has_any_null();
                for i in 0..n {
                    let key = seg.sst.row_map.key(i);
                    if let Some(ref mut s) = seen {
                        if !s.insert(key) {
                            continue;
                        }
                    }
                    if has_deletions && seg.sst.row_map.is_deleted(i) {
                        continue;
                    }
                    let gval = if has_nulls {
                        tseg.get_str(i).unwrap_or("")
                    } else {
                        tseg.get_str_fast(i)
                    };
                    let av = afix
                        .as_ref()
                        .and_then(|f| f.get_f64(i).or_else(|| f.get_i64(i).map(|x| x as f64)));

                    // Fast path: entry exists → update count+sum (no String alloc).
                    if let Some(entry) = groups.get_mut(gval) {
                        entry.0 += 1;
                        if let Some(v) = av {
                            entry.1 += v;
                        }
                    } else {
                        groups.insert(gval.to_string(), (1, av.unwrap_or(0.0)));
                    }
                }
            }
        }
        groups.into_iter().map(|(k, (c, s))| (k, c, s)).collect()
    }

    /// GROUP BY with COUNT + SUM for a fixed-type (Integer/Boolean) group column.
    /// Returns (i64_group_value, count, sum) tuples.
    pub fn group_by_count_sum_fixed_group(
        &self,
        group_col: usize,
        agg_col: usize,
    ) -> Vec<(i64, i64, f64)> {
        let mut groups: std::collections::HashMap<i64, (i64, f64)> =
            std::collections::HashMap::new();
        let segs = self.segments_snapshot();
        let single_seg = segs.len() <= 1;
        let mut seen: Option<std::collections::HashSet<u64>> = if single_seg {
            None
        } else {
            let total_rows: usize = segs.iter().map(|s| s.sst.num_rows).sum();
            Some(std::collections::HashSet::with_capacity(total_rows))
        };
        for seg in segs.iter().rev() {
            let n = seg.sst.num_rows;
            let gfix = seg.sst.read_fixed_i64(group_col).ok();
            let afix = seg.sst.read_fixed_i64(agg_col).ok();
            if let Some(gseg) = gfix.as_ref() {
                for i in 0..n {
                    let key = seg.sst.row_map.key(i);
                    if let Some(ref mut s) = seen {
                        if !s.insert(key) {
                            continue;
                        }
                    }
                    if seg.sst.row_map.is_deleted(i) {
                        continue;
                    }
                    if let Some(gval) = gseg.get_i64(i) {
                        let entry = groups.entry(gval).or_insert((0, 0.0));
                        entry.0 += 1;
                        if let Some(ref f) = afix {
                            if let Some(v) = f.get_f64(i).or_else(|| f.get_i64(i).map(|x| x as f64))
                            {
                                entry.1 += v;
                            }
                        }
                    }
                }
            }
        }
        groups.into_iter().map(|(k, (c, s))| (k, c, s)).collect()
    }

    /// Recover segments from disk after a restart. Reads the MANIFEST to find
    /// active segment ids, opens each .sst file, and loads them into memory.
    /// Ensures no data loss on crash (ACID durability).
    pub fn recover_from_disk(&self) {
        // Read MANIFEST to get active segment ids.
        let manifest_path = self.dir.join("MANIFEST");
        if !manifest_path.exists() {
            return;
        }
        let manifest = match crate::storage::col_segment::manifest::Manifest::open(&manifest_path) {
            Ok(m) => m,
            Err(_) => return,
        };
        let state = manifest.replay();

        // Find the highest segment id to continue numbering.
        let mut max_id = 0u64;
        for &id in &state.active_segments {
            max_id = max_id.max(id);
        }
        // Also check files on disk (in case MANIFEST lags).
        if let Ok(entries) = std::fs::read_dir(&self.dir) {
            for entry in entries.flatten() {
                if let Some(name) = entry.file_name().to_str() {
                    if name.ends_with(".sst") {
                        if let Ok(id) = name.trim_end_matches(".sst").parse::<u64>() {
                            max_id = max_id.max(id);
                            if !state.active_segments.contains(&id) {
                                // File on disk but not in MANIFEST — orphan, skip.
                                continue;
                            }
                        }
                    }
                }
            }
        }
        self.next_segment_id.store(max_id + 1, Ordering::Relaxed);

        // Load each active segment.
        let mut segs = self.segments.write();
        let mut loaded_ids: Vec<u64> = Vec::new();
        for &id in &state.active_segments {
            let path = self.dir.join(format!("{:010}.sst", id));
            if path.exists() {
                if let Ok(seg) = Segment::open(&path, id) {
                    segs.push_back(Arc::new(seg));
                    loaded_ids.push(id);
                }
            }
        }
        // Clean up obsolete files (superseded by compaction but not yet GC'd).
        for &id in &state.obsolete_files {
            let path = self.dir.join(format!("{:010}.sst", id));
            let _ = std::fs::remove_file(&path);
        }

        // Sort segments by id (creation order).
        segs.make_contiguous();
        // Already in push order (ascending id) — correct.
    }

    pub fn col_types(&self) -> &[ColumnType] {
        &self.col_types
    }

    pub fn needs_compaction(&self) -> bool {
        self.segments.read().len() >= COMPACTION_SEGMENT_THRESHOLD
    }

    /// Run one compaction pass (synchronous; called by bg thread or test).
    /// Merges all active segments into one, deduplicating keys and dropping
    /// tombstoned/superseded versions.
    pub fn compact_once(&self) -> Result<()> {
        let old_segs: Vec<Arc<Segment>> = {
            let segs = self.segments.read();
            if segs.len() < COMPACTION_SEGMENT_THRESHOLD {
                return Ok(());
            }
            segs.iter().cloned().collect()
        };
        self.merge_segments(old_segs)
    }

    /// Force-merge ALL segments into one, ignoring the count threshold.
    /// Used by sync_col_segment_to_sstables so legacy aggregate paths see
    /// the complete dataset in a single SSTable. No-op if < 2 segments.
    pub fn force_compact_all(&self) -> Result<()> {
        let old_segs: Vec<Arc<Segment>> = {
            let segs = self.segments.read();
            if segs.len() < 2 {
                return Ok(());
            }
            segs.iter().cloned().collect()
        };
        self.merge_segments(old_segs)
    }

    /// Return the maximum row_id (key & 0xFFFFFFFF) across all segments + buffer.
    /// Used on reopen to initialize next_row_id so new INSERTs don't reuse a
    /// row_id from a previous session (which would collide with existing data).
    pub fn max_row_id(&self) -> u64 {
        let mut max = 0u64;
        for (key, _) in self.write_buf.lock().latest_entries() {
            max = max.max(key & 0xFFFFFFFF);
        }
        for seg in self.segments.read().iter() {
            for i in 0..seg.sst.num_rows {
                let key = seg.sst.row_map.key(i);
                max = max.max(key & 0xFFFFFFFF);
            }
        }
        max
    }

    /// Shared merge logic: merge `old_segs` into one new segment, dedup keys
    /// (newest version wins), drop tombstones, update manifest + GC old files.
    fn merge_segments(&self, old_segs: Vec<Arc<Segment>>) -> Result<()> {
        if old_segs.is_empty() {
            return Ok(());
        }
        // Serialize with flush_buffer: wait for any in-progress flush to
        // complete before merging, and hold the lock so no new flush can
        // create a segment that this merge would miss.
        let _guard = self.flush_merge_lock.lock();
        let old_ids: Vec<u64> = old_segs.iter().map(|s| s.id).collect();
        let ncols = self.col_types.len();

        let id = self.next_segment_id.fetch_add(1, Ordering::Relaxed);
        let path = self.dir.join(format!("{:010}.sst", id));
        let mut builder = ColumnarSSTableBuilder::new(&path, self.col_types.clone());

        // Check if ALL columns are fixed-width (integer/float/bool/timestamp).
        // If so, use the fast column-direct path (no Vec<Value>).
        // all_fixed = every column is 8-byte fixed (Integer/Float/Timestamp).
        // Boolean is fixed-width but only 1 byte, so it must go through the
        // mixed path (which reads via the type-correct accessor); including it
        // here would write 8 bytes per Boolean row and corrupt the segment.
        let all_fixed = self.col_types.iter().all(|ct| {
            matches!(
                ct,
                ColumnType::Integer | ColumnType::Float | ColumnType::Timestamp
            )
        });

        if all_fixed {
            // Column-direct compaction: extract raw i64 bytes per row, no Value.
            // 🔑 CRITICAL: collect ALL rows, sort by key, THEN add to builder.
            // The builder's row_map stores keys in insertion order and find_key()
            // uses binary search (requires sorted keys). Without sorting, a merge
            // of multiple segments (iterated newest-first) produces an unsorted
            // row_map, breaking point lookups (get/where id=...) after compaction.
            let single_seg = old_segs.len() <= 1;
            let mut seen: std::collections::HashSet<u64> = std::collections::HashSet::new();
            let mut collected: Vec<(u64, u64, Vec<[u8; 8]>, Vec<bool>)> = Vec::new();
            for seg in old_segs.iter().rev() {
                let n = seg.sst.num_rows;
                let fixed_cols: Vec<Option<crate::storage::lsm::columnar::FixedSegment>> = (0
                    ..ncols)
                    .map(|ci| {
                        if ci < seg.sst.column_tags.len() && seg.sst.column_tags[ci].is_fixed() {
                            seg.sst.read_fixed_i64(ci).ok()
                        } else {
                            None
                        }
                    })
                    .collect();
                for i in 0..n {
                    let key = seg.sst.row_map.key(i);
                    if !seen.insert(key) {
                        continue;
                    }
                    if seg.sst.row_map.is_deleted(i) {
                        continue;
                    }
                    let ts = seg.sst.row_map.timestamp(i);
                    let mut col_vals: Vec<[u8; 8]> = Vec::with_capacity(ncols);
                    let mut col_nulls: Vec<bool> = Vec::with_capacity(ncols);
                    for ci in 0..ncols {
                        match fixed_cols
                            .get(ci)
                            .and_then(|x| x.as_ref())
                            .and_then(|f| f.get_i64(i))
                        {
                            Some(v) => {
                                col_vals.push(v.to_le_bytes());
                                col_nulls.push(false);
                            }
                            None => {
                                col_vals.push(0i64.to_le_bytes());
                                col_nulls.push(true);
                            }
                        }
                    }
                    collected.push((key, ts, col_vals, col_nulls));
                }
            }
            // Single-segment data is already sorted (sequential insert); skip the
            // sort for that case to avoid the O(N log N) overhead.
            if !single_seg {
                collected.sort_unstable_by_key(|(k, _, _, _)| *k);
            }
            for (key, ts, col_vals, col_nulls) in collected {
                let col_bytes: Vec<&[u8]> = col_vals.iter().map(|b| b.as_slice()).collect();
                builder.add_values_raw_with_nulls(key, ts, false, &col_bytes, &col_nulls)?;
            }
        } else {
            // Mixed columns (has Text and/or Vector/Spatial): direct copy with
            // temp buffers. Avoids MergeCursor's per-row Vec<Value> + SegmentCursor
            // pre-decode.
            // 🔑 CRITICAL: collect ALL rows, sort by key, THEN add (see note above).
            // 🔑 Vector/Spatial columns must be decoded+re-encoded here (they have
            // no zero-copy segment readers); otherwise a multi-segment merge would
            // silently DROP those columns (each row's buffer stayed empty).
            use crate::storage::lsm::columnar::ColumnTypeTag;
            let single_seg = old_segs.len() <= 1;
            let mut seen: std::collections::HashSet<u64> = std::collections::HashSet::new();
            let mut collected: Vec<(u64, u64, Vec<Vec<u8>>, Vec<bool>)> = Vec::new();
            for seg in old_segs.iter().rev() {
                let n = seg.sst.num_rows;
                let fixed_cols: Vec<Option<crate::storage::lsm::columnar::FixedSegment>> = (0
                    ..ncols)
                    .map(|ci| {
                        if ci < seg.sst.column_tags.len() && seg.sst.column_tags[ci].is_fixed() {
                            seg.sst.read_fixed_i64(ci).ok()
                        } else {
                            None
                        }
                    })
                    .collect();
                let text_cols: Vec<Option<crate::storage::lsm::columnar::TextSegment>> = (0..ncols)
                    .map(|ci| {
                        if ci < seg.sst.column_tags.len()
                            && matches!(
                                seg.sst.column_tags[ci],
                                crate::storage::lsm::columnar::ColumnTypeTag::Text
                            )
                        {
                            seg.sst.read_text(ci).ok()
                        } else {
                            None
                        }
                    })
                    .collect();
                // Pre-decode Vector columns into per-idx option vecs.
                let vec_cols: Vec<Vec<Option<Vec<f32>>>> = (0..ncols)
                    .map(|ci| {
                        if ci < seg.sst.column_tags.len()
                            && matches!(seg.sst.column_tags[ci], ColumnTypeTag::Vector)
                        {
                            let decoded = seg.sst.read_vectors(ci).unwrap_or_default();
                            let mut per_row = vec![None; n];
                            let mut di = 0usize;
                            for i in 0..n {
                                if seg.sst.row_map.is_deleted(i) {
                                    continue;
                                }
                                let ek = seg.sst.row_map.key(i) & 0xFFFFFFFF;
                                while di < decoded.len() && decoded[di].0 != ek {
                                    di += 1;
                                }
                                if di < decoded.len() {
                                    per_row[i] = Some(decoded[di].1.clone());
                                    di += 1;
                                }
                            }
                            per_row
                        } else {
                            Vec::new()
                        }
                    })
                    .collect();
                // Pre-decode Spatial columns into per-idx option vecs.
                let spatial_cols: Vec<Vec<Option<crate::types::Geometry>>> = (0..ncols)
                    .map(|ci| {
                        if ci < seg.sst.column_tags.len()
                            && matches!(seg.sst.column_tags[ci], ColumnTypeTag::Spatial)
                        {
                            let decoded = seg.sst.read_spatial(ci).unwrap_or_default();
                            let mut per_row = vec![None; n];
                            let mut di = 0usize;
                            for i in 0..n {
                                if seg.sst.row_map.is_deleted(i) {
                                    continue;
                                }
                                let ek = seg.sst.row_map.key(i) & 0xFFFFFFFF;
                                while di < decoded.len() && decoded[di].0 != ek {
                                    di += 1;
                                }
                                if di < decoded.len() {
                                    per_row[i] = Some(decoded[di].1.clone());
                                    di += 1;
                                }
                            }
                            per_row
                        } else {
                            Vec::new()
                        }
                    })
                    .collect();
                for i in 0..n {
                    let key = seg.sst.row_map.key(i);
                    if !seen.insert(key) {
                        continue;
                    }
                    if seg.sst.row_map.is_deleted(i) {
                        continue;
                    }
                    let ts = seg.sst.row_map.timestamp(i);
                    let mut row_bytes: Vec<Vec<u8>> = Vec::with_capacity(ncols);
                    let mut row_nulls: Vec<bool> = Vec::with_capacity(ncols);
                    for ci in 0..ncols {
                        let mut buf = Vec::new();
                        let tag = seg.sst.column_tags.get(ci).copied();
                        if matches!(
                            tag,
                            Some(crate::storage::lsm::columnar::ColumnTypeTag::Bool)
                        ) {
                            // Boolean: 1-byte fixed. Read via get_bool, write 1 byte.
                            match fixed_cols
                                .get(ci)
                                .and_then(|x| x.as_ref())
                                .and_then(|f| f.get_bool(i))
                            {
                                Some(b) => {
                                    buf.push(if b { 1u8 } else { 0u8 });
                                    row_nulls.push(false);
                                }
                                None => {
                                    buf.push(0u8);
                                    row_nulls.push(true);
                                }
                            }
                        } else if let Some(f) = fixed_cols.get(ci).and_then(|x| x.as_ref()) {
                            match f.get_i64(i) {
                                Some(v) => {
                                    buf.extend_from_slice(&v.to_le_bytes());
                                    row_nulls.push(false);
                                }
                                None => {
                                    buf.extend_from_slice(&0i64.to_le_bytes());
                                    row_nulls.push(true);
                                }
                            }
                        } else if let Some(t) = text_cols.get(ci).and_then(|x| x.as_ref()) {
                            match t.get_str(i) {
                                Some(s) => {
                                    let len = s.len().min(65535) as u16;
                                    buf.extend_from_slice(&len.to_le_bytes());
                                    buf.extend_from_slice(&s.as_bytes()[..len as usize]);
                                    row_nulls.push(false);
                                }
                                None => {
                                    buf.extend_from_slice(&0u16.to_le_bytes());
                                    row_nulls.push(true);
                                }
                            }
                        } else if ci < vec_cols.len() && !vec_cols[ci].is_empty() {
                            // Vector: re-encode [dim:u16][f32×dim] (NULL → dim=0).
                            if let Some(ref v) = vec_cols[ci][i] {
                                buf.extend_from_slice(&(v.len() as u16).to_le_bytes());
                                for x in v {
                                    buf.extend_from_slice(&x.to_le_bytes());
                                }
                                row_nulls.push(false);
                            } else {
                                buf.extend_from_slice(&0u16.to_le_bytes());
                                row_nulls.push(true);
                            }
                        } else if ci < spatial_cols.len() && !spatial_cols[ci].is_empty() {
                            // Spatial: re-encode [len:u16][bincode(Geometry)] (NULL → len=0).
                            if let Some(ref g) = spatial_cols[ci][i] {
                                let bytes = bincode::serialize(g).unwrap_or_default();
                                let len = bytes.len().min(65535) as u16;
                                buf.extend_from_slice(&len.to_le_bytes());
                                buf.extend_from_slice(&bytes[..len as usize]);
                                row_nulls.push(false);
                            } else {
                                buf.extend_from_slice(&0u16.to_le_bytes());
                                row_nulls.push(true);
                            }
                        } else {
                            row_nulls.push(false);
                        }
                        row_bytes.push(buf);
                    }
                    collected.push((key, ts, row_bytes, row_nulls));
                }
            }
            if !single_seg {
                collected.sort_unstable_by_key(|(k, _, _, _)| *k);
            }
            for (key, ts, row_bytes, row_nulls) in collected {
                let col_slices: Vec<&[u8]> = row_bytes.iter().map(|b| b.as_slice()).collect();
                builder.add_values_raw_with_nulls(key, ts, false, &col_slices, &row_nulls)?;
            }
        }
        builder.finish()?;

        let new_seg = Arc::new(Segment::open(&path, id)?);

        // Record compaction in manifest FIRST (crash safety), then swap memory.
        self.manifest.lock().record_compaction(id, &old_ids)?;
        {
            let mut segs = self.segments.write();
            let old_set: std::collections::HashSet<u64> = old_ids.iter().copied().collect();
            let new_list: VecDeque<Arc<Segment>> = segs
                .iter()
                .filter(|s| !old_set.contains(&s.id))
                .cloned()
                .collect();
            *segs = new_list;
            segs.push_back(new_seg);
        }

        // Clear column caches + release mmap pages to keep peak RSS low.
        {
            let segs = self.segments.read();
            for seg in segs.iter() {
                seg.clear_cache();
                seg.release_pages();
            }
        }
        // GC: delete old files, record in manifest.
        for oid in &old_ids {
            let p = self.dir.join(format!("{:010}.sst", oid));
            let _ = std::fs::remove_file(p);
        }
        self.manifest.lock().record_gc(&old_ids)?;
        Ok(())
    }
}