appletreeisyellow commented on code in PR #11347:
URL: https://github.com/apache/datafusion/pull/11347#discussion_r1671322760


##########
datafusion/functions/src/datetime/to_local_time.rs:
##########
@@ -0,0 +1,601 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::any::Any;
+use std::ops::Add;
+use std::sync::Arc;
+
+use arrow::array::timezone::Tz;
+use arrow::array::{ArrayRef, PrimitiveArray};
+use arrow::datatypes::DataType::Timestamp;
+use arrow::datatypes::{
+    ArrowTimestampType, DataType, TimestampMicrosecondType, 
TimestampMillisecondType,
+    TimestampNanosecondType, TimestampSecondType,
+};
+use arrow::datatypes::{
+    TimeUnit,
+    TimeUnit::{Microsecond, Millisecond, Nanosecond, Second},
+};
+
+use chrono::{Offset, TimeDelta, TimeZone, Utc};
+use datafusion_common::cast::as_primitive_array;
+use datafusion_common::{exec_err, Result, ScalarValue};
+use datafusion_expr::TypeSignature::Exact;
+use datafusion_expr::{
+    ColumnarValue, ScalarUDFImpl, Signature, Volatility, TIMEZONE_WILDCARD,
+};
+
+/// A UDF function that converts a timezone-aware timestamp to local time 
(with no offset or
+/// timezone information). In other words, this function strips off the 
timezone from the timestamp,
+/// while keep the display value of the timestamp the same.
+///
+/// # Example 1
+///
+/// ```
+/// # use datafusion_common::ScalarValue;
+/// # use datafusion_expr::ColumnarValue;
+/// # use datafusion_functions::datetime::to_local_time::ToLocalTimeFunc;
+/// # use datafusion_expr::ScalarUDFImpl;
+///
+/// // 2019-03-31 01:00:00 +01:00
+/// let res = ToLocalTimeFunc::new()
+///     .invoke(&[ColumnarValue::Scalar(ScalarValue::TimestampSecond(
+///         Some(1_553_990_400),
+///         Some("Europe/Brussels".into()),
+///     ))])
+///     .unwrap();
+///
+/// // 2019-03-31 01:00:00 <-- this timestamp no longer has +01:00 offset
+/// let expected = ScalarValue::TimestampSecond(Some(1_553_994_000), None);
+///
+/// match res {
+///   ColumnarValue::Scalar(res) => {
+///       assert_eq!(res, expected);
+///   }
+///   _ => panic!("unexpected return type"),
+/// }
+/// ```
+///
+/// # Example 2
+///
+/// ```
+/// # use datafusion_common::ScalarValue;
+/// # use datafusion_expr::ColumnarValue;
+/// # use chrono::NaiveDateTime;
+/// # use datafusion_functions::datetime::to_local_time::ToLocalTimeFunc;
+/// # use datafusion_expr::ScalarUDFImpl;
+///
+/// let timestamp_str = "2020-03-31T13:40:00";
+/// let timezone_str = "America/New_York";
+/// let tz: arrow::array::timezone::Tz =
+///     timezone_str.parse().expect("Invalid timezone");
+///
+/// let timestamp = timestamp_str
+///     .parse::<NaiveDateTime>()
+///     .unwrap()
+///     .and_local_timezone(tz) // this is in a local timezone
+///     .unwrap()
+///     .timestamp_nanos_opt()
+///     .unwrap();
+///
+/// let expected_timestamp = timestamp_str
+///     .parse::<NaiveDateTime>()
+///     .unwrap()
+///     .and_utc() // this is in UTC
+///     .timestamp_nanos_opt()
+///     .unwrap();
+///
+/// let input =
+///     ScalarValue::TimestampNanosecond(Some(timestamp), 
Some(timezone_str.into()));
+/// let res = ToLocalTimeFunc::new()
+///     .invoke(&[ColumnarValue::Scalar(input)])
+///     .unwrap();
+/// let expected = ScalarValue::TimestampNanosecond(Some(expected_timestamp), 
None);
+/// match res {
+///     ColumnarValue::Scalar(res) => {
+///         assert_eq!(res, expected);
+///         }
+///     _ => panic!("unexpected return type"),
+/// }
+/// ```
+#[derive(Debug)]
+pub struct ToLocalTimeFunc {
+    signature: Signature,
+}
+
+impl Default for ToLocalTimeFunc {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl ToLocalTimeFunc {
+    pub fn new() -> Self {
+        let base_sig = |array_type: TimeUnit| {
+            vec![
+                Exact(vec![Timestamp(array_type, None)]),
+                Exact(vec![Timestamp(array_type, 
Some(TIMEZONE_WILDCARD.into()))]),
+            ]
+        };
+
+        let full_sig = [Nanosecond, Microsecond, Millisecond, Second]
+            .into_iter()
+            .map(base_sig)
+            .collect::<Vec<_>>()
+            .concat();

Review Comment:
   Neat! Updated in 
https://github.com/apache/datafusion/pull/11347/commits/5fe18d5e12c2479bfeaf0ce6cc84cdcf7670c93d



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