datui-lib 0.4.4

Data Exploration in the Terminal (library)
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
//! The readers of the formats Polars reads: Parquet, delimited text, JSON, Arrow IPC,
//! Avro, ORC and Excel.

use color_eyre::Result;

use super::{BASE, EVERYWHERE, Kind, Reader, ScanIn, Signature, Trusted, Unnamed};
#[cfg(feature = "cloud")]
use crate::error_display::FileError;
use crate::export::export_modal::ExportFormat;
use crate::export::python_script::{self as py, Python};
use crate::loading::scan::Scan;
use std::fs::File;
use std::path::{Path, PathBuf};

use arrow::array::types::{
    Date32Type, Date64Type, Float32Type, Float64Type, Int8Type, Int16Type, Int32Type, Int64Type,
    TimestampMillisecondType, UInt8Type, UInt16Type, UInt32Type, UInt64Type,
};
use arrow::array::{Array, AsArray};
use arrow::record_batch::RecordBatch;
use orc_rust::ArrowReaderBuilder;
use polars::prelude::*;

use super::Read;
use crate::loading::unfinished::Writer;
use crate::{OpenOptions, ParseStringsTarget};

/// A prefix of CSV in an object store, read with the flags the user gave as they are
/// for a local file.
#[cfg(feature = "cloud")]
fn bucket_csv(input: super::BucketIn<'_>) -> Result<polars::prelude::LazyFrame> {
    use polars::prelude::{LazyCsvReader, LazyFileListReader};
    let super::BucketIn {
        url,
        path,
        cloud,
        glob,
        options,
        format,
    } = input;
    let named = std::path::Path::new(url);
    let failed = |e: polars::prelude::PolarsError| {
        FileError::new(
            named,
            format!("could not read it as {}: {}", format.name(), said(&e)),
        )
    };
    let reader = || {
        LazyCsvReader::new(path.clone())
            .with_cloud_options(Some(cloud.clone()))
            .with_glob(glob)
    };
    // Each object has its own header lines, and the scan reads them all as one; the
    // names cannot come from one of them.
    if options.header_rows().is_some() {
        return Err(FileError::new(
            named,
            "--header-rows reads a file's own lines, so it cannot read these in place. Download the files, or name the header with --skip-lines.",
        )
        .into());
    }
    let nv = super::csv::build_null_values_with(options, None, || {
        super::csv::csv_schema_for_null_values(reader(), options)
    })?;
    // No `--infer-types` (its sample would reread the bucket), nor `try_parse_dates` (one
    // unparsable value fails the whole read): timestamps and padded numbers stay text.
    let lf = super::csv::configure_csv_reader(reader(), options, nv.as_ref())
        .finish()
        .and_then(|lf| crate::formats::csv_dialect::name_columns(lf, None))
        .and_then(|lf| {
            if !options.skip_initial_space {
                return Ok(lf);
            }
            crate::formats::csv_dialect::skip_initial_space(lf, |column| {
                super::csv::csv_null_values_for(options, column)
            })
        })
        .map_err(failed)?;
    super::csv::apply_skip_tail_rows_csv(lf, options)
        .map_err(|e| crate::error_display::in_file(named, e))
}

/// A prefix of NDJSON in an object store.
#[cfg(feature = "cloud")]
fn bucket_json_lines(input: super::BucketIn<'_>) -> Result<polars::prelude::LazyFrame> {
    use polars::prelude::LazyFileListReader;
    polars::prelude::LazyJsonLineReader::new(input.path)
        .with_cloud_options(Some(input.cloud))
        .finish()
        .map_err(|e| {
            FileError::new(
                std::path::Path::new(input.url),
                format!("could not read it as {}: {}", input.format.name(), said(&e)),
            )
            .into()
        })
}

/// IPC files in an object store, by range from their footers. A prefix is listed
/// before it gets here (`cloud_arrow`), so this is a glob: a stream among its objects
/// has no footer, and is read by its folder, which downloads it.
#[cfg(feature = "cloud")]
fn bucket_arrow(input: super::BucketIn<'_>) -> Result<polars::prelude::LazyFrame> {
    let url = input.url;
    let args = polars::prelude::UnifiedScanArgs {
        cloud_options: Some(input.cloud),
        glob: input.glob,
        ..Default::default()
    };
    polars::prelude::LazyFrame::scan_ipc(input.path, Default::default(), args).map_err(|e| {
        let folder = url
            .split('*')
            .next()
            .and_then(|head| head.rsplit_once('/'))
            .map_or(url, |(folder, _)| folder);
        FileError::new(
            std::path::Path::new(url),
            format!(
                "could not read it as Arrow IPC files: {}. A glob reads IPC files in place; Arrow streams are read by their folder: open {folder}/",
                said(&e)
            ),
        )
        .into()
    })
}

/// Polars' words for `e`, as a clause: tidied, without its full stop.
#[cfg(feature = "cloud")]
fn said(e: &polars::prelude::PolarsError) -> String {
    let said = crate::error_display::user_message_from_polars(e);
    said.trim_end_matches('.').to_string()
}

/// The frame of a state a Polars reader built, with what the read did to its rows for
/// Copy as Python.
fn frame(read: Read, input: ScanIn<'_>) -> Result<Scan> {
    let lf = resolved(read.lf)?;
    input.report.read_python = read.python;
    input.report.read_notes = read.notes;
    input.report.typing = read.typing;
    if let (Some(units), Some(delimited)) = (read.units, input.report.delimited.as_mut()) {
        let mut merged = (**delimited).clone();
        merged.units = units;
        *delimited = std::sync::Arc::new(merged);
    }
    Ok(lf.into())
}

/// `lf` with its schema resolved, as an open needs it: a file Polars cannot read
/// fails here, at the scan.
pub(crate) fn resolved(mut lf: LazyFrame) -> Result<LazyFrame> {
    lf.collect_schema()?;
    Ok(lf)
}

/// A JSON reader's frame. JSON is read into memory whole, so its sample costs no read
/// of the file.
fn json_frame(lf: LazyFrame, input: ScanIn<'_>) -> Result<Scan> {
    apply_parse_dates_to_json_lazyframe(resolved(lf)?, input.options, &mut input.report.read_python)
        .map(Scan::from)
}

fn scan_parquet(input: ScanIn<'_>) -> Result<Scan> {
    frame(each(input.paths, parquet)?.into(), input)
}

fn scan_csv(input: ScanIn<'_>) -> Result<Scan> {
    let read = match input.paths {
        [one] => super::csv::read_delimited(one, b',', input.options, &Writer::default())?,
        many => super::csv::from_csv_paths(many, input.options)?,
    };
    frame(read, input)
}

/// TSV and PSV: one file, with the descriptor's separator.
fn scan_delimited(input: ScanIn<'_>) -> Result<Scan> {
    let separator = input.format.separator().unwrap_or(b',');
    let read =
        super::csv::read_delimited(input.path(), separator, input.options, &Writer::default())?;
    frame(read, input)
}

fn scan_json(input: ScanIn<'_>) -> Result<Scan> {
    let lf = each(input.paths, |p| json(p, JsonFormat::Json))?;
    json_frame(lf, input)
}

/// NDJSON, read whole; followed, scanned, so the frame reads more of it as it grows.
fn scan_json_lines(input: ScanIn<'_>) -> Result<Scan> {
    if input.options.follow {
        let path = input.paths[0].clone();
        return crate::loading::follow::scan_lines(
            &path,
            input.options,
            false,
            &mut input.report.read_python,
        )
        .map(Scan::from);
    }
    let lf = each(input.paths, |p| json(p, JsonFormat::JsonLines))?;
    json_frame(lf, input)
}

/// Arrow IPC files are scanned where they are; streams, which have no footer, are
/// converted first. The first file says for a list: a `datasets` cache is all streams,
/// and the conversion reads each file anyway.
fn scan_arrow(input: ScanIn<'_>) -> Result<Scan> {
    let paths = input.paths;
    if crate::formats::ipc_stream::starts_with_stream(paths) {
        return Ok(Scan::Streams(paths.to_vec()));
    }
    let lf = match paths {
        [one] => ipc(one)?,
        // Polars reads every IPC file's footer for the schema, and fails on a stream
        // among them: only then is each file looked at.
        many => match each(many, ipc).and_then(resolved) {
            Ok(lf) => lf,
            Err(_) if crate::formats::ipc_stream::any_stream(many) => {
                return Ok(Scan::Streams(many.to_vec()));
            }
            Err(e) => return Err(e),
        },
    };
    frame(lf.into(), input)
}

fn scan_avro(input: ScanIn<'_>) -> Result<Scan> {
    frame(each(input.paths, avro)?.into(), input)
}

fn scan_orc(input: ScanIn<'_>) -> Result<Scan> {
    frame(each(input.paths, orc)?.into(), input)
}

fn scan_excel(input: ScanIn<'_>) -> Result<Scan> {
    let (lf, detail) = crate::formats::excel::read(input.path(), input.options)?;
    input.report.opened = Some(std::sync::Arc::new(crate::formats::members::Opened {
        detail: Some(std::sync::Arc::new(detail)),
        ..Default::default()
    }));
    frame(lf.into(), input)
}

pub(crate) const PARQUET: Reader = Reader {
    preview: Some(super::Preview::RowGroup),
    python: Some(Python {
        call: "pl.scan_parquet",
        eager: false,
        glob_flag: true,
        arguments: Some(py::parquet_arguments),
    }),
    scan: scan_parquet,
    facts: Some(super::Facts {
        read: crate::formats::parquet_footer::facts,
        footer: true,
    }),
    // `PAR1` at both ends of a file, because at the front alone it is a truncated
    // write: the footer is what a reader needs.
    signatures: &[Signature {
        says: |head, file| {
            head.starts_with(b"PAR1") && file.is_none_or(crate::home::discover::has_parquet_magic)
        },
        kind: Kind::Magic,
        trusted: Trusted {
            open: Unnamed::NoExtension,
            ..EVERYWHERE
        },
    }],
    export: Some(ExportFormat::Parquet),
    ..BASE
};

/// CSV, TSV and PSV each export as themselves: TSV and PSV are CSV presets.
pub(crate) const CSV: Reader = Reader {
    #[cfg(feature = "cloud")]
    bucket_scan: Some(bucket_csv),
    preview: Some(super::Preview::Scan),
    python: Some(Python {
        call: "pl.scan_csv",
        eager: false,
        glob_flag: true,
        arguments: Some(py::csv_arguments),
    }),
    scan: scan_csv,
    export: Some(ExportFormat::Csv),
    ..BASE
};

pub(crate) const TSV: Reader = Reader {
    scan: scan_delimited,
    // No prefix of it is read in place.
    #[cfg(feature = "cloud")]
    bucket_scan: None,
    export: Some(ExportFormat::Tsv),
    ..CSV
};

pub(crate) const PSV: Reader = Reader {
    export: Some(ExportFormat::Psv),
    ..TSV
};

pub(crate) const JSON: Reader = Reader {
    python: Some(Python {
        call: "pl.read_json",
        eager: true,
        glob_flag: false,
        arguments: None,
    }),
    scan: scan_json,
    export: Some(ExportFormat::Json),
    ..BASE
};

pub(crate) const JSONL: Reader = Reader {
    #[cfg(feature = "cloud")]
    bucket_scan: Some(bucket_json_lines),
    preview: Some(super::Preview::Scan),
    // `scan_ndjson` has no `glob` flag: a name with a glob character is escaped.
    python: Some(Python {
        call: "pl.scan_ndjson",
        eager: false,
        glob_flag: false,
        arguments: Some(py::ndjson_arguments),
    }),
    scan: scan_json_lines,
    export: Some(ExportFormat::Ndjson),
    ..BASE
};

pub(crate) const ARROW: Reader = Reader {
    #[cfg(feature = "cloud")]
    bucket_scan: Some(bucket_arrow),
    preview: Some(super::Preview::Scan),
    python: Some(Python {
        call: "pl.scan_ipc",
        eager: false,
        glob_flag: true,
        arguments: Some(py::arrow_arguments),
    }),
    scan: scan_arrow,
    facts: Some(super::Facts {
        read: super::facts::arrow,
        footer: false,
    }),
    signatures: &[
        Signature {
            says: |head, _| head.starts_with(b"ARROW1"),
            kind: Kind::Magic,
            trusted: Trusted {
                open: Unnamed::Never,
                ..EVERYWHERE
            },
        },
        // A stream has no magic, only its schema message: a file's is read whole to be
        // sure, what is piped in is judged on its first bytes.
        Signature {
            says: |head, file| match file {
                Some(file) => crate::formats::ipc_stream::is_stream_file(file),
                None => crate::formats::ipc_stream::is_stream_head(head),
            },
            kind: Kind::Structure,
            trusted: Trusted {
                open: Unnamed::NoExtension,
                ..EVERYWHERE
            },
        },
    ],
    export: Some(ExportFormat::Ipc),
    ..BASE
};

pub(crate) const AVRO: Reader = Reader {
    python: Some(Python {
        call: "pl.read_avro",
        eager: true,
        glob_flag: false,
        arguments: None,
    }),
    scan: scan_avro,
    facts: Some(super::Facts {
        read: super::facts::avro,
        footer: false,
    }),
    signatures: &[Signature {
        says: |head, _| head.starts_with(b"Obj\x01"),
        kind: Kind::Magic,
        trusted: Trusted {
            open: Unnamed::Never,
            ..EVERYWHERE
        },
    }],
    export: Some(ExportFormat::Avro),
    ..BASE
};

/// Datui writes no ORC. Its magic is three letters a text file may start with, so it
/// is believed only of a file a listing looks inside.
pub(crate) const ORC: Reader = Reader {
    scan: scan_orc,
    facts: Some(super::Facts {
        read: super::facts::orc,
        footer: false,
    }),
    signatures: &[Signature {
        says: |head, _| head.starts_with(b"ORC"),
        kind: Kind::Magic,
        trusted: Trusted {
            pipe: false,
            open: Unnamed::Never,
            listing: true,
            tables: false,
        },
    }],
    ..BASE
};

/// Datui writes no workbook.
pub(crate) const EXCEL: Reader = Reader {
    python: Some(Python {
        call: "pl.read_excel",
        eager: true,
        glob_flag: false,
        arguments: Some(py::excel_arguments),
    }),
    scan: scan_excel,
    // A workbook whose sheets its directory lists: `.xlsx` and `.xlsm`, which are zip
    // files. Never piped, and never asked of a listing, which would open every zip.
    signatures: &[Signature {
        says: crate::formats::excel::is_listable,
        kind: Kind::Magic,
        trusted: Trusted {
            pipe: false,
            open: Unnamed::Any,
            listing: false,
            tables: true,
        },
    }],
    tables: Some(crate::formats::excel::sheets),
    ..BASE
};

/// One frame of `paths`, each read by `read`, stacked in order.
fn each(paths: &[PathBuf], read: impl Fn(&Path) -> Result<LazyFrame>) -> Result<LazyFrame> {
    match paths {
        [] => Err(color_eyre::eyre::eyre!("No paths provided")),
        [one] => read(one),
        many => {
            let frames = many.iter().map(|p| read(p)).collect::<Result<Vec<_>>>()?;
            Ok(concat(frames.as_slice(), Default::default())?)
        }
    }
}

pub(super) fn parquet(path: &Path) -> Result<LazyFrame> {
    let args = ScanArgsParquet {
        glob: crate::cloud::source::expands_as_glob(path),
        ..Default::default()
    };
    Ok(LazyFrame::scan_parquet(
        PlRefPath::try_from_path(path)?,
        args,
    )?)
}

/// An Arrow IPC / Feather v2 file, scanned.
pub(super) fn ipc(path: &Path) -> Result<LazyFrame> {
    let args = UnifiedScanArgs {
        glob: crate::cloud::source::expands_as_glob(path),
        ..Default::default()
    };
    Ok(LazyFrame::scan_ipc(
        PlRefPath::try_from_path(path)?,
        Default::default(),
        args,
    )?)
}

/// An Avro file, read whole.
pub(super) fn avro(path: &Path) -> Result<LazyFrame> {
    let file = File::open(path)?;
    Ok(polars::io::avro::AvroReader::new(file).finish()?.lazy())
}

/// An ORC file, read whole through orc-rust's Arrow batches; see
/// `docs/formats/columnar-and-json.md`.
pub(super) fn orc(path: &Path) -> Result<LazyFrame> {
    let file = File::open(path)?;
    let reader = ArrowReaderBuilder::try_new(file)
        .map_err(|e| color_eyre::eyre::eyre!("ORC: {}", e))?
        .build();
    let batches: Vec<RecordBatch> = reader
        .collect::<std::result::Result<Vec<_>, _>>()
        .map_err(|e| color_eyre::eyre::eyre!("ORC: {}", e))?;
    Ok(arrow_record_batches_to_dataframe(&batches)?.lazy())
}

/// A JSON or NDJSON file, read whole.
fn json(path: &Path, format: JsonFormat) -> Result<LazyFrame> {
    let file = File::open(path)?;
    Ok(JsonReader::new(file)
        .with_json_format(format)
        .finish()?
        .lazy())
}

/// How one dataset's files stack into one table: `diagonal` (a file predating a column
/// keeps its rows, null there, the Notes saying which) and `to_supertypes` (per-file CSV
/// inference types `amount` String in one file and Int64 in another). Opt-ins elsewhere
/// (DuckDB `union_by_name`, Spark `mergeSchema`), defaults here because datui notes
/// what it did and `Enter` goes inside directories that are not one table; this is what
/// their `(all files)` row reads with. Only for formats that rule judges (CSV, NDJSON;
/// Parquet via `lenient_scan`): Arrow, Avro, ORC and `.json` would be silent, unnoted
/// unions. Identical schemas stack as before; converted Arrow streams stack with IPC
/// files read in place (`App::scan_arrow_parts`).
pub(crate) fn union_of_files() -> polars::prelude::UnionArgs {
    polars::prelude::UnionArgs {
        diagonal: true,
        to_supertypes: true,
        ..Default::default()
    }
}

/// Convert Arrow (arrow crate 57) RecordBatches to Polars DataFrame by value (ORC uses
/// arrow 57; Polars uses polars-arrow, so we cannot use Series::from_arrow).
fn arrow_record_batches_to_dataframe(batches: &[RecordBatch]) -> Result<DataFrame> {
    if batches.is_empty() {
        return Ok(DataFrame::empty());
    }
    let mut all_dfs = Vec::with_capacity(batches.len());
    for batch in batches {
        let n_cols = batch.num_columns();
        let schema = batch.schema();
        let mut series_vec = Vec::with_capacity(n_cols);
        for (i, col) in batch.columns().iter().enumerate() {
            let name = schema.field(i).name().as_str();
            let s = arrow_array_to_polars_series(name, col)?;
            series_vec.push(s.into());
        }
        let df = DataFrame::new_infer_height(series_vec)?;
        all_dfs.push(df);
    }
    let mut out = all_dfs.remove(0);
    for df in all_dfs {
        out = out.vstack(&df)?;
    }
    Ok(out)
}

fn arrow_array_to_polars_series(name: &str, array: &dyn Array) -> Result<Series> {
    use arrow::datatypes::DataType as ArrowDataType;
    let strings = |values: Vec<Option<&str>>| Series::new(name.into(), values);
    match array.data_type() {
        ArrowDataType::Int8 => primitive::<Int8Type>(name, array, "Int8"),
        ArrowDataType::Int16 => primitive::<Int16Type>(name, array, "Int16"),
        ArrowDataType::Int32 => primitive::<Int32Type>(name, array, "Int32"),
        ArrowDataType::Int64 => primitive::<Int64Type>(name, array, "Int64"),
        ArrowDataType::UInt8 => primitive::<UInt8Type>(name, array, "UInt8"),
        ArrowDataType::UInt16 => primitive::<UInt16Type>(name, array, "UInt16"),
        ArrowDataType::UInt32 => primitive::<UInt32Type>(name, array, "UInt32"),
        ArrowDataType::UInt64 => primitive::<UInt64Type>(name, array, "UInt64"),
        ArrowDataType::Float32 => primitive::<Float32Type>(name, array, "Float32"),
        ArrowDataType::Float64 => primitive::<Float64Type>(name, array, "Float64"),
        ArrowDataType::Date32 => primitive::<Date32Type>(name, array, "Date32"),
        ArrowDataType::Date64 => primitive::<Date64Type>(name, array, "Date64"),
        ArrowDataType::Timestamp(_, _) => {
            primitive::<TimestampMillisecondType>(name, array, "Timestamp")
        }
        ArrowDataType::Boolean => {
            let a = array
                .as_boolean_opt()
                .ok_or_else(|| color_eyre::eyre::eyre!("ORC: expected Boolean array"))?;
            Ok(Series::new(name.into(), a.iter().collect::<Vec<_>>()))
        }
        ArrowDataType::Utf8 => {
            let a = array
                .as_string_opt::<i32>()
                .ok_or_else(|| color_eyre::eyre::eyre!("ORC: expected Utf8 array"))?;
            Ok(strings(a.iter().collect()))
        }
        ArrowDataType::LargeUtf8 => {
            let a = array
                .as_string_opt::<i64>()
                .ok_or_else(|| color_eyre::eyre::eyre!("ORC: expected LargeUtf8 array"))?;
            Ok(strings(a.iter().collect()))
        }
        other => Err(color_eyre::eyre::eyre!(
            "ORC: unsupported column type {:?} for column '{}'",
            other,
            name
        )),
    }
}

/// An Arrow array of primitive `T` as a Series of its native values, nulls kept.
fn primitive<T: arrow::datatypes::ArrowPrimitiveType>(
    name: &str,
    array: &dyn Array,
    type_name: &str,
) -> Result<Series>
where
    Series: NamedFrom<Vec<Option<T::Native>>, [Option<T::Native>]>,
{
    let a = array
        .as_primitive_opt::<T>()
        .ok_or_else(|| color_eyre::eyre::eyre!("ORC: expected {type_name} array"))?;
    Ok(Series::new(name.into(), a.iter().collect::<Vec<_>>()))
}

/// Dates and timestamps a JSON file holds as strings, typed the way a CSV's are.
/// JSON already says which values are numbers, so a string only ever becomes a
/// date, datetime or time, and one that is none of those is left as it was read.
pub(crate) fn apply_parse_dates_to_json_lazyframe(
    lf: LazyFrame,
    options: &OpenOptions,
    read: &mut Vec<String>,
) -> Result<LazyFrame> {
    if !options.parse_dates {
        return Ok(lf);
    }
    super::csv::type_string_columns(
        lf,
        &ParseStringsTarget::All,
        options.parse_strings_sample_rows,
        super::csv::StringTypes {
            dates: true,
            numbers: false,
        },
        read,
        &[],
        &mut Vec::new(),
    )
}

#[cfg(test)]
mod reader_errors {
    use crate::FileFormat;
    use crate::formats::readers::bad_input::each_names_its_file;

    /// Bytes no Polars reader can read name their file, in the one shape, at the scan
    /// or at the first rows.
    #[test]
    fn errors_name_the_file() {
        let garbage: &[u8] = b"\x00\x01\x02 this is not a file of any format \xff\xfe";
        for (format, name) in [
            (FileFormat::Parquet, "bad.parquet"),
            (FileFormat::Arrow, "bad.arrow"),
            (FileFormat::Avro, "bad.avro"),
            (FileFormat::Orc, "bad.orc"),
            (FileFormat::Excel, "bad.xlsx"),
            (FileFormat::Json, "bad.json"),
            (FileFormat::Jsonl, "bad.jsonl"),
        ] {
            each_names_its_file(format, &[(name, garbage, "")]);
        }
        each_names_its_file(FileFormat::Csv, &[("ragged.csv", b"a,b\n1,2\n\"3,4\n", "")]);
    }
}