clickhouse-native-client 0.1.0

Async ClickHouse client using the native TCP protocol with LZ4/ZSTD compression and TLS support
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
//! Array column implementation
//!
//! **ClickHouse Documentation:** <https://clickhouse.com/docs/en/sql-reference/data-types/array>
//!
//! ## Overview
//!
//! Array columns store variable-length arrays of elements. All elements are
//! stored in a single nested column (flattened), with offsets tracking where
//! each array begins/ends.
//!
//! ## Important Restriction
//!
//! **Arrays cannot be wrapped in Nullable:**
//! - ❌ `Nullable(Array(String))` - Error: "Nested type Array(String) cannot
//!   be inside Nullable type" (Error code 43)
//! - ✅ `Array(Nullable(String))` - CORRECT: Each element can be NULL
//!
//! If you need to represent "no array", use an empty array `[]` instead of
//! NULL.
//!
//! See: <https://github.com/ClickHouse/ClickHouse/issues/1062>
//!
//! ## Wire Format
//!
//! ```text
//! [offsets: UInt64 * num_arrays]  // Cumulative element counts
//! [nested_column_data]             // All elements concatenated
//! ```
//!
//! Example: `[[1,2], [3], [4,5,6]]`
//! - Offsets: `[2, 3, 6]` (2 elements in first array, 3 total after second, 6
//!   total after third)
//! - Nested data: `[1, 2, 3, 4, 5, 6]`

use super::{
    Column,
    ColumnRef,
};
use crate::{
    types::Type,
    Error,
    Result,
};
use bytes::{
    Buf,
    BytesMut,
};
use std::{
    marker::PhantomData,
    sync::Arc,
};

/// Column for arrays of variable length
///
/// Stores a nested column with all array elements concatenated,
/// and an offsets array that marks where each array ends.
///
/// **Reference Implementation:** See
/// `clickhouse-cpp/clickhouse/columns/array.cpp`
pub struct ColumnArray {
    type_: Type,
    nested: ColumnRef,
    offsets: Vec<u64>, /* Cumulative offsets: offsets[i] = total elements
                        * up to and including array i */
}

impl ColumnArray {
    /// Create a new array column from an array type
    pub fn new(type_: Type) -> Self {
        // Extract nested type and create nested column
        let nested = match &type_ {
            Type::Array { item_type } => {
                crate::io::block_stream::create_column(item_type)
                    .expect("Failed to create nested column")
            }
            _ => panic!("ColumnArray requires Array type"),
        };

        Self { type_, nested, offsets: Vec::new() }
    }

    /// Create a new array column with an existing nested column
    pub fn with_nested(nested: ColumnRef) -> Self {
        let nested_type = nested.column_type().clone();
        Self { type_: Type::array(nested_type), nested, offsets: Vec::new() }
    }

    /// Create a new array column from parts (for geo types that need custom
    /// type names)
    pub(crate) fn from_parts(type_: Type, nested: ColumnRef) -> Self {
        Self { type_, nested, offsets: Vec::new() }
    }

    /// Create with reserved capacity
    pub fn with_capacity(type_: Type, capacity: usize) -> Self {
        let nested = match &type_ {
            Type::Array { item_type } => {
                crate::io::block_stream::create_column(item_type)
                    .expect("Failed to create nested column")
            }
            _ => panic!("ColumnArray requires Array type"),
        };

        Self { type_, nested, offsets: Vec::with_capacity(capacity) }
    }

    /// Append an array (specified by the number of elements in the nested
    /// column to consume) The caller must ensure that `len` elements have
    /// been added to the nested column
    pub fn append_len(&mut self, len: u64) {
        let new_offset = if self.offsets.is_empty() {
            len
        } else {
            self.offsets.last().unwrap() + len
        };
        self.offsets.push(new_offset);
    }

    /// Get the start and end indices for the array at the given index
    pub fn get_array_range(&self, index: usize) -> Option<(usize, usize)> {
        if index >= self.offsets.len() {
            return None;
        }

        let end = self.offsets[index] as usize;
        let start =
            if index == 0 { 0 } else { self.offsets[index - 1] as usize };

        Some((start, end))
    }

    /// Get the length of the array at the given index
    pub fn get_array_len(&self, index: usize) -> Option<usize> {
        self.get_array_range(index).map(|(start, end)| end - start)
    }

    /// Get a reference to the nested column as a specific type
    ///
    /// # Example
    /// ```ignore
    /// let col: ColumnArray = /* ... */;
    /// let nested: &ColumnUInt32 = col.nested();
    /// ```
    pub fn nested<T: Column + 'static>(&self) -> &T {
        self.nested
            .as_any()
            .downcast_ref::<T>()
            .expect("Failed to downcast nested column to requested type")
    }

    /// Get mutable reference to the nested column as a specific type
    ///
    /// # Example
    /// ```ignore
    /// let mut col: ColumnArray = /* ... */;
    /// let nested_mut: &mut ColumnUInt32 = col.nested_mut();
    /// ```
    pub fn nested_mut<T: Column + 'static>(&mut self) -> &mut T {
        Arc::get_mut(&mut self.nested)
            .expect("Cannot get mutable reference to shared nested column")
            .as_any_mut()
            .downcast_mut::<T>()
            .expect("Failed to downcast nested column to requested type")
    }

    /// Get the nested column as a `ColumnRef` (`Arc<dyn Column>`)
    pub fn nested_ref(&self) -> ColumnRef {
        self.nested.clone()
    }

    /// Get the offsets
    pub fn offsets(&self) -> &[u64] {
        &self.offsets
    }

    /// Append an entire array column as a single array element
    /// This takes all the data from the provided column and adds it as one
    /// array
    pub fn append_array(&mut self, array_data: ColumnRef) {
        let len = array_data.size() as u64;

        // Append the array data to nested column
        let nested_mut = Arc::get_mut(&mut self.nested)
            .expect("Cannot append to shared array column - column has multiple references");
        nested_mut
            .append_column(array_data)
            .expect("Failed to append array data to nested column");

        // Update offsets
        self.append_len(len);
    }

    /// Get the array at the given index as a sliced column
    pub fn at(&self, index: usize) -> ColumnRef {
        if let Some((start, end)) = self.get_array_range(index) {
            self.nested.slice(start, end - start).expect("Valid slice")
        } else {
            panic!("Array index out of bounds: {}", index);
        }
    }

    /// Get the number of arrays (alias for size())
    pub fn len(&self) -> usize {
        self.offsets.len()
    }

    /// Check if the array column is empty
    pub fn is_empty(&self) -> bool {
        self.offsets.is_empty()
    }
}

impl Column for ColumnArray {
    fn column_type(&self) -> &Type {
        &self.type_
    }

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

    fn clear(&mut self) {
        self.offsets.clear();
        // CRITICAL: Must also clear nested data to maintain consistency
        // If we clear offsets but not nested data, the column is in a corrupt
        // state
        let nested_mut = Arc::get_mut(&mut self.nested)
            .expect("Cannot clear shared array column - column has multiple references");
        nested_mut.clear();
    }

    fn reserve(&mut self, new_cap: usize) {
        self.offsets.reserve(new_cap);
    }

    fn append_column(&mut self, other: ColumnRef) -> Result<()> {
        let other =
            other.as_any().downcast_ref::<ColumnArray>().ok_or_else(|| {
                Error::TypeMismatch {
                    expected: self.type_.name(),
                    actual: other.column_type().name(),
                }
            })?;

        // Check that nested types match
        if self.nested.column_type().name()
            != other.nested.column_type().name()
        {
            return Err(Error::TypeMismatch {
                expected: self.nested.column_type().name(),
                actual: other.nested.column_type().name(),
            });
        }

        // Adjust offsets from other and append
        let offset_base = self.offsets.last().copied().unwrap_or(0);
        for &offset in &other.offsets {
            self.offsets.push(offset_base + offset);
        }

        // CRITICAL: Must also append the nested data!
        // Without this, offsets point to wrong/missing data → DATA CORRUPTION
        let nested_mut = Arc::get_mut(&mut self.nested)
            .ok_or_else(|| Error::Protocol(
                "Cannot append to shared array column - column has multiple references".to_string()
            ))?;
        nested_mut.append_column(other.nested.clone())?;

        Ok(())
    }

    fn load_from_buffer(
        &mut self,
        buffer: &mut &[u8],
        rows: usize,
    ) -> Result<()> {
        self.offsets.reserve(rows);

        // Read offsets (fixed UInt64, not varint!)
        // Wire format: UInt64 values stored as 8-byte little-endian
        let bytes_needed = rows * 8;
        if buffer.len() < bytes_needed {
            return Err(Error::Protocol(format!(
                "Buffer underflow reading array offsets: need {} bytes, have {}",
                bytes_needed,
                buffer.len()
            )));
        }

        // Use bulk copy for performance
        self.offsets.reserve(rows);
        let current_len = self.offsets.len();
        unsafe {
            // Set length first to claim ownership of the memory
            self.offsets.set_len(current_len + rows);
            // Cast dest to bytes and use byte offset
            let dest_ptr =
                (self.offsets.as_mut_ptr() as *mut u8).add(current_len * 8);
            std::ptr::copy_nonoverlapping(
                buffer.as_ptr(),
                dest_ptr,
                bytes_needed,
            );
        }

        buffer.advance(bytes_needed);

        // CRITICAL: Must also load the nested column data
        // The total number of nested elements is the last offset value
        let total_nested_elements =
            self.offsets.last().copied().unwrap_or(0) as usize;
        if total_nested_elements > 0 {
            let nested_mut = Arc::get_mut(&mut self.nested)
                .ok_or_else(|| Error::Protocol(
                    "Cannot load into shared array column - column has multiple references".to_string()
                ))?;
            nested_mut.load_from_buffer(buffer, total_nested_elements)?;
        }

        Ok(())
    }

    fn load_prefix(&mut self, buffer: &mut &[u8], rows: usize) -> Result<()> {
        // Delegate to nested column's load_prefix
        // Critical for Array(LowCardinality(X)) to read LowCardinality
        // key_version before offsets
        let nested_mut = Arc::get_mut(&mut self.nested).ok_or_else(|| {
            Error::Protocol(
                "Cannot load prefix for shared array column".to_string(),
            )
        })?;
        nested_mut.load_prefix(buffer, rows)
    }

    fn save_prefix(&self, buffer: &mut BytesMut) -> Result<()> {
        // Delegate to nested column's save_prefix
        // Critical for Array(LowCardinality(X)) to write LowCardinality
        // version before offsets
        self.nested.save_prefix(buffer)
    }

    fn save_to_buffer(&self, buffer: &mut BytesMut) -> Result<()> {
        // Write offsets as fixed UInt64 (not varints!)
        // Wire format: UInt64 values stored as 8-byte little-endian
        // This matches load_from_buffer which reads fixed UInt64
        if !self.offsets.is_empty() {
            let byte_slice = unsafe {
                std::slice::from_raw_parts(
                    self.offsets.as_ptr() as *const u8,
                    self.offsets.len() * 8,
                )
            };
            buffer.extend_from_slice(byte_slice);
        }

        // Write nested column data
        self.nested.save_to_buffer(buffer)?;

        Ok(())
    }

    fn clone_empty(&self) -> ColumnRef {
        Arc::new(ColumnArray::with_nested(self.nested.clone_empty()))
    }

    fn slice(&self, begin: usize, len: usize) -> Result<ColumnRef> {
        if begin + len > self.offsets.len() {
            return Err(Error::InvalidArgument(format!(
                "Slice out of bounds: begin={}, len={}, size={}",
                begin,
                len,
                self.offsets.len()
            )));
        }

        // Calculate the range of nested elements we need
        let nested_start =
            if begin == 0 { 0 } else { self.offsets[begin - 1] as usize };
        let nested_end = self.offsets[begin + len - 1] as usize;
        let nested_len = nested_end - nested_start;

        // Slice the nested column
        let sliced_nested = self.nested.slice(nested_start, nested_len)?;

        // Adjust offsets for the slice
        let mut sliced_offsets = Vec::with_capacity(len);
        let offset_base = if begin == 0 { 0 } else { self.offsets[begin - 1] };

        for i in begin..begin + len {
            sliced_offsets.push(self.offsets[i] - offset_base);
        }

        let mut result = ColumnArray::with_nested(sliced_nested);
        result.offsets = sliced_offsets;

        Ok(Arc::new(result))
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

/// Typed wrapper for ColumnArray that provides type-safe access to nested
/// column
///
/// This is analogous to `ColumnArrayT<T>` in clickhouse-cpp, providing
/// compile-time type safety for array operations.
///
/// **Reference Implementation:** See
/// `clickhouse-cpp/clickhouse/columns/array.h`
pub struct ColumnArrayT<T>
where
    T: Column + 'static,
{
    inner: ColumnArray,
    _phantom: PhantomData<fn() -> T>,
}

impl<T> ColumnArrayT<T>
where
    T: Column + 'static,
{
    /// Create a new typed array column from a typed nested column
    pub fn with_nested(nested: Arc<T>) -> Self {
        let inner = ColumnArray::with_nested(nested);
        Self { inner, _phantom: PhantomData }
    }

    /// Create a new typed array column from an array type
    ///
    /// Returns an error if the nested column type doesn't match T
    pub fn new(type_: Type) -> Result<Self> {
        let inner = ColumnArray::new(type_);
        // Verify the nested column is of the expected type
        let _ = inner.nested_ref().as_any().downcast_ref::<T>().ok_or_else(
            || {
                Error::InvalidArgument(format!(
                    "Type mismatch: expected nested column of type {}",
                    std::any::type_name::<T>()
                ))
            },
        )?;
        Ok(Self { inner, _phantom: PhantomData })
    }

    /// Create with reserved capacity
    pub fn with_capacity(type_: Type, capacity: usize) -> Result<Self> {
        let inner = ColumnArray::with_capacity(type_, capacity);
        // Verify type
        let _ = inner.nested_ref().as_any().downcast_ref::<T>().ok_or_else(
            || {
                Error::InvalidArgument(format!(
                    "Type mismatch: expected nested column of type {}",
                    std::any::type_name::<T>()
                ))
            },
        )?;
        Ok(Self { inner, _phantom: PhantomData })
    }

    /// Get typed reference to the nested column
    ///
    /// This is safe because we verify the type at construction
    pub fn nested_typed(&self) -> &T {
        // Use the generic nested method to get typed access directly
        self.inner.nested::<T>()
    }

    /// Get typed mutable reference to the nested column
    ///
    /// Returns an error if the column has multiple Arc references
    pub fn nested_typed_mut(&mut self) -> Result<&mut T> {
        // Use the generic nested_mut to get typed access directly
        Ok(self.inner.nested_mut::<T>())
    }

    /// Append an array by building it with a closure
    ///
    /// The closure receives a mutable reference to the nested column
    /// and can append elements. The array length is calculated automatically.
    ///
    /// # Example
    /// ```ignore
    /// let mut arr =
    /// ColumnArrayT::<ColumnUInt64>::new(Type::array(Type::uint64()))?;
    /// arr.append_array(|nested| {
    ///     nested.append(1);
    ///     nested.append(2);
    ///     nested.append(3);
    /// })?;
    /// ```
    pub fn append_array<F>(&mut self, build_fn: F) -> Result<()>
    where
        F: FnOnce(&mut T),
    {
        let start_len = self.inner.nested_ref().size();
        let nested = self.nested_typed_mut()?;
        build_fn(nested);
        let end_len = self.inner.nested_ref().size();
        let array_len = end_len - start_len;
        self.inner.append_len(array_len as u64);
        Ok(())
    }

    /// Append an entire column as a single array element
    pub fn append_array_column(&mut self, array_data: ColumnRef) {
        self.inner.append_array(array_data)
    }

    /// Append an array specified by length
    ///
    /// The caller must ensure that `len` elements have been added to the
    /// nested column
    pub fn append_len(&mut self, len: u64) {
        self.inner.append_len(len)
    }

    /// Get the array at the given index as a sliced column
    pub fn at(&self, index: usize) -> ColumnRef {
        self.inner.at(index)
    }

    /// Get the start and end indices for the array at the given index
    pub fn get_array_range(&self, index: usize) -> Option<(usize, usize)> {
        self.inner.get_array_range(index)
    }

    /// Get the length of the array at the given index
    pub fn get_array_len(&self, index: usize) -> Option<usize> {
        self.inner.get_array_len(index)
    }

    /// Get the offsets
    pub fn offsets(&self) -> &[u64] {
        self.inner.offsets()
    }

    /// Get the number of arrays
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Check if the array column is empty
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    /// Get reference to inner ColumnArray
    pub fn inner(&self) -> &ColumnArray {
        &self.inner
    }

    /// Get mutable reference to inner ColumnArray
    pub fn inner_mut(&mut self) -> &mut ColumnArray {
        &mut self.inner
    }

    /// Convert into inner ColumnArray
    pub fn into_inner(self) -> ColumnArray {
        self.inner
    }
}

impl<T> Column for ColumnArrayT<T>
where
    T: Column + 'static,
{
    fn column_type(&self) -> &Type {
        self.inner.column_type()
    }

    fn size(&self) -> usize {
        self.inner.size()
    }

    fn clear(&mut self) {
        self.inner.clear()
    }

    fn reserve(&mut self, new_cap: usize) {
        self.inner.reserve(new_cap)
    }

    fn append_column(&mut self, other: ColumnRef) -> Result<()> {
        self.inner.append_column(other)
    }

    fn load_from_buffer(
        &mut self,
        buffer: &mut &[u8],
        rows: usize,
    ) -> Result<()> {
        self.inner.load_from_buffer(buffer, rows)
    }

    fn load_prefix(&mut self, buffer: &mut &[u8], rows: usize) -> Result<()> {
        self.inner.load_prefix(buffer, rows)
    }

    fn save_prefix(&self, buffer: &mut BytesMut) -> Result<()> {
        self.inner.save_prefix(buffer)
    }

    fn save_to_buffer(&self, buffer: &mut BytesMut) -> Result<()> {
        self.inner.save_to_buffer(buffer)
    }

    fn clone_empty(&self) -> ColumnRef {
        Arc::new(ColumnArrayT::<T> {
            inner: ColumnArray::with_nested(
                self.inner.nested_ref().clone_empty(),
            ),
            _phantom: PhantomData,
        })
    }

    fn slice(&self, begin: usize, len: usize) -> Result<ColumnRef> {
        let sliced_inner = self.inner.slice(begin, len)?;

        // The sliced result is a ColumnArray with proper offsets and nested
        // data We need to extract it and wrap it in ColumnArrayT
        let sliced_array = sliced_inner
            .as_any()
            .downcast_ref::<ColumnArray>()
            .ok_or_else(|| {
                Error::InvalidArgument(
                    "Failed to downcast sliced column".to_string(),
                )
            })?;

        // Clone the sliced array structure (preserves offsets and nested)
        let cloned_inner = ColumnArray {
            type_: sliced_array.column_type().clone(),
            nested: sliced_array.nested_ref().clone(),
            offsets: sliced_array.offsets().to_vec(),
        };

        Ok(Arc::new(ColumnArrayT::<T> {
            inner: cloned_inner,
            _phantom: PhantomData,
        }))
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

// Helper functions removed - using buffer_utils module

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use super::*;
    use crate::{
        column::{
            numeric::ColumnUInt64,
            string::ColumnString,
        },
        types::Type,
    };

    #[test]
    fn test_array_creation() {
        let nested = Arc::new(ColumnUInt64::new());
        let col = ColumnArray::with_nested(nested);
        assert_eq!(col.size(), 0);
    }

    #[test]
    fn test_array_append() {
        let mut nested = ColumnUInt64::new();
        // First array: [1, 2, 3]
        nested.append(1);
        nested.append(2);
        nested.append(3);

        let mut col = ColumnArray::with_nested(Arc::new(nested));
        col.append_len(3); // Array of 3 elements

        // Second array: [4, 5]
        let nested_mut: &mut ColumnUInt64 = col.nested_mut();
        nested_mut.append(4);
        nested_mut.append(5);

        col.append_len(2); // Array of 2 more elements

        assert_eq!(col.size(), 2);
        assert_eq!(col.get_array_len(0), Some(3));
        assert_eq!(col.get_array_len(1), Some(2));
        assert_eq!(col.get_array_range(0), Some((0, 3)));
        assert_eq!(col.get_array_range(1), Some((3, 5)));
    }

    #[test]
    fn test_array_offsets() {
        let nested = Arc::new(ColumnUInt64::new());
        let mut col = ColumnArray::with_nested(nested);

        col.append_len(3); // Array with 3 elements
        col.append_len(0); // Empty array
        col.append_len(2); // Array with 2 elements

        assert_eq!(col.offsets(), &[3, 3, 5]);
        assert_eq!(col.get_array_len(0), Some(3));
        assert_eq!(col.get_array_len(1), Some(0));
        assert_eq!(col.get_array_len(2), Some(2));
    }

    #[test]
    fn test_array_empty_arrays() {
        let nested = Arc::new(ColumnUInt64::new());
        let mut col = ColumnArray::with_nested(nested);

        col.append_len(0);
        col.append_len(0);
        col.append_len(0);

        assert_eq!(col.size(), 3);
        assert_eq!(col.get_array_len(0), Some(0));
        assert_eq!(col.get_array_len(1), Some(0));
        assert_eq!(col.get_array_len(2), Some(0));
    }

    #[test]
    fn test_array_save_load() {
        let nested = Arc::new(ColumnUInt64::new());
        let mut col = ColumnArray::with_nested(nested);

        col.append_len(3);
        col.append_len(2);
        col.append_len(1);

        let mut buffer = BytesMut::new();
        col.save_to_buffer(&mut buffer).unwrap();

        // Verify offsets are written
        assert!(!buffer.is_empty());
    }

    #[test]
    fn test_array_load_offsets() {
        use bytes::BufMut;

        let nested = Arc::new(ColumnUInt64::new());
        let mut col = ColumnArray::with_nested(nested);

        // Encode offsets manually as fixed UInt64: 3, 5, 8 (total 8 nested
        // elements)
        let mut data = BytesMut::new();
        data.put_u64_le(3);
        data.put_u64_le(5);
        data.put_u64_le(8);

        // Must also include nested data (8 UInt64 values)
        for i in 0..8u64 {
            data.put_u64_le(i);
        }

        let mut reader = &data[..];
        col.load_from_buffer(&mut reader, 3).unwrap();

        assert_eq!(col.size(), 3);
        assert_eq!(col.offsets(), &[3, 5, 8]);
    }

    #[test]
    fn test_array_slice() {
        let mut nested = ColumnUInt64::new();
        // Arrays: [1,2,3], [4,5], [6], [7,8,9,10]
        for i in 1..=10 {
            nested.append(i);
        }

        let mut col = ColumnArray::with_nested(Arc::new(nested));
        col.append_len(3); // offset: 3
        col.append_len(2); // offset: 5
        col.append_len(1); // offset: 6
        col.append_len(4); // offset: 10

        let sliced = col.slice(1, 2).unwrap(); // Take arrays [4,5] and [6]
        let sliced_col =
            sliced.as_any().downcast_ref::<ColumnArray>().unwrap();

        assert_eq!(sliced_col.size(), 2);
        assert_eq!(sliced_col.offsets(), &[2, 3]); // Adjusted offsets
    }

    #[test]
    fn test_array_with_strings() {
        let nested = Arc::new(ColumnString::new(Type::string()));
        let mut col = ColumnArray::with_nested(nested);

        col.append_len(2); // Array with 2 strings
        col.append_len(3); // Array with 3 strings

        assert_eq!(col.size(), 2);
        assert_eq!(col.get_array_len(0), Some(2));
        assert_eq!(col.get_array_len(1), Some(3));
    }

    #[test]
    fn test_array_type_mismatch() {
        let nested1 = Arc::new(ColumnUInt64::new());
        let mut col1 = ColumnArray::with_nested(nested1);

        let nested2 = Arc::new(ColumnString::new(Type::string()));
        let col2 = ColumnArray::with_nested(nested2);

        let result = col1.append_column(Arc::new(col2));
        assert!(result.is_err());
    }

    #[test]
    fn test_array_out_of_bounds() {
        let nested = Arc::new(ColumnUInt64::new());
        let mut col = ColumnArray::with_nested(nested);

        col.append_len(3);
        col.append_len(2);

        assert_eq!(col.get_array_len(100), None);
        assert_eq!(col.get_array_range(100), None);
    }

    #[test]
    fn test_array_append_column() {
        // Create first array column with data: [[1, 2], [3]]
        let mut nested1 = ColumnUInt64::new();
        nested1.append(1);
        nested1.append(2);
        nested1.append(3);

        let mut col1 = ColumnArray::with_nested(Arc::new(nested1));
        col1.append_len(2); // First array: [1, 2]
        col1.append_len(1); // Second array: [3]

        // Create second array column with data: [[4, 5, 6]]
        let mut nested2 = ColumnUInt64::new();
        nested2.append(4);
        nested2.append(5);
        nested2.append(6);

        let mut col2 = ColumnArray::with_nested(Arc::new(nested2));
        col2.append_len(3); // Third array: [4, 5, 6]

        // Append col2 to col1
        col1.append_column(Arc::new(col2))
            .expect("append_column should succeed");

        // Verify we have 3 arrays total
        assert_eq!(col1.size(), 3, "Should have 3 arrays after append");

        // Verify array lengths
        assert_eq!(
            col1.get_array_len(0),
            Some(2),
            "First array should have 2 elements"
        );
        assert_eq!(
            col1.get_array_len(1),
            Some(1),
            "Second array should have 1 element"
        );
        assert_eq!(
            col1.get_array_len(2),
            Some(3),
            "Third array should have 3 elements"
        );

        // CRITICAL: Verify nested data was actually appended
        // The nested column should contain [1, 2, 3, 4, 5, 6]
        let nested: &ColumnUInt64 = col1.nested();
        assert_eq!(
            nested.size(),
            6,
            "Nested column should have 6 total elements"
        );

        // Verify offsets are correct: [2, 3, 6]
        assert_eq!(col1.offsets(), &[2, 3, 6], "Offsets should be [2, 3, 6]");
    }

    #[test]
    #[should_panic(
        expected = "Cannot clear shared array column - column has multiple references"
    )]
    fn test_array_clear_panics_on_shared_nested() {
        // Create an array column
        let mut nested = ColumnUInt64::new();
        nested.append(1);
        nested.append(2);
        nested.append(3);

        let nested_arc = Arc::new(nested);
        let mut col = ColumnArray::with_nested(nested_arc.clone());
        col.append_len(3);

        // Create a second reference to the nested column
        let _shared_ref = nested_arc.clone();

        // Now nested has multiple Arc references, so clear() MUST panic
        // to prevent data corruption (clearing offsets but not nested data)
        col.clear();
    }

    #[test]
    fn test_array_roundtrip_nested_data() {
        use bytes::BytesMut;

        // Create array column with actual nested data: [[1, 2], [3, 4, 5]]
        let mut nested = ColumnUInt64::new();
        nested.append(1);
        nested.append(2);
        nested.append(3);
        nested.append(4);
        nested.append(5);

        let mut col = ColumnArray::with_nested(Arc::new(nested));
        col.append_len(2); // First array: [1, 2]
        col.append_len(3); // Second array: [3, 4, 5]

        assert_eq!(col.size(), 2, "Original should have 2 arrays");

        // Save to buffer
        let mut buffer = BytesMut::new();
        col.save_to_buffer(&mut buffer).expect("save should succeed");

        // Load into new array column
        let nested_empty = Arc::new(ColumnUInt64::new());
        let mut col_loaded = ColumnArray::with_nested(nested_empty);

        let mut buf_slice = &buffer[..];
        col_loaded
            .load_from_buffer(&mut buf_slice, 2)
            .expect("load should succeed");

        // Verify arrays structure
        assert_eq!(col_loaded.size(), 2, "Loaded should have 2 arrays");
        assert_eq!(
            col_loaded.get_array_len(0),
            Some(2),
            "First array should have 2 elements"
        );
        assert_eq!(
            col_loaded.get_array_len(1),
            Some(3),
            "Second array should have 3 elements"
        );

        // CRITICAL: Verify nested data was actually loaded
        let nested_loaded: &ColumnUInt64 = col_loaded.nested();
        assert_eq!(
            nested_loaded.size(),
            5,
            "Nested should have 5 total elements after load"
        );

        // Verify we can retrieve the actual arrays
        let arr0 = col_loaded.at(0);
        let arr0_data = arr0.as_any().downcast_ref::<ColumnUInt64>().unwrap();
        assert_eq!(arr0_data.size(), 2, "First array should have 2 elements");

        let arr1 = col_loaded.at(1);
        let arr1_data = arr1.as_any().downcast_ref::<ColumnUInt64>().unwrap();
        assert_eq!(arr1_data.size(), 3, "Second array should have 3 elements");
    }

    // ColumnArrayT tests
    #[test]
    fn test_array_t_creation() {
        let nested = Arc::new(ColumnUInt64::new());
        let col = ColumnArrayT::<ColumnUInt64>::with_nested(nested);
        assert_eq!(col.size(), 0);
        assert!(col.is_empty());
    }

    #[test]
    fn test_array_t_new() {
        let col =
            ColumnArrayT::<ColumnUInt64>::new(Type::array(Type::uint64()))
                .unwrap();
        assert_eq!(col.size(), 0);
    }

    #[test]
    fn test_array_t_append_array() {
        let mut col =
            ColumnArrayT::<ColumnUInt64>::new(Type::array(Type::uint64()))
                .unwrap();

        // Append first array: [1, 2, 3]
        col.append_array(|nested| {
            nested.append(1);
            nested.append(2);
            nested.append(3);
        })
        .unwrap();

        // Append second array: [4, 5]
        col.append_array(|nested| {
            nested.append(4);
            nested.append(5);
        })
        .unwrap();

        assert_eq!(col.size(), 2);
        assert_eq!(col.get_array_len(0), Some(3));
        assert_eq!(col.get_array_len(1), Some(2));
        assert_eq!(col.offsets(), &[3, 5]);
    }

    #[test]
    fn test_array_t_typed_access() {
        let mut col =
            ColumnArrayT::<ColumnUInt64>::new(Type::array(Type::uint64()))
                .unwrap();

        col.append_array(|nested| {
            nested.append(10);
            nested.append(20);
        })
        .unwrap();

        // Get typed access to nested column
        let nested = col.nested_typed();
        assert_eq!(nested.size(), 2);
        assert_eq!(nested.at(0), 10);
        assert_eq!(nested.at(1), 20);
    }

    #[test]
    fn test_array_t_with_strings() {
        let mut col =
            ColumnArrayT::<ColumnString>::new(Type::array(Type::string()))
                .unwrap();

        col.append_array(|nested| {
            nested.append("hello");
            nested.append("world");
        })
        .unwrap();

        col.append_array(|nested| {
            nested.append("foo");
        })
        .unwrap();

        assert_eq!(col.size(), 2);
        assert_eq!(col.get_array_len(0), Some(2));
        assert_eq!(col.get_array_len(1), Some(1));

        let nested = col.nested_typed();
        assert_eq!(nested.at(0), "hello");
        assert_eq!(nested.at(1), "world");
        assert_eq!(nested.at(2), "foo");
    }

    #[test]
    fn test_array_t_empty_arrays() {
        let mut col =
            ColumnArrayT::<ColumnUInt64>::new(Type::array(Type::uint64()))
                .unwrap();

        col.append_array(|_nested| {
            // Empty array
        })
        .unwrap();

        col.append_array(|nested| {
            nested.append(42);
        })
        .unwrap();

        col.append_array(|_nested| {
            // Another empty array
        })
        .unwrap();

        assert_eq!(col.size(), 3);
        assert_eq!(col.get_array_len(0), Some(0));
        assert_eq!(col.get_array_len(1), Some(1));
        assert_eq!(col.get_array_len(2), Some(0));
        assert_eq!(col.offsets(), &[0, 1, 1]);
    }

    #[test]
    fn test_array_t_append_column() {
        let mut col1 =
            ColumnArrayT::<ColumnUInt64>::new(Type::array(Type::uint64()))
                .unwrap();
        col1.append_array(|nested| {
            nested.append(1);
            nested.append(2);
        })
        .unwrap();

        let mut col2 =
            ColumnArrayT::<ColumnUInt64>::new(Type::array(Type::uint64()))
                .unwrap();
        col2.append_array(|nested| {
            nested.append(3);
            nested.append(4);
            nested.append(5);
        })
        .unwrap();

        col1.append_column(Arc::new(col2.into_inner()))
            .expect("append_column should succeed");

        assert_eq!(col1.size(), 2);
        assert_eq!(col1.get_array_len(0), Some(2));
        assert_eq!(col1.get_array_len(1), Some(3));

        let nested = col1.nested_typed();
        assert_eq!(nested.size(), 5);
    }

    #[test]
    fn test_array_t_slice() {
        let mut col =
            ColumnArrayT::<ColumnUInt64>::new(Type::array(Type::uint64()))
                .unwrap();

        // Arrays: [1,2,3], [4,5], [6], [7,8,9,10]
        col.append_array(|n| {
            n.append(1);
            n.append(2);
            n.append(3);
        })
        .unwrap();
        col.append_array(|n| {
            n.append(4);
            n.append(5);
        })
        .unwrap();
        col.append_array(|n| {
            n.append(6);
        })
        .unwrap();
        col.append_array(|n| {
            n.append(7);
            n.append(8);
            n.append(9);
            n.append(10);
        })
        .unwrap();

        // Slice arrays [4,5] and [6] (indices 1-2)
        let sliced = col.slice(1, 2).unwrap();
        let sliced_col = sliced
            .as_any()
            .downcast_ref::<ColumnArrayT<ColumnUInt64>>()
            .unwrap();

        assert_eq!(sliced_col.size(), 2);
        assert_eq!(sliced_col.offsets(), &[2, 3]);

        let nested = sliced_col.nested_typed();
        assert_eq!(nested.size(), 3);
        assert_eq!(nested.at(0), 4);
        assert_eq!(nested.at(1), 5);
        assert_eq!(nested.at(2), 6);
    }

    #[test]
    fn test_array_t_roundtrip() {
        use bytes::BytesMut;

        let mut col =
            ColumnArrayT::<ColumnUInt64>::new(Type::array(Type::uint64()))
                .unwrap();

        col.append_array(|n| {
            n.append(1);
            n.append(2);
        })
        .unwrap();
        col.append_array(|n| {
            n.append(3);
            n.append(4);
            n.append(5);
        })
        .unwrap();

        // Save to buffer
        let mut buffer = BytesMut::new();
        col.save_to_buffer(&mut buffer).unwrap();

        // Load into new column
        let mut col_loaded =
            ColumnArrayT::<ColumnUInt64>::new(Type::array(Type::uint64()))
                .unwrap();
        let mut buf_slice = &buffer[..];
        col_loaded.load_from_buffer(&mut buf_slice, 2).unwrap();

        assert_eq!(col_loaded.size(), 2);
        assert_eq!(col_loaded.get_array_len(0), Some(2));
        assert_eq!(col_loaded.get_array_len(1), Some(3));

        let nested = col_loaded.nested_typed();
        assert_eq!(nested.size(), 5);
        assert_eq!(nested.at(0), 1);
        assert_eq!(nested.at(1), 2);
        assert_eq!(nested.at(2), 3);
        assert_eq!(nested.at(3), 4);
        assert_eq!(nested.at(4), 5);
    }
}