questdb-rs 7.0.0

QuestDB Client Library for Rust
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
/*******************************************************************************
 *     ___                  _   ____  ____
 *    / _ \ _   _  ___  ___| |_|  _ \| __ )
 *   | | | | | | |/ _ \/ __| __| | | |  _ \
 *   | |_| | |_| |  __/\__ \ |_| |_| | |_) |
 *    \__\_\\__,_|\___||___/\__|____/|____/
 *
 *  Copyright (c) 2014-2019 Appsicle
 *  Copyright (c) 2019-2025 QuestDB
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

//! `DecodedBatch` → `arrow::array::RecordBatch` conversion.

use std::sync::Arc;

use aligned_vec::{AVec, ConstAlign};
use arrow::array::ArrayDataBuilder;
use arrow::array::{
    Array, ArrayRef, BinaryArray, BooleanArray, Decimal64Array, Decimal128Array, Decimal256Array,
    DictionaryArray, FixedSizeBinaryArray, Int8Array, Int16Array, Int32Array, Int64Array,
    ListArray, RecordBatch, StringArray, TimestampMicrosecondArray, TimestampMillisecondArray,
    TimestampNanosecondArray,
};
use arrow::buffer::{Buffer, NullBuffer};
use arrow::datatypes::{DataType, Field, Schema as ArrowSchema, TimeUnit};
use arrow::error::ArrowError;
use bytes::Bytes;

use crate::egress::arrow::schema::to_arrow_export;
use crate::egress::column_kind::ColumnKind;
use crate::egress::decoder::{ArrayBuffers, ColumnBuffer, DecodedBatch, DecodedColumn};
use crate::egress::schema::Schema;
use crate::egress::symbol_dict::SymbolDict;
use crate::error::{Error, Result, fmt};

type ABytes = AVec<u8, ConstAlign<64>>;

// `Bytes::from_owner` requires the owner to be `Send + Sync + 'static`.
// arrow-rs's RecordBatch can be dropped on any thread (Python consumers
// release on a worker pool), so the AVec we hand it must satisfy these
// bounds. A future aligned-vec release that adds a !Send field would
// silently break the FFI export path — this static check fails to
// compile if that happens.
const _: fn() = || {
    fn assert_send_sync_static<T: Send + Sync + 'static>() {}
    assert_send_sync_static::<ABytes>();
};

/// Per-cursor cache of the connection dict's `Utf8` values array, keyed on dict
/// length: the dict is append-only and shared by every delta-mode SYMBOL column,
/// so the array is interned once and reused (cheap `Arc` clone) until it grows.
#[derive(Default)]
pub(crate) struct SymbolValuesCache {
    len: usize,
    values: Option<StringArray>,
}

const SYMBOL_REMAP_UNSET: u32 = u32::MAX;

#[derive(Default)]
pub(crate) struct SymbolBuildScratch {
    remap: Vec<u32>,
    touched: Vec<u32>,
}

pub(crate) fn batch_to_record_batch(
    schema_ref: Arc<ArrowSchema>,
    egress_schema: &Schema,
    batch: DecodedBatch,
    dict: &SymbolDict,
) -> Result<RecordBatch> {
    let mut sym_values = SymbolValuesCache::default();
    batch_to_record_batch_with(
        schema_ref,
        egress_schema,
        batch,
        dict,
        &mut sym_values,
        None,
    )
}

/// As [`batch_to_record_batch`] but reuses a caller-owned [`SymbolValuesCache`]
/// so a streaming cursor interns the connection dict once across batches.
/// A `Some` `sym_scratch` switches SYMBOL columns to the compact builder.
pub(crate) fn batch_to_record_batch_with(
    schema_ref: Arc<ArrowSchema>,
    egress_schema: &Schema,
    batch: DecodedBatch,
    dict: &SymbolDict,
    sym_values: &mut SymbolValuesCache,
    mut sym_scratch: Option<&mut SymbolBuildScratch>,
) -> Result<RecordBatch> {
    let DecodedBatch {
        row_count, columns, ..
    } = batch;
    if columns.len() != schema_ref.fields().len() {
        return Err(fmt!(
            ProtocolError,
            "schema/batch column count mismatch: schema={} batch={}",
            schema_ref.fields().len(),
            columns.len()
        ));
    }
    let mut arrays: Vec<ArrayRef> = Vec::with_capacity(columns.len());
    // Single degenerate-list slack pool shared by every array column in this
    // batch. Threading one budget (rather than re-granting it per column) stops
    // a wide batch of `[huge, 0]` columns from multiplying offset-buffer
    // allocation by the column count: total degenerate expansion per batch is
    // capped at ~64 MiB regardless of how many array columns the batch carries.
    let mut degenerate_node_slack = MAX_DEGENERATE_LIST_NODES;
    for (idx, decoded) in columns.into_iter().enumerate() {
        let field = schema_ref.field(idx);
        let kind = egress_schema
            .column(idx)
            .map(|c| c.kind)
            .ok_or_else(|| fmt!(InvalidApiCall, "egress schema missing column {}", idx))?;
        arrays.push(column_to_array(
            field,
            kind,
            decoded,
            row_count,
            dict,
            sym_values,
            sym_scratch.as_deref_mut(),
            &mut degenerate_node_slack,
        )?);
    }
    RecordBatch::try_new(schema_ref, arrays)
        .map_err(|e| fmt!(ProtocolError, "failed to assemble record batch: {}", e))
}

#[allow(clippy::too_many_arguments)]
fn column_to_array(
    field: &Field,
    kind: ColumnKind,
    decoded: DecodedColumn,
    row_count: usize,
    dict: &SymbolDict,
    sym_values: &mut SymbolValuesCache,
    sym_scratch: Option<&mut SymbolBuildScratch>,
    degenerate_node_slack: &mut usize,
) -> Result<ArrayRef> {
    Ok(match (kind, decoded) {
        (ColumnKind::Boolean, DecodedColumn::Boolean(buf)) => {
            boolean_array(buf, row_count).map(|a| Arc::new(a) as ArrayRef)?
        }
        (ColumnKind::Byte, DecodedColumn::Byte(buf)) => {
            primitive_array(buf, row_count, DataType::Int8)?
        }
        (ColumnKind::Short, DecodedColumn::Short(buf)) => {
            primitive_array(buf, row_count, DataType::Int16)?
        }
        (ColumnKind::Int, DecodedColumn::Int(buf)) => {
            primitive_array(buf, row_count, DataType::Int32)?
        }
        (ColumnKind::Long, DecodedColumn::Long(buf)) => {
            primitive_array(buf, row_count, DataType::Int64)?
        }
        (ColumnKind::Float, DecodedColumn::Float(buf)) => {
            primitive_array(buf, row_count, DataType::Float32)?
        }
        (ColumnKind::Double, DecodedColumn::Double(buf)) => {
            primitive_array(buf, row_count, DataType::Float64)?
        }
        (ColumnKind::Char, DecodedColumn::Char(buf)) => {
            primitive_array(buf, row_count, DataType::UInt16)?
        }
        (ColumnKind::Ipv4, DecodedColumn::Ipv4(buf)) => {
            primitive_array(buf, row_count, DataType::UInt32)?
        }
        (ColumnKind::Timestamp, DecodedColumn::Timestamp(buf)) => {
            timestamp_array(buf, row_count, TimeUnit::Microsecond)?
        }
        (ColumnKind::TimestampNanos, DecodedColumn::TimestampNanos(buf)) => {
            timestamp_array(buf, row_count, TimeUnit::Nanosecond)?
        }
        (ColumnKind::Date, DecodedColumn::Date(buf)) => {
            timestamp_array(buf, row_count, TimeUnit::Millisecond)?
        }
        (ColumnKind::Uuid, DecodedColumn::Uuid(buf)) => fixed_bytes_array(buf, row_count, 16)?,
        (ColumnKind::Long256, DecodedColumn::Long256(buf)) => {
            fixed_bytes_array(buf, row_count, 32)?
        }
        (ColumnKind::Decimal64, DecodedColumn::Decimal64 { buffer, scale }) => {
            decimal_array(buffer, row_count, DataType::Decimal64(18, scale))?
        }
        (ColumnKind::Decimal128, DecodedColumn::Decimal128 { buffer, scale }) => {
            decimal_array(buffer, row_count, DataType::Decimal128(38, scale))?
        }
        (ColumnKind::Decimal256, DecodedColumn::Decimal256 { buffer, scale }) => {
            decimal_array(buffer, row_count, DataType::Decimal256(76, scale))?
        }
        (
            ColumnKind::Varchar,
            DecodedColumn::Varchar {
                offsets,
                data,
                validity,
            },
        ) => varlen_string_array(field, offsets, data, validity, row_count)?,
        (
            ColumnKind::Binary,
            DecodedColumn::Binary {
                offsets,
                data,
                validity,
            },
        ) => varlen_binary_array(field, offsets, data, validity, row_count)?,
        (
            ColumnKind::Geohash,
            DecodedColumn::Geohash {
                buffer,
                byte_width,
                precision_bits,
            },
        ) => geohash_array(buffer, byte_width, precision_bits, row_count)?,
        (
            ColumnKind::Symbol,
            DecodedColumn::Symbol {
                codes,
                validity,
                local_dict,
            },
        ) => match (local_dict.as_ref(), sym_scratch) {
            (Some(local), _) => symbol_array(codes, validity, local, row_count, None)?,
            (None, Some(scratch)) => {
                symbol_array_compact(codes, validity, dict, row_count, scratch)?
            }
            (None, None) => symbol_array(codes, validity, dict, row_count, Some(sym_values))?,
        },
        (ColumnKind::DoubleArray, DecodedColumn::DoubleArray(b)) => array_column_to_arrow(
            field,
            b,
            row_count,
            ArrayLeaf::Float64,
            degenerate_node_slack,
        )?,
        (ColumnKind::LongArray, DecodedColumn::LongArray(b)) => {
            array_column_to_arrow(field, b, row_count, ArrayLeaf::Int64, degenerate_node_slack)?
        }
        (kind, decoded) => {
            return Err(fmt!(
                ProtocolError,
                "kind/decoded mismatch: kind={:?} variant={:?}",
                kind,
                decoded
            ));
        }
    })
}

fn primitive_array(buf: ColumnBuffer, row_count: usize, dtype: DataType) -> Result<ArrayRef> {
    let nulls = bytes_null_buffer(&buf.validity, row_count)?;
    let values = buffer_to_arrow(&buf.values);
    let data = ArrayDataBuilder::new(dtype)
        .len(row_count)
        .add_buffer(values)
        .nulls(nulls)
        .align_buffers(true)
        .build()
        .map_err(|e| to_arrow_export(e.to_string()))?;
    Ok(arrow::array::make_array(data))
}

fn decimal_array(buf: ColumnBuffer, row_count: usize, dtype: DataType) -> Result<ArrayRef> {
    let nulls = bytes_null_buffer(&buf.validity, row_count)?;
    let values = buffer_to_arrow(&buf.values);
    let data = ArrayDataBuilder::new(dtype.clone())
        .len(row_count)
        .add_buffer(values)
        .nulls(nulls)
        .align_buffers(true)
        .build()
        .map_err(|e| to_arrow_export(e.to_string()))?;
    Ok(match dtype {
        DataType::Decimal64(_, _) => Arc::new(Decimal64Array::from(data)) as ArrayRef,
        DataType::Decimal128(_, _) => Arc::new(Decimal128Array::from(data)) as ArrayRef,
        DataType::Decimal256(_, _) => Arc::new(Decimal256Array::from(data)) as ArrayRef,
        _ => unreachable!(),
    })
}

fn timestamp_array(buf: ColumnBuffer, row_count: usize, unit: TimeUnit) -> Result<ArrayRef> {
    let nulls = bytes_null_buffer(&buf.validity, row_count)?;
    let values = buffer_to_arrow(&buf.values);
    let dtype = DataType::Timestamp(unit, Some(Arc::from("UTC")));
    let data = ArrayDataBuilder::new(dtype)
        .len(row_count)
        .add_buffer(values)
        .nulls(nulls)
        .align_buffers(true)
        .build()
        .map_err(|e| to_arrow_export(e.to_string()))?;
    let arr: ArrayRef = match unit {
        TimeUnit::Microsecond => Arc::new(TimestampMicrosecondArray::from(data)),
        TimeUnit::Nanosecond => Arc::new(TimestampNanosecondArray::from(data)),
        TimeUnit::Millisecond => Arc::new(TimestampMillisecondArray::from(data)),
        other => {
            return Err(fmt!(
                ProtocolError,
                "unsupported timestamp TimeUnit on egress: {:?}",
                other
            ));
        }
    };
    Ok(arr)
}

fn fixed_bytes_array(buf: ColumnBuffer, row_count: usize, n: i32) -> Result<ArrayRef> {
    let nulls = bytes_null_buffer(&buf.validity, row_count)?;
    let values = buffer_to_arrow(&buf.values);
    let data = ArrayDataBuilder::new(DataType::FixedSizeBinary(n))
        .len(row_count)
        .add_buffer(values)
        .nulls(nulls)
        .align_buffers(true)
        .build()
        .map_err(|e| to_arrow_export(e.to_string()))?;
    Ok(Arc::new(FixedSizeBinaryArray::from(data)) as ArrayRef)
}

fn varlen_string_array(
    _field: &Field,
    offsets: Vec<u32>,
    data: Bytes,
    validity: Option<Bytes>,
    row_count: usize,
) -> Result<ArrayRef> {
    let nulls = bytes_null_buffer(&validity, row_count)?;
    let off = offsets_to_arrow_buffer(offsets)?;
    // `decode_varlen` (utf8 = true) already validated the data buffer as UTF-8
    // with every offset on a codepoint boundary, so Arrow's `build()` would
    // re-run an O(data) UTF-8 scan for nothing — skip it with `build_unchecked`.
    // `align_buffers(true)` is still honored (it is independent of validation).
    //
    // SAFETY: per `decoder::decode_varlen`, the offsets are monotonic, start at
    // 0, end at `data.len()`, and every offset lies on a UTF-8 codepoint
    // boundary; `bytes_null_buffer` yields a `row_count`-bit null buffer and the
    // offset buffer has `row_count + 1` entries — exactly the `Utf8` layout.
    let data = unsafe {
        ArrayDataBuilder::new(DataType::Utf8)
            .len(row_count)
            .add_buffer(off)
            .add_buffer(bytes_to_arrow(data))
            .nulls(nulls)
            .align_buffers(true)
            .build_unchecked()
    };
    Ok(Arc::new(StringArray::from(data)) as ArrayRef)
}

fn varlen_binary_array(
    _field: &Field,
    offsets: Vec<u32>,
    data: Bytes,
    validity: Option<Bytes>,
    row_count: usize,
) -> Result<ArrayRef> {
    let nulls = bytes_null_buffer(&validity, row_count)?;
    let off = offsets_to_arrow_buffer(offsets)?;
    let data = ArrayDataBuilder::new(DataType::Binary)
        .len(row_count)
        .add_buffer(off)
        .add_buffer(bytes_to_arrow(data))
        .nulls(nulls)
        .align_buffers(true)
        .build()
        .map_err(|e| to_arrow_export(e.to_string()))?;
    Ok(Arc::new(BinaryArray::from(data)) as ArrayRef)
}

fn boolean_array(buf: ColumnBuffer, row_count: usize) -> Result<BooleanArray> {
    let nulls = bytes_null_buffer(&buf.validity, row_count)?;
    if buf.values.len() < row_count {
        return Err(fmt!(
            ProtocolError,
            "boolean wire payload truncated: have {} bytes, need {}",
            buf.values.len(),
            row_count
        ));
    }
    let mut packed = ABytes::with_capacity(64, row_count.div_ceil(8));
    packed.resize(row_count.div_ceil(8), 0);
    for (i, &b) in buf.values.iter().take(row_count).enumerate() {
        if b != 0 {
            packed[i >> 3] |= 1u8 << (i & 7);
        }
    }
    let buf = Buffer::from(bytes_from_avec(packed));
    let data = ArrayDataBuilder::new(DataType::Boolean)
        .len(row_count)
        .add_buffer(buf)
        .nulls(nulls)
        .align_buffers(true)
        .build()
        .map_err(|e| to_arrow_export(e.to_string()))?;
    Ok(BooleanArray::from(data))
}

fn geohash_array(
    buf: ColumnBuffer,
    byte_width: u8,
    precision_bits: u8,
    row_count: usize,
) -> Result<ArrayRef> {
    let nulls = bytes_null_buffer(&buf.validity, row_count)?;
    let (dtype, target_width) = match precision_bits {
        1..=7 => (DataType::Int8, 1usize),
        8..=15 => (DataType::Int16, 2),
        16..=31 => (DataType::Int32, 4),
        32..=60 => (DataType::Int64, 8),
        other => {
            return Err(fmt!(
                ProtocolError,
                "geohash precision_bits {} not in 1..=60",
                other
            ));
        }
    };
    let bw = byte_width as usize;
    let required = row_count
        .checked_mul(bw)
        .ok_or_else(|| fmt!(ProtocolError, "geohash payload size overflows usize"))?;
    if buf.values.len() < required {
        return Err(fmt!(
            ProtocolError,
            "geohash wire payload truncated: have {} bytes, need row_count={} * byte_width={} = {}",
            buf.values.len(),
            row_count,
            bw,
            required
        ));
    }
    let values_buf = if bw == target_width {
        buffer_to_arrow(&buf.values)
    } else if bw < target_width {
        widen_zero_extend(&buf.values, bw, target_width, row_count)?
    } else {
        return Err(fmt!(
            ProtocolError,
            "geohash wire byte_width {} exceeds Arrow target width {} for precision_bits {}",
            byte_width,
            target_width,
            precision_bits
        ));
    };
    let data = ArrayDataBuilder::new(dtype.clone())
        .len(row_count)
        .add_buffer(values_buf)
        .nulls(nulls)
        .align_buffers(true)
        .build()
        .map_err(|e| to_arrow_export(e.to_string()))?;
    Ok(match dtype {
        DataType::Int8 => Arc::new(Int8Array::from(data)) as ArrayRef,
        DataType::Int16 => Arc::new(Int16Array::from(data)) as ArrayRef,
        DataType::Int32 => Arc::new(Int32Array::from(data)) as ArrayRef,
        DataType::Int64 => Arc::new(Int64Array::from(data)) as ArrayRef,
        _ => unreachable!(),
    })
}

fn widen_zero_extend(
    src: &Bytes,
    src_width: usize,
    dst_width: usize,
    row_count: usize,
) -> Result<Buffer> {
    let dst_len = row_count.checked_mul(dst_width).ok_or_else(|| {
        fmt!(
            ProtocolError,
            "widen_zero_extend output size overflows usize"
        )
    })?;
    let mut out = ABytes::with_capacity(64, dst_len);
    out.resize(dst_len, 0);
    for r in 0..row_count {
        let s = r * src_width;
        let d = r * dst_width;
        out[d..d + src_width].copy_from_slice(&src[s..s + src_width]);
    }
    Ok(Buffer::from(bytes_from_avec(out)))
}

/// Build a SYMBOL column as `Dictionary(UInt32, Utf8)` by adopting the
/// decoder's global codes directly as the Arrow keys and the full active dict
/// as the values — no per-row remap. `decode_symbol` bounds-checks every
/// non-null code against the active dict size (`= dict.len()`) and leaves null
/// rows at code `0`, so each key is a valid index into `values` and the null
/// buffer masks the null rows. With `cache`, the values array is reused across
/// columns/batches and only rebuilt when the dict grows.
fn symbol_array(
    codes: Vec<u32>,
    validity: Option<Bytes>,
    dict: &SymbolDict,
    row_count: usize,
    cache: Option<&mut SymbolValuesCache>,
) -> Result<ArrayRef> {
    let nulls = bytes_null_buffer(&validity, row_count)?;
    let values = match cache {
        Some(c) if c.len == dict.len() && c.values.is_some() => {
            let cached = c.values.clone().unwrap();
            // A cache hit must reflect the current dict: every dict
            // replacement is required to run through `reset_symbol_caches`,
            // which clears this. Pin that invariant so a future replacement
            // path that forgets the reset trips here in test/CI builds rather
            // than silently serving stale symbol strings.
            debug_assert_eq!(
                cached.len(),
                dict.len(),
                "stale SymbolValuesCache: dict replaced without reset_symbol_caches"
            );
            cached
        }
        Some(c) => {
            let v = symbol_dict_values(dict)?;
            c.len = dict.len();
            c.values = Some(v.clone());
            v
        }
        None => symbol_dict_values(dict)?,
    };
    // `codes` already holds one `u32` global code per row; adopt its allocation
    // as the key buffer with no copy.
    let keys_buf = Buffer::from_vec(codes);
    let dict_data = ArrayDataBuilder::new(DataType::Dictionary(
        Box::new(DataType::UInt32),
        Box::new(DataType::Utf8),
    ))
    .len(row_count)
    .add_buffer(keys_buf)
    .add_child_data(values.into_data())
    .nulls(nulls)
    .build()
    .map_err(|e| to_arrow_export(e.to_string()))?;
    Ok(
        Arc::new(DictionaryArray::<arrow::array::types::UInt32Type>::from(
            dict_data,
        )) as ArrayRef,
    )
}

fn symbol_array_compact(
    codes: Vec<u32>,
    validity: Option<Bytes>,
    dict: &SymbolDict,
    row_count: usize,
    scratch: &mut SymbolBuildScratch,
) -> Result<ArrayRef> {
    let nulls = bytes_null_buffer(&validity, row_count)?;
    if scratch.remap.len() < dict.len() {
        scratch.remap.resize(dict.len(), SYMBOL_REMAP_UNSET);
    }
    let remap = scratch.remap.as_mut_slice();
    let touched = &mut scratch.touched;
    let mut union_offsets: Vec<i32> = Vec::with_capacity(dict.len().min(256) + 1);
    union_offsets.push(0);
    let mut union_bytes: ABytes = ABytes::new(64);
    let mut dense = ABytes::with_capacity(64, codes.len() * 4);
    dense.resize(codes.len() * 4, 0);
    let build = build_symbol_dense(
        &codes,
        nulls.as_ref(),
        dict,
        remap,
        touched,
        &mut union_offsets,
        &mut union_bytes,
        &mut dense[..],
    );
    for &code in touched.iter() {
        remap[code as usize] = SYMBOL_REMAP_UNSET;
    }
    touched.clear();
    build?;
    let values_data = ArrayDataBuilder::new(DataType::Utf8)
        .len(union_offsets.len() - 1)
        .add_buffer(Buffer::from_vec(union_offsets))
        .add_buffer(Buffer::from(bytes_from_avec(union_bytes)))
        .build()
        .map_err(|e| to_arrow_export(e.to_string()))?;
    let values = StringArray::from(values_data);
    let dict_data = ArrayDataBuilder::new(DataType::Dictionary(
        Box::new(DataType::UInt32),
        Box::new(DataType::Utf8),
    ))
    .len(row_count)
    .add_buffer(Buffer::from(bytes_from_avec(dense)))
    .add_child_data(values.into_data())
    .nulls(nulls)
    .build()
    .map_err(|e| to_arrow_export(e.to_string()))?;
    Ok(
        Arc::new(DictionaryArray::<arrow::array::types::UInt32Type>::from(
            dict_data,
        )) as ArrayRef,
    )
}

#[allow(clippy::too_many_arguments)]
fn build_symbol_dense(
    codes: &[u32],
    nulls: Option<&NullBuffer>,
    dict: &SymbolDict,
    remap: &mut [u32],
    touched: &mut Vec<u32>,
    union_offsets: &mut Vec<i32>,
    union_bytes: &mut ABytes,
    dense: &mut [u8],
) -> Result<()> {
    match nulls {
        None => {
            for (row, &code) in codes.iter().enumerate() {
                let dense_code =
                    resolve_symbol(code, remap, touched, union_offsets, union_bytes, dict)?;
                let base = row * 4;
                dense[base..base + 4].copy_from_slice(&dense_code.to_le_bytes());
            }
        }
        Some(n) => {
            for row in n.valid_indices() {
                let code = codes[row];
                let dense_code =
                    resolve_symbol(code, remap, touched, union_offsets, union_bytes, dict)?;
                let base = row * 4;
                dense[base..base + 4].copy_from_slice(&dense_code.to_le_bytes());
            }
        }
    }
    Ok(())
}

fn resolve_symbol(
    code: u32,
    remap: &mut [u32],
    touched: &mut Vec<u32>,
    union_offsets: &mut Vec<i32>,
    union_bytes: &mut ABytes,
    dict: &SymbolDict,
) -> Result<u32> {
    let slot = remap
        .get(code as usize)
        .copied()
        .ok_or_else(|| fmt!(ProtocolError, "symbol code {} not in dict", code))?;
    if slot != SYMBOL_REMAP_UNSET {
        return Ok(slot);
    }
    let s = dict
        .get(code)
        .ok_or_else(|| fmt!(ProtocolError, "symbol code {} not in dict", code))?;
    union_bytes.extend_from_slice(s.as_bytes());
    let next_off = i32::try_from(union_bytes.len()).map_err(|_| {
        fmt!(
            ProtocolError,
            "symbol union dictionary exceeds {} bytes",
            i32::MAX
        )
    })?;
    union_offsets.push(next_off);
    let assigned = (union_offsets.len() - 2) as u32;
    remap[code as usize] = assigned;
    touched.push(code);
    Ok(assigned)
}

/// The active dict's strings as a `Utf8` array, one entry per code. The dict's
/// arena is the entries' UTF-8 bytes back-to-back, so its offsets are the
/// entry offsets plus the arena length as the final terminator.
fn symbol_dict_values(dict: &SymbolDict) -> Result<StringArray> {
    let entries = dict.entries();
    let arena = dict.arena();
    let mut offsets: Vec<i32> = Vec::with_capacity(entries.len() + 1);
    for e in entries {
        offsets.push(i32::try_from(e.offset).map_err(|_| {
            fmt!(
                ProtocolError,
                "symbol dict offset {} exceeds i32::MAX",
                e.offset
            )
        })?);
    }
    offsets.push(i32::try_from(arena.len()).map_err(|_| {
        fmt!(
            ProtocolError,
            "symbol dict heap is {} bytes, exceeds i32::MAX",
            arena.len()
        )
    })?);
    let values_data = ArrayDataBuilder::new(DataType::Utf8)
        .len(entries.len())
        .add_buffer(Buffer::from_vec(offsets))
        .add_buffer(Buffer::from(arena.to_vec()))
        .build()
        .map_err(|e| to_arrow_export(e.to_string()))?;
    Ok(StringArray::from(values_data))
}

#[derive(Clone, Copy)]
enum ArrayLeaf {
    Float64,
    Int64,
}

fn array_column_to_arrow(
    field: &Field,
    b: ArrayBuffers,
    row_count: usize,
    leaf: ArrayLeaf,
    degenerate_node_slack: &mut usize,
) -> Result<ArrayRef> {
    let ArrayBuffers {
        data_offsets,
        data,
        shapes,
        shape_offsets,
        validity,
    } = b;
    let nulls = bytes_null_buffer(&validity, row_count)?;
    let leaf_dtype = match leaf {
        ArrayLeaf::Float64 => DataType::Float64,
        ArrayLeaf::Int64 => DataType::Int64,
    };
    let elem_size = 8usize;
    if !data.len().is_multiple_of(elem_size) {
        return Err(fmt!(
            ProtocolError,
            "ARRAY wire data length {} not a multiple of element size {}",
            data.len(),
            elem_size
        ));
    }
    let total_elements = data.len() / elem_size;
    if let Some(&last_off) = data_offsets.last()
        && last_off as usize != data.len()
    {
        return Err(fmt!(
            ProtocolError,
            "ARRAY data_offsets tail {} disagrees with data length {}",
            last_off,
            data.len()
        ));
    }
    let ndim = ndim_from_field(field)?;
    let leaf_buf = bytes_to_arrow(data);
    let leaf_data = ArrayDataBuilder::new(leaf_dtype)
        .len(total_elements)
        .add_buffer(leaf_buf)
        .align_buffers(true)
        .build()
        .map_err(|e| to_arrow_export(e.to_string()))?;
    let leaf_array: ArrayRef = match leaf {
        ArrayLeaf::Float64 => Arc::new(arrow::array::Float64Array::from(leaf_data)),
        ArrayLeaf::Int64 => Arc::new(arrow::array::Int64Array::from(leaf_data)),
    };
    let per_level_counts = compute_per_level_counts(
        &shapes,
        &shape_offsets,
        ndim,
        row_count,
        total_elements,
        degenerate_node_slack,
    )?;
    nest_lists(field, leaf_array, per_level_counts, nulls, ndim)
}

fn ndim_from_field(field: &Field) -> Result<usize> {
    fn depth(dt: &DataType, acc: usize) -> usize {
        match dt {
            DataType::List(inner) | DataType::LargeList(inner) => depth(inner.data_type(), acc + 1),
            _ => acc,
        }
    }
    let d = depth(field.data_type(), 0);
    if d == 0 {
        return Err(fmt!(
            InvalidApiCall,
            "expected nested list field, got {:?}",
            field.data_type()
        ));
    }
    Ok(d)
}

/// Initial value of the per-batch list-node *slack* pool, so genuinely empty
/// shapes (`[3, 0]`) decode. This is not re-granted per column: a single budget
/// seeded with this value is shared across every array column in a batch (see
/// `batch_to_record_batch`), so a wide batch of degenerate `[huge, 0]` columns
/// expands to at most ~64 MiB of offset buffers *in total*, not ~64 MiB *per
/// column*. The bound is per batch, not stream-global: a consumer that retains
/// every decoded batch still accumulates memory, which is inherent to holding
/// unbounded batches and is bounded per batch by the transport's
/// `MAX_BATCH_WIRE_BYTES` cap.
const MAX_DEGENERATE_LIST_NODES: usize = 16 * 1024 * 1024;

fn compute_per_level_counts(
    shapes: &[u32],
    shape_offsets: &[u32],
    ndim: usize,
    row_count: usize,
    leaf_elements: usize,
    degenerate_node_slack: &mut usize,
) -> Result<Vec<Vec<u32>>> {
    // Cap total list-node expansion: a degenerate `[huge, 0]` shape carries a
    // tiny payload yet asks for billions of empty inner lists. Valid dense
    // data needs at most `ndim * leaf + row_count` nodes and is always granted
    // per column; the *slack* covers genuine empty shapes like `[3, 0]` and is
    // drawn from `degenerate_node_slack`, a single pool shared across every
    // column in the batch, so the column count cannot multiply the degenerate
    // bound.
    let legit_nodes = leaf_elements.saturating_mul(ndim).saturating_add(row_count);
    let mut node_budget = legit_nodes.saturating_add(*degenerate_node_slack);
    let mut levels: Vec<Vec<u32>> = vec![Vec::new(); ndim];
    for row in 0..row_count {
        let lo = *shape_offsets
            .get(row)
            .ok_or_else(|| fmt!(ProtocolError, "shape_offsets missing row {}", row))?
            as usize;
        let hi = *shape_offsets.get(row + 1).ok_or_else(|| {
            fmt!(
                ProtocolError,
                "shape_offsets missing row {} terminator",
                row
            )
        })? as usize;
        if hi < lo || hi > shapes.len() {
            return Err(fmt!(
                ProtocolError,
                "row {} shape range [{}, {}) out of shapes len {}",
                row,
                lo,
                hi,
                shapes.len()
            ));
        }
        let span = hi - lo;
        if span == 0 {
            // A null / empty outer row is one empty list at level 0 and
            // contributes zero parent elements, hence zero entries at every
            // inner level. Pushing to inner levels would shift their offsets
            // and silently misalign every following non-null row.
            node_budget = node_budget
                .checked_sub(1)
                .ok_or_else(|| array_expansion_exceeded(row))?;
            levels[0].push(0);
            continue;
        }
        if span != ndim {
            return Err(fmt!(
                ProtocolError,
                "row {} has shape len {} expected ndim {}",
                row,
                span,
                ndim
            ));
        }
        let row_shape = &shapes[lo..hi];
        let mut group_count: u32 = 1;
        for (level, &dim) in row_shape.iter().enumerate() {
            let push_count = if level == 0 { 1 } else { group_count as usize };
            node_budget = node_budget
                .checked_sub(push_count)
                .ok_or_else(|| array_expansion_exceeded(row))?;
            for _ in 0..push_count {
                levels[level].push(dim);
            }
            group_count = group_count.checked_mul(dim).ok_or_else(|| {
                fmt!(
                    ProtocolError,
                    "row {} shape product overflows u32 at level {}",
                    row,
                    level
                )
            })?;
        }
    }
    // Debit the shared pool by the slack this column actually spent. Expansion
    // beyond the legitimate, payload-backed allowance comes out of the slack, so
    // the remainder `node_budget` is exactly the slack left when this column dug
    // into it; when it did not, `node_budget >= *degenerate_node_slack` and the
    // pool is unchanged. `min` captures both cases.
    *degenerate_node_slack = (*degenerate_node_slack).min(node_budget);
    Ok(levels)
}

fn array_expansion_exceeded(row: usize) -> Error {
    fmt!(
        ProtocolError,
        "array row {} list expansion exceeds the per-batch element budget",
        row
    )
}

fn nest_lists(
    field: &Field,
    leaf: ArrayRef,
    per_level_counts: Vec<Vec<u32>>,
    outer_nulls: Option<NullBuffer>,
    ndim: usize,
) -> Result<ArrayRef> {
    let mut current = leaf;
    let mut current_dtype = leaf_dtype_at_depth(field.data_type(), ndim);
    for level in (1..ndim).rev() {
        let counts = &per_level_counts[level];
        let offsets = counts_to_offsets_i32(counts)?;
        let next_field = Arc::new(Field::new("item", current_dtype, true));
        let dtype = DataType::List(next_field);
        let data = ArrayDataBuilder::new(dtype.clone())
            .len(counts.len())
            .add_buffer(Buffer::from(bytes_from_avec(offsets)))
            .add_child_data(current.to_data())
            .build()
            .map_err(|e| to_arrow_export(e.to_string()))?;
        current = Arc::new(ListArray::from(data)) as ArrayRef;
        current_dtype = dtype;
    }
    let counts0 = &per_level_counts[0];
    let outer_offsets = counts_to_offsets_i32(counts0)?;
    let outer_field = Arc::new(Field::new("item", current_dtype, true));
    let outer_dtype = DataType::List(outer_field);
    let data = ArrayDataBuilder::new(outer_dtype)
        .len(counts0.len())
        .add_buffer(Buffer::from(bytes_from_avec(outer_offsets)))
        .add_child_data(current.to_data())
        .nulls(outer_nulls)
        .build()
        .map_err(|e| to_arrow_export(e.to_string()))?;
    Ok(Arc::new(ListArray::from(data)) as ArrayRef)
}

fn leaf_dtype_at_depth(dt: &DataType, depth: usize) -> DataType {
    if depth == 0 {
        return dt.clone();
    }
    match dt {
        DataType::List(inner) | DataType::LargeList(inner) => {
            leaf_dtype_at_depth(inner.data_type(), depth - 1)
        }
        _ => dt.clone(),
    }
}

/// Returns Err on overflow. Per the server-side per-batch wire cap
/// (`MAX_BATCH_WIRE_BYTES = MAX_ZSTD_DECOMPRESSED = 64 MiB`) and
/// `MAX_ARRAY_ELEMENTS_PER_ROW = 16M`, the cumulative element count for
/// any List level in a single batch is bounded by ~8M, far below
/// i32::MAX. The error path is defensive.
fn counts_to_offsets_i32(counts: &[u32]) -> Result<ABytes> {
    let mut out = ABytes::with_capacity(64, (counts.len() + 1) * 4);
    let mut running: i32 = 0;
    out.extend_from_slice(&running.to_le_bytes());
    for &c in counts {
        let c = i32::try_from(c)
            .map_err(|_| fmt!(ProtocolError, "List child count {} exceeds i32::MAX", c))?;
        running = running
            .checked_add(c)
            .ok_or_else(|| fmt!(ProtocolError, "List offset overflows i32"))?;
        out.extend_from_slice(&running.to_le_bytes());
    }
    Ok(out)
}

/// Adopt the decoder's `u32` varlen offsets as Arrow's `i32` offset buffer
/// with no copy. Offsets are monotonic, so checking `last` bounds the slice.
fn offsets_to_arrow_buffer(offsets: Vec<u32>) -> Result<Buffer> {
    if let Some(&last) = offsets.last()
        && last > i32::MAX as u32
    {
        return Err(fmt!(
            ProtocolError,
            "varlen offset {} exceeds i32::MAX",
            last
        ));
    }
    let mut offsets = std::mem::ManuallyDrop::new(offsets);
    let ptr = offsets.as_mut_ptr() as *mut i32;
    let len = offsets.len();
    let cap = offsets.capacity();
    // SAFETY: `u32`/`i32` share layout and every value is `<= i32::MAX`
    // (checked); `offsets` is leaked so the allocation has a single owner.
    let offsets_i32 = unsafe { Vec::from_raw_parts(ptr, len, cap) };
    Ok(Buffer::from_vec(offsets_i32))
}

fn buffer_to_arrow(b: &Bytes) -> Buffer {
    Buffer::from(b.clone())
}

fn bytes_to_arrow(b: Bytes) -> Buffer {
    Buffer::from(b)
}

fn bytes_from_avec(v: ABytes) -> Bytes {
    Bytes::from_owner(v)
}

fn bytes_null_buffer(validity: &Option<Bytes>, row_count: usize) -> Result<Option<NullBuffer>> {
    let bytes = match validity {
        None => return Ok(None),
        Some(b) => b,
    };
    let needed = row_count.div_ceil(8);
    if bytes.len() < needed {
        return Err(fmt!(
            ProtocolError,
            "validity bitmap is {} bytes but row_count={} needs at least {}",
            bytes.len(),
            row_count,
            needed
        ));
    }
    let mut inverted = ABytes::with_capacity(64, needed);
    inverted.extend_from_slice(&bytes[..needed]);
    for b in inverted.iter_mut() {
        *b = !*b;
    }
    // Mask post-inversion trailing bits — pads were 0, would flip to 1
    // (=valid) and pollute downstream raw-bitmap hashers/copiers.
    let trailing_bits = row_count % 8;
    if trailing_bits != 0
        && let Some(last) = inverted.last_mut()
    {
        *last &= (1u8 << trailing_bits) - 1;
    }
    Ok(Some(NullBuffer::new(arrow::buffer::BooleanBuffer::new(
        Buffer::from(bytes_from_avec(inverted)),
        0,
        row_count,
    ))))
}

/// Boxes a QuestDB [`Error`] as an [`ArrowError::ExternalError`].
/// Recover via [`try_downcast_questdb`](super::reader::try_downcast_questdb).
pub fn external_arrow_error(e: Error) -> ArrowError {
    ArrowError::ExternalError(Box::new(e))
}

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

    #[test]
    fn per_level_counts_rejects_degenerate_expansion() {
        let shapes = [1u32 << 24, 0, 1u32 << 24, 0];
        let shape_offsets = [0u32, 2, 4];
        let mut slack = MAX_DEGENERATE_LIST_NODES;
        let err =
            compute_per_level_counts(&shapes, &shape_offsets, 2, 2, 0, &mut slack).unwrap_err();
        assert_eq!(err.code(), ErrorCode::ProtocolError);
    }

    #[test]
    fn per_level_counts_slack_is_shared_across_columns() {
        // Two array columns in one batch, each a single degenerate `[16M, 0]`
        // row, sharing one slack pool. The first column drains the pool; the
        // second is then rejected. This caps per-batch degenerate expansion at
        // ~64 MiB no matter how many such columns the batch carries (the old
        // per-column reset allowed ~64 MiB * column_count).
        let shapes = [1u32 << 24, 0];
        let shape_offsets = [0u32, 2];
        let mut slack = MAX_DEGENERATE_LIST_NODES;

        let counts =
            compute_per_level_counts(&shapes, &shape_offsets, 2, 1, 0, &mut slack).unwrap();
        assert_eq!(counts[0], vec![1u32 << 24]);
        assert_eq!(slack, 0, "first degenerate column should drain the pool");

        let err =
            compute_per_level_counts(&shapes, &shape_offsets, 2, 1, 0, &mut slack).unwrap_err();
        assert_eq!(err.code(), ErrorCode::ProtocolError);
    }

    #[test]
    fn per_level_counts_accepts_genuine_empty_shape() {
        let shapes = [3u32, 0];
        let shape_offsets = [0u32, 2];
        let mut slack = MAX_DEGENERATE_LIST_NODES;
        let counts =
            compute_per_level_counts(&shapes, &shape_offsets, 2, 1, 0, &mut slack).unwrap();
        assert_eq!(counts[0], vec![3]);
        assert_eq!(counts[1], vec![0, 0, 0]);
    }

    #[test]
    fn per_level_counts_accepts_dense_shape() {
        let shapes = [2u32, 3];
        let shape_offsets = [0u32, 2];
        let mut slack = MAX_DEGENERATE_LIST_NODES;
        let counts =
            compute_per_level_counts(&shapes, &shape_offsets, 2, 1, 6, &mut slack).unwrap();
        assert_eq!(counts[0], vec![2]);
        assert_eq!(counts[1], vec![3, 3]);
    }

    #[test]
    fn per_level_counts_dense_does_not_drain_slack() {
        // Legitimate, payload-backed dense data is granted per column and must
        // not consume the shared degenerate pool, so it never starves sibling
        // columns in the same batch.
        let shapes = [2u32, 3];
        let shape_offsets = [0u32, 2];
        let mut slack = MAX_DEGENERATE_LIST_NODES;
        let _ = compute_per_level_counts(&shapes, &shape_offsets, 2, 1, 6, &mut slack).unwrap();
        assert_eq!(slack, MAX_DEGENERATE_LIST_NODES);
    }

    #[test]
    fn counts_to_offsets_rejects_child_count_above_i32_max() {
        let err = counts_to_offsets_i32(&[i32::MAX as u32 + 1]).unwrap_err();
        assert_eq!(err.code(), ErrorCode::ProtocolError);
        assert!(err.msg().contains("exceeds i32::MAX"), "got: {}", err.msg());
    }

    #[test]
    fn counts_to_offsets_rejects_cumulative_i32_overflow() {
        let err = counts_to_offsets_i32(&[i32::MAX as u32, 1]).unwrap_err();
        assert_eq!(err.code(), ErrorCode::ProtocolError);
        assert!(err.msg().contains("overflows i32"), "got: {}", err.msg());
    }

    #[test]
    fn offsets_to_arrow_buffer_rejects_offset_above_i32_max() {
        let err = offsets_to_arrow_buffer(vec![0, i32::MAX as u32 + 1]).unwrap_err();
        assert_eq!(err.code(), ErrorCode::ProtocolError);
        assert!(err.msg().contains("varlen offset"), "got: {}", err.msg());
    }
}