parthchandra commented on code in PR #3804:
URL: https://github.com/apache/datafusion-comet/pull/3804#discussion_r3042337792


##########
native/spark-expr/src/datetime_funcs/hours.rs:
##########
@@ -0,0 +1,299 @@
+// 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.
+
+//! Spark-compatible `hours` V2 partition transform.
+//!
+//! Computes the number of hours since the Unix epoch (1970-01-01 00:00:00 
UTC).
+//!
+//! - For `Timestamp(Microsecond, Some(tz))`: applies timezone offset before 
computing.
+//! - For `Timestamp(Microsecond, None)` (NTZ): uses raw microseconds directly.
+
+use arrow::array::cast::as_primitive_array;
+use arrow::array::types::TimestampMicrosecondType;
+use arrow::array::{Array, Int32Array};
+use arrow::datatypes::{DataType, TimeUnit::Microsecond};
+use arrow::temporal_conversions::as_datetime;
+use chrono::{Offset, TimeZone};
+use datafusion::common::{internal_datafusion_err, DataFusionError};
+use datafusion::logical_expr::{
+    ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
+};
+use std::{any::Any, fmt::Debug, sync::Arc};
+
+use crate::timezone::Tz;
+
+const MICROS_PER_HOUR: i64 = 3_600_000_000;
+
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkHoursTransform {
+    signature: Signature,
+    timezone: String,
+}
+
+impl SparkHoursTransform {
+    pub fn new(timezone: String) -> Self {
+        Self {
+            signature: Signature::user_defined(Volatility::Immutable),
+            timezone,
+        }
+    }
+}
+
+impl ScalarUDFImpl for SparkHoursTransform {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn name(&self) -> &str {
+        "hours_transform"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, _arg_types: &[DataType]) -> 
datafusion::common::Result<DataType> {
+        Ok(DataType::Int32)
+    }
+
+    fn invoke_with_args(
+        &self,
+        args: ScalarFunctionArgs,
+    ) -> datafusion::common::Result<ColumnarValue> {
+        let args: [ColumnarValue; 1] = args.args.try_into().map_err(|_| {
+            internal_datafusion_err!("hours_transform expects exactly one 
argument")
+        })?;
+
+        match args {
+            [ColumnarValue::Array(array)] => {
+                let ts_array = 
as_primitive_array::<TimestampMicrosecondType>(&array);
+                let result: Int32Array = match array.data_type() {
+                    DataType::Timestamp(Microsecond, Some(_)) => {
+                        let tz: Tz = self.timezone.parse().map_err(|e| {
+                            DataFusionError::Execution(format!(
+                                "Failed to parse timezone '{}': {}",
+                                self.timezone, e
+                            ))
+                        })?;
+                        arrow::compute::kernels::arity::try_unary(ts_array, 
|micros| {
+                            let dt = 
as_datetime::<TimestampMicrosecondType>(micros).ok_or_else(
+                                || {
+                                    DataFusionError::Execution(format!(
+                                        "Cannot convert {micros} to datetime"
+                                    ))
+                                },
+                            )?;
+                            let offset_secs =
+                                
tz.offset_from_utc_datetime(&dt).fix().local_minus_utc() as i64;
+                            let local_micros = micros + offset_secs * 
1_000_000;

Review Comment:
   In Spark's corresponding implementation in [InMemoryBaseTable 
](https://github.com/apache/spark/blob/af9c8b346673c0ffa79ce3b0a76e53e9df51fe76/sql/catalyst/src/test/scala/org/apache/spark/sql/connector/catalog/InMemoryBaseTable.scala#L232)
 it looks like the session timezone is not being applied. 
   Can you add a unit test that reads from InMemoryBaseTable and compares with 
the results produced by Spark ?



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