This is an automated email from the ASF dual-hosted git repository.

comphead pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 58e2904dde Minor: Avoid some repetition in to_timestamp (#11116)
58e2904dde is described below

commit 58e2904dde4c020fc858cf8f60e484bed357b56c
Author: Andrew Lamb <[email protected]>
AuthorDate: Tue Jun 25 11:35:30 2024 -0400

    Minor: Avoid some repetition in to_timestamp (#11116)
---
 datafusion/functions/src/datetime/to_timestamp.rs | 46 ++++++++---------------
 1 file changed, 16 insertions(+), 30 deletions(-)

diff --git a/datafusion/functions/src/datetime/to_timestamp.rs 
b/datafusion/functions/src/datetime/to_timestamp.rs
index 5af24af0c2..4cb91447f3 100644
--- a/datafusion/functions/src/datetime/to_timestamp.rs
+++ b/datafusion/functions/src/datetime/to_timestamp.rs
@@ -20,8 +20,8 @@ use std::any::Any;
 use arrow::datatypes::DataType::Timestamp;
 use arrow::datatypes::TimeUnit::{Microsecond, Millisecond, Nanosecond, Second};
 use arrow::datatypes::{
-    ArrowTimestampType, DataType, TimestampMicrosecondType, 
TimestampMillisecondType,
-    TimestampNanosecondType, TimestampSecondType,
+    ArrowTimestampType, DataType, TimeUnit, TimestampMicrosecondType,
+    TimestampMillisecondType, TimestampNanosecondType, TimestampSecondType,
 };
 
 use datafusion_common::{exec_err, Result, ScalarType};
@@ -144,12 +144,7 @@ impl ScalarUDFImpl for ToTimestampFunc {
     }
 
     fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
-        match &arg_types[0] {
-            DataType::Timestamp(_, Some(tz)) => {
-                Ok(Timestamp(Nanosecond, Some(tz.clone())))
-            }
-            _ => Ok(Timestamp(Nanosecond, None)),
-        }
+        Ok(return_type_for(&arg_types[0], Nanosecond))
     }
 
     fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
@@ -202,10 +197,7 @@ impl ScalarUDFImpl for ToTimestampSecondsFunc {
     }
 
     fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
-        match &arg_types[0] {
-            DataType::Timestamp(_, Some(tz)) => Ok(Timestamp(Second, 
Some(tz.clone()))),
-            _ => Ok(Timestamp(Second, None)),
-        }
+        Ok(return_type_for(&arg_types[0], Second))
     }
 
     fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
@@ -255,12 +247,7 @@ impl ScalarUDFImpl for ToTimestampMillisFunc {
     }
 
     fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
-        match &arg_types[0] {
-            DataType::Timestamp(_, Some(tz)) => {
-                Ok(Timestamp(Millisecond, Some(tz.clone())))
-            }
-            _ => Ok(Timestamp(Millisecond, None)),
-        }
+        Ok(return_type_for(&arg_types[0], Millisecond))
     }
 
     fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
@@ -310,12 +297,7 @@ impl ScalarUDFImpl for ToTimestampMicrosFunc {
     }
 
     fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
-        match &arg_types[0] {
-            DataType::Timestamp(_, Some(tz)) => {
-                Ok(Timestamp(Microsecond, Some(tz.clone())))
-            }
-            _ => Ok(Timestamp(Microsecond, None)),
-        }
+        Ok(return_type_for(&arg_types[0], Microsecond))
     }
 
     fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
@@ -365,12 +347,7 @@ impl ScalarUDFImpl for ToTimestampNanosFunc {
     }
 
     fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
-        match &arg_types[0] {
-            DataType::Timestamp(_, Some(tz)) => {
-                Ok(Timestamp(Nanosecond, Some(tz.clone())))
-            }
-            _ => Ok(Timestamp(Nanosecond, None)),
-        }
+        Ok(return_type_for(&arg_types[0], Nanosecond))
     }
 
     fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
@@ -406,6 +383,15 @@ impl ScalarUDFImpl for ToTimestampNanosFunc {
     }
 }
 
+/// Returns the return type for the to_timestamp_* function, preserving
+/// the timezone if it exists.
+fn return_type_for(arg: &DataType, unit: TimeUnit) -> DataType {
+    match arg {
+        Timestamp(_, Some(tz)) => Timestamp(unit, Some(tz.clone())),
+        _ => Timestamp(unit, None),
+    }
+}
+
 fn to_timestamp_impl<T: ArrowTimestampType + ScalarType<i64>>(
     args: &[ColumnarValue],
     name: &str,


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to