Skip to content

Commit

Permalink
Minor: remove some unwrap (#6738)
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb authored Jun 21, 2023
1 parent 3908411 commit b275e1d
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions datafusion/physical-expr/src/datetime_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ fn date_trunc_single(granularity: &str, value: i64) -> Result<i64> {
)));
}
};
// `with_x(0)` are infalible because `0` are always a valid
// `with_x(0)` are infallible because `0` are always a valid
Ok(value.unwrap().timestamp_nanos())
}

Expand All @@ -284,44 +284,34 @@ pub fn date_trunc(args: &[ColumnarValue]) -> Result<ColumnarValue> {
ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(v, tz_opt)) => {
let nano = (f)(*v)?;

if nano.is_none() {
return Ok(ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(
None,
tz_opt.clone(),
)));
}

match granularity.as_str() {
"minute" => {
// trunc to minute
let second = ScalarValue::TimestampNanosecond(
Some(nano.unwrap() / 1_000_000_000 * 1_000_000_000),
nano.map(|nano| nano / 1_000_000_000 * 1_000_000_000),
tz_opt.clone(),
);
ColumnarValue::Scalar(second)
}
"second" => {
// trunc to second
let mill = ScalarValue::TimestampNanosecond(
Some(nano.unwrap() / 1_000_000 * 1_000_000),
nano.map(|nano| nano / 1_000_000 * 1_000_000),
tz_opt.clone(),
);
ColumnarValue::Scalar(mill)
}
"millisecond" => {
// trunc to microsecond
let micro = ScalarValue::TimestampNanosecond(
Some(nano.unwrap() / 1_000 * 1_000),
nano.map(|nano| nano / 1_000 * 1_000),
tz_opt.clone(),
);
ColumnarValue::Scalar(micro)
}
_ => {
// trunc to nanosecond
let nano = ScalarValue::TimestampNanosecond(
Some(nano.unwrap()),
tz_opt.clone(),
);
let nano = ScalarValue::TimestampNanosecond(nano, tz_opt.clone());
ColumnarValue::Scalar(nano)
}
}
Expand Down

0 comments on commit b275e1d

Please sign in to comment.