sunchao commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r963904942


##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -1502,21 +1503,37 @@ where
 
     if let Some(tz) = tz {
         let mut scratch = Parsed::new();
-        // The macro calls `value_as_datetime_with_tz` on timestamp values of 
the array.
+        // The macro calls `as_datetime` on timestamp values of the array.
         // After applying timezone offset on the datatime, calling `to_string` 
to get
         // the strings.
+        let iter = ArrayIter::new(array);
         extract_component_from_array!(
-            array,
+            iter,
             builder,
             to_string,
-            value_as_datetime_with_tz,
+            |value, tz| as_datetime::<T>(<i64 as From<
+                <T as ArrowPrimitiveType>::Native,
+            >>::from(value))
+            .map(|datetime| datetime + tz),
             tz,
             scratch,
+            |value| as_datetime::<T>(
+                <i64 as From<<T as ArrowPrimitiveType>::Native>>::from(value)
+            ),
             |h| h
         )
     } else {
         // No timezone available. Calling `to_string` on the datatime value 
simply.
-        extract_component_from_array!(array, builder, to_string, 
value_as_datetime, |h| h)
+        let iter = ArrayIter::new(array);
+        extract_component_from_array!(
+            iter,
+            builder,
+            to_string,
+            |value| as_datetime::<T>(
+                <i64 as From<<T as ArrowPrimitiveType>::Native>>::from(value)

Review Comment:
   nit: we can just use `<i64 as From<_>>::from(value)` and let type inference 
do the work



##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -171,335 +172,747 @@ pub fn using_chrono_tz_and_utc_naive_date_time(
         .ok()
 }
 
-/// Extracts the hours of a given temporal array as an array of integers
+/// Extracts the hours of a given temporal primitive array as an array of 
integers

Review Comment:
   nit: add comments explaining what exactly are the returned integers, are 
they within range `[0, 24)`?



##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -171,335 +172,747 @@ pub fn using_chrono_tz_and_utc_naive_date_time(
         .ok()
 }
 
-/// Extracts the hours of a given temporal array as an array of integers
+/// Extracts the hours of a given temporal primitive array as an array of 
integers
 pub fn hour<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+    T: ArrowTemporalType + ArrowNumericType,
+    i64: std::convert::From<T::Native>,
+{
+    hour_generic::<T, _>(array)
+}
+
+/// Extracts the hours of a given temporal array as an array of integers
+pub fn hour_generic<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> 
Result<Int32Array>
+where
+    T: ArrowTemporalType + ArrowNumericType,
+    i64: std::convert::From<T::Native>,
+{
+    match array.data_type().clone() {
+        DataType::Dictionary(_, value_type) => {
+            hour_internal::<T, A>(array, value_type.as_ref())
+        }
+        dt => hour_internal::<T, A>(array, &dt),
+    }
+}
+
+/// Extracts the hours of a given temporal array as an array of integers
+fn hour_internal<T, A: ArrayAccessor<Item = T::Native>>(
+    array: A,
+    dt: &DataType,
+) -> Result<Int32Array>
 where
     T: ArrowTemporalType + ArrowNumericType,
     i64: std::convert::From<T::Native>,
 {
     let mut b = Int32Builder::with_capacity(array.len());
-    match array.data_type() {
-        &DataType::Time32(_) | &DataType::Time64(_) => {
-            extract_component_from_array!(array, b, hour, value_as_time, |h| h 
as i32)
+    match dt {
+        DataType::Time32(_) | DataType::Time64(_) => {
+            let iter = ArrayIter::new(array);
+            extract_component_from_array!(
+                iter,
+                b,
+                hour,
+                |value| as_time::<T>(i64::from(value)),
+                |h| h as i32
+            );
         }
-        &DataType::Date32 | &DataType::Date64 | &DataType::Timestamp(_, None) 
=> {
-            extract_component_from_array!(array, b, hour, value_as_datetime, 
|h| h as i32)
+        DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, None) => {
+            let iter = ArrayIter::new(array);
+            extract_component_from_array!(
+                iter,
+                b,
+                hour,
+                |value| as_datetime::<T>(i64::from(value)),
+                |h| h as i32
+            )
         }
-        &DataType::Timestamp(_, Some(ref tz)) => {
+        DataType::Timestamp(_, Some(tz)) => {
             let mut scratch = Parsed::new();
+            let iter = ArrayIter::new(array);
             extract_component_from_array!(
-                array,
+                iter,
                 b,
                 hour,
-                value_as_datetime_with_tz,
+                |value, tz| as_datetime::<T>(i64::from(value))
+                    .map(|datetime| datetime + tz),
                 tz,
                 scratch,
+                |value| as_datetime::<T>(i64::from(value)),
                 |h| h as i32
             )
         }
-        dt => return_compute_error_with!("hour does not support", dt),
+        _ => return_compute_error_with!("hour does not support", 
array.data_type()),
     }
 
     Ok(b.finish())
 }
 
-/// Extracts the years of a given temporal array as an array of integers
+/// Extracts the years of a given temporal primitive array as an array of 
integers
 pub fn year<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+    T: ArrowTemporalType + ArrowNumericType,
+    i64: std::convert::From<T::Native>,
+{
+    year_generic::<T, _>(array)
+}
+
+/// Extracts the years of a given temporal array as an array of integers
+pub fn year_generic<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> 
Result<Int32Array>
+where
+    T: ArrowTemporalType + ArrowNumericType,
+    i64: std::convert::From<T::Native>,
+{
+    match array.data_type().clone() {
+        DataType::Dictionary(_, value_type) => {
+            year_internal::<T, A>(array, value_type.as_ref())
+        }
+        dt => year_internal::<T, A>(array, &dt),
+    }
+}
+
+/// Extracts the years of a given temporal array as an array of integers
+fn year_internal<T, A: ArrayAccessor<Item = T::Native>>(
+    array: A,
+    dt: &DataType,
+) -> Result<Int32Array>
 where
     T: ArrowTemporalType + ArrowNumericType,
     i64: std::convert::From<T::Native>,
 {
     let mut b = Int32Builder::with_capacity(array.len());
-    match array.data_type() {
-        &DataType::Date32 | &DataType::Date64 | &DataType::Timestamp(_, _) => {
-            extract_component_from_array!(array, b, year, value_as_datetime, 
|h| h as i32)
+    match dt {
+        DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, _) => {
+            let iter = ArrayIter::new(array);
+            extract_component_from_array!(
+                iter,
+                b,
+                year,
+                |value| as_datetime::<T>(i64::from(value)),
+                |h| h as i32
+            )
         }
-        dt => return_compute_error_with!("year does not support", dt),
+        _t => return_compute_error_with!("year does not support", 
array.data_type()),
     }
 
     Ok(b.finish())
 }
 
-/// Extracts the quarter of a given temporal array as an array of integers
+/// Extracts the quarter of a given temporal primitive array as an array of 
integers

Review Comment:
   ditto: what do the returned integers represent?



##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -28,40 +29,40 @@ use chrono::format::{parse, Parsed};
 use chrono::FixedOffset;
 
 macro_rules! extract_component_from_array {
-    ($array:ident, $builder:ident, $extract_fn:ident, $using:ident, 
$convert:expr) => {
-        for i in 0..$array.len() {
-            if $array.is_null(i) {
-                $builder.append_null();
-            } else {
-                match $array.$using(i) {
+    ($iter:ident, $builder:ident, $extract_fn:ident, $using:expr, 
$convert:expr) => {

Review Comment:
   +1. It'd be better to improve the readability here. 



##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -171,335 +172,747 @@ pub fn using_chrono_tz_and_utc_naive_date_time(
         .ok()
 }
 
-/// Extracts the hours of a given temporal array as an array of integers
+/// Extracts the hours of a given temporal primitive array as an array of 
integers
 pub fn hour<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+    T: ArrowTemporalType + ArrowNumericType,
+    i64: std::convert::From<T::Native>,
+{
+    hour_generic::<T, _>(array)
+}
+
+/// Extracts the hours of a given temporal array as an array of integers
+pub fn hour_generic<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> 
Result<Int32Array>

Review Comment:
   why we need this `hour_generic` method? I feel we just need `hour`.



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

Reply via email to