Skip to main content

rypipe_core/columnar/
scalar_parse.rs

1use crate::plan::FieldType;
2use arrow::datatypes::TimeUnit;
3
4/// Whether `s` parses into the declared type `ft`, mirroring the lenient
5/// conversions in [`super::ColumnBuilder::push_str`]. String and dictionary columns
6/// accept any string. Used by strict-types mode to detect malformed values.
7pub(crate) fn parses_as(s: &str, ft: &FieldType) -> bool {
8    match ft {
9        FieldType::String | FieldType::Dictionary => true,
10        FieldType::Int64 => lexical::parse::<i64, _>(s.as_bytes()).is_ok(),
11        FieldType::Float64 => lexical::parse::<f64, _>(s.as_bytes()).is_ok(),
12        FieldType::Boolean => s.parse::<bool>().is_ok(),
13        FieldType::Date32 => parse_date32(s).is_some(),
14        FieldType::Timestamp(unit, fmt) => parse_timestamp(s, *unit, fmt.as_deref()).is_some(),
15        FieldType::Decimal128(scale) => parse_decimal128(s, *scale).is_some(),
16    }
17}
18
19/// Parse an ISO-8601 date (`YYYY-MM-DD`) into days since the Unix epoch.
20pub fn parse_date32(s: &str) -> Option<i32> {
21    let d = chrono::NaiveDate::parse_from_str(s.trim(), "%Y-%m-%d").ok()?;
22    let epoch = chrono::NaiveDate::from_ymd_opt(1970, 1, 1)?;
23    Some((d - epoch).num_days() as i32)
24}
25
26/// Parse a decimal string into a scaled `i128` for Arrow `Decimal128`.
27/// Handles signs and fractional parts; extra fraction digits beyond `scale`
28/// are truncated. Returns `None` for non-numeric input.
29pub fn parse_decimal128(s: &str, scale: u8) -> Option<i128> {
30    let s = s.trim();
31    let (neg, s) = match s.strip_prefix('-') {
32        Some(rest) => (true, rest),
33        None => (false, s.strip_prefix('+').unwrap_or(s)),
34    };
35    let (int_part, frac_part) = s.split_once('.').map_or((s, ""), |(i, f)| (i, f));
36    if int_part.is_empty() && frac_part.is_empty() {
37        return None;
38    }
39    if !int_part
40        .bytes()
41        .chain(frac_part.bytes())
42        .all(|b| b.is_ascii_digit())
43    {
44        return None;
45    }
46    let int_val: i128 = if int_part.is_empty() {
47        0
48    } else {
49        int_part.parse().ok()?
50    };
51    let scale = scale as u32;
52    let mut val = int_val.checked_mul(10i128.checked_pow(scale)?)?;
53    if !frac_part.is_empty() {
54        let digits = &frac_part[..frac_part.len().min(scale as usize)];
55        if !digits.is_empty() {
56            let frac: i128 = digits.parse().ok()?;
57            let frac_scaled = frac.checked_mul(10i128.checked_pow(scale - digits.len() as u32)?)?;
58            val = val.checked_add(frac_scaled)?;
59        }
60    }
61    Some(if neg { -val } else { val })
62}
63
64pub(crate) fn format_decimal128(value: i128, scale: u8) -> String {
65    if scale == 0 {
66        return value.to_string();
67    }
68    let sign = if value.is_negative() { "-" } else { "" };
69    let mut digits = value.unsigned_abs().to_string();
70    let scale = scale as usize;
71    if digits.len() <= scale {
72        digits = format!("{:0>width$}", digits, width = scale + 1);
73    }
74    let split = digits.len() - scale;
75    format!("{sign}{}.{}", &digits[..split], &digits[split..])
76}
77
78/// Parse an ISO-8601 datetime (or bare date = midnight) into an integer in
79/// `unit`. Naive parsing only; adapters handling timezones should emit
80/// `Value::Timestamp` directly.
81pub fn parse_timestamp(s: &str, unit: TimeUnit, format: Option<&str>) -> Option<i64> {
82    let t = s.trim();
83    let dt = format
84        .and_then(|fmt| {
85            chrono::NaiveDateTime::parse_from_str(t, fmt)
86                .ok()
87                .or_else(|| {
88                    chrono::NaiveDate::parse_from_str(t, fmt)
89                        .ok()
90                        .map(|d| d.and_hms_opt(0, 0, 0).expect("midnight is valid"))
91                })
92        })
93        .or_else(|| {
94            chrono::NaiveDateTime::parse_from_str(t, "%Y-%m-%dT%H:%M:%S%.f")
95                .or_else(|_| chrono::NaiveDateTime::parse_from_str(t, "%Y-%m-%d %H:%M:%S%.f"))
96                .or_else(|_| {
97                    chrono::NaiveDate::parse_from_str(t, "%Y-%m-%d")
98                        .map(|d| d.and_hms_opt(0, 0, 0).expect("midnight is valid"))
99                })
100                .ok()
101        })?;
102    let utc = dt.and_utc();
103    Some(match unit {
104        TimeUnit::Second => utc.timestamp(),
105        TimeUnit::Millisecond => utc.timestamp_millis(),
106        TimeUnit::Microsecond => utc.timestamp_micros(),
107        TimeUnit::Nanosecond => utc
108            .timestamp_nanos_opt()
109            .unwrap_or_else(|| utc.timestamp_micros().saturating_mul(1_000)),
110    })
111}
112
113/// Format days-since-epoch back to ISO-8601 (`YYYY-MM-DD`).
114pub(crate) fn format_date32(days: i32) -> String {
115    match chrono::NaiveDate::from_ymd_opt(1970, 1, 1)
116        .and_then(|epoch| epoch.checked_add_signed(chrono::Duration::days(days as i64)))
117    {
118        Some(d) => d.format("%Y-%m-%d").to_string(),
119        None => days.to_string(),
120    }
121}
122
123/// Format a raw timestamp integer to ISO-8601 (`YYYY-MM-DDTHH:MM:SS(.fff…)`).
124pub(crate) fn format_timestamp(v: i64, unit: TimeUnit) -> String {
125    let (secs, subsec_nanos) = match unit {
126        TimeUnit::Second => (v, 0u32),
127        TimeUnit::Millisecond => (
128            v.div_euclid(1_000),
129            (v.rem_euclid(1_000) * 1_000_000) as u32,
130        ),
131        TimeUnit::Microsecond => (
132            v.div_euclid(1_000_000),
133            (v.rem_euclid(1_000_000) * 1_000) as u32,
134        ),
135        TimeUnit::Nanosecond => (
136            v.div_euclid(1_000_000_000),
137            v.rem_euclid(1_000_000_000) as u32,
138        ),
139    };
140    match chrono::DateTime::from_timestamp(secs, subsec_nanos) {
141        Some(dt) => dt.naive_utc().to_string(),
142        None => v.to_string(),
143    }
144}