MCPcopy Create free account
hub / github.com/apache/arrow-rs / parse_interval_components

Function parse_interval_components

arrow-cast/src/parse.rs:1457–1494  ·  view source on GitHub ↗

parse the string into a vector of interval components i.e. (amount, unit) tuples

(
    value: &str,
    config: &IntervalParseConfig,
)

Source from the content-addressed store, hash-verified

1455
1456/// parse the string into a vector of interval components i.e. (amount, unit) tuples
1457fn parse_interval_components(
1458 value: &str,
1459 config: &IntervalParseConfig,
1460) -> Result<Vec<(IntervalAmount, IntervalUnit)>, ArrowError> {
1461 let raw_pairs = split_interval_components(value);
1462
1463 // parse amounts and units
1464 let Ok(pairs): Result<Vec<(IntervalAmount, IntervalUnit)>, ArrowError> = raw_pairs
1465 .iter()
1466 .map(|(a, u)| Ok((a.parse()?, IntervalUnit::from_str_or_config(*u, config)?)))
1467 .collect()
1468 else {
1469 return Err(ArrowError::ParseError(format!(
1470 "Invalid input syntax for type interval: {value:?}"
1471 )));
1472 };
1473
1474 // collect parsed results
1475 let (amounts, units): (Vec<_>, Vec<_>) = pairs.into_iter().unzip();
1476
1477 // duplicate units?
1478 let mut observed_interval_types = 0;
1479 for (unit, (_, raw_unit)) in units.iter().zip(raw_pairs) {
1480 if observed_interval_types & (*unit as u16) != 0 {
1481 return Err(ArrowError::ParseError(format!(
1482 "Invalid input syntax for type interval: {:?}. Repeated type '{}'",
1483 value,
1484 raw_unit.unwrap_or_default(),
1485 )));
1486 }
1487
1488 observed_interval_types |= *unit as u16;
1489 }
1490
1491 let result = amounts.iter().copied().zip(units.iter().copied());
1492
1493 Ok(result.collect::<Vec<_>>())
1494}
1495
1496/// Split an interval into a vec of amounts and units.
1497///

Callers 1

parseMethod · 0.85

Calls 6

collectMethod · 0.80
zipMethod · 0.80
iterMethod · 0.45
parseMethod · 0.45
into_iterMethod · 0.45

Tested by

no test coverage detected