lightstream 0.5.0

Composable, zero-copy Arrow IPC and native data streaming for Rust with SIMD-aligned I/O, async support, and memory-mapping.
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
// Copyright Peter G. Bower 2025-2026.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! # CSV Decoder for Minarrow Tables
//!
//! - Accepts a CSV byte slice or any [`BufRead`](std::io::BufRead).
//! - Infers schema or uses a provided schema (optional).
//! - Supports: `Int32`, `Int64`, `UInt32`, `UInt64`, `Float32`, `Float64`, `Boolean`, `String32`, `Categorical32`, `Categorical8`.
//! - Custom delimiter, nulls, quoting, and dictionary mapping for categoricals.
//! - Produces a single [`Table`](minarrow::Table) via [`decode_csv`](crate::models::decoders::csv::decode_csv), or multiple batches via repeated calls to [`decode_csv_batch`](crate::models::decoders::csv::decode_csv_batch).
//!
//! ## Fast path
//!
//! `decode_csv` reads the entire input in one `read_to_end` call,
//! then pre-scans the byte buffer with `memchr3` (quote-aware) to
//! build an index of field positions. Columns are built directly
//! from byte slices into the buffer, so the only per-cell allocation
//! comes from quoted cells that contain embedded `""` escapes.
//! Schema inference samples the first 1024 data rows.
//!
//! Notes:
//! - Input is treated as UTF-8; invalid byte sequences are lossily decoded via `String::from_utf8_lossy`.
//! - See [`CsvDecodeOptions`](crate::models::decoders::csv::CsvDecodeOptions) for configurable delimiter, quoting, header handling, and schema control.

use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::io::{self, BufRead, Cursor};
use std::sync::Arc;

use minarrow::ffi::arrow_dtype::CategoricalIndexType;
use minarrow::{
    Array, ArrowType, Bitmask, Buffer, Field, FieldArray, FloatArray, IntegerArray, NumericArray,
    Table, TextArray, Vec64, vec64,
};

/// Options for CSV decoding.
#[derive(Debug, Clone)]
pub struct CsvDecodeOptions {
    /// Delimiter (e.g., b',' for CSV, b'\t' for TSV).
    pub delimiter: u8,
    /// String(s) that should be parsed as nulls.
    pub nulls: Vec<&'static str>,
    /// Quote character to use (default: '"').
    pub quote: u8,
    /// Whether to use the first row as a header.
    pub has_header: bool,
    /// Optional schema. If None, schema is inferred.
    pub schema: Option<Vec<Field>>,
    /// If true, all columns are loaded as String32.
    pub all_as_text: bool,
    /// For categoricals: columns that should be parsed as categorical.
    pub categorical_cols: HashSet<String>,
}

impl Default for CsvDecodeOptions {
    fn default() -> Self {
        CsvDecodeOptions {
            delimiter: b',',
            nulls: vec!["", "NA", "null", "NULL"],
            quote: b'"',
            has_header: true,
            schema: None,
            all_as_text: false,
            categorical_cols: HashSet::new(),
        }
    }
}

/// Number of data rows sampled for schema type inference.
const INFER_SAMPLE: usize = 1024;

/// Attempt to read *up to* `batch_size` *data* rows (plus one header row if `has_header`
/// is still true) from `reader`, and decode them into a single `Table`.  Returns
/// `Ok(None)` if there are no more rows to read.
pub fn decode_csv_batch<R: BufRead>(
    reader: &mut R,
    options: &CsvDecodeOptions,
    batch_size: usize,
) -> io::Result<Option<Table>> {
    let opts = options.clone();
    let need_header = opts.has_header;
    let mut buf = Vec::new();
    let mut chunk = Vec::new();
    let mut saw_any = false;
    let mut lines_to_read = batch_size;
    if need_header {
        // we need to read one extra line for the header
        lines_to_read += 1;
    }

    for _ in 0..lines_to_read {
        buf.clear();
        let n = reader.read_until(b'\n', &mut buf)?;
        if n == 0 {
            break;
        }
        // strip "\r\n" or "\n"
        if buf.ends_with(b"\r\n") {
            buf.truncate(buf.len() - 2);
        } else if buf.ends_with(b"\n") {
            buf.truncate(buf.len() - 1);
        }
        // skip leading blank lines
        if buf.is_empty() && !saw_any {
            continue;
        }
        saw_any = true;
        chunk.extend_from_slice(&buf);
        chunk.push(b'\n');
    }

    if !saw_any {
        // nothing read at all -> EOF
        return Ok(None);
    }

    // Now decode exactly that chunk
    let table = decode_csv(Cursor::new(chunk), &opts)?;
    Ok(Some(table))
}

/// Decodes CSV from a BufRead into a Minarrow Table.
/// Schema is inferred unless provided.
/// Errors propagate if CSV is malformed or parsing fails.
///
/// # Arguments
/// - `reader`: Any `BufRead` (e.g., `&[u8]`, `File`).
/// - `options`: CSV decode options.
///
/// # Returns
/// - On success, a Minarrow Table.
pub fn decode_csv<R: BufRead>(mut reader: R, options: &CsvDecodeOptions) -> io::Result<Table> {
    let mut buf = Vec::new();
    reader.read_to_end(&mut buf)?;
    decode_csv_bytes(&buf, options)
}

/// Decode an in-memory CSV byte slice into a Table.
///
/// Reads the entire byte buffer once to build a structural index of
/// field positions (quote-aware) and then builds columns directly
/// from byte slices into the buffer. Suitable for callers that
/// already have the CSV in memory (e.g. from `mmap` or
/// `read_to_end`).
pub fn decode_csv_bytes(buf: &[u8], options: &CsvDecodeOptions) -> io::Result<Table> {
    let CsvDecodeOptions {
        delimiter,
        ref nulls,
        quote,
        has_header,
        ref schema,
        all_as_text,
        ref categorical_cols,
    } = *options;

    // Skip any leading blank lines so the header detection lines up with
    // the first row that actually has data.
    let mut head = 0usize;
    while head < buf.len() && (buf[head] == b'\n' || buf[head] == b'\r') {
        head += 1;
    }
    let buf = &buf[head..];

    if buf.is_empty() {
        return Ok(Table {
            name: "csv".to_string(),
            cols: Vec::new(),
            n_rows: 0,
            ..Default::default()
        });
    }

    // Pass 1: quote-aware structural scan.
    //
    // `field_starts[i]` is the byte offset of the first byte of the i-th
    // field. The byte immediately before `field_starts[i+1]` is the
    // delimiter or newline that terminated field i (so field i's bytes
    // are `buf[field_starts[i] .. field_starts[i+1] - 1]`). The last
    // field is sentineled by an entry at `buf.len() + 1` so the same
    // `end - 1` math gives `buf.len()` for it.
    //
    // `row_end_fields[r]` is the index into `field_starts` of the field
    // that ended row r (the field whose terminator was a newline rather
    // than a delimiter).
    let n = buf.len();
    let mut field_starts: Vec<u32> = Vec::with_capacity(n / 16 + 16);
    field_starts.push(0);
    let mut row_end_fields: Vec<u32> = Vec::with_capacity(n / 64 + 16);

    let mut pos: usize = 0;
    while pos < n {
        let off = match memchr::memchr3(delimiter, b'\n', quote, &buf[pos..]) {
            Some(o) => o,
            None => break,
        };
        let abs = pos + off;
        let b = buf[abs];

        if b == quote {
            // Skip past the matching close quote, honouring `""` as an
            // embedded escape.
            pos = abs + 1;
            loop {
                let qoff = match memchr::memchr(quote, &buf[pos..]) {
                    Some(o) => o,
                    None => {
                        // Unterminated quote: stop scanning. Subsequent
                        // row-width validation will surface the error.
                        pos = n;
                        break;
                    }
                };
                let qabs = pos + qoff;
                if qabs + 1 < n && buf[qabs + 1] == quote {
                    pos = qabs + 2;
                } else {
                    pos = qabs + 1;
                    break;
                }
            }
        } else {
            // delimiter or newline: this field is now closed.
            let just_closed = field_starts.len() - 1;
            field_starts.push((abs + 1) as u32);
            if b == b'\n' {
                row_end_fields.push(just_closed as u32);
            }
            pos = abs + 1;
        }
    }

    // Trailing data without a closing newline still forms a row.
    let trailing_data = field_starts
        .last()
        .copied()
        .map(|s| (s as usize) < n)
        .unwrap_or(false);
    if trailing_data {
        let just_closed = field_starts.len() - 1;
        // Sentinel so cell end == `buf.len()` for the last field.
        field_starts.push((n + 1) as u32);
        row_end_fields.push(just_closed as u32);
    }

    let n_total_rows = row_end_fields.len();
    if n_total_rows == 0 {
        return Ok(Table {
            name: "csv".to_string(),
            cols: Vec::new(),
            n_rows: 0,
            ..Default::default()
        });
    }

    // Number of columns = number of fields in the first row.
    let n_cols = (row_end_fields[0] + 1) as usize;

    // Validate row width consistency.
    for r in 1..n_total_rows {
        let prev = row_end_fields[r - 1] as usize;
        let curr = row_end_fields[r] as usize;
        if curr - prev != n_cols {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "inconsistent row length",
            ));
        }
    }

    // Resolve the byte slice for a given flat field index, stripping a
    // trailing `\r` (so `\r\n` line endings parse cleanly).
    let field_bytes = |idx: usize| -> &[u8] {
        let s = field_starts[idx] as usize;
        let e_raw = field_starts[idx + 1] as usize;
        let e = e_raw.saturating_sub(1).min(n);
        let raw = &buf[s..e];
        if raw.last() == Some(&b'\r') {
            &raw[..raw.len() - 1]
        } else {
            raw
        }
    };

    // After row-width validation we know every row has exactly
    // `n_cols` fields, so the flat field index for cell (row, col)
    // collapses to a direct multiply. This is much hotter than the
    // row_end_fields lookup we used during scanning.
    let cell_field_idx = |row: usize, col: usize| -> usize { row * n_cols + col };

    // Header row: take column names from row 0, then everything else is data.
    let (data_row_offset, col_names): (usize, Vec<String>) = if has_header {
        let mut names = Vec::with_capacity(n_cols);
        for c in 0..n_cols {
            let f = field_bytes(cell_field_idx(0, c));
            let unq = unquote(f, quote);
            names.push(String::from_utf8_lossy(&unq).into_owned());
        }
        (1, names)
    } else {
        (0, (0..n_cols).map(|i| format!("col{}", i + 1)).collect())
    };

    let n_rows = n_total_rows - data_row_offset;

    // Schema inference (sample-based) when not provided. Operates on
    // byte slices directly - no per-cell String allocation, no UTF-8
    // round trips.
    let schema: Vec<Field> = if let Some(s) = schema.clone() {
        s
    } else if all_as_text {
        col_names
            .iter()
            .map(|name| Field {
                name: name.clone(),
                dtype: ArrowType::String,
                nullable: true,
                metadata: Default::default(),
            })
            .collect()
    } else {
        let sample = n_rows.min(INFER_SAMPLE);
        let mut types: Vec<ArrowType> = vec![ArrowType::String; n_cols];
        for col in 0..n_cols {
            let is_cat_col = categorical_cols.contains(&col_names[col]);
            let mut is_bool = true;
            let mut is_i32 = true;
            let mut is_i64 = true;
            let mut is_u32 = true;
            let mut is_u64 = true;
            let mut is_f32 = true;
            let mut is_f64 = true;

            for row in 0..sample {
                let raw = field_bytes(cell_field_idx(row + data_row_offset, col));
                let unq = unquote(raw, quote);
                let vb = trim_ascii(&unq);
                if is_null_bytes(vb, nulls) {
                    continue;
                }
                if is_bool && !is_bool_token(vb) {
                    is_bool = false;
                }
                if is_i32 && super::int_ascii::parse_ascii_int::<i32>(vb).is_none() {
                    is_i32 = false;
                }
                if is_i64 && super::int_ascii::parse_ascii_int::<i64>(vb).is_none() {
                    is_i64 = false;
                }
                if is_u32 && super::int_ascii::parse_ascii_int::<u32>(vb).is_none() {
                    is_u32 = false;
                }
                if is_u64 && super::int_ascii::parse_ascii_int::<u64>(vb).is_none() {
                    is_u64 = false;
                }
                if is_f32 && fast_float2::parse::<f32, _>(vb).is_err() {
                    is_f32 = false;
                }
                if is_f64 && fast_float2::parse::<f64, _>(vb).is_err() {
                    is_f64 = false;
                }
            }

            types[col] = if is_bool {
                ArrowType::Boolean
            } else if is_i32 {
                ArrowType::Int32
            } else if is_i64 {
                ArrowType::Int64
            } else if is_u32 {
                ArrowType::UInt32
            } else if is_u64 {
                ArrowType::UInt64
            } else if is_f64 {
                ArrowType::Float64
            } else if is_f32 {
                ArrowType::Float32
            } else if is_cat_col {
                #[cfg(not(feature = "default_categorical_8"))]
                {
                    ArrowType::Dictionary(CategoricalIndexType::UInt32)
                }
                #[cfg(feature = "default_categorical_8")]
                {
                    ArrowType::Dictionary(CategoricalIndexType::UInt8)
                }
            } else {
                ArrowType::String
            };
        }

        col_names
            .iter()
            .enumerate()
            .map(|(i, name)| Field {
                name: name.clone(),
                dtype: types[i].clone(),
                nullable: true,
                metadata: Default::default(),
            })
            .collect()
    };

    // Build each column in a single pass: extract cell bytes, unquote,
    // trim, null-check, and parse straight into the output buffer. No
    // Vec<Cow<[u8]>> intermediate, no double-walk over the data.
    let mut cols: Vec<FieldArray> = Vec::with_capacity(n_cols);
    for (col_idx, field) in schema.iter().enumerate() {
        let mut null_bools = vec![true; n_rows];
        let mut null_count = 0usize;

        let array = match &field.dtype {
            ArrowType::Int32 => build_int_col_inline::<i32>(
                buf,
                &field_starts,
                n_cols,
                data_row_offset,
                col_idx,
                n_rows,
                quote,
                nulls,
                &mut null_bools,
                &mut null_count,
            )?,
            ArrowType::Int64 => build_int_col_inline::<i64>(
                buf,
                &field_starts,
                n_cols,
                data_row_offset,
                col_idx,
                n_rows,
                quote,
                nulls,
                &mut null_bools,
                &mut null_count,
            )?,
            ArrowType::UInt32 => build_int_col_inline::<u32>(
                buf,
                &field_starts,
                n_cols,
                data_row_offset,
                col_idx,
                n_rows,
                quote,
                nulls,
                &mut null_bools,
                &mut null_count,
            )?,
            ArrowType::UInt64 => build_int_col_inline::<u64>(
                buf,
                &field_starts,
                n_cols,
                data_row_offset,
                col_idx,
                n_rows,
                quote,
                nulls,
                &mut null_bools,
                &mut null_count,
            )?,
            ArrowType::Float32 => build_float_col_inline::<f32>(
                buf,
                &field_starts,
                n_cols,
                data_row_offset,
                col_idx,
                n_rows,
                quote,
                nulls,
                &mut null_bools,
                &mut null_count,
            )?,
            ArrowType::Float64 => build_float_col_inline::<f64>(
                buf,
                &field_starts,
                n_cols,
                data_row_offset,
                col_idx,
                n_rows,
                quote,
                nulls,
                &mut null_bools,
                &mut null_count,
            )?,
            ArrowType::Boolean => build_bool_col_inline(
                buf,
                &field_starts,
                n_cols,
                data_row_offset,
                col_idx,
                n_rows,
                quote,
                nulls,
                &mut null_bools,
                &mut null_count,
            )?,
            ArrowType::String => build_string_col_inline(
                buf,
                &field_starts,
                n_cols,
                data_row_offset,
                col_idx,
                n_rows,
                quote,
                nulls,
                &mut null_bools,
                &mut null_count,
            )?,
            ArrowType::Dictionary(_) => build_categorical_col_inline(
                buf,
                &field_starts,
                n_cols,
                data_row_offset,
                col_idx,
                n_rows,
                quote,
                nulls,
                &mut null_bools,
                &mut null_count,
            )?,
            _ => build_string_col_inline(
                buf,
                &field_starts,
                n_cols,
                data_row_offset,
                col_idx,
                n_rows,
                quote,
                nulls,
                &mut null_bools,
                &mut null_count,
            )?,
        };

        cols.push(FieldArray {
            field: Arc::new(field.clone()),
            array,
            null_count,
        });
    }

    Ok(Table {
        name: "csv".to_string(),
        cols,
        n_rows,
        ..Default::default()
    })
}

/// Resolve the byte slice for a given flat field index. Strips a
/// trailing `\r` so `\r\n` line endings parse cleanly.
#[inline]
fn cell_raw<'a>(buf: &'a [u8], field_starts: &[u32], field_idx: usize) -> &'a [u8] {
    let s = field_starts[field_idx] as usize;
    let e_raw = field_starts[field_idx + 1] as usize;
    let e = e_raw.saturating_sub(1).min(buf.len());
    let raw = &buf[s..e];
    if raw.last() == Some(&b'\r') {
        &raw[..raw.len() - 1]
    } else {
        raw
    }
}

/// Test whether the (trimmed) byte slice matches any configured null
/// token, including the implicit empty-string null. ASCII-case
/// insensitive to mirror the original `str` comparison.
#[inline]
fn is_null_bytes(bytes: &[u8], nulls: &[&'static str]) -> bool {
    if bytes.is_empty() {
        return true;
    }
    for n in nulls {
        let nb = n.as_bytes();
        if nb.len() == bytes.len()
            && nb
                .iter()
                .zip(bytes.iter())
                .all(|(a, b)| a.eq_ignore_ascii_case(b))
        {
            return true;
        }
    }
    false
}

/// Recognise the standard CSV boolean tokens.
#[inline]
fn is_bool_token(bytes: &[u8]) -> bool {
    matches!(
        bytes,
        b"true"
            | b"True"
            | b"TRUE"
            | b"false"
            | b"False"
            | b"FALSE"
            | b"1"
            | b"0"
            | b"t"
            | b"T"
            | b"f"
            | b"F"
    )
}

/// Recognise the standard CSV boolean true tokens. False if not a
/// boolean token at all; callers should have null-checked first.
#[inline]
fn parse_bool(bytes: &[u8]) -> bool {
    matches!(bytes, b"true" | b"True" | b"TRUE" | b"1" | b"t" | b"T")
}

/// Strip outer quote characters and unescape doubled-`""` sequences.
/// The common case (no outer quote) returns a borrow; only quoted
/// cells containing an embedded `""` allocate.
#[inline]
fn unquote<'a>(bytes: &'a [u8], quote: u8) -> Cow<'a, [u8]> {
    if bytes.len() >= 2 && bytes[0] == quote && bytes[bytes.len() - 1] == quote {
        let inner = &bytes[1..bytes.len() - 1];
        if memchr::memchr(quote, inner).is_some() {
            let mut out = Vec::with_capacity(inner.len());
            let mut i = 0;
            while i < inner.len() {
                if inner[i] == quote && i + 1 < inner.len() && inner[i + 1] == quote {
                    out.push(quote);
                    i += 2;
                } else {
                    out.push(inner[i]);
                    i += 1;
                }
            }
            Cow::Owned(out)
        } else {
            Cow::Borrowed(inner)
        }
    } else {
        Cow::Borrowed(bytes)
    }
}

/// Trim leading/trailing ASCII whitespace without allocating.
#[inline]
fn trim_ascii(bytes: &[u8]) -> &[u8] {
    let start = bytes
        .iter()
        .position(|b| !b.is_ascii_whitespace())
        .unwrap_or(bytes.len());
    let end = bytes
        .iter()
        .rposition(|b| !b.is_ascii_whitespace())
        .map(|p| p + 1)
        .unwrap_or(0);
    if start >= end {
        &[]
    } else {
        &bytes[start..end]
    }
}

// ------- Column builders -------

fn mask_to_bitmask(mask: &[bool]) -> Bitmask {
    Bitmask::from_bools(mask)
}

/// Per-row inline integer column builder: extract cell bytes,
/// unquote, trim, null-check, parse straight into the output buffer.
/// No `Vec<Cow<[u8]>>` intermediate.
#[allow(clippy::too_many_arguments)]
fn build_int_col_inline<T>(
    buf: &[u8],
    field_starts: &[u32],
    n_cols: usize,
    data_row_offset: usize,
    col_idx: usize,
    n_rows: usize,
    quote: u8,
    nulls: &[&'static str],
    null_bools: &mut [bool],
    null_count: &mut usize,
) -> io::Result<Array>
where
    T: super::int_ascii::ParseAsciiInt + Copy + Default + 'static,
{
    let mut out: Vec64<T> = vec64![T::default(); n_rows];
    for r in 0..n_rows {
        let raw = cell_raw(buf, field_starts, (r + data_row_offset) * n_cols + col_idx);
        let unq = unquote(raw, quote);
        let vb = trim_ascii(&unq);
        if is_null_bytes(vb, nulls) {
            null_bools[r] = false;
            *null_count += 1;
            continue;
        }
        out[r] = super::int_ascii::parse_ascii_int::<T>(vb)
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "failed to parse integer"))?;
    }
    pack_numeric(out, null_bools)
}

/// Per-row inline float column builder. Uses fast-float2's
/// Eisel-Lemire fast path, the same algorithm Rust std uses since
/// 1.55.
#[allow(clippy::too_many_arguments)]
fn build_float_col_inline<T>(
    buf: &[u8],
    field_starts: &[u32],
    n_cols: usize,
    data_row_offset: usize,
    col_idx: usize,
    n_rows: usize,
    quote: u8,
    nulls: &[&'static str],
    null_bools: &mut [bool],
    null_count: &mut usize,
) -> io::Result<Array>
where
    T: fast_float2::FastFloat + Copy + Default + 'static,
{
    let mut out: Vec64<T> = vec64![T::default(); n_rows];
    for r in 0..n_rows {
        let raw = cell_raw(buf, field_starts, (r + data_row_offset) * n_cols + col_idx);
        let unq = unquote(raw, quote);
        let vb = trim_ascii(&unq);
        if is_null_bytes(vb, nulls) {
            null_bools[r] = false;
            *null_count += 1;
            continue;
        }
        out[r] = fast_float2::parse::<T, _>(vb)
            .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "failed to parse float"))?;
    }
    pack_numeric(out, null_bools)
}

fn pack_numeric<T: 'static>(out: Vec64<T>, null_mask: &[bool]) -> io::Result<Array> {
    let mask = Some(mask_to_bitmask(null_mask));
    // SAFETY note for the six `transmute::<Vec64<T>, Vec64<U>>` calls
    // below: each branch is gated by `TypeId::of::<T>() == TypeId::of::<U>()`,
    // so the source and target Vec64s have the same `T = U` element type,
    // size, alignment, and validity invariants. The transmute is an identity
    // copy that the compiler cannot prove statically because T is generic.
    let arr = if std::any::TypeId::of::<T>() == std::any::TypeId::of::<i32>() {
        Array::NumericArray(NumericArray::Int32(
            IntegerArray {
                data: Buffer::from(unsafe { std::mem::transmute::<Vec64<T>, Vec64<i32>>(out) }),
                null_mask: mask,
            }
            .into(),
        ))
    } else if std::any::TypeId::of::<T>() == std::any::TypeId::of::<i64>() {
        Array::NumericArray(NumericArray::Int64(
            IntegerArray {
                data: Buffer::from(unsafe { std::mem::transmute::<Vec64<T>, Vec64<i64>>(out) }),
                null_mask: mask,
            }
            .into(),
        ))
    } else if std::any::TypeId::of::<T>() == std::any::TypeId::of::<u32>() {
        Array::NumericArray(NumericArray::UInt32(
            IntegerArray {
                data: Buffer::from(unsafe { std::mem::transmute::<Vec64<T>, Vec64<u32>>(out) }),
                null_mask: mask,
            }
            .into(),
        ))
    } else if std::any::TypeId::of::<T>() == std::any::TypeId::of::<u64>() {
        Array::NumericArray(NumericArray::UInt64(
            IntegerArray {
                data: Buffer::from(unsafe { std::mem::transmute::<Vec64<T>, Vec64<u64>>(out) }),
                null_mask: mask,
            }
            .into(),
        ))
    } else if std::any::TypeId::of::<T>() == std::any::TypeId::of::<f32>() {
        Array::NumericArray(NumericArray::Float32(
            FloatArray {
                data: Buffer::from(unsafe { std::mem::transmute::<Vec64<T>, Vec64<f32>>(out) }),
                null_mask: mask,
            }
            .into(),
        ))
    } else if std::any::TypeId::of::<T>() == std::any::TypeId::of::<f64>() {
        Array::NumericArray(NumericArray::Float64(
            FloatArray {
                data: Buffer::from(unsafe { std::mem::transmute::<Vec64<T>, Vec64<f64>>(out) }),
                null_mask: mask,
            }
            .into(),
        ))
    } else {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "unsupported numeric type",
        ));
    };
    Ok(arr)
}

#[allow(clippy::too_many_arguments)]
fn build_bool_col_inline(
    buf: &[u8],
    field_starts: &[u32],
    n_cols: usize,
    data_row_offset: usize,
    col_idx: usize,
    n_rows: usize,
    quote: u8,
    nulls: &[&'static str],
    null_bools: &mut [bool],
    null_count: &mut usize,
) -> io::Result<Array> {
    let mut out: Vec64<bool> = vec64![false; n_rows];
    for r in 0..n_rows {
        let raw = cell_raw(buf, field_starts, (r + data_row_offset) * n_cols + col_idx);
        let unq = unquote(raw, quote);
        let vb = trim_ascii(&unq);
        if is_null_bytes(vb, nulls) {
            null_bools[r] = false;
            *null_count += 1;
            continue;
        }
        out[r] = parse_bool(vb);
    }
    Ok(Array::BooleanArray(
        minarrow::BooleanArray::new(Bitmask::from_bools(&out), Some(mask_to_bitmask(null_bools)))
            .into(),
    ))
}

#[allow(clippy::too_many_arguments)]
fn build_string_col_inline(
    buf: &[u8],
    field_starts: &[u32],
    n_cols: usize,
    data_row_offset: usize,
    col_idx: usize,
    n_rows: usize,
    quote: u8,
    nulls: &[&'static str],
    null_bools: &mut [bool],
    null_count: &mut usize,
) -> io::Result<Array> {
    let mut offsets: Vec64<u32> = vec64![0u32; n_rows + 1];
    // 8 bytes/row is a conservative starting guess; Vec64 doubles
    // from here if needed.
    let mut data: Vec64<u8> = Vec64::with_capacity(n_rows * 8);
    let mut pos: u32 = 0;
    for r in 0..n_rows {
        let raw = cell_raw(buf, field_starts, (r + data_row_offset) * n_cols + col_idx);
        let unq = unquote(raw, quote);
        // Strings keep leading/trailing whitespace; only trim for the
        // null check.
        let trimmed = trim_ascii(&unq);
        if is_null_bytes(trimmed, nulls) {
            null_bools[r] = false;
            *null_count += 1;
        } else {
            data.extend_from_slice(&unq);
            pos += unq.len() as u32;
        }
        offsets[r + 1] = pos;
    }
    Ok(Array::TextArray(TextArray::String32(
        minarrow::StringArray {
            offsets: Buffer::from(offsets),
            data: Buffer::from(data),
            null_mask: Some(mask_to_bitmask(null_bools)),
        }
        .into(),
    )))
}

#[cfg(not(feature = "default_categorical_8"))]
#[allow(clippy::too_many_arguments)]
fn build_categorical_col_inline(
    buf: &[u8],
    field_starts: &[u32],
    n_cols: usize,
    data_row_offset: usize,
    col_idx: usize,
    n_rows: usize,
    quote: u8,
    nulls: &[&'static str],
    null_bools: &mut [bool],
    null_count: &mut usize,
) -> io::Result<Array> {
    let mut uniques: Vec<String> = Vec::new();
    let mut dict: HashMap<String, u32> = HashMap::new();
    let mut codes: Vec64<u32> = vec64![0u32; n_rows];

    for r in 0..n_rows {
        let raw = cell_raw(buf, field_starts, (r + data_row_offset) * n_cols + col_idx);
        let unq = unquote(raw, quote);
        let trimmed = trim_ascii(&unq);
        if is_null_bytes(trimmed, nulls) {
            null_bools[r] = false;
            *null_count += 1;
            continue;
        }
        let s = String::from_utf8_lossy(&unq).into_owned();
        let code = if let Some(&idx) = dict.get(&s) {
            idx
        } else {
            let idx = uniques.len() as u32;
            dict.insert(s.clone(), idx);
            uniques.push(s);
            idx
        };
        codes[r] = code;
    }
    Ok(Array::TextArray(TextArray::Categorical32(
        minarrow::CategoricalArray {
            data: Buffer::from(codes),
            unique_values: uniques.into(),
            null_mask: Some(mask_to_bitmask(null_bools)),
        }
        .into(),
    )))
}

#[cfg(feature = "default_categorical_8")]
#[allow(clippy::too_many_arguments)]
fn build_categorical_col_inline(
    buf: &[u8],
    field_starts: &[u32],
    n_cols: usize,
    data_row_offset: usize,
    col_idx: usize,
    n_rows: usize,
    quote: u8,
    nulls: &[&'static str],
    null_bools: &mut [bool],
    null_count: &mut usize,
) -> io::Result<Array> {
    let mut uniques: Vec<String> = Vec::new();
    let mut dict: HashMap<String, u8> = HashMap::new();
    let mut codes: Vec64<u8> = vec64![0u8; n_rows];

    for r in 0..n_rows {
        let raw = cell_raw(buf, field_starts, (r + data_row_offset) * n_cols + col_idx);
        let unq = unquote(raw, quote);
        let trimmed = trim_ascii(&unq);
        if is_null_bytes(trimmed, nulls) {
            null_bools[r] = false;
            *null_count += 1;
            continue;
        }
        let s = String::from_utf8_lossy(&unq).into_owned();
        let code = if let Some(&idx) = dict.get(&s) {
            idx
        } else {
            let idx = uniques.len() as u8;
            dict.insert(s.clone(), idx);
            uniques.push(s);
            idx
        };
        codes[r] = code;
    }
    Ok(Array::TextArray(TextArray::Categorical8(
        minarrow::CategoricalArray {
            data: Buffer::from(codes),
            unique_values: uniques.into(),
            null_mask: Some(mask_to_bitmask(null_bools)),
        }
        .into(),
    )))
}

#[cfg(test)]
mod tests {
    use std::io::Cursor;

    use super::*;

    #[test]
    fn test_decode_basic_csv() {
        let csv = b"ints,strings,bools\n1,hello,true\n2,,false\n3,world,1\n4,rust,0\n";
        let opts = CsvDecodeOptions::default();
        let table = decode_csv(Cursor::new(&csv[..]), &opts).unwrap();

        assert_eq!(table.n_rows, 4);
        assert_eq!(table.cols.len(), 3);
        assert_eq!(table.cols[0].field.name, "ints");
        assert_eq!(table.cols[1].field.name, "strings");

        // Int column: 1..4
        match &table.cols[0].array {
            Array::NumericArray(NumericArray::Int32(arr)) => {
                let vals: Vec64<_> = arr.data.as_ref().iter().copied().collect();
                assert_eq!(vals, vec64![1, 2, 3, 4]);
            }
            _ => panic!("wrong type"),
        }

        // Bool column
        match &table.cols[2].array {
            Array::BooleanArray(arr) => {
                let actual: Vec<bool> = (0..arr.data.len()).map(|i| arr.data.get(i)).collect();
                assert_eq!(actual, vec![true, false, true, false]);
            }
            _ => panic!("wrong type"),
        }

        // Nulls - strings column has values ["hello", "", "world", "rust"] where "" is null
        // So 3 valid values, 1 null -> count_ones() should be 3
        match &table.cols[1].array {
            Array::TextArray(TextArray::String32(arr)) => {
                assert_eq!(arr.null_mask.as_ref().unwrap().count_ones(), 3); // 3 valid, 1 null
                assert_eq!(table.cols[1].null_count, 1); // Verify null count is correct
            }
            _ => panic!("wrong type"),
        }
    }

    #[test]
    fn test_decode_csv_custom_delim_and_quotes() {
        let csv = b"i|s|b\n1|\"h|ello\"|T\n2||f\n";
        let mut opts = CsvDecodeOptions::default();
        opts.delimiter = b'|';
        let table = decode_csv(Cursor::new(&csv[..]), &opts).unwrap();
        assert_eq!(table.n_rows, 2);
        match &table.cols[1].array {
            Array::TextArray(TextArray::String32(arr)) => {
                let s = std::str::from_utf8(&arr.data.as_ref()[..]).unwrap();
                assert!(s.contains("h|ello"));
            }
            _ => panic!("wrong type"),
        }
    }

    #[test]
    fn test_decode_csv_batch_basic() {
        use std::io::Cursor;
        // simple 3-row CSV with header
        let csv = b"col1,col2\n10,A\n20,B\n30,C\n";
        let mut reader = Cursor::new(&csv[..]);
        let mut opts = CsvDecodeOptions::default();

        // first batch_size = 2 -> should return rows 10,A and 20,B
        let batch1 = decode_csv_batch(&mut reader, &opts, 2)
            .unwrap()
            .expect("first batch should be Some");
        assert_eq!(batch1.n_rows, 2);
        // header should be correctly carried through
        assert_eq!(batch1.cols[0].field.name, "col1");
        assert_eq!(batch1.cols[1].field.name, "col2");
        // check values
        match &batch1.cols[0].array {
            Array::NumericArray(NumericArray::Int32(arr)) => {
                let v: Vec<i32> = arr.data.as_ref().iter().copied().collect();
                assert_eq!(v, vec![10, 20]);
            }
            _ => panic!("wrong type for col1"),
        }
        match &batch1.cols[1].array {
            Array::TextArray(TextArray::String32(arr)) => {
                let s = std::str::from_utf8(&arr.data.as_ref()[..]).unwrap();
                assert!(s.starts_with("AB")); // "A" + "B"
            }
            _ => panic!("wrong type for col2"),
        }

        // turn off header for next batch so we don't try to re-consume it
        opts.has_header = false;
        let batch2 = decode_csv_batch(&mut reader, &opts, 2)
            .unwrap()
            .expect("second batch should be Some");
        // only one row remains
        assert_eq!(batch2.n_rows, 1);
        match &batch2.cols[0].array {
            Array::NumericArray(NumericArray::Int32(arr)) => {
                assert_eq!(arr.data.as_ref()[0], 30);
            }
            _ => panic!(),
        }

        // third call ->  no more rows -> None
        let batch3 = decode_csv_batch(&mut reader, &opts, 2).unwrap();
        assert!(batch3.is_none());
    }

    #[test]
    fn decode_escaped_quotes() {
        let csv = b"id,msg\n1,\"She said \"\"hi\"\" yesterday\"\n";
        let table = decode_csv(std::io::Cursor::new(csv.as_ref()), &Default::default()).unwrap();
        match &table.cols[1].array {
            Array::TextArray(TextArray::String32(arr)) => {
                let text = std::str::from_utf8(&arr.data.as_ref()[..]).unwrap();
                assert_eq!(text, "She said \"hi\" yesterday");
            }
            _ => panic!(),
        }
    }

    #[test]
    fn decode_embedded_newline() {
        let csv = b"id,comment\n1,\"line1\nline2\"\n";
        // default parser should keep newline inside
        let tbl = decode_csv(std::io::Cursor::new(csv.as_ref()), &Default::default()).unwrap();
        match &tbl.cols[1].array {
            Array::TextArray(TextArray::String32(arr)) => {
                let text = std::str::from_utf8(&arr.data.as_ref()[..]).unwrap();
                assert_eq!(text, "line1\nline2");
            }
            _ => panic!(),
        }
    }

    #[test]
    fn decode_with_explicit_schema() {
        use minarrow::{ArrowType, Field};
        let csv = b"a,b\n001,1.23\n";
        let schema = vec![
            Field::new("a", ArrowType::String, false, None),
            Field::new("b", ArrowType::Float64, false, None),
        ];
        let opts = CsvDecodeOptions {
            schema: Some(schema.clone()),
            ..Default::default()
        };
        let tbl = decode_csv(std::io::Cursor::new(csv.as_ref()), &opts).unwrap();
        assert_eq!(tbl.cols[0].field.dtype, ArrowType::String); // honoured
    }

    #[test]
    fn decode_no_header() {
        let csv = b"10,20\n30,40\n";
        let opts = CsvDecodeOptions {
            has_header: false,
            ..Default::default()
        };
        let t = decode_csv(std::io::Cursor::new(csv.as_ref()), &opts).unwrap();
        assert_eq!(t.cols[0].field.name, "col1");
        assert_eq!(t.n_rows, 2);
    }

    #[test]
    fn decode_no_trailing_newline() {
        let csv = b"a,b\n1,2\n3,4";
        let t = decode_csv(std::io::Cursor::new(csv.as_ref()), &Default::default()).unwrap();
        assert_eq!(t.n_rows, 2);
        match &t.cols[0].array {
            Array::NumericArray(NumericArray::Int32(arr)) => {
                assert_eq!(arr.data.as_ref(), &[1, 3]);
            }
            _ => panic!("wrong type"),
        }
    }

    #[test]
    fn decode_crlf_line_endings() {
        let csv = b"a,b\r\n1,2\r\n3,4\r\n";
        let t = decode_csv(std::io::Cursor::new(csv.as_ref()), &Default::default()).unwrap();
        assert_eq!(t.n_rows, 2);
        match &t.cols[0].array {
            Array::NumericArray(NumericArray::Int32(arr)) => {
                assert_eq!(arr.data.as_ref(), &[1, 3]);
            }
            _ => panic!("wrong type"),
        }
    }
}