Skip to content

Commit

Permalink
make IntervalParseConfig public
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Aug 9, 2024
1 parent 05cc4d9 commit 3bd47c9
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions arrow-cast/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,9 +996,8 @@ pub fn parse_interval_day_time(

pub fn parse_interval_month_day_nano_config(
value: &str,
default_unit: IntervalUnit,
config: IntervalParseConfig,
) -> Result<<IntervalMonthDayNanoType as ArrowPrimitiveType>::Native, ArrowError> {
let config = IntervalParseConfig::new(default_unit);
let interval = Interval::parse(value, &config)?;

let (months, days, nanos) = interval.to_month_day_nanos();
Expand All @@ -1009,7 +1008,7 @@ pub fn parse_interval_month_day_nano_config(
pub fn parse_interval_month_day_nano(
value: &str,
) -> Result<<IntervalMonthDayNanoType as ArrowPrimitiveType>::Native, ArrowError> {
parse_interval_month_day_nano_config(value, IntervalUnit::Month)
parse_interval_month_day_nano_config(value, IntervalParseConfig::new(IntervalUnit::Month))
}

const NANOS_PER_MILLIS: i64 = 1_000_000;
Expand All @@ -1019,6 +1018,19 @@ const NANOS_PER_HOUR: i64 = 60 * NANOS_PER_MINUTE;
#[cfg(test)]
const NANOS_PER_DAY: i64 = 24 * NANOS_PER_HOUR;

pub struct IntervalParseConfig {
/// The default unit to use if none is specified
/// e.g. `INTERVAL 1` represents `INTERVAL 1 SECOND` when default_unit = IntervalType::Second
default_unit: IntervalUnit,
}

impl IntervalParseConfig {
pub fn new(default_unit: IntervalUnit) -> Self {
Self { default_unit }
}
}


#[rustfmt::skip]
#[derive(Debug, Clone, Copy)]
#[repr(u16)]
Expand Down Expand Up @@ -1379,18 +1391,6 @@ impl Interval {
}
}

struct IntervalParseConfig {
/// The default unit to use if none is specified
/// e.g. `INTERVAL 1` represents `INTERVAL 1 SECOND` when default_unit = IntervalType::Second
default_unit: IntervalUnit,
}

impl IntervalParseConfig {
fn new(default_unit: IntervalUnit) -> Self {
Self { default_unit }
}
}

/// parse the string into a vector of interval components i.e. (amount, unit) tuples
fn parse_interval_components(
value: &str,
Expand Down Expand Up @@ -2274,7 +2274,7 @@ mod tests {
Interval::parse("1us", &config).unwrap()
);
assert_eq!(
Interval::new(0, 0, 1_000_000_000),
Interval::new(0, 0, NANOS_PER_SECOND),
Interval::parse("1s", &config).unwrap()
);
assert_eq!(
Expand Down Expand Up @@ -2730,4 +2730,12 @@ mod tests {
assert_eq!(TimestampNanosecondType::parse(""), None);
assert_eq!(Date32Type::parse(""), None);
}

#[test]
fn test_parse_interval_month_day_nano_config() {
let interval = parse_interval_month_day_nano_config("1", IntervalParseConfig::new(IntervalUnit::Second)).unwrap();
assert_eq!(interval.months, 0);
assert_eq!(interval.days, 0);
assert_eq!(interval.nanoseconds, NANOS_PER_SECOND);
}
}

0 comments on commit 3bd47c9

Please sign in to comment.