lightstream 0.4.4

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
//! # CSV Decoder for Minarrow Tables
//!
//! - Accepts a CSV byte slice or any [`BufRead`].
//! - Infers schema or uses a provided schema (optional).
//! - Supports: `Int32`, `Int64`, `UInt32`, `UInt64`, `Float32`, `Float64`, `Boolean`, `String32`, `Categorical32`.
//! - Custom delimiter, nulls, quoting, and dictionary mapping for categoricals.
//! - Produces a single [`Table`] via [`decode_csv`], or multiple batches via repeated calls to [`decode_csv_batch`].
//! - No external dependencies.
//!
//! Notes:
//! - Input is treated as UTF-8; invalid byte sequences are lossily decoded via `String::from_utf8_lossy`.
//! - See [`CsvDecodeOptions`] for configurable delimiter, quoting, header handling, and schema control.

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(),
        }
    }
}

/// 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 CsvDecodeOptions {
        delimiter,
        nulls,
        quote,
        has_header,
        schema,
        all_as_text,
        categorical_cols,
    } = options.clone();

    let mut header: Vec<String> = Vec::new();
    let mut rows: Vec<Vec<String>> = Vec::new();
    let mut buf = Vec::new();

    // --- Read and split lines (basic stateful CSV parser, no allocation on fields) ---
    let mut first_row_is_header = false;
    let mut col_count = 0;
    loop {
        buf.clear();
        let n = reader.read_until(b'\n', &mut buf)?;
        if n == 0 {
            break;
        }
        let mut quote_balance = buf.iter().filter(|&&b| b == quote).count() % 2;
        while quote_balance == 1
        /* we are inside an open quote */
        {
            let m = reader.read_until(b'\n', &mut buf)?;
            if m == 0 {
                break;
            } // EOF inside quotes -> let parse fail later
            quote_balance ^= buf[n..].iter().filter(|&&b| b == quote).count() % 2;
        }

        // Strip trailing \r\n or \n
        let line = {
            let l = if let Some(&b'\r') = buf.get(buf.len().saturating_sub(2)) {
                &buf[..buf.len() - 2]
            } else if buf.last() == Some(&b'\n') {
                &buf[..buf.len() - 1]
            } else {
                &buf[..]
            };
            l
        };

        if line.is_empty() && rows.is_empty() {
            continue;
        } // skip blank leading lines

        let fields = parse_csv_line(line, delimiter, quote);
        if fields.is_empty() {
            continue;
        }

        if header.is_empty() && has_header {
            // first non-blank line is the header
            header = fields;
            col_count = header.len();
            first_row_is_header = true;
        } else {
            // actual data rows
            if col_count == 0 {
                col_count = fields.len();
            }
            if fields.len() != col_count {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "inconsistent row length",
                ));
            }
            rows.push(fields);
        }
    }

    // Use header or default names
    let col_names: Vec<String> = if first_row_is_header {
        header
    } else {
        (0..col_count).map(|i| format!("col{}", i + 1)).collect()
    };

    let n_rows = rows.len();

    // --- Infer schema if needed ---
    let schema: Vec<Field> = if let Some(fields) = schema {
        fields
    } else if all_as_text {
        col_names
            .iter()
            .map(|name| Field {
                name: name.clone(),
                dtype: ArrowType::String,
                nullable: true,
                metadata: Default::default(),
            })
            .collect()
    } else {
        infer_schema(&rows, &col_names, &categorical_cols, &nulls)
    };

    // --- Build columns ---
    let mut cols: Vec<FieldArray> = Vec::with_capacity(col_count);
    for (col_idx, field) in schema.iter().enumerate() {
        let mut null_mask = vec![true; n_rows]; // Arrow: true=valid, false=null
        let mut str_values: Vec<Option<&str>> = Vec::with_capacity(n_rows);

        for row in 0..n_rows {
            let val = rows[row][col_idx].trim();
            let is_null = nulls.iter().any(|n| n.eq_ignore_ascii_case(val));
            if is_null {
                null_mask[row] = false; // Arrow: false=null
                str_values.push(None);
            } else {
                str_values.push(Some(val));
            }
        }

        let array = match &field.dtype {
            ArrowType::Int32 => parse_numeric_column::<i32>(&str_values, &null_mask)?,
            ArrowType::Int64 => parse_numeric_column::<i64>(&str_values, &null_mask)?,
            ArrowType::UInt32 => parse_numeric_column::<u32>(&str_values, &null_mask)?,
            ArrowType::UInt64 => parse_numeric_column::<u64>(&str_values, &null_mask)?,
            ArrowType::Float32 => parse_numeric_column::<f32>(&str_values, &null_mask)?,
            ArrowType::Float64 => parse_numeric_column::<f64>(&str_values, &null_mask)?,
            ArrowType::Boolean => parse_bool_column(&str_values, &null_mask)?,
            ArrowType::String => parse_string_column(&str_values, &null_mask)?,
            ArrowType::Dictionary(_) => {
                // Always use categorical inference if requested or if declared
                parse_categorical_column(&str_values, &null_mask)?
            }
            _ => {
                // Fallback: treat as string
                parse_string_column(&str_values, &null_mask)?
            }
        };

        // Count false values in null_mask (false = null, true = valid)
        let null_count = null_mask.iter().filter(|x| !**x).count();

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

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

/// Parse a CSV line into fields (no heap per field, only String vec return).
#[inline]
fn parse_csv_line(line: &[u8], delimiter: u8, quote: u8) -> Vec<String> {
    let mut fields = Vec::new();
    let mut field = Vec::with_capacity(32);
    let mut in_quotes = false;
    let mut i = 0;
    while i < line.len() {
        let b = line[i];
        if in_quotes {
            if b == quote {
                if i + 1 < line.len() && line[i + 1] == quote {
                    // Escaped quote
                    field.push(quote);
                    i += 1;
                } else {
                    in_quotes = false;
                }
            } else {
                field.push(b);
            }
        } else if b == quote {
            in_quotes = true;
        } else if b == delimiter {
            fields.push(String::from_utf8_lossy(&field).into_owned());
            field.clear();
        } else {
            field.push(b);
        }
        i += 1;
    }
    fields.push(String::from_utf8_lossy(&field).into_owned());
    fields
}

/// Infer schema from sampled rows, prefer smallest type supporting all data.
/// Dictionary if column is categorical.
fn infer_schema(
    rows: &[Vec<String>],
    col_names: &[String],
    categorical_cols: &HashSet<String>,
    nulls: &[&'static str],
) -> Vec<Field> {
    let n_cols = col_names.len();
    let mut types: Vec<ArrowType> = vec![ArrowType::String; n_cols];
    for col in 0..n_cols {
        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;
        let is_cat = categorical_cols.contains(&col_names[col]);

        for row in rows {
            let val = row[col].trim();
            if nulls.iter().any(|n| n.eq_ignore_ascii_case(val)) {
                continue;
            }
            if is_bool && !matches!(val, "true" | "false" | "1" | "0" | "t" | "f" | "T" | "F") {
                is_bool = false;
            }
            if is_i32 && val.parse::<i32>().is_err() {
                is_i32 = false;
            }
            if is_i64 && val.parse::<i64>().is_err() {
                is_i64 = false;
            }
            if is_u32 && val.parse::<u32>().is_err() {
                is_u32 = false;
            }
            if is_u64 && val.parse::<u64>().is_err() {
                is_u64 = false;
            }
            if is_f32 && val.parse::<f32>().is_err() {
                is_f32 = false;
            }
            if is_f64 && val.parse::<f64>().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 {
            ArrowType::Dictionary(CategoricalIndexType::UInt32)
        } else {
            ArrowType::String
        };
    }

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

// -- Column parsers --

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

// ------- Numeric (Integer/Floating) -------
fn parse_numeric_column<T: std::str::FromStr + Copy + Default + 'static>(
    values: &[Option<&str>],
    null_mask: &[bool],
) -> std::io::Result<Array> {
    let mut out = vec64![T::default(); values.len()];
    for (i, v) in values.iter().enumerate() {
        if null_mask[i] {
            // Arrow: true=valid
            out[i] = v.unwrap().parse::<T>().map_err(|_| {
                std::io::Error::new(std::io::ErrorKind::InvalidData, "failed to parse number")
            })?;
        }
    }

    // Construct correct NumericArray variant for T
    let arr = if std::any::TypeId::of::<T>() == std::any::TypeId::of::<i32>() {
        Array::NumericArray(NumericArray::Int32(
            IntegerArray {
                data: Buffer::from(
                    // SAFETY: Cast is valid because T == i32
                    unsafe { std::mem::transmute::<Vec64<T>, Vec64<i32>>(out) },
                ),
                null_mask: Some(mask_to_bitmask(null_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: Some(mask_to_bitmask(null_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: Some(mask_to_bitmask(null_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: Some(mask_to_bitmask(null_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: Some(mask_to_bitmask(null_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: Some(mask_to_bitmask(null_mask)),
            }
            .into(),
        ))
    } else {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "unsupported numeric type",
        ));
    };

    Ok(arr)
}

// ------- Boolean -------
fn parse_bool_column(values: &[Option<&str>], null_mask: &[bool]) -> std::io::Result<Array> {
    let mut out = vec64![false; values.len()];
    for (i, v) in values.iter().enumerate() {
        if null_mask[i] {
            // Arrow: true=valid
            let s = v.unwrap().to_ascii_lowercase();
            out[i] = s == "true" || s == "1" || s == "t";
        }
    }
    Ok(Array::BooleanArray(
        minarrow::BooleanArray::new(Bitmask::from_bools(&out), Some(mask_to_bitmask(null_mask)))
            .into(),
    ))
}

fn parse_string_column(values: &[Option<&str>], null_mask: &[bool]) -> io::Result<Array> {
    let mut offsets = vec64![0u32; values.len() + 1];
    let mut data = Vec64::with_capacity(values.len() * 8);
    let mut pos = 0u32;
    for (i, v) in values.iter().enumerate() {
        if null_mask[i] {
            // Arrow: true=valid
            let s = v.unwrap().as_bytes();
            data.extend_from_slice(s);
            pos += s.len() as u32;
        }
        offsets[i + 1] = pos;
    }
    Ok(Array::TextArray(TextArray::String32(
        minarrow::StringArray {
            offsets: Buffer::from(offsets),
            data: Buffer::from(data),
            null_mask: Some(mask_to_bitmask(null_mask)),
        }
        .into(),
    )))
}

fn parse_categorical_column(values: &[Option<&str>], null_mask: &[bool]) -> io::Result<Array> {
    let mut uniques: Vec<String> = Vec::new();
    let mut dict: HashMap<&str, u32> = HashMap::new();
    let mut codes = vec64![0u32; values.len()];

    for (i, v) in values.iter().enumerate() {
        if !null_mask[i] {
            // Arrow: false=null, so skip nulls
            continue;
        }
        let s = v.unwrap();
        let code = if let Some(&idx) = dict.get(s) {
            idx
        } else {
            let idx = uniques.len() as u32;
            dict.insert(s, idx);
            uniques.push(s.to_string());
            idx
        };
        codes[i] = code;
    }
    Ok(Array::TextArray(TextArray::Categorical32(
        minarrow::CategoricalArray {
            data: Buffer::from(codes),
            unique_values: uniques.into(),
            null_mask: Some(mask_to_bitmask(null_mask)),
        }
        .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() {
        use crate::models::decoders::csv::decode_csv;
        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() {
        use crate::models::decoders::csv::decode_csv;
        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 crate::models::decoders::csv::{CsvDecodeOptions, decode_csv};
        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() {
        use crate::models::decoders::csv::{CsvDecodeOptions, decode_csv};
        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);
    }
}