Jefffrey commented on code in PR #11572:
URL: https://github.com/apache/arrow-rs/pull/11572#discussion_r4475980996
##########
arrow-cast/src/parse.rs:
##########
@@ -1314,23 +1314,36 @@ const NANOS_PER_MILLIS: i64 = 1_000_000;
const NANOS_PER_SECOND: i64 = 1_000 * NANOS_PER_MILLIS;
const NANOS_PER_MINUTE: i64 = 60 * NANOS_PER_SECOND;
const NANOS_PER_HOUR: i64 = 60 * NANOS_PER_MINUTE;
-#[cfg(test)]
-const NANOS_PER_DAY: i64 = 24 * NANOS_PER_HOUR;
+pub(crate) const NANOS_PER_DAY: i64 = 24 * NANOS_PER_HOUR;
/// Config to parse interval strings
///
-/// Currently stores the `default_unit` to use if the string doesn't have one
specified
+/// Stores the `default_unit` to use if the string doesn't have one specified,
+/// and whether fractional calendar units may be spilled into smaller units.
#[derive(Debug, Clone)]
pub struct IntervalParseConfig {
/// The default unit to use if none is specified
/// e.g. `INTERVAL 1` represents `INTERVAL 1 SECOND` when default_unit =
[IntervalUnit::Second]
default_unit: IntervalUnit,
+ /// Whether fractional calendar units (centuries, decades, years, months)
+ /// may be spilled into smaller units. `0.5 months` is parsed as 15 days
+ /// when this is `true`.
+ allow_fractional_calendar_units: bool,
}
impl IntervalParseConfig {
/// Create a new [IntervalParseConfig] with the given default unit
pub fn new(default_unit: IntervalUnit) -> Self {
- Self { default_unit }
+ Self {
+ default_unit,
+ allow_fractional_calendar_units: true,
+ }
+ }
+
+ /// Reject fractional calendar units instead of spilling them into smaller
units
+ pub fn reject_fractional_calendar_units(mut self) -> Self {
Review Comment:
```suggestion
pub(crate) fn reject_fractional_calendar_units(mut self) -> Self {
```
we can just keep this internal
##########
arrow-cast/src/cast/string.rs:
##########
@@ -132,6 +136,111 @@ fn parse_string_iter<
Ok(Arc::new(array) as ArrayRef)
}
+/// Parse a string to a duration, accepting integer strings (interpreted
+/// in the target unit) and interval strings with units, such as
+/// `"1 hour 10 minutes"`.
+fn parse_duration<D>(string: &str) -> Result<i64, ArrowError>
+where
+ D: ArrowTemporalType<Native = i64> + Parser,
+{
+ if let Some(duration) = D::parse(string) {
+ return Ok(duration);
+ }
+
+ let (unit, scale) = match D::DATA_TYPE {
+ DataType::Duration(TimeUnit::Second) => (IntervalParseUnit::Second,
1_000_000_000),
+ DataType::Duration(TimeUnit::Millisecond) =>
(IntervalParseUnit::Millisecond, 1_000_000),
+ DataType::Duration(TimeUnit::Microsecond) =>
(IntervalParseUnit::Microsecond, 1_000),
+ DataType::Duration(TimeUnit::Nanosecond) =>
(IntervalParseUnit::Nanosecond, 1),
+ _ => unreachable!(),
+ };
+
+ let interval = parse_interval_month_day_nano_config(string,
IntervalParseConfig::new(unit))
Review Comment:
we'll do in a followup then
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]