Skip to content

Commit

Permalink
date_trunc small update for readability (#12479)
Browse files Browse the repository at this point in the history
* Fix date_trunc error message

It was suggesting only nanosecond timestamps are supported, while all
arrow time units are.

* Remove dead code from date_trunc

`process_array::<TimestampNanosecondType>` can succeed only for an array
that can be downcast to array of nanosecond timestamps, but that case is
explicitly handled earlier.

* Simplify date_trunc time unit switch

Make it clear to the compiler (and the reader) that all time units are
covered.
  • Loading branch information
findepi authored Sep 16, 2024
1 parent 7f8a4c4 commit 5c7721b
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions datafusion/functions/src/datetime/date_trunc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,36 +189,37 @@ impl ScalarUDFImpl for DateTruncFunc {
}
ColumnarValue::Array(array) => {
let array_type = array.data_type();
match array_type {
Timestamp(Second, tz_opt) => {
process_array::<TimestampSecondType>(array, granularity, tz_opt)?
if let Timestamp(unit, tz_opt) = array_type {
match unit {
Second => process_array::<TimestampSecondType>(
array,
granularity,
tz_opt,
)?,
Millisecond => process_array::<TimestampMillisecondType>(
array,
granularity,
tz_opt,
)?,
Microsecond => process_array::<TimestampMicrosecondType>(
array,
granularity,
tz_opt,
)?,
Nanosecond => process_array::<TimestampNanosecondType>(
array,
granularity,
tz_opt,
)?,
}
Timestamp(Millisecond, tz_opt) => process_array::<
TimestampMillisecondType,
>(
array, granularity, tz_opt
)?,
Timestamp(Microsecond, tz_opt) => process_array::<
TimestampMicrosecondType,
>(
array, granularity, tz_opt
)?,
Timestamp(Nanosecond, tz_opt) => process_array::<
TimestampNanosecondType,
>(
array, granularity, tz_opt
)?,
_ => process_array::<TimestampNanosecondType>(
array,
granularity,
&None,
)?,
} else {
return exec_err!("second argument of `date_trunc` is an unsupported array type: {array_type}");
}
}
_ => {
return exec_err!(
"second argument of `date_trunc` must be nanosecond timestamp scalar or array"
);
"second argument of `date_trunc` must be timestamp scalar or array"
);
}
})
}
Expand Down

0 comments on commit 5c7721b

Please sign in to comment.