datui-lib 0.4.0

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
659
660
661
662
663
664
//! Text typed as a value for a column, read as the column's own type.
//!
//! A filter and a Data Quality partition compare a column with a value someone
//! typed. Compared as text it either fails (Polars will not compare a date with a
//! string) or casts every row to text, which costs a pass over the column and
//! blinds Parquet statistics and SQLite to the predicate. Read here into a literal
//! of the column's type, `col op lit` stays a plain comparison.

use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, TimeZone as _};
use polars::prelude::*;

/// The forms a value of `dtype` is written in, for a message about one that is not.
pub fn written_as(dtype: &DataType) -> &'static str {
    match dtype {
        DataType::Date => "a date written YYYY-MM-DD",
        DataType::Datetime(..) => "a date and time written YYYY-MM-DD HH:MM[:SS[.f]]",
        DataType::Time => "a time written HH:MM[:SS[.f]]",
        DataType::Duration(_) => "a duration such as 1d 2h 30m or 1500ms",
        DataType::Boolean => "true or false",
        dtype if dtype.is_integer() => "a whole number",
        _ => "a number",
    }
}

/// `text` as a literal of `dtype`: a number, a flag, a date, a date and time read as
/// a clock in the column's zone, a time, a duration or a decimal at the column's
/// scale. Text and categories stay text. The error says what `text` should look like.
///
/// Whole numbers are read as `i64` or `u64` whatever their width, so `< 300` on an
/// `i8` column still compares; a float is read at the column's own precision, so
/// `0.1` is the `f32` a Float32 column stores.
pub fn parse(text: &str, dtype: &DataType) -> Result<Scalar, String> {
    let bad = || format!("{text:?} is not {}", written_as(dtype));
    let scalar = |value: AnyValue<'static>| Scalar::new(dtype.clone(), value);
    match dtype {
        DataType::String | DataType::Categorical(..) | DataType::Enum(..) => Ok(Scalar::new(
            DataType::String,
            AnyValue::StringOwned(text.into()),
        )),
        DataType::Boolean => text
            .parse()
            .map(|b| scalar(AnyValue::Boolean(b)))
            .map_err(|_| bad()),
        DataType::Int8 | DataType::Int16 | DataType::Int32 | DataType::Int64 => text
            .parse()
            .map(|i| Scalar::new(DataType::Int64, AnyValue::Int64(i)))
            .map_err(|_| bad()),
        DataType::UInt8 | DataType::UInt16 | DataType::UInt32 | DataType::UInt64 => text
            .parse()
            .map(|u| Scalar::new(DataType::UInt64, AnyValue::UInt64(u)))
            .map_err(|_| bad()),
        DataType::Float64 => text
            .parse()
            .map(|f| scalar(AnyValue::Float64(f)))
            .map_err(|_| bad()),
        DataType::Float32 => text
            .parse()
            .map(|f| scalar(AnyValue::Float32(f)))
            .map_err(|_| bad()),
        DataType::Date => parse_date(text.trim())
            .map(|days| scalar(AnyValue::Date(days)))
            .ok_or_else(bad),
        DataType::Datetime(unit, zone) => {
            let v = parse_datetime(text.trim(), *unit, zone.as_ref()).map_err(|why| {
                why.unwrap_or_else(bad)
                    .replace("{text}", &format!("{text:?}"))
            })?;
            Ok(scalar(AnyValue::DatetimeOwned(
                v,
                *unit,
                zone.clone().map(Arc::new),
            )))
        }
        DataType::Time => parse_time(text.trim())
            .map(|ns| scalar(AnyValue::Time(ns)))
            .ok_or_else(bad),
        DataType::Duration(unit) => {
            let ns = parse_duration(text.trim()).ok_or_else(bad)?;
            let v = in_unit(ns, *unit).ok_or_else(|| {
                format!("{text:?} is finer than the column's {}", unit_name(*unit))
            })?;
            Ok(scalar(AnyValue::Duration(v, *unit)))
        }
        DataType::Decimal(..) => {
            // Polars reads the text at the column's scale, as a cast of the column would.
            let read = Series::new(PlSmallStr::EMPTY, [text.trim()])
                .strict_cast(dtype)
                .map_err(|_| bad())?;
            match read.get(0).map_err(|_| bad())? {
                AnyValue::Null => Err(bad()),
                value => Ok(scalar(value.into_static())),
            }
        }
        _ => Err(format!("a {dtype} column has no value to compare with")),
    }
}

/// The text [`parse`] reads back to exactly `value`, a value of `dtype`: what `+` and
/// `-` put in a filter. A float is its shortest exact decimal, a date and time its
/// clock in the column's zone to the last digit (with its offset where that clock
/// happens twice). `None` for a null, and for a type [`parse`] does not read.
pub fn text_of(value: &AnyValue, dtype: &DataType) -> Option<String> {
    if value.is_null() {
        return None;
    }
    let text = match (dtype, value) {
        (DataType::String, _) => value.get_str()?.to_string(),
        (DataType::Float64, AnyValue::Float64(f)) => crate::exact::f64_text(*f),
        (DataType::Float32, AnyValue::Float32(f)) => crate::exact::f32_text(*f),
        (DataType::Date | DataType::Time, _) => match crate::exact::out_of_range(value) {
            Some(stored) => stored,
            None => match value {
                AnyValue::Date(days) => epoch_date()
                    .checked_add_signed(chrono::TimeDelta::days(i64::from(*days)))?
                    .format("%Y-%m-%d")
                    .to_string(),
                AnyValue::Time(ns) => time_of(*ns)?.format("%H:%M:%S%.f").to_string(),
                _ => return None,
            },
        },
        (
            DataType::Datetime(unit, zone),
            AnyValue::Datetime(v, ..) | AnyValue::DatetimeOwned(v, ..),
        ) => {
            if let Some(stored) = crate::exact::out_of_range(value) {
                stored
            } else {
                let utc = utc_of(*v, *unit)?;
                let clock = "%Y-%m-%d %H:%M:%S%.f";
                match zone {
                    None => utc.format(clock).to_string(),
                    Some(zone) => {
                        let local = zone.to_chrono().ok()?.from_utc_datetime(&utc);
                        let plain = local.format(clock).to_string();
                        if same(&plain, dtype, value) {
                            plain
                        } else {
                            local.format("%Y-%m-%d %H:%M:%S%.f%:z").to_string()
                        }
                    }
                }
            }
        }
        (DataType::Duration(unit), AnyValue::Duration(v, _)) => {
            let shown = crate::exact::str_value(value).into_owned();
            if same(&shown, dtype, value) {
                shown
            } else {
                format!("{v}{}", unit_suffix(*unit))
            }
        }
        (
            DataType::Boolean
            | DataType::Int8
            | DataType::Int16
            | DataType::Int32
            | DataType::Int64
            | DataType::UInt8
            | DataType::UInt16
            | DataType::UInt32
            | DataType::UInt64
            | DataType::Decimal(..)
            | DataType::Categorical(..)
            | DataType::Enum(..),
            _,
        ) => crate::exact::str_value(value).into_owned(),
        _ => return None,
    };
    let reads_back = matches!(dtype, DataType::Categorical(..) | DataType::Enum(..))
        || same(&text, dtype, value);
    reads_back.then_some(text)
}

/// Whether `text` reads back to `value`.
fn same(text: &str, dtype: &DataType, value: &AnyValue) -> bool {
    parse(text, dtype).is_ok_and(|read| {
        let read = read.into_value();
        match (&read, value) {
            // Read wide, stored narrow.
            (AnyValue::Int64(a), _) => value.extract::<i64>() == Some(*a),
            (AnyValue::UInt64(a), _) => value.extract::<u64>() == Some(*a),
            _ => read == value.clone().into_static(),
        }
    })
}

/// A literal as Python Polars: `pl.date(2024, 1, 1)`, `pl.datetime(...)`,
/// `pl.duration(...)`, a number, a string.
pub fn python(scalar: &Scalar) -> String {
    use crate::python_script::{py_bool, py_float, py_str};
    let unit_arg = |unit: TimeUnit| format!("time_unit={}", py_str(unit_name_short(unit)));
    match (scalar.dtype(), scalar.value()) {
        (_, AnyValue::Int64(i)) => i.to_string(),
        (_, AnyValue::UInt64(u)) => u.to_string(),
        (_, AnyValue::Float64(f)) => py_float(*f),
        (_, AnyValue::Float32(f)) => py_float(f64::from(*f)),
        (_, AnyValue::Boolean(b)) => py_bool(*b).to_string(),
        (_, AnyValue::StringOwned(s)) => py_str(s),
        (_, AnyValue::String(s)) => py_str(s),
        (DataType::Date, AnyValue::Date(days)) => {
            match epoch_date().checked_add_signed(chrono::TimeDelta::days(i64::from(*days))) {
                Some(date) if crate::exact::out_of_range(scalar.value()).is_none() => {
                    use chrono::Datelike;
                    format!("pl.date({}, {}, {})", date.year(), date.month(), date.day())
                }
                _ => format!("pl.lit({days}).cast(pl.Date)"),
            }
        }
        (DataType::Datetime(unit, zone), AnyValue::DatetimeOwned(v, ..)) => {
            let zone_arg = zone
                .as_ref()
                .map_or(String::new(), |_| ", time_zone=\"UTC\"".to_string());
            let convert = zone.as_ref().map_or(String::new(), |zone| {
                format!(".dt.convert_time_zone({})", py_str(zone))
            });
            match utc_of(*v, *unit) {
                Some(utc) if crate::exact::out_of_range(scalar.value()).is_none() => {
                    use chrono::{Datelike, Timelike};
                    let nanos = utc.nanosecond();
                    if nanos % 1000 == 0 {
                        format!(
                            "pl.datetime({}, {}, {}, {}, {}, {}, {}, {}{zone_arg}){convert}",
                            utc.year(),
                            utc.month(),
                            utc.day(),
                            utc.hour(),
                            utc.minute(),
                            utc.second(),
                            nanos / 1000,
                            unit_arg(*unit)
                        )
                    } else {
                        format!(
                            "pl.lit({}).str.to_datetime(\"%Y-%m-%d %H:%M:%S%.f\", {}{zone_arg}){convert}",
                            py_str(&utc.format("%Y-%m-%d %H:%M:%S%.f").to_string()),
                            unit_arg(*unit)
                        )
                    }
                }
                _ => format!(
                    "pl.lit({v}).cast(pl.Datetime({}{}))",
                    py_str(unit_name_short(*unit)),
                    zone.as_ref()
                        .map_or(String::new(), |zone| format!(", {}", py_str(zone)))
                ),
            }
        }
        (DataType::Time, AnyValue::Time(ns)) => match time_of(*ns) {
            Some(time) if ns % 1000 == 0 => {
                use chrono::Timelike;
                format!(
                    "pl.time({}, {}, {}, {})",
                    time.hour(),
                    time.minute(),
                    time.second(),
                    time.nanosecond() / 1000
                )
            }
            Some(time) => format!(
                "pl.lit({}).str.to_time(\"%H:%M:%S%.f\")",
                py_str(&time.format("%H:%M:%S%.f").to_string())
            ),
            None => format!("pl.lit({ns}).cast(pl.Time)"),
        },
        (DataType::Duration(unit), AnyValue::Duration(v, _)) => {
            let name = match unit {
                TimeUnit::Milliseconds => "milliseconds",
                TimeUnit::Microseconds => "microseconds",
                TimeUnit::Nanoseconds => "nanoseconds",
            };
            format!("pl.duration({name}={v}, {})", unit_arg(*unit))
        }
        (DataType::Decimal(precision, scale), value) => format!(
            "pl.lit({}).cast(pl.Decimal({precision}, {scale}))",
            py_str(&crate::exact::str_value(value))
        ),
        (_, value) => py_str(&crate::exact::str_value(value)),
    }
}

fn epoch_date() -> NaiveDate {
    NaiveDate::from_ymd_opt(1970, 1, 1).expect("the epoch is a date")
}

/// `YYYY-MM-DD`, or a date past the calendar as [`crate::exact::out_of_range`] writes
/// it: days since the epoch.
fn parse_date(text: &str) -> Option<i32> {
    if let Some(days) = text.strip_suffix(" days since 1970-01-01") {
        return days.trim().parse().ok();
    }
    let date = NaiveDate::parse_from_str(text, "%Y-%m-%d").ok()?;
    i32::try_from((date - epoch_date()).num_days()).ok()
}

/// `HH:MM`, `HH:MM:SS` or `HH:MM:SS.f`, as nanoseconds since midnight.
fn parse_time(text: &str) -> Option<i64> {
    if let Some(ns) = text.strip_suffix(" ns since midnight") {
        return ns.trim().parse().ok();
    }
    let time = NaiveTime::parse_from_str(text, "%H:%M:%S%.f")
        .or_else(|_| NaiveTime::parse_from_str(text, "%H:%M"))
        .ok()?;
    use chrono::Timelike;
    Some(i64::from(time.num_seconds_from_midnight()) * 1_000_000_000 + i64::from(time.nanosecond()))
}

fn time_of(ns: i64) -> Option<NaiveTime> {
    let secs = u32::try_from(ns.div_euclid(1_000_000_000)).ok()?;
    let nanos = u32::try_from(ns.rem_euclid(1_000_000_000)).ok()?;
    NaiveTime::from_num_seconds_from_midnight_opt(secs, nanos)
}

/// A date and time as a stored number in `unit`. The forms: a date alone (its
/// midnight), then `T` or a space, `HH:MM`, `HH:MM:SS` or `HH:MM:SS.f`, and an
/// optional offset (`+01:00`, `Z`). Without an offset the clock is read in `zone`.
/// `Err(Some(why))` names a problem other than the form; `{text}` in it stands for
/// the text.
fn parse_datetime(
    text: &str,
    unit: TimeUnit,
    zone: Option<&TimeZone>,
) -> Result<i64, Option<String>> {
    if let Some(stored) = text.strip_suffix(" since 1970-01-01 UTC") {
        let (v, written) = stored.trim().split_once(' ').ok_or(None)?;
        let v: i64 = v.parse().map_err(|_| None)?;
        return (written == unit_name_short(unit)).then_some(v).ok_or(None);
    }
    // One separator, a space, whichever was typed; `Z` is the offset it stands for.
    let mut text = text.to_string();
    if text.len() > 10 && text.as_bytes()[10] == b'T' {
        text.replace_range(10..11, " ");
    }
    if let Some(stripped) = text.strip_suffix('Z') {
        text = format!("{stripped}+00:00");
    }
    let with_offset = ["%Y-%m-%d %H:%M:%S%.f%#z", "%Y-%m-%d %H:%M%#z"]
        .iter()
        .find_map(|format| DateTime::parse_from_str(&text, format).ok());
    let utc = if let Some(at) = with_offset {
        if zone.is_none() {
            return Err(Some(
                "{text} carries an offset, and the column has no time zone".to_string(),
            ));
        }
        at.naive_utc()
    } else {
        let clock = ["%Y-%m-%d %H:%M:%S%.f", "%Y-%m-%d %H:%M"]
            .iter()
            .find_map(|format| NaiveDateTime::parse_from_str(&text, format).ok())
            .or_else(|| {
                NaiveDate::parse_from_str(&text, "%Y-%m-%d")
                    .ok()
                    .and_then(|date| date.and_hms_opt(0, 0, 0))
            })
            .ok_or(None)?;
        match zone {
            None => clock,
            Some(zone) => {
                let tz = zone.to_chrono().map_err(|e| Some(e.to_string()))?;
                match tz.from_local_datetime(&clock) {
                    chrono::LocalResult::Single(at) => at.naive_utc(),
                    _ => {
                        return Err(Some(format!(
                            "{{text}} does not happen in {zone}, or happens twice there; \
                             add its offset, such as +01:00"
                        )));
                    }
                }
            }
        }
    };
    let utc = utc.and_utc();
    let nanos = i64::from(utc.timestamp_subsec_nanos());
    let finer = || {
        Some(format!(
            "{{text}} is finer than the column's {}",
            unit_name(unit)
        ))
    };
    match unit {
        TimeUnit::Nanoseconds => utc.timestamp_nanos_opt().ok_or(None),
        TimeUnit::Microseconds if nanos % 1_000 == 0 => Ok(utc.timestamp_micros()),
        TimeUnit::Milliseconds if nanos % 1_000_000 == 0 => Ok(utc.timestamp_millis()),
        _ => Err(finer()),
    }
}

/// The UTC clock of a value stored as `v` in `unit`.
fn utc_of(v: i64, unit: TimeUnit) -> Option<NaiveDateTime> {
    let at = match unit {
        TimeUnit::Milliseconds => DateTime::from_timestamp_millis(v)?,
        TimeUnit::Microseconds => DateTime::from_timestamp_micros(v)?,
        TimeUnit::Nanoseconds => DateTime::from_timestamp_nanos(v),
    };
    Some(at.naive_utc())
}

/// A duration as Polars writes one (`1d 2h 3m 4s 5µs`, `-1m -30s`, `1500ms`): parts
/// of a whole number and a unit, `d h m s ms us µs ns`, each signed on its own, as
/// nanoseconds.
fn parse_duration(text: &str) -> Option<i128> {
    let mut total: i128 = 0;
    let mut parts = 0;
    for part in text.split_whitespace() {
        let digits_end = part
            .char_indices()
            .find(|(i, c)| !(c.is_ascii_digit() || (*i == 0 && *c == '-')))
            .map(|(i, _)| i)?;
        let (number, suffix) = part.split_at(digits_end);
        let number: i128 = number.parse().ok()?;
        let per: i128 = match suffix {
            "d" => 86_400_000_000_000,
            "h" => 3_600_000_000_000,
            "m" => 60_000_000_000,
            "s" => 1_000_000_000,
            "ms" => 1_000_000,
            "us" | "µs" => 1_000,
            "ns" => 1,
            _ => return None,
        };
        total = total.checked_add(number.checked_mul(per)?)?;
        parts += 1;
    }
    (parts > 0).then_some(total)
}

/// `ns` in `unit`, when it is a whole number of them.
fn in_unit(ns: i128, unit: TimeUnit) -> Option<i64> {
    let per: i128 = match unit {
        TimeUnit::Milliseconds => 1_000_000,
        TimeUnit::Microseconds => 1_000,
        TimeUnit::Nanoseconds => 1,
    };
    (ns % per == 0)
        .then(|| i64::try_from(ns / per).ok())
        .flatten()
}

fn unit_name(unit: TimeUnit) -> &'static str {
    match unit {
        TimeUnit::Milliseconds => "milliseconds",
        TimeUnit::Microseconds => "microseconds",
        TimeUnit::Nanoseconds => "nanoseconds",
    }
}

fn unit_name_short(unit: TimeUnit) -> &'static str {
    match unit {
        TimeUnit::Milliseconds => "ms",
        TimeUnit::Microseconds => "us",
        TimeUnit::Nanoseconds => "ns",
    }
}

fn unit_suffix(unit: TimeUnit) -> &'static str {
    match unit {
        TimeUnit::Milliseconds => "ms",
        TimeUnit::Microseconds => "us",
        TimeUnit::Nanoseconds => "ns",
    }
}

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

    fn tz(name: &str) -> Option<TimeZone> {
        TimeZone::opt_try_new(Some(name)).unwrap()
    }

    fn datetime(unit: TimeUnit, zone: Option<&str>) -> DataType {
        DataType::Datetime(unit, zone.and_then(tz))
    }

    fn stored(text: &str, dtype: &DataType) -> i64 {
        match parse(text, dtype).unwrap().into_value() {
            AnyValue::DatetimeOwned(v, ..) | AnyValue::Duration(v, _) | AnyValue::Time(v) => v,
            AnyValue::Date(days) => i64::from(days),
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn dates_and_times_read_in_their_forms() {
        assert_eq!(stored("2024-01-01", &DataType::Date), 19723);
        assert_eq!(
            stored("-800000 days since 1970-01-01", &DataType::Date),
            -800000
        );
        let us = datetime(TimeUnit::Microseconds, None);
        let midnight = 1_704_067_200_000_000;
        assert_eq!(stored("2024-01-01", &us), midnight);
        assert_eq!(
            stored("2024-01-01 05:00", &us),
            midnight + 5 * 3_600_000_000
        );
        assert_eq!(
            stored("2024-01-01T05:00:00", &us),
            midnight + 5 * 3_600_000_000
        );
        assert_eq!(
            stored("2024-01-01 05:00:00.25", &us),
            midnight + 5 * 3_600_000_000 + 250_000
        );
        // A clock in the column's zone: 05:00 in Paris is 04:00 UTC.
        let paris = datetime(TimeUnit::Microseconds, Some("Europe/Paris"));
        assert_eq!(
            stored("2024-01-01 05:00", &paris),
            midnight + 4 * 3_600_000_000
        );
        assert_eq!(
            stored("2024-01-01 05:00+00:00", &paris),
            midnight + 5 * 3_600_000_000
        );
        assert_eq!(
            stored("2024-01-01T05:00:00Z", &paris),
            midnight + 5 * 3_600_000_000
        );
        assert_eq!(
            stored("05:06", &DataType::Time),
            (5 * 3600 + 6 * 60) * 1_000_000_000
        );
        assert_eq!(
            stored("05:06:07.5", &DataType::Time),
            (5 * 3600 + 6 * 60 + 7) * 1_000_000_000 + 500_000_000
        );
        let ms = DataType::Duration(TimeUnit::Milliseconds);
        assert_eq!(stored("1d 2h", &ms), 26 * 3_600_000);
        assert_eq!(stored("-1m -30s", &ms), -90_000);
        assert_eq!(
            stored("1500µs", &DataType::Duration(TimeUnit::Microseconds)),
            1500
        );
    }

    #[test]
    fn what_does_not_read_says_why() {
        let us = datetime(TimeUnit::Microseconds, None);
        let ms = datetime(TimeUnit::Milliseconds, None);
        let paris = datetime(TimeUnit::Microseconds, Some("Europe/Paris"));
        let err = |text: &str, dtype: &DataType| parse(text, dtype).unwrap_err();
        assert_eq!(
            err("2024-13-01", &DataType::Date),
            "\"2024-13-01\" is not a date written YYYY-MM-DD"
        );
        assert!(err("noon", &us).contains("is not a date and time"));
        assert!(
            err("2024-01-01 05:00:00.0001", &ms).contains("finer than the column's milliseconds")
        );
        assert!(err("2024-01-01 05:00+01:00", &us).contains("no time zone"));
        // Spring forward skips 02:30 in Paris; fall back repeats it.
        assert!(err("2024-03-31 02:30", &paris).contains("does not happen in Europe/Paris"));
        assert!(err("2024-10-27 02:30", &paris).contains("happens twice"));
        assert!(err("25:00", &DataType::Time).contains("is not a time"));
        assert!(
            err("1 fortnight", &DataType::Duration(TimeUnit::Microseconds))
                .contains("is not a duration")
        );
        assert!(err("1ns", &DataType::Duration(TimeUnit::Microseconds)).contains("finer"));
        assert!(err("abc", &DataType::Decimal(10, 2)).contains("is not a number"));
        assert!(err("yes", &DataType::Boolean).contains("true or false"));
    }

    #[test]
    fn decimals_read_at_the_column_scale() {
        let dtype = DataType::Decimal(10, 2);
        assert_eq!(
            parse("1.5", &dtype).unwrap(),
            parse("1.50", &dtype).unwrap()
        );
        assert_eq!(
            parse("1.5", &dtype).unwrap().into_value(),
            AnyValue::Decimal(150, 10, 2)
        );
    }

    /// The text `+` and `-` write reads back to the very value, every type.
    #[test]
    fn every_value_reads_back_from_its_text() {
        let paris = tz("Europe/Paris");
        let cases: Vec<(DataType, AnyValue<'static>)> = vec![
            (DataType::Float64, AnyValue::Float64(0.1 + 0.2)),
            (DataType::Float32, AnyValue::Float32(0.1)),
            (DataType::Int8, AnyValue::Int8(-5)),
            (DataType::UInt64, AnyValue::UInt64(u64::MAX)),
            (DataType::Boolean, AnyValue::Boolean(true)),
            (DataType::Date, AnyValue::Date(19723)),
            (DataType::Date, AnyValue::Date(-800_000_000)),
            (
                DataType::Datetime(TimeUnit::Nanoseconds, None),
                AnyValue::DatetimeOwned(1_704_085_200_123_456_789, TimeUnit::Nanoseconds, None),
            ),
            (
                DataType::Datetime(TimeUnit::Microseconds, paris.clone()),
                AnyValue::DatetimeOwned(
                    1_704_081_600_000_001,
                    TimeUnit::Microseconds,
                    paris.clone().map(Arc::new),
                ),
            ),
            // 02:30 on the night Paris falls back, the second time: only its offset tells it.
            (
                DataType::Datetime(TimeUnit::Microseconds, paris.clone()),
                AnyValue::DatetimeOwned(
                    1_729_992_600_000_000,
                    TimeUnit::Microseconds,
                    paris.clone().map(Arc::new),
                ),
            ),
            (DataType::Time, AnyValue::Time(18_367_123_456_789)),
            (
                DataType::Duration(TimeUnit::Microseconds),
                AnyValue::Duration(93_784_000_005, TimeUnit::Microseconds),
            ),
            (
                DataType::Duration(TimeUnit::Nanoseconds),
                AnyValue::Duration(-1_500, TimeUnit::Nanoseconds),
            ),
            (DataType::Decimal(10, 2), AnyValue::Decimal(150, 10, 2)),
        ];
        for (dtype, value) in cases {
            let text = text_of(&value, &dtype).unwrap_or_else(|| panic!("{dtype} {value:?}"));
            assert!(same(&text, &dtype, &value), "{dtype}: {text}");
        }
    }

    #[test]
    fn literals_read_back_in_python() {
        let py = |text: &str, dtype: DataType| python(&parse(text, &dtype).unwrap());
        assert_eq!(py("2024-01-01", DataType::Date), "pl.date(2024, 1, 1)");
        assert_eq!(
            py(
                "2024-01-01 05:00:00.25",
                datetime(TimeUnit::Microseconds, None)
            ),
            "pl.datetime(2024, 1, 1, 5, 0, 0, 250000, time_unit=\"us\")"
        );
        assert_eq!(
            py(
                "2024-01-01 05:00",
                datetime(TimeUnit::Microseconds, Some("Europe/Paris"))
            ),
            "pl.datetime(2024, 1, 1, 4, 0, 0, 0, time_unit=\"us\", time_zone=\"UTC\")\
             .dt.convert_time_zone(\"Europe/Paris\")"
        );
        assert_eq!(
            py(
                "2024-01-01 05:00:00.000000001",
                datetime(TimeUnit::Nanoseconds, None)
            ),
            "pl.lit(\"2024-01-01 05:00:00.000000001\").str.to_datetime(\"%Y-%m-%d %H:%M:%S%.f\", time_unit=\"ns\")"
        );
        assert_eq!(py("05:06:07.5", DataType::Time), "pl.time(5, 6, 7, 500000)");
        assert_eq!(
            py("1d", DataType::Duration(TimeUnit::Milliseconds)),
            "pl.duration(milliseconds=86400000, time_unit=\"ms\")"
        );
        assert_eq!(
            py("1.5", DataType::Decimal(10, 2)),
            "pl.lit(\"1.50\").cast(pl.Decimal(10, 2))"
        );
        assert_eq!(py("0.1", DataType::Float32), "0.10000000149011612");
    }
}