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


##########
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:
   It is the quarter within the range of [1, 4].



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