alamb commented on code in PR #18356:
URL: https://github.com/apache/datafusion/pull/18356#discussion_r2477489831
##########
datafusion/functions/src/datetime/date_trunc.rs:
##########
@@ -456,37 +457,40 @@ fn general_date_trunc_array_fine_granularity<T:
ArrowTimestampType>(
granularity: &str,
) -> Result<ArrayRef> {
let unit = match (tu, granularity) {
- (Second, "minute") => Some(Int64Array::new_scalar(60)),
- (Second, "hour") => Some(Int64Array::new_scalar(3600)),
- (Second, "day") => Some(Int64Array::new_scalar(86400)),
-
- (Millisecond, "second") => Some(Int64Array::new_scalar(1_000)),
- (Millisecond, "minute") => Some(Int64Array::new_scalar(60_000)),
- (Millisecond, "hour") => Some(Int64Array::new_scalar(3_600_000)),
- (Millisecond, "day") => Some(Int64Array::new_scalar(86_400_000)),
-
- (Microsecond, "millisecond") => Some(Int64Array::new_scalar(1_000)),
- (Microsecond, "second") => Some(Int64Array::new_scalar(1_000_000)),
- (Microsecond, "minute") => Some(Int64Array::new_scalar(60_000_000)),
- (Microsecond, "hour") => Some(Int64Array::new_scalar(3_600_000_000)),
- (Microsecond, "day") => Some(Int64Array::new_scalar(86_400_000_000)),
-
- (Nanosecond, "microsecond") => Some(Int64Array::new_scalar(1_000)),
- (Nanosecond, "millisecond") => Some(Int64Array::new_scalar(1_000_000)),
- (Nanosecond, "second") => Some(Int64Array::new_scalar(1_000_000_000)),
- (Nanosecond, "minute") => Some(Int64Array::new_scalar(60_000_000_000)),
- (Nanosecond, "hour") =>
Some(Int64Array::new_scalar(3_600_000_000_000)),
- (Nanosecond, "day") =>
Some(Int64Array::new_scalar(86_400_000_000_000)),
+ (Second, "minute") => NonZeroI64::new(60),
+ (Second, "hour") => NonZeroI64::new(3600),
+ (Second, "day") => NonZeroI64::new(86400),
+
+ (Millisecond, "second") => NonZeroI64::new(1_000),
+ (Millisecond, "minute") => NonZeroI64::new(60_000),
+ (Millisecond, "hour") => NonZeroI64::new(3_600_000),
+ (Millisecond, "day") => NonZeroI64::new(86_400_000),
+
+ (Microsecond, "millisecond") => NonZeroI64::new(1_000),
+ (Microsecond, "second") => NonZeroI64::new(1_000_000),
+ (Microsecond, "minute") => NonZeroI64::new(60_000_000),
+ (Microsecond, "hour") => NonZeroI64::new(3_600_000_000),
+ (Microsecond, "day") => NonZeroI64::new(86_400_000_000),
+
+ (Nanosecond, "microsecond") => NonZeroI64::new(1_000),
+ (Nanosecond, "millisecond") => NonZeroI64::new(1_000_000),
+ (Nanosecond, "second") => NonZeroI64::new(1_000_000_000),
+ (Nanosecond, "minute") => NonZeroI64::new(60_000_000_000),
+ (Nanosecond, "hour") => NonZeroI64::new(3_600_000_000_000),
+ (Nanosecond, "day") => NonZeroI64::new(86_400_000_000_000),
_ => None,
};
if let Some(unit) = unit {
- let original_type = array.data_type();
- let array = arrow::compute::cast(array, &DataType::Int64)?;
- let array = arrow::compute::kernels::numeric::div(&array, &unit)?;
- let array = arrow::compute::kernels::numeric::mul(&array, &unit)?;
- let array = arrow::compute::cast(&array, original_type)?;
- Ok(array)
+ let unit = unit.get();
+ let array = PrimitiveArray::<T>::from_iter_values_with_nulls(
+ array
+ .values()
+ .iter()
+ .map(|v| *v - i64::rem_euclid(*v, unit)),
Review Comment:
this is pretty fancy
--
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]