xiedeyantu commented on code in PR #22315:
URL: https://github.com/apache/datafusion/pull/22315#discussion_r3303812605
##########
datafusion/functions/src/datetime/date_bin.rs:
##########
@@ -575,53 +578,85 @@ fn date_bin_impl(
return exec_err!("DATE_BIN stride must be non-zero");
}
- fn stride_map_fn<T: ArrowTimestampType>(
- origin: i64,
- stride: i64,
- stride_fn: BinFunction,
- ) -> impl Fn(i64) -> Result<i64> {
- let scale = match T::UNIT {
+ fn timestamp_scale<T: ArrowTimestampType>() -> i64 {
+ match T::UNIT {
Nanosecond => 1,
Microsecond => NANOS_PER_MICRO,
Millisecond => NANOS_PER_MILLI,
Second => NANOSECONDS,
- };
- move |x: i64| match stride_fn(stride, x * scale, origin) {
- Ok(result) => Ok(result / scale),
- Err(e) => Err(e),
}
}
+ fn scale_timestamp_to_nanos(x: i64, scale: i64) -> Result<i64> {
+ x.checked_mul(scale).ok_or_else(|| {
+ DataFusionError::Execution(format!(
+ "DATE_BIN source timestamp {x} cannot be represented in
nanoseconds"
+ ))
+ })
+ }
+
Ok(match array {
ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(v, tz_opt)) => {
- let apply_stride_fn =
- stride_map_fn::<TimestampNanosecondType>(origin, stride,
stride_fn);
+ let scale = timestamp_scale::<TimestampNanosecondType>();
ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(
- v.and_then(|val| apply_stride_fn(val).ok()),
+ match *v {
+ Some(val) => {
+ let scaled = scale_timestamp_to_nanos(val, scale)?;
+ match stride_fn(stride, scaled, origin) {
+ Ok(result) => Some(result / scale),
+ Err(_) => None,
+ }
Review Comment:
Should I fix these two issues in this PR?
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]