martin-g commented on code in PR #19640:
URL: https://github.com/apache/datafusion/pull/19640#discussion_r2664621711


##########
datafusion/functions/src/datetime/date_trunc.rs:
##########
@@ -315,40 +324,77 @@ impl ScalarUDFImpl for DateTruncFunc {
             ColumnarValue::Scalar(ScalarValue::TimestampSecond(v, tz_opt)) => {
                 process_scalar::<TimestampSecondType>(v, granularity, tz_opt)?
             }
+            ColumnarValue::Scalar(ScalarValue::Time64Nanosecond(v)) => {
+                let truncated = v.map(|val| truncate_time_nanos(val, 
granularity));
+                ColumnarValue::Scalar(ScalarValue::Time64Nanosecond(truncated))
+            }
+            ColumnarValue::Scalar(ScalarValue::Time64Microsecond(v)) => {
+                let truncated = v.map(|val| truncate_time_micros(val, 
granularity));
+                
ColumnarValue::Scalar(ScalarValue::Time64Microsecond(truncated))
+            }
+            ColumnarValue::Scalar(ScalarValue::Time32Millisecond(v)) => {
+                let truncated = v.map(|val| truncate_time_millis(val, 
granularity));
+                
ColumnarValue::Scalar(ScalarValue::Time32Millisecond(truncated))
+            }
+            ColumnarValue::Scalar(ScalarValue::Time32Second(v)) => {
+                let truncated = v.map(|val| truncate_time_secs(val, 
granularity));
+                ColumnarValue::Scalar(ScalarValue::Time32Second(truncated))
+            }
             ColumnarValue::Array(array) => {
                 let array_type = array.data_type();
-                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,
-                        )?,
+                match array_type {
+                    Timestamp(Second, tz_opt) => {
+                        process_array::<TimestampSecondType>(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
+                    )?,
+                    Time64(Nanosecond) => {
+                        let arr = 
as_primitive_array::<Time64NanosecondType>(array)?;
+                        let result: PrimitiveArray<Time64NanosecondType> =
+                            arr.unary(|v| truncate_time_nanos(v, granularity));
+                        ColumnarValue::Array(Arc::new(result))
+                    }
+                    Time64(Microsecond) => {
+                        let arr = 
as_primitive_array::<Time64MicrosecondType>(array)?;
+                        let result: PrimitiveArray<Time64MicrosecondType> =
+                            arr.unary(|v| truncate_time_micros(v, 
granularity));
+                        ColumnarValue::Array(Arc::new(result))
+                    }
+                    Time32(Millisecond) => {
+                        let arr = 
as_primitive_array::<Time32MillisecondType>(array)?;
+                        let result: PrimitiveArray<Time32MillisecondType> =
+                            arr.unary(|v| truncate_time_millis(v, 
granularity));
+                        ColumnarValue::Array(Arc::new(result))
+                    }
+                    Time32(Second) => {
+                        let arr = 
as_primitive_array::<Time32SecondType>(array)?;
+                        let result: PrimitiveArray<Time32SecondType> =
+                            arr.unary(|v| truncate_time_secs(v, granularity));
+                        ColumnarValue::Array(Arc::new(result))
+                    }
+                    _ => {
+                        return exec_err!(
+                            "second argument of `date_trunc` is an unsupported 
array type: {array_type}"
+                        );
                     }
-                } 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 timestamp scalar 
or array"
+                    "second argument of `date_trunc` must be timestamp, time 
scalar or array"

Review Comment:
   ```suggestion
                       "second argument of `date_trunc` must be a 
timestamp/time scalar or an array"
   ```



##########
datafusion/sqllogictest/test_files/information_schema.slt:
##########
@@ -793,14 +793,11 @@ string_agg String AGGREGATE
 query TTTTTTTBTTTT rowsort
 select * from information_schema.routines where routine_name = 'date_trunc' OR 
routine_name = 'string_agg' OR routine_name = 'rank' ORDER BY routine_name
 ----
-datafusion public date_trunc datafusion public date_trunc FUNCTION true 
Timestamp(Microsecond, None) SCALAR Truncates a timestamp value to a specified 
precision. date_trunc(precision, expression)
-datafusion public date_trunc datafusion public date_trunc FUNCTION true 
Timestamp(Microsecond, Some("+TZ")) SCALAR Truncates a timestamp value to a 
specified precision. date_trunc(precision, expression)
-datafusion public date_trunc datafusion public date_trunc FUNCTION true 
Timestamp(Millisecond, None) SCALAR Truncates a timestamp value to a specified 
precision. date_trunc(precision, expression)
-datafusion public date_trunc datafusion public date_trunc FUNCTION true 
Timestamp(Millisecond, Some("+TZ")) SCALAR Truncates a timestamp value to a 
specified precision. date_trunc(precision, expression)
-datafusion public date_trunc datafusion public date_trunc FUNCTION true 
Timestamp(Nanosecond, None) SCALAR Truncates a timestamp value to a specified 
precision. date_trunc(precision, expression)
-datafusion public date_trunc datafusion public date_trunc FUNCTION true 
Timestamp(Nanosecond, Some("+TZ")) SCALAR Truncates a timestamp value to a 
specified precision. date_trunc(precision, expression)
-datafusion public date_trunc datafusion public date_trunc FUNCTION true 
Timestamp(Second, None) SCALAR Truncates a timestamp value to a specified 
precision. date_trunc(precision, expression)
-datafusion public date_trunc datafusion public date_trunc FUNCTION true 
Timestamp(Second, Some("+TZ")) SCALAR Truncates a timestamp value to a 
specified precision. date_trunc(precision, expression)
+datafusion public date_trunc datafusion public date_trunc FUNCTION true Date 
SCALAR Truncates a timestamp or time value to a specified precision. 
date_trunc(precision, expression)
+datafusion public date_trunc datafusion public date_trunc FUNCTION true String 
SCALAR Truncates a timestamp or time value to a specified precision. 
date_trunc(precision, expression)

Review Comment:
   Are those return types correct - `Date` and `String` ? Shouldn't they be 
converted to `Timestamp(Nanosecond, None)`
   I think the reason is here - 
https://github.com/apache/datafusion/pull/19640/changes#diff-7a6e17bfd876e9e88341d68750224b8a2294d9f401eef2126a49ade1665c336cR228



-- 
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]

Reply via email to