Skip to content

Commit

Permalink
refactor(rust): cleanup the match block for date inference (#11677)
Browse files Browse the repository at this point in the history
  • Loading branch information
svaningelgem authored Oct 12, 2023
1 parent 69b7fbb commit 86aafa8
Showing 1 changed file with 26 additions and 89 deletions.
115 changes: 26 additions & 89 deletions crates/polars-time/src/chunkedarray/utf8/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,97 +231,34 @@ impl TryFromWithUnit<Pattern> for DatetimeInfer<Int64Type> {

fn try_from_with_unit(value: Pattern, time_unit: Option<TimeUnit>) -> PolarsResult<Self> {
let time_unit = time_unit.expect("time_unit must be provided for datetime");
match (value, time_unit) {
(Pattern::DatetimeDMY | Pattern::DateDMY, TimeUnit::Milliseconds) => {
Ok(DatetimeInfer {
pattern: Pattern::DatetimeDMY,
patterns: patterns::DATETIME_D_M_Y,
latest_fmt: patterns::DATETIME_D_M_Y[0],
transform: transform_datetime_ms,
transform_bytes: StrpTimeState::default(),
fmt_len: 0,
logical_type: DataType::Datetime(TimeUnit::Milliseconds, None),
})
},
(Pattern::DatetimeDMY | Pattern::DateDMY, TimeUnit::Microseconds) => {
Ok(DatetimeInfer {
pattern: Pattern::DatetimeDMY,
patterns: patterns::DATETIME_D_M_Y,
latest_fmt: patterns::DATETIME_D_M_Y[0],
transform: transform_datetime_us,
transform_bytes: StrpTimeState::default(),
fmt_len: 0,
logical_type: DataType::Datetime(TimeUnit::Microseconds, None),
})
},
(Pattern::DatetimeDMY | Pattern::DateDMY, TimeUnit::Nanoseconds) => Ok(DatetimeInfer {
pattern: Pattern::DatetimeDMY,
patterns: patterns::DATETIME_D_M_Y,
latest_fmt: patterns::DATETIME_D_M_Y[0],
transform: transform_datetime_ns,
transform_bytes: StrpTimeState::default(),
fmt_len: 0,
logical_type: DataType::Datetime(TimeUnit::Nanoseconds, None),
}),
(Pattern::DatetimeYMD | Pattern::DateYMD, TimeUnit::Milliseconds) => {
Ok(DatetimeInfer {
pattern: Pattern::DatetimeYMD,
patterns: patterns::DATETIME_Y_M_D,
latest_fmt: patterns::DATETIME_Y_M_D[0],
transform: transform_datetime_ms,
transform_bytes: StrpTimeState::default(),
fmt_len: 0,
logical_type: DataType::Datetime(TimeUnit::Milliseconds, None),
})

let transform = match (time_unit, value) {
(TimeUnit::Milliseconds, Pattern::DatetimeYMDZ) => transform_tzaware_datetime_ms,
(TimeUnit::Milliseconds, _) => transform_datetime_ms,
(TimeUnit::Microseconds, Pattern::DatetimeYMDZ) => transform_tzaware_datetime_us,
(TimeUnit::Microseconds, _) => transform_datetime_us,
(TimeUnit::Nanoseconds, Pattern::DatetimeYMDZ) => transform_tzaware_datetime_ns,
(TimeUnit::Nanoseconds, _) => transform_datetime_ns,
};
let (pattern, patterns) = match value {
Pattern::DatetimeDMY | Pattern::DateDMY => {
(Pattern::DatetimeDMY, patterns::DATETIME_D_M_Y)
},
(Pattern::DatetimeYMD | Pattern::DateYMD, TimeUnit::Microseconds) => {
Ok(DatetimeInfer {
pattern: Pattern::DatetimeYMD,
patterns: patterns::DATETIME_Y_M_D,
latest_fmt: patterns::DATETIME_Y_M_D[0],
transform: transform_datetime_us,
transform_bytes: StrpTimeState::default(),
fmt_len: 0,
logical_type: DataType::Datetime(TimeUnit::Microseconds, None),
})
Pattern::DatetimeYMD | Pattern::DateYMD => {
(Pattern::DatetimeYMD, patterns::DATETIME_Y_M_D)
},
(Pattern::DatetimeYMD | Pattern::DateYMD, TimeUnit::Nanoseconds) => Ok(DatetimeInfer {
pattern: Pattern::DatetimeYMD,
patterns: patterns::DATETIME_Y_M_D,
latest_fmt: patterns::DATETIME_Y_M_D[0],
transform: transform_datetime_ns,
transform_bytes: StrpTimeState::default(),
fmt_len: 0,
logical_type: DataType::Datetime(TimeUnit::Nanoseconds, None),
}),
(Pattern::DatetimeYMDZ, TimeUnit::Milliseconds) => Ok(DatetimeInfer {
pattern: Pattern::DatetimeYMDZ,
patterns: patterns::DATETIME_Y_M_D_Z,
latest_fmt: patterns::DATETIME_Y_M_D_Z[0],
transform: transform_tzaware_datetime_ms,
transform_bytes: StrpTimeState::default(),
fmt_len: 0,
logical_type: DataType::Datetime(TimeUnit::Milliseconds, None),
}),
(Pattern::DatetimeYMDZ, TimeUnit::Microseconds) => Ok(DatetimeInfer {
pattern: Pattern::DatetimeYMDZ,
patterns: patterns::DATETIME_Y_M_D_Z,
latest_fmt: patterns::DATETIME_Y_M_D_Z[0],
transform: transform_tzaware_datetime_us,
transform_bytes: StrpTimeState::default(),
fmt_len: 0,
logical_type: DataType::Datetime(TimeUnit::Microseconds, None),
}),
(Pattern::DatetimeYMDZ, TimeUnit::Nanoseconds) => Ok(DatetimeInfer {
pattern: Pattern::DatetimeYMDZ,
patterns: patterns::DATETIME_Y_M_D_Z,
latest_fmt: patterns::DATETIME_Y_M_D_Z[0],
transform: transform_tzaware_datetime_ns,
transform_bytes: StrpTimeState::default(),
fmt_len: 0,
logical_type: DataType::Datetime(TimeUnit::Nanoseconds, None),
}),
}
Pattern::DatetimeYMDZ => (Pattern::DatetimeYMDZ, patterns::DATETIME_Y_M_D_Z),
};

Ok(DatetimeInfer {
pattern,
patterns,
latest_fmt: patterns[0],
transform,
transform_bytes: StrpTimeState::default(),
fmt_len: 0,
logical_type: DataType::Datetime(time_unit, None),
})
}
}

Expand Down

0 comments on commit 86aafa8

Please sign in to comment.