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


##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -171,335 +172,751 @@ 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 within
+/// the range of [0, 23].
 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 within
+/// the range of [0, 23].
+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 within
+/// the range of [1, 4].
 pub fn quarter<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+    T: ArrowTemporalType + ArrowNumericType,
+    i64: std::convert::From<T::Native>,
+{
+    quarter_generic::<T, _>(array)
+}
+
+/// Extracts the quarter of a given temporal array as an array of integersa 
within
+/// the range of [1, 4].
+pub fn quarter_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) => {
+            quarter_internal::<T, A>(array, value_type.as_ref())
+        }
+        dt => quarter_internal::<T, A>(array, &dt),
+    }
+}
+
+/// Extracts the quarter of a given temporal array as an array of integers
+fn quarter_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(_, None) 
=> {
-            extract_component_from_array!(array, b, quarter, 
value_as_datetime, |h| h
-                as i32)
+    match dt {
+        DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, None) => {
+            let iter = ArrayIter::new(array);
+            extract_component_from_array!(
+                iter,
+                b,
+                quarter,
+                |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,
                 quarter,
-                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!("quarter does not support", dt),
+        _ => return_compute_error_with!("quarter does not support", 
array.data_type()),
     }
 
     Ok(b.finish())
 }
 
-/// Extracts the month of a given temporal array as an array of integers
+/// Extracts the month of a given temporal primitive array as an array of 
integers

Review Comment:
   nit: is the returned integers 0-based or 1-based? might add a comment. We 
should follow the methods like `num_days_from_monday` which have some nice 
documentation.



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