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
//! Columnar SSTable — column-oriented persistent storage.
//!
//! ## Motivation
//!
//! Row-based SSTables store all columns of a row together:
//! ```text
//! [row0: id|customer|amount|region] [row1: id|customer|amount|region] ...
//! ```
//!
//! Columnar SSTables store each column independently:
//! ```text
//! [id: i64×N] [customer: str_dict] [amount: f64×N] [region: str_dict] [RowMap]
//! ```
//!
//! ## Benefits
//!
//! - **Projection**: SELECT id, amount reads 2/4 columns → 2× less I/O
//! - **SIMD**: Fixed columns are contiguous typed arrays → vectorizable
//! - **Compression**: Same-type data compresses 3-5× better
//! - **Memory**: No per-row Vec<Value> allocation during scan
//!
//! ## File Format
//!
//! ```text
//! [Header: 32 bytes]
//!   magic: u32 = 0x434D5442 ("BTMC" — Columnar MoTeDB)
//!   version: u32 = 1
//!   num_rows: u32
//!   num_columns: u16
//!   column_types: [u8; 16]  (type tag per column, max 16 columns)
//!   reserved: [u8; 6]
//!
//! [Column Index: (offset: u64, size: u64) × num_columns]
//!
//! [Column 0 Segment]
//! [Column 1 Segment]
//! ...
//!
//! [Row Map Segment]
//!   keys: u64 × num_rows
//!   timestamps: u64 × num_rows
//!   deleted: u8 × ceil(num_rows/8)
//!
//! [Footer: 16 bytes]
//!   column_index_offset: u64
//!   row_map_offset: u64
//!   _padding: [u8; 4]  (alignment to keep magic at end)
//!   magic: u32 = 0x434D5442
//! ```
//!
//! ## Column Segment Format
//!
//! **Fixed-width (Integer, Float, Bool, Timestamp):**
//! ```text
//! [null_bitmap: u8 × ceil(num_rows/8)]
//! [data: T × num_rows]  (T = i64, f64, u8, i64)
//! ```
//!
//! **Variable-width (Text):**
//! ```text
//! [null_bitmap: u8 × ceil(num_rows/8)]
//! [offsets: u32 × (num_rows + 1)]  (cumulative byte offsets into string_data)
//! [string_data: bytes]
//! ```
//!
//! **Vector/Spatial (variable but fixed-stride):**
//! ```text
//! [null_bitmap: u8 × ceil(num_rows/8)]
//! [dim: u16] [stride: u16]
//! [data: f32 × num_rows × stride]
//! ```

use crate::types::{ColumnType, RowId, Value};
use crate::{Result, StorageError};
use std::fs::{File, OpenOptions};
use std::io::{BufWriter, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use memmap2::Mmap;

// ── Constants ─────────────────────────────────────────────────────

const COLUMNAR_MAGIC: u32 = 0x434D5442; // "BTMC"
const COLUMNAR_VERSION: u32 = 2; // v2: MAX_COLUMNS 16 → 128, header grew to 144 bytes
const HEADER_SIZE: usize = 144; // 14 (fixed prefix) + 128 (column_tags) + 2 (reserved)
const FOOTER_SIZE: usize = 20;
/// Maximum number of columns supported by the columnar SSTable format.
/// The on-disk header reserves a fixed-width slot per column, so this is a
/// hard format limit. CREATE TABLE rejects tables exceeding it.
pub const MAX_COLUMNS: usize = 128;

/// Column type tags for the columnar format (compact u8 representation).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ColumnTypeTag {
    Integer = 0,
    Float = 1,
    Bool = 2,
    Timestamp = 3,
    Text = 4,
    Vector = 5,
    Spatial = 6,
}

impl ColumnTypeTag {
    fn from_column_type(ct: &ColumnType) -> Self {
        match ct {
            ColumnType::Integer => Self::Integer,
            ColumnType::Float => Self::Float,
            ColumnType::Boolean => Self::Bool,
            ColumnType::Timestamp => Self::Timestamp,
            ColumnType::Text => Self::Text,
            ColumnType::Tensor(_) => Self::Vector,
            ColumnType::Spatial => Self::Spatial,
        }
    }

    #[allow(dead_code)]
    pub(crate) fn to_column_type(&self) -> ColumnType {
        match self {
            Self::Integer => ColumnType::Integer,
            Self::Float => ColumnType::Float,
            Self::Bool => ColumnType::Boolean,
            Self::Timestamp => ColumnType::Timestamp,
            Self::Text => ColumnType::Text,
            Self::Vector => ColumnType::Tensor(0), // dim reconstructed from segment header
            Self::Spatial => ColumnType::Spatial,
        }
    }

    pub(crate) fn is_fixed(&self) -> bool {
        matches!(
            self,
            Self::Integer | Self::Float | Self::Bool | Self::Timestamp
        )
    }

    fn fixed_size(&self) -> usize {
        match self {
            Self::Integer | Self::Float | Self::Timestamp => 8,
            Self::Bool => 1,
            _ => 0,
        }
    }
}

// ── Header ─────────────────────────────────────────────────────────

#[derive(Clone, Debug)]
pub(crate) struct ColumnarHeader {
    num_rows: u32,
    num_columns: u16,
    column_tags: [u8; MAX_COLUMNS],
}

impl ColumnarHeader {
    fn serialize(&self) -> [u8; HEADER_SIZE] {
        let mut buf = [0u8; HEADER_SIZE];
        buf[0..4].copy_from_slice(&COLUMNAR_MAGIC.to_le_bytes());
        buf[4..8].copy_from_slice(&COLUMNAR_VERSION.to_le_bytes());
        buf[8..12].copy_from_slice(&self.num_rows.to_le_bytes());
        buf[12..14].copy_from_slice(&self.num_columns.to_le_bytes());
        buf[14..14 + MAX_COLUMNS].copy_from_slice(&self.column_tags);
        // bytes 142-143: reserved
        buf
    }

    fn deserialize(data: &[u8]) -> Result<Self> {
        if data.len() < HEADER_SIZE {
            return Err(StorageError::InvalidData(
                "Columnar header too short".into(),
            ));
        }
        let magic = u32::from_le_bytes([data[0], data[1], data[2], data[3]]);
        if magic != COLUMNAR_MAGIC {
            return Err(StorageError::InvalidData(format!(
                "Bad columnar magic: 0x{:08X}",
                magic
            )));
        }
        let version = u32::from_le_bytes([data[4], data[5], data[6], data[7]]);
        if version != COLUMNAR_VERSION {
            return Err(StorageError::InvalidData(format!(
                "Unsupported columnar version: {}",
                version
            )));
        }
        let num_rows = u32::from_le_bytes([data[8], data[9], data[10], data[11]]);
        let num_columns = u16::from_le_bytes([data[12], data[13]]);
        let mut column_tags = [0u8; MAX_COLUMNS];
        column_tags.copy_from_slice(&data[14..14 + MAX_COLUMNS]);
        Ok(Self {
            num_rows,
            num_columns,
            column_tags,
        })
    }
}

// ── Column Index Entry ─────────────────────────────────────────────

#[derive(Clone, Debug)]
pub struct ColumnIndexEntry {
    pub offset: u64,
    pub size: u64,
}

const COLUMN_INDEX_ENTRY_SIZE: usize = 16; // (offset: u64, size: u64)

// ── Row Map ────────────────────────────────────────────────────────

/// Row Map: MVCC metadata for each row in columnar order.
///
/// Stored as three contiguous arrays:
/// - keys: u64 × num_rows (composite keys, preserves row order)
/// - timestamps: u64 × num_rows (MVCC version)
/// - deleted: bitset (u8 × ceil(num_rows/8))
#[derive(Clone, Debug)]
pub struct RowMap {
    pub num_rows: usize,
    data: SegData, // Owned for builder, Mmap for zero-copy reads
    keys_offset: usize,
    timestamps_offset: usize,
    deleted_offset: usize,
    #[allow(dead_code)]
    deleted_len: usize,
}

impl RowMap {
    fn compute_sizes(num_rows: usize) -> (usize, usize, usize, usize) {
        let keys_size = num_rows * 8;
        let timestamps_size = num_rows * 8;
        let deleted_len = num_rows.div_ceil(8);
        (
            keys_size + timestamps_size + deleted_len,
            keys_size,
            timestamps_size,
            deleted_len,
        )
    }

    #[allow(dead_code)]
    pub(crate) fn from_bytes(data: Vec<u8>, num_rows: usize) -> Self {
        let (_, keys_size, timestamps_size, deleted_len) = Self::compute_sizes(num_rows);
        Self {
            num_rows,
            keys_offset: 0,
            timestamps_offset: keys_size,
            deleted_offset: keys_size + timestamps_size,
            deleted_len,
            data: SegData::Owned(data),
        }
    }

    /// Zero-copy view into mmap data.
    #[allow(dead_code)]
    pub(crate) fn from_mmap(mmap: Arc<Mmap>, offset: usize, num_rows: usize) -> Result<Self> {
        let (_total, keys_size, timestamps_size, deleted_len) = Self::compute_sizes(num_rows);
        Ok(Self {
            num_rows,
            keys_offset: offset,
            timestamps_offset: offset + keys_size,
            deleted_offset: offset + keys_size + timestamps_size,
            deleted_len,
            data: SegData::Mmap { mmap, offset },
        })
    }

    #[inline]
    pub fn key(&self, row_idx: usize) -> u64 {
        let off = self.keys_offset + row_idx * 8;
        let s = self.data.slice(off, 8);
        u64::from_le_bytes([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]])
    }

    #[inline]
    pub fn timestamp(&self, row_idx: usize) -> u64 {
        let off = self.timestamps_offset + row_idx * 8;
        let s = self.data.slice(off, 8);
        u64::from_le_bytes([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]])
    }

    /// Binary search for a key in the RowMap. Returns row index if found.
    pub fn find_key(&self, target: u64) -> Option<usize> {
        let mut lo = 0usize;
        let mut hi = self.num_rows;
        while lo < hi {
            let mid = (lo + hi) / 2;
            let k = self.key(mid);
            if k < target {
                lo = mid + 1;
            } else if k > target {
                hi = mid;
            } else {
                return Some(mid);
            }
        }
        None
    }

    #[inline]
    pub fn is_deleted(&self, row_idx: usize) -> bool {
        let byte = self.data.get(self.deleted_offset + row_idx / 8);
        (byte >> (row_idx % 8)) & 1 != 0
    }

    /// Check if ANY row is marked deleted. O(deleted_bitmap_size / 8) — fast.
    /// Used to skip per-row is_deleted checks when no deletions exist.
    pub fn has_any_deleted(&self) -> bool {
        let n = self.num_rows;
        let nb = n.div_ceil(8);
        for i in 0..nb {
            if self.data.get(self.deleted_offset + i) != 0 {
                return true;
            }
        }
        false
    }
}

// ── Column Segment Views ───────────────────────────────────────────

/// Data source for a column segment: owned bytes (legacy) or mmap reference (zero-copy).
#[derive(Clone, Debug)]
enum SegData {
    #[allow(dead_code)]
    Owned(Vec<u8>),
    #[allow(dead_code)]
    Mmap { mmap: Arc<Mmap>, offset: usize },
}

impl SegData {
    #[inline]
    fn get(&self, idx: usize) -> u8 {
        match self {
            SegData::Owned(v) => v[idx],
            SegData::Mmap { mmap, offset } => mmap[offset + idx],
        }
    }
    fn slice(&self, start: usize, len: usize) -> &[u8] {
        match self {
            SegData::Owned(v) => &v[start..start + len],
            SegData::Mmap { mmap, offset } => &mmap[*offset + start..*offset + start + len],
        }
    }
    fn len(&self) -> usize {
        match self {
            SegData::Owned(v) => v.len(),
            SegData::Mmap { mmap, offset } => mmap.len().saturating_sub(*offset),
        }
    }
    /// Return the entire backing buffer as &[u8]. Used by batch scans that walk
    /// the raw bytes (e.g. prefix_match_indices) to avoid per-element slice() calls.
    fn as_bytes(&self) -> &[u8] {
        match self {
            SegData::Owned(v) => v.as_slice(),
            SegData::Mmap { mmap, offset } => &mmap[*offset..],
        }
    }
}

/// Typed view into a fixed-width column segment. Zero-copy from mmap when available.
#[derive(Clone)]
pub struct FixedSegment {
    pub num_rows: usize,
    null_bitmap: SegData,
    data: SegData,
    #[allow(dead_code)]
    elem_size: usize,
    #[allow(dead_code)]
    tag: ColumnTypeTag,
}

impl FixedSegment {
    #[allow(dead_code)]
    pub(crate) fn from_bytes(data: &[u8], num_rows: usize, tag: ColumnTypeTag) -> Result<Self> {
        let null_bytes = num_rows.div_ceil(8);
        let elem_size = tag.fixed_size();
        let data_size = num_rows * elem_size;
        let expected = null_bytes + data_size;
        if data.len() < expected {
            return Err(StorageError::InvalidData(format!(
                "Fixed segment too short: {} < {}",
                data.len(),
                expected
            )));
        }
        Ok(Self {
            num_rows,
            null_bitmap: SegData::Owned(data[..null_bytes].to_vec()),
            data: SegData::Owned(data[null_bytes..null_bytes + data_size].to_vec()),
            elem_size,
            tag,
        })
    }

    #[allow(dead_code)]
    pub(crate) fn from_mmap(
        mmap: Arc<Mmap>,
        offset: usize,
        num_rows: usize,
        tag: ColumnTypeTag,
    ) -> Self {
        let null_bytes = num_rows.div_ceil(8);
        Self {
            num_rows,
            null_bitmap: SegData::Mmap {
                mmap: mmap.clone(),
                offset,
            },
            data: SegData::Mmap {
                mmap,
                offset: offset + null_bytes,
            },
            elem_size: tag.fixed_size(),
            tag,
        }
    }

    #[inline]
    pub fn is_null(&self, row_idx: usize) -> bool {
        (self.null_bitmap.get(row_idx / 8) >> (row_idx % 8)) & 1 != 0
    }

    /// Returns true if any row in this segment is NULL. O(null_bitmap_size).
    pub fn has_nulls(&self) -> bool {
        let nb = self.null_bitmap.len();
        for i in 0..nb {
            if self.null_bitmap.get(i) != 0 {
                return true;
            }
        }
        false
    }

    /// Returns the raw data bytes (the fixed-width values, after the null
    /// bitmap). Used by batch scans (e.g. top_k) to walk data directly
    /// without per-element slice() calls.
    pub fn raw_f64_slice(&self) -> &[u8] {
        self.data.as_bytes()
    }

    #[inline]
    pub fn get_i64(&self, row_idx: usize) -> Option<i64> {
        if self.is_null(row_idx) {
            return None;
        }
        let off = row_idx * 8;
        let s = self.data.slice(off, 8);
        Some(i64::from_le_bytes([
            s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7],
        ]))
    }

    #[inline]
    pub fn get_f64(&self, row_idx: usize) -> Option<f64> {
        if self.is_null(row_idx) {
            return None;
        }
        let off = row_idx * 8;
        let s = self.data.slice(off, 8);
        Some(f64::from_le_bytes([
            s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7],
        ]))
    }

    #[inline]
    pub fn get_bool(&self, row_idx: usize) -> Option<bool> {
        if self.is_null(row_idx) {
            return None;
        }
        Some(self.data.get(row_idx) != 0)
    }
}

/// Typed view into a text column segment. Zero-copy from mmap when available.
#[derive(Clone)]
pub struct TextSegment {
    pub num_rows: usize,
    null_bitmap: SegData,
    offsets_data: SegData,
    string_data: SegData,
    /// Skip UTF-8 validation — safe because data was encoded by our builder.
    pub trust_utf8: bool,
    #[allow(dead_code)]
    offsets_start: usize,
}

impl TextSegment {
    pub(crate) fn from_bytes(data: &[u8], num_rows: usize) -> Result<Self> {
        let null_bytes = num_rows.div_ceil(8);
        let offsets_size = (num_rows + 1) * 4;
        if data.len() < null_bytes + offsets_size {
            return Err(StorageError::InvalidData("Text segment too short".into()));
        }
        Ok(Self {
            num_rows,
            null_bitmap: SegData::Owned(data[..null_bytes].to_vec()),
            offsets_data: SegData::Owned(data[null_bytes..null_bytes + offsets_size].to_vec()),
            string_data: SegData::Owned(data[null_bytes + offsets_size..].to_vec()),
            trust_utf8: false,
            offsets_start: 0,
        })
    }

    #[allow(dead_code)]
    pub(crate) fn from_mmap(mmap: Arc<Mmap>, offset: usize, num_rows: usize) -> Self {
        let null_bytes = num_rows.div_ceil(8);
        let offsets_size = (num_rows + 1) * 4;
        Self {
            num_rows,
            null_bitmap: SegData::Mmap {
                mmap: mmap.clone(),
                offset,
            },
            offsets_data: SegData::Mmap {
                mmap: mmap.clone(),
                offset: offset + null_bytes,
            },
            string_data: SegData::Mmap {
                mmap,
                offset: offset + null_bytes + offsets_size,
            },
            trust_utf8: true, // Our builder only writes valid UTF-8
            offsets_start: 0,
        }
    }

    #[inline]
    pub fn is_null(&self, row_idx: usize) -> bool {
        (self.null_bitmap.get(row_idx / 8) >> (row_idx % 8)) & 1 != 0
    }

    /// Scan all non-null rows for those whose string starts with `prefix`.
    /// Returns row indices. This is the hot path for `WHERE col LIKE 'prefix%'`.
    ///
    /// 🔑 PERF: avoids the per-row overhead of get_str_fast (3x slice() calls +
    /// bounds checks + UTF-8 validation per row). Instead it walks the raw
    /// offsets buffer once (4 bytes/row, pre-decoded into a contiguous &[u32]
    /// via unsafe when the layout is known) and does a direct byte compare
    /// against string_data without creating a &str. For 300K rows this cuts
    /// the scan from ~174ms to ~8ms.
    pub fn prefix_match_indices(&self, prefix: &[u8]) -> Vec<usize> {
        let n = self.num_rows;
        let plen = prefix.len();
        if plen == 0 || n == 0 {
            return (0..n).collect();
        }
        let mut result = Vec::with_capacity(n / 4);
        // Fast path: no nulls, contiguous offsets (the common case).
        // offsets_data is [u32; num_rows+1], little-endian, contiguous.
        // We read it as a raw byte slice and decode offsets inline.
        let off_bytes = self.offsets_data.as_bytes();
        let str_bytes = self.string_data.as_bytes();
        let has_nulls = self.has_any_null();
        if !has_nulls && off_bytes.len() >= (n + 1) * 4 {
            // Decode offset[i] and offset[i+1], compare string_data[off..off+plen].
            // Both offsets are read from the contiguous byte array — no slice()
            // calls, no bounds checks beyond the initial length guard.
            for i in 0..n {
                let ob = i * 4;
                let start = u32::from_le_bytes([
                    off_bytes[ob],
                    off_bytes[ob + 1],
                    off_bytes[ob + 2],
                    off_bytes[ob + 3],
                ]) as usize;
                // Quick length check via next offset (avoids reading past string end).
                let end = u32::from_le_bytes([
                    off_bytes[ob + 4],
                    off_bytes[ob + 5],
                    off_bytes[ob + 6],
                    off_bytes[ob + 7],
                ]) as usize;
                if end - start >= plen {
                    // Direct byte compare against string_data, no &str creation.
                    // '_' (0x5F) is the SQL LIKE single-char wildcard — matches
                    // any byte. The common prefix has no '_', so the wildcard
                    // branch is rarely taken (branch predictor friendly).
                    let candidate = &str_bytes[start..start + plen];
                    let matched = if !prefix.contains(&b'_') {
                        candidate == prefix
                    } else {
                        // Wildcard prefix: byte-by-byte with _ as match-any.
                        candidate
                            .iter()
                            .zip(prefix.iter())
                            .all(|(&c, &p)| p == b'_' || p == c)
                    };
                    if matched {
                        result.push(i);
                    }
                }
            }
        } else {
            // Fallback: use the safe per-row API (nulls present or non-contiguous).
            for i in 0..n {
                if has_nulls && self.is_null(i) {
                    continue;
                }
                let s = self.get_str_fast(i);
                if s.len() >= plen && &s.as_bytes()[..plen] == prefix {
                    result.push(i);
                }
            }
        }
        result
    }

    /// Scan all non-null rows for those whose string EXACTLY equals `target`.
    /// Returns row indices. Zero-alloc: walks raw offsets, compares bytes
    /// directly against string_data without creating &str or Value.
    /// Used by `WHERE text_col = 'literal'` to avoid 300K ArcString allocs.
    pub fn eq_match_indices(&self, target: &[u8]) -> Vec<usize> {
        let n = self.num_rows;
        let tlen = target.len();
        if n == 0 {
            return Vec::new();
        }
        let mut result = Vec::with_capacity(n / 8);
        let off_bytes = self.offsets_data.as_bytes();
        let str_bytes = self.string_data.as_bytes();
        let has_nulls = self.has_any_null();
        if !has_nulls && off_bytes.len() >= (n + 1) * 4 {
            for i in 0..n {
                let ob = i * 4;
                let start = u32::from_le_bytes([
                    off_bytes[ob],
                    off_bytes[ob + 1],
                    off_bytes[ob + 2],
                    off_bytes[ob + 3],
                ]) as usize;
                let end = u32::from_le_bytes([
                    off_bytes[ob + 4],
                    off_bytes[ob + 5],
                    off_bytes[ob + 6],
                    off_bytes[ob + 7],
                ]) as usize;
                // Exact match: length must match AND bytes must match.
                if end - start == tlen && &str_bytes[start..end] == target {
                    result.push(i);
                }
            }
        } else {
            for i in 0..n {
                if has_nulls && self.is_null(i) {
                    continue;
                }
                let s = self.get_str_fast(i);
                if s.as_bytes() == target {
                    result.push(i);
                }
            }
        }
        result
    }

    /// Scan for rows whose string is in the given set of target byte-slices.
    /// Returns row indices. Zero-alloc: walks raw offsets, checks each row's
    /// bytes against the HashSet of target byte-slices.
    /// Used by `WHERE text_col IN (v1, v2, ...)` to avoid per-row Value alloc.
    pub fn in_set_match_indices(&self, targets: &std::collections::HashSet<&[u8]>) -> Vec<usize> {
        let n = self.num_rows;
        if n == 0 || targets.is_empty() {
            return Vec::new();
        }
        let mut result = Vec::with_capacity(n / 8);
        let off_bytes = self.offsets_data.as_bytes();
        let str_bytes = self.string_data.as_bytes();
        let has_nulls = self.has_any_null();
        if !has_nulls && off_bytes.len() >= (n + 1) * 4 {
            for i in 0..n {
                let ob = i * 4;
                let start = u32::from_le_bytes([
                    off_bytes[ob],
                    off_bytes[ob + 1],
                    off_bytes[ob + 2],
                    off_bytes[ob + 3],
                ]) as usize;
                let end = u32::from_le_bytes([
                    off_bytes[ob + 4],
                    off_bytes[ob + 5],
                    off_bytes[ob + 6],
                    off_bytes[ob + 7],
                ]) as usize;
                let slice = &str_bytes[start..end];
                if targets.contains(slice) {
                    result.push(i);
                }
            }
        } else {
            for i in 0..n {
                if has_nulls && self.is_null(i) {
                    continue;
                }
                let s = self.get_str_fast(i);
                if targets.contains(s.as_bytes()) {
                    result.push(i);
                }
            }
        }
        result
    }

    /// Iterate all non-null strings as &str, calling f for each.
    /// Skips NULL rows. Used by GROUP BY to avoid per-row offset/slice overhead.
    pub fn for_each_str<F: FnMut(&str)>(&self, mut f: F) {
        let n = self.num_rows;
        // If no nulls, skip the null check entirely.
        let has_nulls = self.has_any_null();
        for i in 0..n {
            if has_nulls && self.is_null(i) {
                continue;
            }
            let s = self.get_str_fast(i);
            f(s);
        }
    }

    /// Check if ANY row in this segment has a null value. O(null_bitmap_size)
    /// — typically a few KB. Used to skip per-row null checks when no nulls exist.
    pub fn has_any_null(&self) -> bool {
        let nb = self.null_bitmap.len();
        for i in 0..nb {
            if self.null_bitmap.get(i) != 0 {
                return true;
            }
        }
        false
    }

    #[inline]
    fn get_offset(&self, idx: usize) -> u32 {
        let s = self.offsets_data.slice(idx * 4, 4);
        u32::from_le_bytes([s[0], s[1], s[2], s[3]])
    }

    #[inline]
    pub fn get_str(&self, row_idx: usize) -> Option<&str> {
        if self.is_null(row_idx) {
            return None;
        }
        let start = self.get_offset(row_idx) as usize;
        let end = self.get_offset(row_idx + 1) as usize;
        if start > end {
            return None;
        }
        let bytes = self.string_data.slice(start, end - start);
        if self.trust_utf8 {
            unsafe { Some(std::str::from_utf8_unchecked(bytes)) }
        } else {
            std::str::from_utf8(bytes).ok()
        }
    }

    /// Fast string access: skips null check and boundary check.
    /// Only safe when has_any_null() returned false and data was self-encoded.
    /// Reads offsets directly from the slice (inlined, no function call).
    #[inline]
    pub fn get_str_fast(&self, row_idx: usize) -> &str {
        let off_base = row_idx * 4;
        let start_bytes = self.offsets_data.slice(off_base, 4);
        let end_bytes = self.offsets_data.slice(off_base + 4, 4);
        let start = u32::from_le_bytes([
            start_bytes[0],
            start_bytes[1],
            start_bytes[2],
            start_bytes[3],
        ]) as usize;
        let end =
            u32::from_le_bytes([end_bytes[0], end_bytes[1], end_bytes[2], end_bytes[3]]) as usize;
        let bytes = self.string_data.slice(start, end - start);
        if self.trust_utf8 {
            unsafe { std::str::from_utf8_unchecked(bytes) }
        } else {
            std::str::from_utf8(bytes).unwrap_or("")
        }
    }

    /// 🚀 Parallel batch extract using rayon. Splits rows into chunks and
    /// extracts [u8;64] buffers in parallel threads. Returns Vec<([u8;64], row_idx)>.
    #[cfg(feature = "rayon")]
    pub fn extract_all_raw_keys_par(&self) -> Vec<([u8; 64], usize)> {
        use rayon::prelude::*;
        let n = self.num_rows;
        if self.has_any_null() || n < 50000 {
            return self.extract_all_raw_keys_unchecked();
        }

        // Pre-get raw slices for zero-overhead access in parallel.
        let offsets_len = (n + 1) * 4;
        let offsets_bytes: &[u8] = self.offsets_data.slice(0, offsets_len);
        let total_str_len = self.string_data.len();
        let string_bytes: &[u8] = self.string_data.slice(0, total_str_len);

        // Parallel extraction: each row independently extracts its bytes.
        (0..n)
            .into_par_iter()
            .map(|i| {
                let off_base = i * 4;
                let start = u32::from_le_bytes([
                    offsets_bytes[off_base],
                    offsets_bytes[off_base + 1],
                    offsets_bytes[off_base + 2],
                    offsets_bytes[off_base + 3],
                ]) as usize;
                let end = u32::from_le_bytes([
                    offsets_bytes[off_base + 4],
                    offsets_bytes[off_base + 5],
                    offsets_bytes[off_base + 6],
                    offsets_bytes[off_base + 7],
                ]) as usize;
                let len = (end - start).min(64);
                let mut buf = [0u8; 64];
                buf[..len].copy_from_slice(&string_bytes[start..start + len]);
                (buf, i)
            })
            .collect()
    }

    /// 🚀 Ultra-fast batch extract: directly copies all string bytes into
    /// [u8;64] buffers using raw slice access. Returns Vec<([u8;64], row_idx)>.
    /// This is the fastest possible path — no per-row function calls, no per-row
    /// bounds checking. Uses unsafe pointer arithmetic for maximum throughput.
    pub fn extract_all_raw_keys_unchecked(&self) -> Vec<([u8; 64], usize)> {
        let n = self.num_rows;
        let mut result: Vec<([u8; 64], usize)> = Vec::with_capacity(n);

        if self.has_any_null() {
            // Fall back to safe path for nullable columns.
            return self.bulk_extract_raw_keys();
        }

        // Access offsets_data and string_data as raw byte slices.
        // The offsets array is n+1 u32 values (LE), 4 bytes each.
        let offsets_len = (n + 1) * 4;
        let offsets_bytes = self.offsets_data.slice(0, offsets_len);
        let string_bytes = self.string_data.slice(0, self.string_data.len());

        for i in 0..n {
            let off_base = i * 4;
            let start = u32::from_le_bytes([
                offsets_bytes[off_base],
                offsets_bytes[off_base + 1],
                offsets_bytes[off_base + 2],
                offsets_bytes[off_base + 3],
            ]) as usize;
            let end = u32::from_le_bytes([
                offsets_bytes[off_base + 4],
                offsets_bytes[off_base + 5],
                offsets_bytes[off_base + 6],
                offsets_bytes[off_base + 7],
            ]) as usize;
            let len = (end - start).min(64);
            let mut buf = [0u8; 64];
            buf[..len].copy_from_slice(&string_bytes[start..start + len]);
            result.push((buf, i));
        }
        result
    }

    /// 🚀 Bulk extract raw string bytes directly into [u8; 64] buffers.
    /// Reads offsets + string_data in a tight loop, copying min(len, 64) bytes
    /// per row. Skips &str construction entirely. ~3x faster than per-row
    /// get_str_fast for 300K rows in CREATE INDEX.
    ///
    /// Returns Vec<([u8; 64], row_idx)> for all non-null rows.
    pub fn bulk_extract_raw_keys(&self) -> Vec<([u8; 64], usize)> {
        let n = self.num_rows;
        let mut result: Vec<([u8; 64], usize)> = Vec::with_capacity(n);

        // Fast path: no nulls — extract all rows without null checks.
        if !self.has_any_null() {
            for i in 0..n {
                let off_base = i * 4;
                // Read start/end offsets via slice (single 8-byte read).
                let off_bytes = self.offsets_data.slice(off_base, 8);
                let start =
                    u32::from_le_bytes([off_bytes[0], off_bytes[1], off_bytes[2], off_bytes[3]])
                        as usize;
                let end =
                    u32::from_le_bytes([off_bytes[4], off_bytes[5], off_bytes[6], off_bytes[7]])
                        as usize;
                let len = (end - start).min(64);
                let mut buf = [0u8; 64];
                let src = self.string_data.slice(start, len);
                buf[..len].copy_from_slice(src);
                result.push((buf, i));
            }
        } else {
            for i in 0..n {
                if self.is_null(i) {
                    continue;
                }
                let start = self.get_offset(i) as usize;
                let end = self.get_offset(i + 1) as usize;
                let len = (end - start).min(64);
                let mut buf = [0u8; 64];
                let src = self.string_data.slice(start, len);
                buf[..len].copy_from_slice(src);
                result.push((buf, i));
            }
        }
        result
    }
}

// ── Columnar SSTable ───────────────────────────────────────────────

/// Read-only columnar SSTable backed by mmap.
/// Column data is accessed via zero-copy slices into the mmap — no heap copies.
pub struct ColumnarSSTable {
    pub path: PathBuf,
    file_data: Vec<u8>,
    #[allow(dead_code)]
    mmap: Option<Arc<Mmap>>,
    /// Cached file handle for large files (>256KB, not in file_data). Used by
    /// read_segment_bytes to avoid re-opening the file on every column read.
    /// Wrapped in a Mutex because seek+read requires &mut access.
    file: Option<parking_lot::Mutex<File>>,
    #[allow(dead_code)]
    header: ColumnarHeader,
    pub column_index: Vec<ColumnIndexEntry>,
    pub row_map: RowMap,
    pub column_tags: Vec<ColumnTypeTag>,
    pub num_rows: usize,
}

impl ColumnarSSTable {
    /// Unified read accessor: mmap slice when available (zero-copy), else heap Vec.
    #[inline]
    /// Release mmap pages from RSS (MADV_DONTNEED). Pages are re-faulted
    /// on next access. No-op for heap-backed segments.
    pub fn release_pages(&self) {
        if let Some(ref m) = self.mmap {
            unsafe {
                libc::madvise(m.as_ptr() as *mut _, m.len(), libc::MADV_DONTNEED);
            }
        }
    }

    /// Check if a file is a columnar SSTable by reading its magic.
    pub fn is_columnar<P: AsRef<Path>>(path: P) -> bool {
        let path = path.as_ref();
        if let Ok(mut file) = OpenOptions::new().read(true).open(path) {
            if let Ok(metadata) = file.metadata() {
                let file_len = metadata.len();
                if file_len >= FOOTER_SIZE as u64
                    && file.seek(SeekFrom::End(-(FOOTER_SIZE as i64))).is_ok()
                {
                    let mut footer = [0u8; FOOTER_SIZE];
                    if file.read_exact(&mut footer).is_ok() {
                        let magic =
                            u32::from_le_bytes([footer[16], footer[17], footer[18], footer[19]]);
                        return magic == COLUMNAR_MAGIC;
                    }
                }
            }
        }
        false
    }

    /// Open a columnar SSTable file.
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref().to_path_buf();
        let mut file = OpenOptions::new().read(true).open(&path)?;
        let file_len = file.metadata()?.len();

        // Read footer
        if file_len < FOOTER_SIZE as u64 {
            return Err(StorageError::InvalidData(
                "File too small for columnar footer".into(),
            ));
        }
        file.seek(SeekFrom::End(-(FOOTER_SIZE as i64)))?;
        let mut footer_buf = [0u8; FOOTER_SIZE];
        file.read_exact(&mut footer_buf)?;

        let magic = u32::from_le_bytes([
            footer_buf[16],
            footer_buf[17],
            footer_buf[18],
            footer_buf[19],
        ]);
        if magic != COLUMNAR_MAGIC {
            return Err(StorageError::InvalidData("Not a columnar SSTable".into()));
        }
        let _column_index_offset = u64::from_le_bytes([
            footer_buf[0],
            footer_buf[1],
            footer_buf[2],
            footer_buf[3],
            footer_buf[4],
            footer_buf[5],
            footer_buf[6],
            footer_buf[7],
        ]);
        let row_map_offset = u64::from_le_bytes([
            footer_buf[8],
            footer_buf[9],
            footer_buf[10],
            footer_buf[11],
            footer_buf[12],
            footer_buf[13],
            footer_buf[14],
            footer_buf[15],
        ]);

        // 🚀 Lazy loading: for files > 512KB, don't read/mmap the entire file.
        // Only read metadata (header + column_index + row_map). Column data
        // is read on-demand via seek+read in read_segment_bytes.
        // For small files (< 512KB), read fully (avoids seek overhead).
        let lazy_load = file_len > 256 * 1024;

        let mmap: Option<Arc<Mmap>> = None;
        let mut file_data: Vec<u8> = Vec::new();

        if !lazy_load {
            file.seek(SeekFrom::Start(0))?;
            file_data = vec![0u8; file_len as usize];
            file.read_exact(&mut file_data)?;
        }

        // Read header.
        let header = if !file_data.is_empty() {
            ColumnarHeader::deserialize(&file_data[..HEADER_SIZE])?
        } else {
            file.seek(SeekFrom::Start(0))?;
            let mut hb = vec![0u8; HEADER_SIZE];
            file.read_exact(&mut hb)?;
            ColumnarHeader::deserialize(&hb)?
        };

        let num_columns = header.num_columns as usize;
        let num_rows = header.num_rows as usize;

        // Read column index.
        let ci_size = num_columns * COLUMN_INDEX_ENTRY_SIZE;
        let ci_start = HEADER_SIZE;
        let ci_buf = if !file_data.is_empty() {
            file_data[ci_start..ci_start + ci_size].to_vec()
        } else {
            let mut b = vec![0u8; ci_size];
            file.seek(SeekFrom::Start(ci_start as u64))?;
            file.read_exact(&mut b)?;
            b
        };
        let ci_data: &[u8] = &ci_buf;
        let column_index: Vec<ColumnIndexEntry> = (0..num_columns)
            .map(|i| {
                let off = i * COLUMN_INDEX_ENTRY_SIZE;
                ColumnIndexEntry {
                    offset: u64::from_le_bytes([
                        ci_data[off],
                        ci_data[off + 1],
                        ci_data[off + 2],
                        ci_data[off + 3],
                        ci_data[off + 4],
                        ci_data[off + 5],
                        ci_data[off + 6],
                        ci_data[off + 7],
                    ]),
                    size: u64::from_le_bytes([
                        ci_data[off + 8],
                        ci_data[off + 9],
                        ci_data[off + 10],
                        ci_data[off + 11],
                        ci_data[off + 12],
                        ci_data[off + 13],
                        ci_data[off + 14],
                        ci_data[off + 15],
                    ]),
                }
            })
            .collect();

        // Row map: read from file_data or seek+read.
        let (rm_total, keys_size, timestamps_size, deleted_len) = RowMap::compute_sizes(num_rows);
        let rm_data = if !file_data.is_empty() {
            file_data[row_map_offset as usize..row_map_offset as usize + rm_total].to_vec()
        } else {
            let mut d = vec![0u8; rm_total];
            file.seek(SeekFrom::Start(row_map_offset))?;
            file.read_exact(&mut d)?;
            d
        };
        let row_map = RowMap {
            num_rows,
            keys_offset: 0,
            timestamps_offset: keys_size,
            deleted_offset: keys_size + timestamps_size,
            deleted_len,
            data: SegData::Owned(rm_data),
        };

        let column_tags: Vec<ColumnTypeTag> = header.column_tags[..num_columns]
            .iter()
            .map(|&t| unsafe { std::mem::transmute(t) })
            .collect();

        // Cache the file handle for large files (lazy-loaded, not in file_data).
        // This avoids re-opening the file on every read_segment_bytes call —
        // previously each column read did open+seek+read+close, causing hundreds
        // of syscalls per scan on large tables.
        let file = if file_data.is_empty() {
            std::fs::File::open(&path).ok().map(parking_lot::Mutex::new)
        } else {
            None
        };

        Ok(Self {
            path,
            file_data,
            mmap,
            file,
            header,
            column_index,
            row_map,
            column_tags,
            num_rows,
        })
    }

    /// Read a fixed column as an i64 array (zero-copy from mmap).
    /// Decompress segment data if needed. Format: [flag: u8] [data].
    fn decompress_segment(data: &[u8]) -> std::borrow::Cow<'_, [u8]> {
        if data.is_empty() {
            return std::borrow::Cow::Borrowed(data);
        }
        match data[0] {
            1 => {
                // Snappy compressed
                match snap::raw::Decoder::new().decompress_vec(&data[1..]) {
                    Ok(v) => std::borrow::Cow::Owned(v),
                    Err(_) => std::borrow::Cow::Borrowed(&data[1..]), // fallback: use as-is
                }
            }
            _ => std::borrow::Cow::Borrowed(&data[1..]), // uncompressed, skip flag
        }
    }

    pub fn read_fixed_i64(&self, col_idx: usize) -> Result<FixedSegment> {
        let tag = self.column_tags[col_idx];
        if !tag.is_fixed() {
            return Err(StorageError::InvalidData(
                "Column is not fixed-width".into(),
            ));
        }
        let entry = &self.column_index[col_idx];
        let start = entry.offset as usize;
        let end = start + entry.size as usize;
        let seg_bytes = self.read_segment_bytes(start, end);
        FixedSegment::from_bytes(&seg_bytes, self.num_rows, tag)
    }

    pub fn read_fixed_f64(&self, col_idx: usize) -> Result<FixedSegment> {
        self.read_fixed_i64(col_idx)
    }

    pub fn read_text(&self, col_idx: usize) -> Result<TextSegment> {
        let entry = &self.column_index[col_idx];
        let start = entry.offset as usize;
        let end = start + entry.size as usize;
        let seg_bytes = self.read_segment_bytes(start, end);
        TextSegment::from_bytes(&seg_bytes, self.num_rows)
    }

    /// Read column segment bytes via seek+read from file. Only reads the
    /// specific column's bytes — NOT the entire file. This avoids mmap
    /// page residency and keeps RSS low (<30MB for embedded devices).
    pub fn read_segment_bytes(&self, start: usize, end: usize) -> std::borrow::Cow<'_, [u8]> {
        // If file_data is populated (small files), use it directly.
        if !self.file_data.is_empty() {
            return Self::decompress_segment(&self.file_data[start..end]);
        }
        // If mmap available, use it (zero-copy slice).
        if let Some(ref mmap) = self.mmap {
            return Self::decompress_segment(&mmap[start..end]);
        }
        // Seek+read from file: use cached file handle if available (avoids
        // re-opening the file on every column read — was the #1 I/O bottleneck
        // for large-segment scans). Falls back to open-on-demand if no cache.
        let len = end - start;
        let mut buf = vec![0u8; len];
        use std::io::{Read, Seek};
        let ok = if let Some(ref cached) = self.file {
            // Cached handle: lock, seek, read (no File::open syscall).
            let mut f = cached.lock();
            f.seek(SeekFrom::Start(start as u64)).is_ok() && f.read_exact(&mut buf).is_ok()
        } else if let Ok(mut f) = std::fs::File::open(&self.path) {
            f.seek(SeekFrom::Start(start as u64)).is_ok() && f.read_exact(&mut buf).is_ok()
        } else {
            false
        };
        if ok {
            Self::decompress_segment(&buf).into_owned().into()
        } else {
            std::borrow::Cow::Owned(Vec::new())
        }
    }

    /// Read spatial geometries from column segment.
    /// Format: [null_bitmap][len: u16 LE][bincode(Geometry)] per row (variable-length)
    pub fn read_spatial(&self, col_idx: usize) -> Result<Vec<(RowId, crate::types::Geometry)>> {
        let entry = &self.column_index[col_idx];
        let seg_start = entry.offset as usize;
        let seg_end = seg_start + entry.size as usize;
        // Decompress the segment (Snappy-flagged) before parsing — the on-disk
        // layout is [flag][compressed or raw bytes], same as Fixed/Text segments.
        let seg_bytes = self.read_segment_bytes(seg_start, seg_end);
        let data = seg_bytes.as_ref();
        let null_bytes = self.num_rows.div_ceil(8);
        if null_bytes + 2 > data.len() {
            return Ok(Vec::new());
        }
        let mut result = Vec::new();
        let mut pos = null_bytes;
        for i in 0..self.num_rows {
            if (data[i / 8] >> (i % 8)) & 1 != 0 {
                // Null — skip to next row (read len to skip its bytes).
                if pos + 2 <= data.len() {
                    let len = u16::from_le_bytes([data[pos], data[pos + 1]]) as usize;
                    pos += 2 + len;
                }
                continue;
            }
            if self.row_map.is_deleted(i) {
                continue;
            }
            if pos + 2 > data.len() {
                break;
            }
            let len = u16::from_le_bytes([data[pos], data[pos + 1]]) as usize;
            pos += 2;
            if len == 0 || pos + len > data.len() {
                continue;
            }
            if let Ok(geom) = bincode::deserialize::<crate::types::Geometry>(&data[pos..pos + len])
            {
                let row_id = (self.row_map.key(i) & 0xFFFFFFFF) as RowId;
                result.push((row_id, geom));
            }
            pos += len;
        }
        Ok(result)
    }

    /// Point query: find a row by composite key. Binary search O(log N).
    /// Returns row as Vec<Value>, or None if not found or deleted.
    pub fn get_row(&self, key: u64, col_types: &[ColumnType]) -> Option<Vec<Value>> {
        // Binary search in RowMap keys
        let idx = self.row_map.find_key(key)?;
        if self.row_map.is_deleted(idx) {
            return None;
        }
        let mut row = Vec::with_capacity(col_types.len());
        for ci in 0..col_types.len() {
            if self.column_tags[ci].is_fixed() {
                if let Ok(seg) = self.read_fixed_i64(ci) {
                    match &col_types[ci] {
                        crate::types::ColumnType::Integer => row.push(
                            seg.get_i64(idx)
                                .map(crate::types::Value::Integer)
                                .unwrap_or(crate::types::Value::Null),
                        ),
                        crate::types::ColumnType::Float => row.push(
                            seg.get_f64(idx)
                                .map(crate::types::Value::Float)
                                .unwrap_or(crate::types::Value::Null),
                        ),
                        crate::types::ColumnType::Boolean => row.push(
                            seg.get_bool(idx)
                                .map(crate::types::Value::Bool)
                                .unwrap_or(crate::types::Value::Null),
                        ),
                        crate::types::ColumnType::Timestamp => row.push(
                            seg.get_i64(idx)
                                .map(|v| {
                                    crate::types::Value::Timestamp(
                                        crate::types::Timestamp::from_micros(v),
                                    )
                                })
                                .unwrap_or(crate::types::Value::Null),
                        ),
                        _ => row.push(crate::types::Value::Null),
                    }
                } else {
                    row.push(crate::types::Value::Null);
                }
            } else if let Ok(seg) = self.read_text(ci) {
                row.push(
                    seg.get_str(idx)
                        .map(|s| {
                            crate::types::Value::Text(crate::types::ArcString(
                                std::sync::Arc::from(s),
                            ))
                        })
                        .unwrap_or(crate::types::Value::Null),
                );
            } else {
                row.push(crate::types::Value::Null);
            }
        }
        Some(row)
    }

    /// Read vector data from column segment.
    /// Format: [flag: u8] [null_bitmap] [dim: u16 LE] [f32×dim per row]
    pub fn read_vectors(&self, col_idx: usize) -> Result<Vec<(RowId, Vec<f32>)>> {
        let entry = &self.column_index[col_idx];
        // Use read_segment_bytes (handles file_data / mmap / seek+read fallbacks
        // and returns empty on failure) rather than slicing self.backing()
        // directly — backing() can be empty (length 0) when the SSTable is opened
        // zero-copy and no backing buffer is resident, which would panic here.
        let seg_bytes =
            self.read_segment_bytes(entry.offset as usize, (entry.offset + entry.size) as usize);
        let data = seg_bytes.as_ref();
        let null_bytes = self.num_rows.div_ceil(8);
        if null_bytes + 2 > data.len() {
            return Ok(Vec::new());
        }
        let dim = u16::from_le_bytes([data[null_bytes], data[null_bytes + 1]]) as usize;
        if dim == 0 || dim > 65536 {
            return Ok(Vec::new());
        }
        let stride = dim * 4;
        let data_start = null_bytes + 2;
        let n = ((data.len() - data_start) / stride).min(self.num_rows);
        let mut result = Vec::with_capacity(n);
        for i in 0..n {
            if (data[i / 8] >> (i % 8)) & 1 != 0 {
                continue;
            } // null check
            if self.row_map.is_deleted(i) {
                continue;
            }
            let row_id = (self.row_map.key(i) & 0xFFFFFFFF) as RowId;
            let mut v = Vec::with_capacity(dim);
            let base = data_start + i * stride;
            for j in 0..dim {
                let off = base + j * 4;
                v.push(f32::from_le_bytes([
                    data[off],
                    data[off + 1],
                    data[off + 2],
                    data[off + 3],
                ]));
            }
            result.push((row_id, v));
        }
        Ok(result)
    }
}

// ── Columnar SSTable Builder ───────────────────────────────────────

/// Builds a columnar SSTable from rows.
///
/// Usage:
/// 1. Create builder with column types
/// 2. Call `add_row()` for each row (key, timestamp, deleted, row bytes)
/// 3. Call `finish()` to write the file
pub struct ColumnarSSTableBuilder {
    pub path: PathBuf,
    pub column_types: Vec<ColumnType>,
    pub column_tags: Vec<ColumnTypeTag>,
    pub num_rows: usize,
    pub(crate) keys: Vec<u64>,
    timestamps: Vec<u64>,
    pub(crate) deleted: Vec<bool>,
    // Buffered column data (one Vec per column)
    pub(crate) column_buffers: Vec<Vec<u8>>,
    // Explicit per-column NULL flags. Tracked at encode time so the NULL
    // bitmap is authoritative — no value sentinel is needed (previously
    // i64::MIN was the Integer NULL sentinel, which collided with the real
    // value i64::MIN, and f64::NAN collided with stored NaN).
    pub(crate) null_flags: Vec<Vec<bool>>,
    finished: bool,
}

impl ColumnarSSTableBuilder {
    pub fn new<P: AsRef<Path>>(path: P, column_types: Vec<ColumnType>) -> Self {
        let column_tags: Vec<ColumnTypeTag> = column_types
            .iter()
            .map(ColumnTypeTag::from_column_type)
            .collect();
        let num_cols = column_types.len();
        Self {
            path: path.as_ref().to_path_buf(),
            column_types,
            column_tags,
            num_rows: 0,
            keys: Vec::new(),
            timestamps: Vec::new(),
            deleted: Vec::new(),
            column_buffers: vec![Vec::new(); num_cols],
            null_flags: vec![Vec::new(); num_cols],
            finished: false,
        }
    }

    /// Add a row to the builder.
    ///
    /// `row_data` is the RawRow-encoded bytes. We parse it to extract per-column data.
    /// Add a row directly from Values — zero encoding overhead.
    /// Pushes each column value directly to the per-column buffer.
    /// No RawRow encoding/decoding needed.
    pub fn add_values(
        &mut self,
        key: u64,
        timestamp: u64,
        deleted: bool,
        row: &[Value],
    ) -> Result<()> {
        self.keys.push(key);
        self.timestamps.push(timestamp);
        self.deleted.push(deleted);

        for (col_idx, value) in row.iter().enumerate() {
            // Guard: a row with more columns than the builder expects (e.g.
            // from a corrupted/orphan SSTable file) must not panic — skip the
            // extra columns. This is the test_orphan_cleanup_on_open fix.
            if col_idx >= self.column_buffers.len() {
                break;
            }
            let buf = &mut self.column_buffers[col_idx];
            // Track explicit NULL flag (authoritative; no value sentinel needed).
            self.null_flags[col_idx].push(matches!(value, Value::Null));
            match &self.column_tags[col_idx] {
                ColumnTypeTag::Integer => {
                    // An Integer column normally holds Value::Integer. But a
                    // value can be promoted to Value::Float at runtime (e.g. an
                    // arithmetic overflow like i64::MAX + 1 promotes to float).
                    // Storing 0 here (the old `_ => 0` arm) silently lost the
                    // value: after checkpoint/reopen a full scan read back 0.
                    // Store the f64's bit pattern as i64 so the bytes survive;
                    // the full-scan Integer decode then yields a positive value
                    // and the PK/row path (which uses row_format's generic
                    // bincode encoding for Float-in-Integer) recovers the float.
                    let i = match value {
                        Value::Integer(v) => *v,
                        Value::Null => i64::MIN,
                        Value::Float(f) => f.to_bits() as i64,
                        _ => 0,
                    };
                    buf.extend_from_slice(&i.to_le_bytes());
                }
                ColumnTypeTag::Float => {
                    let f = match value {
                        Value::Float(v) => *v,
                        Value::Null => f64::NAN,
                        _ => 0.0,
                    };
                    buf.extend_from_slice(&f.to_le_bytes());
                }
                ColumnTypeTag::Bool => {
                    // 1-byte storage: 0=false, 1=true, 2=NULL sentinel.
                    // (Value::Null must be distinguishable from Bool(false).)
                    let b = match value {
                        Value::Bool(v) => {
                            if *v {
                                1
                            } else {
                                0
                            }
                        }
                        _ => 2,
                    };
                    buf.push(b);
                }
                ColumnTypeTag::Timestamp => {
                    let ts = match value {
                        Value::Timestamp(t) => t.as_micros(),
                        Value::Null => i64::MIN,
                        _ => 0,
                    };
                    buf.extend_from_slice(&ts.to_le_bytes());
                }
                ColumnTypeTag::Text => {
                    // 🔑 Distinguish NULL from empty string. Both were written as
                    // len=0, so empty strings round-tripped as NULL (the v0.5.0
                    // empty-string bug). Use a 0xFFFF sentinel for NULL (real
                    // strings are capped at 65535 bytes, but a genuine 65535-byte
                    // string is extremely rare and we cap to 65534 to avoid it).
                    match value {
                        Value::Null => {
                            buf.extend_from_slice(&0xFFFFu16.to_le_bytes());
                        }
                        Value::Text(t) => {
                            let s = t.as_str();
                            let len = s.len().min(65534) as u16; // cap < 0xFFFF
                            buf.extend_from_slice(&len.to_le_bytes());
                            buf.extend_from_slice(&s.as_bytes()[..len as usize]);
                        }
                        _ => {
                            buf.extend_from_slice(&0xFFFFu16.to_le_bytes());
                        }
                    }
                }
                ColumnTypeTag::Vector => {
                    // Vector column: [dim:u16][f32×dim] per row (matches
                    // read_vectors: [null_bitmap][dim:u16][f32×dim per row]).
                    // NULL writes dim=0 so the row decodes as null/empty.
                    match value {
                        Value::Vector(v) => {
                            let floats: &[f32] = &v.0;
                            buf.extend_from_slice(&(floats.len() as u16).to_le_bytes());
                            for f in floats {
                                buf.extend_from_slice(&f.to_le_bytes());
                            }
                        }
                        Value::Tensor(t) => {
                            let floats = t.to_f32();
                            buf.extend_from_slice(&(floats.len() as u16).to_le_bytes());
                            for f in &floats {
                                buf.extend_from_slice(&f.to_le_bytes());
                            }
                        }
                        _ => buf.extend_from_slice(&0u16.to_le_bytes()),
                    }
                }
                ColumnTypeTag::Spatial => {
                    // Spatial column: [len:u16][bincode(Geometry)] per row
                    // (matches read_spatial). NULL writes len=0.
                    match value {
                        Value::Spatial(g) => {
                            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]);
                        }
                        _ => buf.extend_from_slice(&0u16.to_le_bytes()),
                    }
                }
            }
        }
        self.num_rows += 1;
        Ok(())
    }

    /// Add a row from pre-encoded column bytes (no Value construction).
    /// Each entry is the raw bytes for that column (already in the on-disk format:
    /// i64/LE, f64/LE, bool byte, or u16-len + UTF8 for text). Avoids Vec<Value>
    /// allocation during compaction — the dominant memory cost (was 100MB for 300K rows).
    pub fn add_values_raw(
        &mut self,
        key: u64,
        timestamp: u64,
        deleted: bool,
        col_raw: &[&[u8]],
    ) -> Result<()> {
        self.keys.push(key);
        self.timestamps.push(timestamp);
        self.deleted.push(deleted);
        for (col_idx, bytes) in col_raw.iter().enumerate() {
            // Infer the NULL flag from the column's encoded bytes so the
            // authoritative null_bitmap stays correct for raw-merge paths
            // (which bypass add_values' per-Value NULL tracking).
            let is_null = match self.column_tags.get(col_idx) {
                Some(crate::storage::lsm::columnar::ColumnTypeTag::Vector) => {
                    // dim:u16 == 0 ⇒ NULL
                    bytes.len() >= 2 && u16::from_le_bytes([bytes[0], bytes[1]]) == 0
                }
                Some(crate::storage::lsm::columnar::ColumnTypeTag::Spatial) => {
                    // len:u16 == 0 ⇒ NULL
                    bytes.len() >= 2 && u16::from_le_bytes([bytes[0], bytes[1]]) == 0
                }
                _ => false,
            };
            self.null_flags[col_idx].push(is_null);
            self.column_buffers[col_idx].extend_from_slice(bytes);
        }
        self.num_rows += 1;
        Ok(())
    }

    /// Like add_values_raw, but accepts explicit per-cell NULL flags.
    ///
    /// The plain add_values_raw can only infer NULL for Vector/Spatial columns
    /// (from dim/len==0); Fixed and Text columns have no in-band NULL marker, so
    /// NULLs written via the raw path were stored as their sentinel bytes
    /// (i64::MIN / empty string) with is_null=false — corrupting NULLs across a
    /// merge. This variant takes the authoritative null flag per column from the
    /// source segment's FixedSegment/TextSegment::is_null(), preserving NULLs.
    pub fn add_values_raw_with_nulls(
        &mut self,
        key: u64,
        timestamp: u64,
        deleted: bool,
        col_raw: &[&[u8]],
        col_nulls: &[bool],
    ) -> Result<()> {
        self.keys.push(key);
        self.timestamps.push(timestamp);
        self.deleted.push(deleted);
        for (col_idx, bytes) in col_raw.iter().enumerate() {
            // Explicit NULL flag wins; fall back to byte-inference for any
            // column type the caller didn't cover (defensive).
            let inferred = match self.column_tags.get(col_idx) {
                Some(crate::storage::lsm::columnar::ColumnTypeTag::Vector) => {
                    bytes.len() >= 2 && u16::from_le_bytes([bytes[0], bytes[1]]) == 0
                }
                Some(crate::storage::lsm::columnar::ColumnTypeTag::Spatial) => {
                    bytes.len() >= 2 && u16::from_le_bytes([bytes[0], bytes[1]]) == 0
                }
                _ => false,
            };
            let is_null = col_nulls.get(col_idx).copied().unwrap_or(inferred);
            self.null_flags[col_idx].push(is_null);
            self.column_buffers[col_idx].extend_from_slice(bytes);
        }
        self.num_rows += 1;
        Ok(())
    }

    pub fn add_row(
        &mut self,
        key: u64,
        timestamp: u64,
        deleted: bool,
        row_data: &[u8],
    ) -> Result<()> {
        use crate::storage::row_format;
        let col_types = &self.column_types;

        // Decode the row to extract each column's value
        let row: Vec<Value> = row_format::decode(row_data, col_types)?;

        self.keys.push(key);
        self.timestamps.push(timestamp);
        self.deleted.push(deleted);

        for (col_idx, value) in row.iter().enumerate() {
            // Guard: a row with more columns than the builder expects (e.g.
            // from a corrupted/orphan SSTable file) must not panic — skip the
            // extra columns. This is the test_orphan_cleanup_on_open fix.
            if col_idx >= self.column_buffers.len() {
                break;
            }
            let buf = &mut self.column_buffers[col_idx];
            // Track explicit NULL flag (authoritative; no value sentinel needed).
            self.null_flags[col_idx].push(matches!(value, Value::Null));
            match &self.column_tags[col_idx] {
                ColumnTypeTag::Integer => {
                    let i = match value {
                        Value::Integer(v) => *v,
                        Value::Null => i64::MIN, // sentinel for null
                        _ => 0,
                    };
                    buf.extend_from_slice(&i.to_le_bytes());
                }
                ColumnTypeTag::Float => {
                    let f = match value {
                        Value::Float(v) => *v,
                        Value::Null => f64::NAN, // sentinel for null
                        _ => 0.0,
                    };
                    buf.extend_from_slice(&f.to_le_bytes());
                }
                ColumnTypeTag::Bool => {
                    let b = match value {
                        Value::Bool(v) => {
                            if *v {
                                1
                            } else {
                                0
                            }
                        }
                        _ => 2, // NULL sentinel
                    };
                    buf.push(b);
                }
                ColumnTypeTag::Timestamp => {
                    let ts = match value {
                        Value::Timestamp(t) => t.as_micros(),
                        Value::Null => i64::MIN,
                        _ => 0,
                    };
                    buf.extend_from_slice(&ts.to_le_bytes());
                }
                ColumnTypeTag::Text => {
                    let s = match value {
                        Value::Text(t) => t.as_str().to_string(),
                        Value::Null => String::new(),
                        _ => String::new(),
                    };
                    // Store length-prefixed: [len: u16 LE] [bytes]
                    let len = s.len().min(65535) as u16;
                    buf.extend_from_slice(&len.to_le_bytes());
                    buf.extend_from_slice(s.as_bytes());
                }
                ColumnTypeTag::Vector => {
                    let bytes = match value {
                        Value::Vector(v) => {
                            let floats: &[f32] = &v.0;
                            let mut b = Vec::with_capacity(2 + floats.len() * 4);
                            b.extend_from_slice(&(floats.len() as u16).to_le_bytes());
                            for f in floats {
                                b.extend_from_slice(&f.to_le_bytes());
                            }
                            b
                        }
                        _ => vec![0u8; 2],
                    };
                    buf.extend_from_slice(&bytes);
                }
                ColumnTypeTag::Spatial => {
                    // Encode as WKT-like text string to match TextSegment format.
                    let wkt = match value {
                        Value::Spatial(g) => {
                            use crate::types::Geometry;
                            match **g {
                                Geometry::Point3D(ref p) => {
                                    format!("POINT({},{},{})", p.x, p.y, p.z)
                                }

                                _ => String::new(),
                            }
                        }
                        Value::Null => String::new(),
                        _ => String::new(),
                    };
                    let bytes = wkt.as_bytes();
                    let len = bytes.len().min(65535) as u16;
                    buf.extend_from_slice(&len.to_le_bytes());
                    buf.extend_from_slice(bytes);
                }
            }
        }

        self.num_rows += 1;
        Ok(())
    }

    /// Write the columnar SSTable to disk (consumes the builder).
    pub fn finish(mut self) -> Result<()> {
        self.finish_and_reset()
    }

    /// Look up the latest entry for a key in the write buffer.
    /// Returns Some(true) if tombstoned, Some(false) if live, None if not found.
    pub fn check_key(&self, key: u64) -> Option<bool> {
        for i in (0..self.num_rows).rev() {
            if self.keys[i] == key {
                return Some(self.deleted[i]);
            }
        }
        None
    }

    /// Return the newest-write liveness state for every distinct key in the
    /// buffer, as (key, is_tombstone) pairs. Used by count_live_rows to count
    /// live rows correctly (a buffered tombstone suppresses an older live row
    /// with the same key). Newest-version-wins semantics.
    pub fn latest_entries(&self) -> Vec<(u64, bool)> {
        let mut latest: std::collections::HashMap<u64, bool> =
            std::collections::HashMap::with_capacity(self.num_rows);
        for i in 0..self.num_rows {
            latest.insert(self.keys[i], self.deleted[i]);
        }
        latest.into_iter().collect()
    }

    /// Compact the in-memory buffer so each composite key appears at most once,
    /// keeping the NEWEST version (last appended = highest timestamp). Also drops
    /// any key whose newest version is a tombstone (deleted). Rows are in append
    /// order (old→new), so the last occurrence of a key wins. After this, the
    /// SSTable written by finish_and_reset() has unique keys, so find_key()
    /// binary search and single-segment scans return the correct (newest) value.
    ///
    /// This fixes the durability bug where an UPDATE (same key, newer value)
    /// followed by a flush+restart returned the OLD value: without dedup the
    /// segment held both versions and find_key picked the wrong one.
    fn dedup_keys_newest_wins(&mut self) {
        // 1. For each key, find the index of its last (newest) occurrence.
        let mut last_idx: std::collections::HashMap<u64, usize> =
            std::collections::HashMap::with_capacity(self.num_rows);
        for (i, &k) in self.keys.iter().enumerate() {
            last_idx.insert(k, i);
        }
        // 2. Build the keep-list in original order: a row is kept iff it is the
        //    newest version of its key. We DO NOT drop tombstones here — a
        //    tombstone's newest version must be preserved so read paths
        //    (is_deleted checks, newest-version-wins scans) see the deletion.
        //    Dropping them would resurrect deleted rows. Only duplicates of the
        //    SAME key (older versions) are removed.
        let keep: Vec<usize> = (0..self.num_rows)
            .filter(|&i| last_idx.get(&self.keys[i]) == Some(&i))
            .collect();
        if keep.len() == self.num_rows {
            return; // no dupes, nothing to do
        }
        // 3. Decode each kept row into Vec<Value>, then reset buffers and re-add.
        //    We re-add via add_values to reuse the existing layout logic for every
        //    column type (fixed + text). Buffer is small (in-memory), so this is
        //    cheap relative to the SSTable write.
        let col_types = self.column_types.clone();
        // (key, timestamp, deleted, row) — preserve the deleted flag so a
        // tombstone's newest version stays a tombstone after rebuild.
        let mut kept_rows: Vec<(u64, u64, bool, Vec<Value>)> = Vec::with_capacity(keep.len());
        for &i in &keep {
            let mut row = Vec::with_capacity(col_types.len());
            for (ci, tag) in self.column_tags.iter().enumerate() {
                match tag {
                    ColumnTypeTag::Integer | ColumnTypeTag::Timestamp => {
                        let buf = &self.column_buffers[ci];
                        let off = i * 8;
                        if off + 8 > buf.len() {
                            row.push(Value::Null);
                            continue;
                        }
                        // Use the authoritative NULL flag (not a value sentinel),
                        // so the real value i64::MIN round-trips correctly.
                        if self.null_flags.get(ci).and_then(|f| f.get(i)) == Some(&true) {
                            row.push(Value::Null);
                            continue;
                        }
                        let val = i64::from_le_bytes([
                            buf[off],
                            buf[off + 1],
                            buf[off + 2],
                            buf[off + 3],
                            buf[off + 4],
                            buf[off + 5],
                            buf[off + 6],
                            buf[off + 7],
                        ]);
                        if matches!(tag, ColumnTypeTag::Timestamp) {
                            row.push(Value::Timestamp(crate::types::Timestamp::from_micros(val)));
                        } else {
                            row.push(Value::Integer(val));
                        }
                    }
                    ColumnTypeTag::Float => {
                        let buf = &self.column_buffers[ci];
                        let off = i * 8;
                        if off + 8 > buf.len() {
                            row.push(Value::Null);
                            continue;
                        }
                        // Authoritative NULL flag (not the NaN sentinel), so a
                        // stored NaN round-trips as Float(NaN) rather than Null.
                        if self.null_flags.get(ci).and_then(|f| f.get(i)) == Some(&true) {
                            row.push(Value::Null);
                            continue;
                        }
                        let bits = u64::from_le_bytes([
                            buf[off],
                            buf[off + 1],
                            buf[off + 2],
                            buf[off + 3],
                            buf[off + 4],
                            buf[off + 5],
                            buf[off + 6],
                            buf[off + 7],
                        ]);
                        row.push(Value::Float(f64::from_bits(bits)));
                    }
                    ColumnTypeTag::Bool => {
                        let buf = &self.column_buffers[ci];
                        // Authoritative NULL flag; Bool has no value sentinel.
                        if self.null_flags.get(ci).and_then(|f| f.get(i)) == Some(&true) {
                            row.push(Value::Null);
                            continue;
                        }
                        row.push(Value::Bool(buf.get(i).copied().unwrap_or(0) != 0));
                    }
                    ColumnTypeTag::Text => {
                        // Text layout: each row = [u16 len][bytes], concatenated.
                        // 0xFFFF len = NULL sentinel.
                        let buf = &self.column_buffers[ci];
                        let mut p = 0usize;
                        let mut r = 0usize;
                        let mut found = None;
                        while p + 2 <= buf.len() {
                            let len = u16::from_le_bytes([buf[p], buf[p + 1]]) as usize;
                            p += 2;
                            if r == i {
                                if len == 0xFFFF {
                                    found = Some(Value::Null);
                                } else if p + len <= buf.len() {
                                    found = Some(Value::text(
                                        String::from_utf8_lossy(&buf[p..p + len]).into_owned(),
                                    ));
                                } else {
                                    found = Some(Value::Null);
                                }
                                break;
                            }
                            p += if len == 0xFFFF { 0 } else { len };
                            r += 1;
                        }
                        row.push(found.unwrap_or(Value::Null));
                    }
                    ColumnTypeTag::Vector => {
                        // Vector layout: [dim:u16][f32×dim] per row, concatenated.
                        let buf = &self.column_buffers[ci];
                        let mut p = 0usize;
                        let mut r = 0usize;
                        let mut found = None;
                        while p + 2 <= buf.len() {
                            let dim = u16::from_le_bytes([buf[p], buf[p + 1]]) as usize;
                            p += 2;
                            if r == i {
                                if dim == 0 {
                                    found = Some(Value::Null);
                                } else if p + dim * 4 <= buf.len() {
                                    let mut v = Vec::with_capacity(dim);
                                    for j in 0..dim {
                                        let off = p + j * 4;
                                        v.push(f32::from_le_bytes([
                                            buf[off],
                                            buf[off + 1],
                                            buf[off + 2],
                                            buf[off + 3],
                                        ]));
                                    }
                                    found = Some(Value::Vector(crate::types::ArcVec(
                                        std::sync::Arc::new(v),
                                    )));
                                } else {
                                    found = Some(Value::Null);
                                }
                                break;
                            }
                            p += dim * 4;
                            r += 1;
                        }
                        row.push(found.unwrap_or(Value::Null));
                    }
                    ColumnTypeTag::Spatial => {
                        // Spatial layout: [len:u16][bincode(Geometry)] per row.
                        let buf = &self.column_buffers[ci];
                        let mut p = 0usize;
                        let mut r = 0usize;
                        let mut found = None;
                        while p + 2 <= buf.len() {
                            let len = u16::from_le_bytes([buf[p], buf[p + 1]]) as usize;
                            p += 2;
                            if r == i {
                                if len == 0 || p + len > buf.len() {
                                    found = Some(Value::Null);
                                } else {
                                    match bincode::deserialize::<crate::types::Geometry>(
                                        &buf[p..p + len],
                                    ) {
                                        Ok(g) => {
                                            found = Some(Value::Spatial(std::boxed::Box::new(g)))
                                        }
                                        Err(_) => found = Some(Value::Null),
                                    }
                                }
                                break;
                            }
                            p += len;
                            r += 1;
                        }
                        row.push(found.unwrap_or(Value::Null));
                    }
                };
            }
            kept_rows.push((self.keys[i], self.timestamps[i], self.deleted[i], row));
        }
        // 4. Reset buffers and re-add the deduplicated rows.
        self.keys.clear();
        self.timestamps.clear();
        self.deleted.clear();
        for b in self.column_buffers.iter_mut() {
            b.clear();
        }
        for f in self.null_flags.iter_mut() {
            f.clear();
        }
        self.num_rows = 0;
        for (key, ts, deleted, row) in kept_rows {
            // Re-add using the SAME encoding + the preserved deleted flag. A
            // tombstone's newest version must be re-added with deleted=true so
            // read paths (is_deleted, newest-version-wins) still see the deletion.
            let _ = self.add_values(key, ts, deleted, &row);
        }
    }

    pub fn finish_and_reset(&mut self) -> Result<()> {
        if self.finished {
            return Ok(());
        }
        if self.num_rows == 0 {
            return Ok(());
        }

        // 🔑 Dedup same-key rows BEFORE writing (newest-version-wins). An UPDATE
        // appends a newer row with the SAME composite key; if both versions are
        // written to the SSTable, find_key() binary search returns an arbitrary
        // one (often the older), so reads after a flush/restart see stale data.
        // Keep only the LAST occurrence of each key (rows are in append order =
        // old→new, so last is newest). Tombstones count as a version too — if a
        // key's newest version is a tombstone, the key is dropped entirely here
        // (no live row), which is correct for a flushed segment.
        if self.num_rows > 1 {
            self.dedup_keys_newest_wins();
        }
        let num_rows = self.num_rows;
        if num_rows == 0 {
            return Ok(());
        }
        let num_cols = self.column_tags.len();

        // Build column segments with null bitmaps
        let mut segments: Vec<Vec<u8>> = Vec::with_capacity(num_cols);
        for col_idx in 0..num_cols {
            let tag = &self.column_tags[col_idx];
            let raw = &self.column_buffers[col_idx];
            let null_bytes = num_rows.div_ceil(8);
            let mut seg = Vec::with_capacity(null_bytes + raw.len());

            if tag.is_fixed() {
                // Fixed segment: [null_bitmap] [data]
                let mut nulls = vec![0u8; null_bytes];
                let elem_size = tag.fixed_size();
                // NULL bitmap is authoritative (tracked at encode time). This
                // avoids the i64::MIN / f64::NAN value-sentinel collision, so
                // those exact values can be stored without being mistaken for NULL.
                let _ = elem_size;
                let null_flags = &self.null_flags[col_idx];
                for row_idx in 0..num_rows {
                    if row_idx < null_flags.len() && null_flags[row_idx] {
                        nulls[row_idx / 8] |= 1 << (row_idx % 8);
                    }
                }
                seg.extend_from_slice(&nulls);
                seg.extend_from_slice(raw);
            } else if matches!(tag, ColumnTypeTag::Text) {
                // Text segment: [null_bitmap] [offsets] [string_data]
                // Raw buffer format: [(len: u16 LE, bytes)] repeated.
                // Use the authoritative null_flags (tracked at add_values time)
                // rather than the 0xFFFF in-band sentinel, so NULLs are preserved
                // regardless of how the raw bytes were encoded (dedup re-add,
                // raw merge, etc.).
                let mut nulls = vec![0u8; null_bytes];
                let mut offsets = Vec::with_capacity((num_rows + 1) * 4);
                let mut str_data = Vec::new();
                let mut current_offset = 0u32;
                let null_flags = &self.null_flags[col_idx];

                let mut pos = 0usize;
                for row_idx in 0..num_rows {
                    if pos + 2 > raw.len() {
                        break;
                    }
                    let len = u16::from_le_bytes([raw[pos], raw[pos + 1]]) as usize;
                    pos += 2;
                    let is_null =
                        null_flags.get(row_idx).copied().unwrap_or(false) || len == 0xFFFF; // also catch legacy sentinel bytes
                    if is_null {
                        nulls[row_idx / 8] |= 1 << (row_idx % 8);
                        offsets.push(current_offset);
                        // NULL rows have no string data; skip their bytes (len
                        // is 0xFFFF sentinel or 0 for an empty-but-flagged NULL).
                        if len != 0xFFFF {
                            pos += len;
                        }
                        continue;
                    }
                    offsets.push(current_offset);
                    if pos + len <= raw.len() {
                        str_data.extend_from_slice(&raw[pos..pos + len]);
                        current_offset += len as u32;
                    }
                    pos += len;
                }
                offsets.push(current_offset);

                seg.extend_from_slice(&nulls);
                for off in &offsets {
                    seg.extend_from_slice(&off.to_le_bytes());
                }
                seg.extend_from_slice(&str_data);
            } else if matches!(tag, ColumnTypeTag::Vector) {
                // Vector segment: [null_bitmap][dim:u16][f32×dim per row].
                // The raw buffer holds [dim:u16][f32×dim] per row (from
                // add_values). Re-pack to a uniform dim so read_vectors can
                // use a fixed stride. Missing/NULL rows get zero-filled.
                let mut nulls = vec![0u8; null_bytes];
                // First pass: determine the column's dimension (max over rows).
                let mut col_dim: usize = 0;
                let mut row_dims: Vec<usize> = Vec::with_capacity(num_rows);
                {
                    let mut pos = 0usize;
                    for row_idx in 0..num_rows {
                        if pos + 2 > raw.len() {
                            row_dims.push(0);
                            continue;
                        }
                        let d = u16::from_le_bytes([raw[pos], raw[pos + 1]]) as usize;
                        if d == 0 {
                            nulls[row_idx / 8] |= 1 << (row_idx % 8);
                            row_dims.push(0);
                        } else {
                            if d > col_dim {
                                col_dim = d;
                            }
                            row_dims.push(d);
                        }
                        pos += 2 + d * 4;
                    }
                }
                seg.extend_from_slice(&nulls);
                seg.extend_from_slice(&(col_dim as u16).to_le_bytes());
                // Second pass: emit col_dim f32 per row (pad shorter/missing).
                let mut pos = 0usize;
                for row_idx in 0..num_rows {
                    let d = row_dims[row_idx];
                    let mut vals = vec![0f32; col_dim];
                    if d > 0 && pos + 2 <= raw.len() {
                        // raw[pos..pos+2] is dim (already read); data follows.
                        let base = pos + 2;
                        for j in 0..d.min(col_dim) {
                            let off = base + j * 4;
                            if off + 4 <= raw.len() {
                                vals[j] = f32::from_le_bytes([
                                    raw[off],
                                    raw[off + 1],
                                    raw[off + 2],
                                    raw[off + 3],
                                ]);
                            }
                        }
                    }
                    for v in &vals {
                        seg.extend_from_slice(&v.to_le_bytes());
                    }
                    pos += 2 + d * 4;
                }
            } else {
                // Spatial (and any other variable column): [null_bitmap]
                // then [len:u16][bytes] per row. The raw buffer already holds
                // [len:u16][bytes] per row from add_values; copy as-is.
                let nulls = vec![0u8; null_bytes];
                seg.extend_from_slice(&nulls);
                seg.extend_from_slice(raw);
            }

            segments.push(seg);
        }

        // Build row map
        let (rm_size, _, _, _deleted_len) = RowMap::compute_sizes(num_rows);
        let mut row_map = vec![0u8; rm_size];

        // Keys
        for (i, k) in self.keys.iter().enumerate() {
            let off = i * 8;
            row_map[off..off + 8].copy_from_slice(&k.to_le_bytes());
        }
        // Timestamps
        let ts_off = num_rows * 8;
        for (i, ts) in self.timestamps.iter().enumerate() {
            let off = ts_off + i * 8;
            row_map[off..off + 8].copy_from_slice(&ts.to_le_bytes());
        }
        // Deleted bitset
        let del_off = num_rows * 16;
        for (i, d) in self.deleted.iter().enumerate() {
            if *d {
                row_map[del_off + i / 8] |= 1 << (i % 8);
            }
        }

        // Build entire file in memory, then write atomically.
        // Avoids BufWriter + mmap interaction issues on macOS for small files.
        let ci_offset = HEADER_SIZE as u64;
        let ci_size = num_cols * COLUMN_INDEX_ENTRY_SIZE;
        let segments_start = HEADER_SIZE + ci_size;
        // Compress segments + compute column index entries
        let mut compressed_segs: Vec<Vec<u8>> = Vec::with_capacity(num_cols);
        let mut column_entries = Vec::with_capacity(num_cols);
        let mut current_offset = segments_start as u64;
        for seg in &segments {
            // Try Snappy compression — only use if it saves space.
            // Compression reduces file size → less file_data heap memory on read.
            let compressed = snap::raw::Encoder::new()
                .compress_vec(seg)
                .unwrap_or_else(|_| seg.clone());
            let seg_data: Vec<u8> = if compressed.len() + 1 < seg.len() {
                let mut out = Vec::with_capacity(1 + compressed.len());
                out.push(1u8); // flag: Snappy compressed
                out.extend_from_slice(&compressed);
                out
            } else {
                let mut out = Vec::with_capacity(1 + seg.len());
                out.push(0u8); // flag: uncompressed
                out.extend_from_slice(seg);
                out
            };
            let size = seg_data.len() as u64;
            column_entries.push(ColumnIndexEntry {
                offset: current_offset,
                size,
            });
            current_offset += size;
            compressed_segs.push(seg_data);
        }
        let row_map_offset = current_offset;

        // Pre-compute total size and allocate buffer
        let total_size = row_map_offset as usize + row_map.len() + FOOTER_SIZE;
        let mut buf = Vec::with_capacity(total_size);

        // Header
        let mut header_tags = [0u8; MAX_COLUMNS];
        for (i, tag) in self.column_tags.iter().enumerate() {
            header_tags[i] = *tag as u8;
        }
        let header = ColumnarHeader {
            num_rows: num_rows as u32,
            num_columns: num_cols as u16,
            column_tags: header_tags,
        };
        buf.extend_from_slice(&header.serialize());

        // Column index
        for entry in &column_entries {
            buf.extend_from_slice(&entry.offset.to_le_bytes());
            buf.extend_from_slice(&entry.size.to_le_bytes());
        }

        // Column segments (compressed)
        for seg in &compressed_segs {
            buf.extend_from_slice(seg);
        }

        // Row map
        buf.extend_from_slice(&row_map);

        // Footer
        let mut footer = [0u8; FOOTER_SIZE];
        footer[0..8].copy_from_slice(&ci_offset.to_le_bytes());
        footer[8..16].copy_from_slice(&row_map_offset.to_le_bytes());
        footer[16..20].copy_from_slice(&COLUMNAR_MAGIC.to_le_bytes());
        buf.extend_from_slice(&footer);

        // Atomic publish via temp-file + rename: write the full buffer to a
        // sibling temp file, fsync it, then atomically rename onto the final
        // path. This guarantees that any concurrent reader (mmap-based) either
        // sees the previous complete file or the new complete file — never a
        // half-written one (which was the root cause of the mmap index-out-of-
        // bounds panics on repeated finalize). Rename also lets us drop the
        // per-finalize fsync of the *live* file: durability is provided by the
        // WAL checkpoint path, and the temp-file fsync is what makes rename
        // crash-safe on POSIX.
        let final_path = self.path.clone();
        let dir = final_path
            .parent()
            .unwrap_or_else(|| std::path::Path::new("."));
        let tmp_path = dir.join(format!(
            ".{}.tmp",
            final_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("col.tmp")
        ));
        let file = OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(&tmp_path)?;
        let mut writer = BufWriter::new(file);
        writer.write_all(&buf)?;
        writer.flush()?;
        // fsync the temp file so the rename is durable across crashes.
        writer.get_ref().sync_all()?;
        drop(writer);
        // Atomic publish. On POSIX, rename guarantees readers see the new file
        // in its entirety once the syscall returns.
        std::fs::rename(&tmp_path, &final_path)?;

        // Success: clear internal state (data is now on disk)
        // Reset finished to false so the builder can be reused for new data.
        self.finished = false;
        self.num_rows = 0;
        self.keys.clear();
        self.timestamps.clear();
        self.deleted.clear();
        self.column_buffers = vec![Vec::new(); num_cols];

        Ok(())
    }
}

// ── Tests ──────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::Value;
    use tempfile::TempDir;

    fn make_test_row(id: i64, name: &str, amount: f64, region: &str) -> Vec<Value> {
        vec![
            Value::Integer(id),
            Value::Text(crate::types::ArcString(std::sync::Arc::from(name))),
            Value::Float(amount),
            Value::Text(crate::types::ArcString(std::sync::Arc::from(region))),
        ]
    }

    #[test]
    #[cfg_attr(
        target_os = "macos",
        ignore = "macOS mmap coherence issue with files < page size"
    )]
    fn test_columnar_build_and_read() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.col.sst");

        let col_types = vec![
            ColumnType::Integer,
            ColumnType::Text,
            ColumnType::Float,
            ColumnType::Text,
        ];

        // Build
        let mut builder = ColumnarSSTableBuilder::new(&path, col_types.clone());
        let rows = vec![
            (1u64, 100u64, false, make_test_row(1, "Alice", 99.5, "US")),
            (2, 101, false, make_test_row(2, "Bob", 50.0, "EU")),
            (3, 102, false, make_test_row(3, "Carol", 75.0, "US")),
            (4, 103, true, make_test_row(4, "Dave", 0.0, "EU")), // deleted
        ];

        for (key, ts, del, row) in &rows {
            let encoded = crate::storage::row_format::encode(row, &col_types).unwrap();
            builder.add_row(*key, *ts, *del, &encoded).unwrap();
        }
        builder.finish().unwrap();

        // Verify file exists and is recognized
        assert!(ColumnarSSTable::is_columnar(&path));

        // Read
        let col_sst = ColumnarSSTable::open(&path).unwrap();
        assert_eq!(col_sst.num_rows, 4);
        assert_eq!(col_sst.column_tags.len(), 4);

        // Check row map
        assert_eq!(col_sst.row_map.key(0), 1);
        assert_eq!(col_sst.row_map.key(3), 4);
        assert!(!col_sst.row_map.is_deleted(0));
        assert!(col_sst.row_map.is_deleted(3));

        // Read id column (fixed i64)
        let id_seg = col_sst.read_fixed_i64(0).unwrap();
        assert_eq!(id_seg.get_i64(0), Some(1));
        assert_eq!(id_seg.get_i64(1), Some(2));
        assert_eq!(id_seg.get_i64(2), Some(3));
        assert_eq!(id_seg.get_i64(3), Some(4));

        // Read amount column (fixed f64)
        let amt_seg = col_sst.read_fixed_f64(2).unwrap();
        assert_eq!(amt_seg.get_f64(0), Some(99.5));
        assert_eq!(amt_seg.get_f64(1), Some(50.0));

        // Read region column (text)
        let reg_seg = col_sst.read_text(3).unwrap();
        assert_eq!(reg_seg.get_str(0), Some("US"));
        assert_eq!(reg_seg.get_str(1), Some("EU"));
        assert_eq!(reg_seg.get_str(2), Some("US"));
        assert_eq!(reg_seg.get_str(3), Some("EU"));
    }

    #[test]
    fn test_columnar_roundtrip_300k() {
        // Test with a larger dataset to verify no data corruption
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("large.col.sst");

        let col_types = vec![
            ColumnType::Integer,
            ColumnType::Text,
            ColumnType::Float,
            ColumnType::Text,
        ];

        let n = 10000;
        let mut builder = ColumnarSSTableBuilder::new(&path, col_types.clone());

        for i in 0..n {
            let region = if i % 3 == 0 { "US" } else { "EU" };
            let row = vec![
                Value::Integer(i as i64),
                Value::Text(crate::types::ArcString(std::sync::Arc::from(format!(
                    "cust_{}",
                    i % 100
                )))),
                Value::Float(i as f64 * 1.5),
                Value::Text(crate::types::ArcString(std::sync::Arc::from(region))),
            ];
            let encoded = crate::storage::row_format::encode(&row, &col_types).unwrap();
            builder
                .add_row(i as u64, i as u64 + 1000, i % 7 == 0, &encoded)
                .unwrap();
        }
        builder.finish().unwrap();

        let col_sst = ColumnarSSTable::open(&path).unwrap();
        assert_eq!(col_sst.num_rows, n as usize);

        // Spot-check
        let id_seg = col_sst.read_fixed_i64(0).unwrap();
        let amt_seg = col_sst.read_fixed_f64(2).unwrap();
        let reg_seg = col_sst.read_text(3).unwrap();

        for i in 0..n {
            assert_eq!(id_seg.get_i64(i), Some(i as i64), "id mismatch at {}", i);
            let expected_amt = i as f64 * 1.5;
            let got_amt = amt_seg.get_f64(i).unwrap();
            assert!(
                (got_amt - expected_amt).abs() < 0.001,
                "amount mismatch at {}",
                i
            );
            let expected_reg = if i % 3 == 0 { "US" } else { "EU" };
            assert_eq!(
                reg_seg.get_str(i),
                Some(expected_reg),
                "region mismatch at {}",
                i
            );
        }
    }
}