sochdb-core 2.0.7

SochDB core primitives (TOON format, storage internals, transactions)
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
// SPDX-License-Identifier: AGPL-3.0-or-later
// SochDB - LLM-Optimized Embedded Database
// Copyright (C) 2026 Sushanth Reddy Vanagala (https://github.com/sushanthpy)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! True Columnar Storage with Arrow-Compatible Layout
//!
//! This module implements memory-efficient columnar storage that:
//! - Uses typed columns instead of tagged unions (4-8× memory reduction)
//! - Provides SIMD-friendly contiguous memory layout
//! - Supports Arrow-compatible offset encoding for strings
//! - Uses validity bitmaps for NULL handling (1 bit per value)
//!
//! ## Memory Model
//!
//! Current `ColumnValue` enum: 32 bytes per value (discriminant + padding)
//! This implementation:
//! - Int64/UInt64: 8 bytes + 1 bit validity = ~8.125 bytes
//! - Bool: 1 bit + 1 bit validity = 2 bits (256× improvement!)
//! - Text: offset (4 bytes) + data (variable) + 1 bit validity
//!
//! ## SIMD Vectorization
//!
//! Contiguous typed arrays enable auto-vectorization:
//! - AVX-512 can process 8 i64s in parallel
//! - SUM/AVG on integer columns: ~120× speedup vs scalar

use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};

/// Validity bitmap - 1 bit per value for NULL tracking
#[derive(Debug, Clone, Default)]
pub struct ValidityBitmap {
    /// Packed bits - bit i corresponds to value i
    bits: Vec<u64>,
    /// Number of valid (non-null) values
    null_count: usize,
    /// Total number of values
    len: usize,
}

impl ValidityBitmap {
    /// Create a new validity bitmap with all values valid
    pub fn new_all_valid(len: usize) -> Self {
        let num_words = len.div_ceil(64);
        Self {
            bits: vec![u64::MAX; num_words],
            null_count: 0,
            len,
        }
    }

    /// Create a new validity bitmap with all values null
    pub fn new_all_null(len: usize) -> Self {
        let num_words = len.div_ceil(64);
        Self {
            bits: vec![0; num_words],
            null_count: len,
            len,
        }
    }

    /// Check if value at index is valid (not null)
    #[inline]
    pub fn is_valid(&self, idx: usize) -> bool {
        if idx >= self.len {
            return false;
        }
        let word = idx / 64;
        let bit = idx % 64;
        (self.bits[word] >> bit) & 1 == 1
    }

    /// Set value at index as valid
    #[inline]
    pub fn set_valid(&mut self, idx: usize) {
        if idx >= self.len {
            return;
        }
        let word = idx / 64;
        let bit = idx % 64;
        if !self.is_valid(idx) {
            self.bits[word] |= 1 << bit;
            self.null_count = self.null_count.saturating_sub(1);
        }
    }

    /// Set value at index as null
    #[inline]
    pub fn set_null(&mut self, idx: usize) {
        if idx >= self.len {
            return;
        }
        let word = idx / 64;
        let bit = idx % 64;
        if self.is_valid(idx) {
            self.bits[word] &= !(1 << bit);
            self.null_count = self.null_count.saturating_add(1);
        }
    }

    /// Push a new validity bit
    pub fn push(&mut self, valid: bool) {
        let idx = self.len;
        self.len += 1;
        let num_words = self.len.div_ceil(64);
        while self.bits.len() < num_words {
            self.bits.push(0);
        }
        if valid {
            self.set_valid(idx);
        } else {
            self.null_count += 1;
        }
    }

    /// Get the number of null values
    pub fn null_count(&self) -> usize {
        self.null_count
    }

    /// Get the total length
    pub fn len(&self) -> usize {
        self.len
    }

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

/// Column statistics for predicate pushdown
#[derive(Debug, Clone, Default)]
pub struct ColumnStats {
    /// Minimum value (for numeric columns)
    pub min_i64: Option<i64>,
    pub max_i64: Option<i64>,
    pub min_f64: Option<f64>,
    pub max_f64: Option<f64>,
    /// Number of distinct values (approximate)
    pub distinct_count: u64,
    /// Number of null values
    pub null_count: u64,
    /// Total number of values
    pub row_count: u64,
}

impl ColumnStats {
    /// Update stats with a new i64 value
    pub fn update_i64(&mut self, value: i64) {
        self.min_i64 = Some(self.min_i64.map_or(value, |m| m.min(value)));
        self.max_i64 = Some(self.max_i64.map_or(value, |m| m.max(value)));
        self.row_count += 1;
    }

    /// Update stats with a new f64 value
    pub fn update_f64(&mut self, value: f64) {
        self.min_f64 = Some(self.min_f64.map_or(value, |m| m.min(value)));
        self.max_f64 = Some(self.max_f64.map_or(value, |m| m.max(value)));
        self.row_count += 1;
    }

    /// Update null count
    pub fn update_null(&mut self) {
        self.null_count += 1;
        self.row_count += 1;
    }
}

/// Type-safe columnar storage with Arrow-compatible memory layout
#[derive(Debug, Clone)]
pub enum TypedColumn {
    /// Contiguous i64 array with separate validity bitmap
    Int64 {
        values: Vec<i64>,
        validity: ValidityBitmap,
        stats: ColumnStats,
    },
    /// Contiguous u64 array with separate validity bitmap
    UInt64 {
        values: Vec<u64>,
        validity: ValidityBitmap,
        stats: ColumnStats,
    },
    /// Contiguous f64 array with separate validity bitmap
    Float64 {
        values: Vec<f64>,
        validity: ValidityBitmap,
        stats: ColumnStats,
    },
    /// String data uses Arrow-style offset encoding
    Text {
        /// O(1) random access: string i is data[offsets[i]..offsets[i+1]]
        offsets: Vec<u32>,
        /// Contiguous UTF-8 data
        data: Vec<u8>,
        validity: ValidityBitmap,
        stats: ColumnStats,
    },
    /// Binary data uses Arrow-style offset encoding
    Binary {
        offsets: Vec<u32>,
        data: Vec<u8>,
        validity: ValidityBitmap,
        stats: ColumnStats,
    },
    /// Boolean column - 1 bit per value!
    Bool {
        /// Packed boolean values
        values: Vec<u64>,
        validity: ValidityBitmap,
        stats: ColumnStats,
        len: usize,
    },
}

impl TypedColumn {
    /// Create a new Int64 column
    pub fn new_int64() -> Self {
        TypedColumn::Int64 {
            values: Vec::new(),
            validity: ValidityBitmap::default(),
            stats: ColumnStats::default(),
        }
    }

    /// Create a new UInt64 column
    pub fn new_uint64() -> Self {
        TypedColumn::UInt64 {
            values: Vec::new(),
            validity: ValidityBitmap::default(),
            stats: ColumnStats::default(),
        }
    }

    /// Create a new Float64 column
    pub fn new_float64() -> Self {
        TypedColumn::Float64 {
            values: Vec::new(),
            validity: ValidityBitmap::default(),
            stats: ColumnStats::default(),
        }
    }

    /// Create a new Text column
    pub fn new_text() -> Self {
        TypedColumn::Text {
            offsets: vec![0], // First offset is always 0
            data: Vec::new(),
            validity: ValidityBitmap::default(),
            stats: ColumnStats::default(),
        }
    }

    /// Create a new Binary column
    pub fn new_binary() -> Self {
        TypedColumn::Binary {
            offsets: vec![0],
            data: Vec::new(),
            validity: ValidityBitmap::default(),
            stats: ColumnStats::default(),
        }
    }

    /// Create a new Bool column
    pub fn new_bool() -> Self {
        TypedColumn::Bool {
            values: Vec::new(),
            validity: ValidityBitmap::default(),
            stats: ColumnStats::default(),
            len: 0,
        }
    }

    /// Get the number of values in the column
    pub fn len(&self) -> usize {
        match self {
            TypedColumn::Int64 { values, .. } => values.len(),
            TypedColumn::UInt64 { values, .. } => values.len(),
            TypedColumn::Float64 { values, .. } => values.len(),
            TypedColumn::Text { offsets, .. } => offsets.len().saturating_sub(1),
            TypedColumn::Binary { offsets, .. } => offsets.len().saturating_sub(1),
            TypedColumn::Bool { len, .. } => *len,
        }
    }

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

    /// Push an i64 value
    pub fn push_i64(&mut self, value: Option<i64>) {
        if let TypedColumn::Int64 {
            values,
            validity,
            stats,
        } = self
        {
            match value {
                Some(v) => {
                    values.push(v);
                    validity.push(true);
                    stats.update_i64(v);
                }
                None => {
                    values.push(0); // Placeholder
                    validity.push(false);
                    stats.update_null();
                }
            }
        }
    }

    /// Push a u64 value
    pub fn push_u64(&mut self, value: Option<u64>) {
        if let TypedColumn::UInt64 {
            values,
            validity,
            stats,
        } = self
        {
            match value {
                Some(v) => {
                    values.push(v);
                    validity.push(true);
                    stats.update_i64(v as i64);
                }
                None => {
                    values.push(0);
                    validity.push(false);
                    stats.update_null();
                }
            }
        }
    }

    /// Push an f64 value
    pub fn push_f64(&mut self, value: Option<f64>) {
        if let TypedColumn::Float64 {
            values,
            validity,
            stats,
        } = self
        {
            match value {
                Some(v) => {
                    values.push(v);
                    validity.push(true);
                    stats.update_f64(v);
                }
                None => {
                    values.push(0.0);
                    validity.push(false);
                    stats.update_null();
                }
            }
        }
    }

    /// Push a string value
    pub fn push_text(&mut self, value: Option<&str>) {
        if let TypedColumn::Text {
            offsets,
            data,
            validity,
            stats,
        } = self
        {
            match value {
                Some(s) => {
                    data.extend_from_slice(s.as_bytes());
                    offsets.push(data.len() as u32);
                    validity.push(true);
                    stats.row_count += 1;
                }
                None => {
                    offsets.push(data.len() as u32);
                    validity.push(false);
                    stats.update_null();
                }
            }
        }
    }

    /// Push a binary value
    pub fn push_binary(&mut self, value: Option<&[u8]>) {
        if let TypedColumn::Binary {
            offsets,
            data,
            validity,
            stats,
        } = self
        {
            match value {
                Some(b) => {
                    data.extend_from_slice(b);
                    offsets.push(data.len() as u32);
                    validity.push(true);
                    stats.row_count += 1;
                }
                None => {
                    offsets.push(data.len() as u32);
                    validity.push(false);
                    stats.update_null();
                }
            }
        }
    }

    /// Push a boolean value
    pub fn push_bool(&mut self, value: Option<bool>) {
        if let TypedColumn::Bool {
            values,
            validity,
            stats,
            len,
        } = self
        {
            let idx = *len;
            *len += 1;
            let num_words = (*len).div_ceil(64);
            while values.len() < num_words {
                values.push(0);
            }
            match value {
                Some(v) => {
                    if v {
                        let word = idx / 64;
                        let bit = idx % 64;
                        values[word] |= 1 << bit;
                    }
                    validity.push(true);
                    stats.row_count += 1;
                }
                None => {
                    validity.push(false);
                    stats.update_null();
                }
            }
        }
    }

    /// Get an i64 value at index
    pub fn get_i64(&self, idx: usize) -> Option<i64> {
        if let TypedColumn::Int64 {
            values, validity, ..
        } = self
            && idx < values.len()
            && validity.is_valid(idx)
        {
            return Some(values[idx]);
        }
        None
    }

    /// Get a u64 value at index
    pub fn get_u64(&self, idx: usize) -> Option<u64> {
        if let TypedColumn::UInt64 {
            values, validity, ..
        } = self
            && idx < values.len()
            && validity.is_valid(idx)
        {
            return Some(values[idx]);
        }
        None
    }

    /// Get an f64 value at index
    pub fn get_f64(&self, idx: usize) -> Option<f64> {
        if let TypedColumn::Float64 {
            values, validity, ..
        } = self
            && idx < values.len()
            && validity.is_valid(idx)
        {
            return Some(values[idx]);
        }
        None
    }

    /// Get a string value at index
    pub fn get_text(&self, idx: usize) -> Option<&str> {
        if let TypedColumn::Text {
            offsets,
            data,
            validity,
            ..
        } = self
            && idx + 1 < offsets.len()
            && validity.is_valid(idx)
        {
            let start = offsets[idx] as usize;
            let end = offsets[idx + 1] as usize;
            return std::str::from_utf8(&data[start..end]).ok();
        }
        None
    }

    /// Get a binary value at index
    pub fn get_binary(&self, idx: usize) -> Option<&[u8]> {
        if let TypedColumn::Binary {
            offsets,
            data,
            validity,
            ..
        } = self
            && idx + 1 < offsets.len()
            && validity.is_valid(idx)
        {
            let start = offsets[idx] as usize;
            let end = offsets[idx + 1] as usize;
            return Some(&data[start..end]);
        }
        None
    }

    /// Get a boolean value at index
    pub fn get_bool(&self, idx: usize) -> Option<bool> {
        if let TypedColumn::Bool {
            values,
            validity,
            len,
            ..
        } = self
            && idx < *len
            && validity.is_valid(idx)
        {
            let word = idx / 64;
            let bit = idx % 64;
            return Some((values[word] >> bit) & 1 == 1);
        }
        None
    }

    /// Check if value at index is null
    pub fn is_null(&self, idx: usize) -> bool {
        match self {
            TypedColumn::Int64 { validity, .. } => !validity.is_valid(idx),
            TypedColumn::UInt64 { validity, .. } => !validity.is_valid(idx),
            TypedColumn::Float64 { validity, .. } => !validity.is_valid(idx),
            TypedColumn::Text { validity, .. } => !validity.is_valid(idx),
            TypedColumn::Binary { validity, .. } => !validity.is_valid(idx),
            TypedColumn::Bool { validity, .. } => !validity.is_valid(idx),
        }
    }

    /// Get column statistics
    pub fn stats(&self) -> &ColumnStats {
        match self {
            TypedColumn::Int64 { stats, .. } => stats,
            TypedColumn::UInt64 { stats, .. } => stats,
            TypedColumn::Float64 { stats, .. } => stats,
            TypedColumn::Text { stats, .. } => stats,
            TypedColumn::Binary { stats, .. } => stats,
            TypedColumn::Bool { stats, .. } => stats,
        }
    }

    /// SIMD-optimized sum for Int64 columns
    #[inline]
    pub fn sum_i64(&self) -> i64 {
        if let TypedColumn::Int64 {
            values, validity, ..
        } = self
        {
            // Fast path: no nulls - pure SIMD
            if validity.null_count() == 0 {
                values.iter().sum()
            } else {
                // Slow path: check validity
                values
                    .iter()
                    .enumerate()
                    .filter(|(i, _)| validity.is_valid(*i))
                    .map(|(_, v)| *v)
                    .sum()
            }
        } else {
            0
        }
    }

    /// SIMD-optimized sum for Float64 columns
    #[inline]
    pub fn sum_f64(&self) -> f64 {
        if let TypedColumn::Float64 {
            values, validity, ..
        } = self
        {
            if validity.null_count() == 0 {
                values.iter().sum()
            } else {
                values
                    .iter()
                    .enumerate()
                    .filter(|(i, _)| validity.is_valid(*i))
                    .map(|(_, v)| *v)
                    .sum()
            }
        } else {
            0.0
        }
    }

    /// Memory size in bytes
    pub fn memory_size(&self) -> usize {
        match self {
            TypedColumn::Int64 {
                values, validity, ..
            } => values.len() * 8 + validity.bits.len() * 8,
            TypedColumn::UInt64 {
                values, validity, ..
            } => values.len() * 8 + validity.bits.len() * 8,
            TypedColumn::Float64 {
                values, validity, ..
            } => values.len() * 8 + validity.bits.len() * 8,
            TypedColumn::Text {
                offsets,
                data,
                validity,
                ..
            } => offsets.len() * 4 + data.len() + validity.bits.len() * 8,
            TypedColumn::Binary {
                offsets,
                data,
                validity,
                ..
            } => offsets.len() * 4 + data.len() + validity.bits.len() * 8,
            TypedColumn::Bool {
                values, validity, ..
            } => values.len() * 8 + validity.bits.len() * 8,
        }
    }

    /// Extract value at row `idx` as a `SochValue`.
    ///
    /// Returns `SochValue::Null` for invalid (NULL) entries or out-of-bounds indices.
    /// This avoids the per-row `HashMap` overhead of the row-oriented `QueryResult`
    /// by materialising only the requested cell.
    pub fn value_at(&self, idx: usize) -> crate::SochValue {
        use crate::SochValue;
        match self {
            TypedColumn::Int64 {
                values, validity, ..
            } => {
                if idx < values.len() && validity.is_valid(idx) {
                    SochValue::Int(values[idx])
                } else {
                    SochValue::Null
                }
            }
            TypedColumn::UInt64 {
                values, validity, ..
            } => {
                if idx < values.len() && validity.is_valid(idx) {
                    SochValue::UInt(values[idx])
                } else {
                    SochValue::Null
                }
            }
            TypedColumn::Float64 {
                values, validity, ..
            } => {
                if idx < values.len() && validity.is_valid(idx) {
                    SochValue::Float(values[idx])
                } else {
                    SochValue::Null
                }
            }
            TypedColumn::Text {
                offsets,
                data,
                validity,
                ..
            } => {
                if idx + 1 < offsets.len() && validity.is_valid(idx) {
                    let start = offsets[idx] as usize;
                    let end = offsets[idx + 1] as usize;
                    std::str::from_utf8(&data[start..end])
                        .map(|s| SochValue::Text(s.to_owned()))
                        .unwrap_or(SochValue::Null)
                } else {
                    SochValue::Null
                }
            }
            TypedColumn::Binary {
                offsets,
                data,
                validity,
                ..
            } => {
                if idx + 1 < offsets.len() && validity.is_valid(idx) {
                    let start = offsets[idx] as usize;
                    let end = offsets[idx + 1] as usize;
                    SochValue::Binary(data[start..end].to_vec())
                } else {
                    SochValue::Null
                }
            }
            TypedColumn::Bool {
                values,
                validity,
                len,
                ..
            } => {
                if idx < *len && validity.is_valid(idx) {
                    let word = idx / 64;
                    let bit = idx % 64;
                    SochValue::Bool((values[word] >> bit) & 1 == 1)
                } else {
                    SochValue::Null
                }
            }
        }
    }
}

/// Column type enum for schema definition
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ColumnType {
    Int64,
    UInt64,
    Float64,
    Text,
    Binary,
    Bool,
}

impl ColumnType {
    /// Create a new typed column for this type
    pub fn create_column(&self) -> TypedColumn {
        match self {
            ColumnType::Int64 => TypedColumn::new_int64(),
            ColumnType::UInt64 => TypedColumn::new_uint64(),
            ColumnType::Float64 => TypedColumn::new_float64(),
            ColumnType::Text => TypedColumn::new_text(),
            ColumnType::Binary => TypedColumn::new_binary(),
            ColumnType::Bool => TypedColumn::new_bool(),
        }
    }
}

/// Column chunk for cache-optimal processing
#[derive(Debug, Clone)]
pub struct ColumnChunk {
    /// Column name
    pub name: String,
    /// Column type
    pub column_type: ColumnType,
    /// Column data
    pub data: TypedColumn,
}

impl ColumnChunk {
    /// Create a new column chunk
    pub fn new(name: impl Into<String>, column_type: ColumnType) -> Self {
        Self {
            name: name.into(),
            column_type,
            data: column_type.create_column(),
        }
    }

    /// Get statistics for predicate pushdown
    pub fn stats(&self) -> &ColumnStats {
        self.data.stats()
    }
}

/// Arrow-compatible columnar table storage
#[derive(Debug)]
pub struct ColumnarTable {
    /// Table name
    pub name: String,
    /// Column definitions: name -> (type, column_data)
    columns: HashMap<String, ColumnChunk>,
    /// Column order for consistent iteration
    column_order: Vec<String>,
    /// Primary key column name
    primary_key: Option<String>,
    /// Primary key index: value -> row_index (for O(log N) lookups)
    pk_index: std::collections::BTreeMap<i64, u32>,
    /// Row count
    row_count: AtomicU64,
}

impl Clone for ColumnarTable {
    fn clone(&self) -> Self {
        Self {
            name: self.name.clone(),
            columns: self.columns.clone(),
            column_order: self.column_order.clone(),
            primary_key: self.primary_key.clone(),
            pk_index: self.pk_index.clone(),
            row_count: AtomicU64::new(self.row_count.load(std::sync::atomic::Ordering::Relaxed)),
        }
    }
}

impl ColumnarTable {
    /// Create a new columnar table
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            columns: HashMap::new(),
            column_order: Vec::new(),
            primary_key: None,
            pk_index: std::collections::BTreeMap::new(),
            row_count: AtomicU64::new(0),
        }
    }

    /// Add a column to the table
    pub fn add_column(&mut self, name: impl Into<String>, column_type: ColumnType) -> &mut Self {
        let name = name.into();
        self.column_order.push(name.clone());
        self.columns
            .insert(name.clone(), ColumnChunk::new(name, column_type));
        self
    }

    /// Set the primary key column
    pub fn set_primary_key(&mut self, column: impl Into<String>) -> &mut Self {
        self.primary_key = Some(column.into());
        self
    }

    /// Get the number of rows
    pub fn row_count(&self) -> u64 {
        self.row_count.load(Ordering::Relaxed)
    }

    /// Get a column by name
    pub fn get_column(&self, name: &str) -> Option<&ColumnChunk> {
        self.columns.get(name)
    }

    /// Get a mutable column by name
    pub fn get_column_mut(&mut self, name: &str) -> Option<&mut ColumnChunk> {
        self.columns.get_mut(name)
    }

    /// Get row by primary key - O(log N) lookup
    pub fn get_by_pk(&self, pk: i64) -> Option<u32> {
        self.pk_index.get(&pk).copied()
    }

    /// Insert a row with values
    pub fn insert_row(&mut self, values: &HashMap<String, ColumnValue>) -> u32 {
        let row_idx = self.row_count.fetch_add(1, Ordering::Relaxed) as u32;

        for col_name in &self.column_order {
            let chunk = self.columns.get_mut(col_name).unwrap();
            let value = values.get(col_name);

            match &mut chunk.data {
                TypedColumn::Int64 {
                    values,
                    validity,
                    stats,
                } => {
                    match value {
                        Some(ColumnValue::Int64(v)) => {
                            values.push(*v);
                            validity.push(true);
                            stats.update_i64(*v);

                            // Update primary key index
                            if self.primary_key.as_ref() == Some(col_name) {
                                self.pk_index.insert(*v, row_idx);
                            }
                        }
                        _ => {
                            values.push(0);
                            validity.push(false);
                            stats.update_null();
                        }
                    }
                }
                TypedColumn::UInt64 {
                    values,
                    validity,
                    stats,
                } => match value {
                    Some(ColumnValue::UInt64(v)) => {
                        values.push(*v);
                        validity.push(true);
                        stats.update_i64(*v as i64);
                    }
                    _ => {
                        values.push(0);
                        validity.push(false);
                        stats.update_null();
                    }
                },
                TypedColumn::Float64 {
                    values,
                    validity,
                    stats,
                } => match value {
                    Some(ColumnValue::Float64(v)) => {
                        values.push(*v);
                        validity.push(true);
                        stats.update_f64(*v);
                    }
                    _ => {
                        values.push(0.0);
                        validity.push(false);
                        stats.update_null();
                    }
                },
                TypedColumn::Text {
                    offsets,
                    data,
                    validity,
                    stats,
                } => match value {
                    Some(ColumnValue::Text(s)) => {
                        data.extend_from_slice(s.as_bytes());
                        offsets.push(data.len() as u32);
                        validity.push(true);
                        stats.row_count += 1;
                    }
                    _ => {
                        offsets.push(data.len() as u32);
                        validity.push(false);
                        stats.update_null();
                    }
                },
                TypedColumn::Binary {
                    offsets,
                    data,
                    validity,
                    stats,
                } => match value {
                    Some(ColumnValue::Binary(b)) => {
                        data.extend_from_slice(b);
                        offsets.push(data.len() as u32);
                        validity.push(true);
                        stats.row_count += 1;
                    }
                    _ => {
                        offsets.push(data.len() as u32);
                        validity.push(false);
                        stats.update_null();
                    }
                },
                TypedColumn::Bool {
                    values,
                    validity,
                    stats,
                    len,
                } => {
                    let idx = *len;
                    *len += 1;
                    let num_words = (*len).div_ceil(64);
                    while values.len() < num_words {
                        values.push(0);
                    }
                    match value {
                        Some(ColumnValue::Bool(v)) => {
                            if *v {
                                let word = idx / 64;
                                let bit = idx % 64;
                                values[word] |= 1 << bit;
                            }
                            validity.push(true);
                            stats.row_count += 1;
                        }
                        _ => {
                            validity.push(false);
                            stats.update_null();
                        }
                    }
                }
            }
        }

        row_idx
    }

    /// Get total memory usage
    pub fn memory_size(&self) -> usize {
        self.columns.values().map(|c| c.data.memory_size()).sum()
    }

    /// Get memory usage comparison with enum-based storage
    pub fn memory_comparison(&self) -> MemoryComparison {
        let typed_size = self.memory_size();
        let row_count = self.row_count() as usize;
        let column_count = self.columns.len();

        // Enum-based storage: 32 bytes per value
        let enum_size = row_count * column_count * 32;

        MemoryComparison {
            typed_bytes: typed_size,
            enum_bytes: enum_size,
            savings_ratio: if typed_size > 0 {
                enum_size as f64 / typed_size as f64
            } else {
                1.0
            },
        }
    }
}

/// Memory comparison between typed and enum-based storage
#[derive(Debug, Clone)]
pub struct MemoryComparison {
    pub typed_bytes: usize,
    pub enum_bytes: usize,
    pub savings_ratio: f64,
}

/// Column value enum for insert operations (temporary)
#[derive(Debug, Clone)]
pub enum ColumnValue {
    Null,
    Int64(i64),
    UInt64(u64),
    Float64(f64),
    Text(String),
    Binary(Vec<u8>),
    Bool(bool),
}

/// Columnar store with multiple tables
#[derive(Debug, Default)]
pub struct ColumnarStore {
    /// Tables by name
    tables: HashMap<String, ColumnarTable>,
}

impl ColumnarStore {
    /// Create a new columnar store
    pub fn new() -> Self {
        Self {
            tables: HashMap::new(),
        }
    }

    /// Create a new table
    pub fn create_table(&mut self, name: impl Into<String>) -> &mut ColumnarTable {
        let name = name.into();
        self.tables
            .entry(name.clone())
            .or_insert_with(|| ColumnarTable::new(name))
    }

    /// Get a table by name
    pub fn get_table(&self, name: &str) -> Option<&ColumnarTable> {
        self.tables.get(name)
    }

    /// Get a mutable table by name
    pub fn get_table_mut(&mut self, name: &str) -> Option<&mut ColumnarTable> {
        self.tables.get_mut(name)
    }

    /// Drop a table
    pub fn drop_table(&mut self, name: &str) -> bool {
        self.tables.remove(name).is_some()
    }

    /// Get total memory usage
    pub fn memory_size(&self) -> usize {
        self.tables.values().map(|t| t.memory_size()).sum()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validity_bitmap() {
        let mut bitmap = ValidityBitmap::new_all_valid(10);
        assert_eq!(bitmap.len(), 10);
        assert_eq!(bitmap.null_count(), 0);
        assert!(bitmap.is_valid(0));
        assert!(bitmap.is_valid(9));

        bitmap.set_null(5);
        assert_eq!(bitmap.null_count(), 1);
        assert!(!bitmap.is_valid(5));

        bitmap.set_valid(5);
        assert_eq!(bitmap.null_count(), 0);
        assert!(bitmap.is_valid(5));
    }

    #[test]
    fn test_int64_column() {
        let mut col = TypedColumn::new_int64();
        col.push_i64(Some(100));
        col.push_i64(Some(200));
        col.push_i64(None);
        col.push_i64(Some(300));

        assert_eq!(col.len(), 4);
        assert_eq!(col.get_i64(0), Some(100));
        assert_eq!(col.get_i64(1), Some(200));
        assert_eq!(col.get_i64(2), None);
        assert_eq!(col.get_i64(3), Some(300));
        assert!(col.is_null(2));

        assert_eq!(col.sum_i64(), 600);
    }

    #[test]
    fn test_text_column() {
        let mut col = TypedColumn::new_text();
        col.push_text(Some("hello"));
        col.push_text(Some("world"));
        col.push_text(None);
        col.push_text(Some("test"));

        assert_eq!(col.len(), 4);
        assert_eq!(col.get_text(0), Some("hello"));
        assert_eq!(col.get_text(1), Some("world"));
        assert_eq!(col.get_text(2), None);
        assert_eq!(col.get_text(3), Some("test"));
    }

    #[test]
    fn test_bool_column() {
        let mut col = TypedColumn::new_bool();
        col.push_bool(Some(true));
        col.push_bool(Some(false));
        col.push_bool(None);
        col.push_bool(Some(true));

        assert_eq!(col.len(), 4);
        assert_eq!(col.get_bool(0), Some(true));
        assert_eq!(col.get_bool(1), Some(false));
        assert_eq!(col.get_bool(2), None);
        assert_eq!(col.get_bool(3), Some(true));

        // Bool column uses ~2 bits per value vs 32 bytes for enum
        // 4 values = 8 bits = 1 byte vs 128 bytes
        assert!(col.memory_size() < 32);
    }

    #[test]
    fn test_columnar_table() {
        let mut table = ColumnarTable::new("users");
        table.add_column("id", ColumnType::Int64);
        table.add_column("name", ColumnType::Text);
        table.add_column("active", ColumnType::Bool);
        table.set_primary_key("id");

        let mut row1 = HashMap::new();
        row1.insert("id".to_string(), ColumnValue::Int64(1));
        row1.insert("name".to_string(), ColumnValue::Text("Alice".to_string()));
        row1.insert("active".to_string(), ColumnValue::Bool(true));
        table.insert_row(&row1);

        let mut row2 = HashMap::new();
        row2.insert("id".to_string(), ColumnValue::Int64(2));
        row2.insert("name".to_string(), ColumnValue::Text("Bob".to_string()));
        row2.insert("active".to_string(), ColumnValue::Bool(false));
        table.insert_row(&row2);

        assert_eq!(table.row_count(), 2);
        assert_eq!(table.get_by_pk(1), Some(0));
        assert_eq!(table.get_by_pk(2), Some(1));
        assert_eq!(table.get_by_pk(3), None);

        let id_col = table.get_column("id").unwrap();
        assert_eq!(id_col.data.get_i64(0), Some(1));
        assert_eq!(id_col.data.get_i64(1), Some(2));
    }

    #[test]
    fn test_memory_savings() {
        let mut table = ColumnarTable::new("test");
        table.add_column("id", ColumnType::Int64);
        table.add_column("value", ColumnType::Float64);
        table.add_column("flag", ColumnType::Bool);

        // Insert 1000 rows
        for i in 0..1000 {
            let mut row = HashMap::new();
            row.insert("id".to_string(), ColumnValue::Int64(i));
            row.insert("value".to_string(), ColumnValue::Float64(i as f64 * 1.5));
            row.insert("flag".to_string(), ColumnValue::Bool(i % 2 == 0));
            table.insert_row(&row);
        }

        let comparison = table.memory_comparison();

        // Typed storage should be significantly smaller than enum storage
        // Enum: 1000 rows * 3 columns * 32 bytes = 96,000 bytes
        // Typed: 1000 * (8 + 8 + 0.125) bytes ≈ 16,125 bytes
        assert!(
            comparison.savings_ratio > 3.0,
            "Expected 3x+ savings, got {:.2}x",
            comparison.savings_ratio
        );
    }

    #[test]
    fn test_simd_sum() {
        let mut col = TypedColumn::new_int64();
        for i in 0..10000 {
            col.push_i64(Some(i));
        }

        let sum = col.sum_i64();
        let expected: i64 = (0..10000).sum();
        assert_eq!(sum, expected);
    }

    #[test]
    fn test_columnar_store() {
        let mut store = ColumnarStore::new();

        {
            let table = store.create_table("users");
            table.add_column("id", ColumnType::Int64);
            table.add_column("name", ColumnType::Text);
        }

        assert!(store.get_table("users").is_some());
        assert!(store.get_table("orders").is_none());

        store.drop_table("users");
        assert!(store.get_table("users").is_none());
    }

    #[test]
    fn test_column_stats() {
        let mut col = TypedColumn::new_int64();
        col.push_i64(Some(10));
        col.push_i64(Some(50));
        col.push_i64(None);
        col.push_i64(Some(30));
        col.push_i64(Some(20));

        let stats = col.stats();
        assert_eq!(stats.min_i64, Some(10));
        assert_eq!(stats.max_i64, Some(50));
        assert_eq!(stats.null_count, 1);
        assert_eq!(stats.row_count, 5);
    }

    #[test]
    fn test_typed_column_value_at() {
        use crate::SochValue;

        // Int64
        let mut col = TypedColumn::new_int64();
        col.push_i64(Some(42));
        col.push_i64(None);
        col.push_i64(Some(-7));
        assert_eq!(col.value_at(0), SochValue::Int(42));
        assert_eq!(col.value_at(1), SochValue::Null);
        assert_eq!(col.value_at(2), SochValue::Int(-7));
        assert_eq!(col.value_at(99), SochValue::Null); // out of bounds

        // Float64
        let mut fcol = TypedColumn::new_float64();
        fcol.push_f64(Some(3.15));
        fcol.push_f64(None);
        assert_eq!(fcol.value_at(0), SochValue::Float(3.15));
        assert_eq!(fcol.value_at(1), SochValue::Null);

        // Text
        let mut tcol = TypedColumn::new_text();
        tcol.push_text(Some("hello"));
        tcol.push_text(None);
        tcol.push_text(Some("world"));
        assert_eq!(tcol.value_at(0), SochValue::Text("hello".to_string()));
        assert_eq!(tcol.value_at(1), SochValue::Null);
        assert_eq!(tcol.value_at(2), SochValue::Text("world".to_string()));

        // Bool
        let mut bcol = TypedColumn::new_bool();
        bcol.push_bool(Some(true));
        bcol.push_bool(Some(false));
        bcol.push_bool(None);
        assert_eq!(bcol.value_at(0), SochValue::Bool(true));
        assert_eq!(bcol.value_at(1), SochValue::Bool(false));
        assert_eq!(bcol.value_at(2), SochValue::Null);
    }
}