tustvold commented on code in PR #5319:
URL: https://github.com/apache/arrow-rs/pull/5319#discussion_r1461707363


##########
arrow-arith/src/temporal.rs:
##########
@@ -19,105 +19,352 @@
 
 use std::sync::Arc;
 
-use chrono::{DateTime, Datelike, NaiveDateTime, NaiveTime, Offset, Timelike};
-
-use arrow_array::builder::*;
-use arrow_array::iterator::ArrayIter;
-use arrow_array::temporal_conversions::{as_datetime, 
as_datetime_with_timezone, as_time};
+use arrow_array::cast::{downcast_array, AsArray};
+use chrono::{DateTime, Datelike, NaiveDateTime, Offset, TimeZone, Timelike, 
Utc};
+
+use arrow_array::temporal_conversions::{
+    date32_to_datetime, date64_to_datetime, time32ms_to_time, time32s_to_time, 
time64ns_to_time,
+    time64us_to_time, timestamp_ms_to_datetime, timestamp_ns_to_datetime, 
timestamp_s_to_datetime,
+    timestamp_us_to_datetime,
+};
 use arrow_array::timezone::Tz;
 use arrow_array::types::*;
 use arrow_array::*;
 use arrow_buffer::ArrowNativeType;
-use arrow_schema::{ArrowError, DataType};
-
-/// This function takes an `ArrayIter` of input array and an extractor `op` 
which takes
-/// an input `NaiveTime` and returns time component (e.g. hour) as `i32` value.
-/// The extracted values are built by the given `builder` to be an 
`Int32Array`.
-fn as_time_with_op<A: ArrayAccessor<Item = T::Native>, T: ArrowTemporalType, 
F>(
-    iter: ArrayIter<A>,
-    mut builder: PrimitiveBuilder<Int32Type>,
-    op: F,
-) -> Int32Array
-where
-    F: Fn(NaiveTime) -> i32,
-    i64: From<T::Native>,
-{
-    iter.into_iter().for_each(|value| {
-        if let Some(value) = value {
-            match as_time::<T>(i64::from(value)) {
-                Some(dt) => builder.append_value(op(dt)),
-                None => builder.append_null(),
-            }
-        } else {
-            builder.append_null();
-        }
-    });
+use arrow_schema::{ArrowError, DataType, TimeUnit};
+
+/// Valid parts to extract from date/timestamp arrays.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum DatePart {
+    /// Quarter of the year, in range `1..=4`
+    Quarter,
+    /// Calendar year
+    Year,
+    /// Month in the year, in range `1..=12`
+    Month,
+    /// ISO week of the year, in range `1..=53`
+    Week,
+    /// Day of the month, in range `1..=31`
+    Day,
+    /// Day of the week, in range `0..=6`, where Sunday is 0
+    DayOfWeekSunday0,
+    /// Day of the week, in range `0..=6`, where Monday is 0
+    DayOfWeekMonday0,
+    /// Day of year, in range `1..=366`
+    DayOfYear,
+    /// Hour of the day, in range `0..=23`
+    Hour,
+    /// Minute of the hour, in range `0..=59`
+    Minute,
+    /// Second of the minute, in range `0..=59`
+    Second,
+    /// Millisecond of the second
+    Millisecond,
+    /// Microsecond of the second
+    Microsecond,
+    /// Nanosecond of the second
+    Nanosecond,
+}
 
-    builder.finish()
+impl std::fmt::Display for DatePart {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{:?}", self)
+    }
 }
 
-/// This function takes an `ArrayIter` of input array and an extractor `op` 
which takes
-/// an input `NaiveDateTime` and returns data time component (e.g. hour) as 
`i32` value.
-/// The extracted values are built by the given `builder` to be an 
`Int32Array`.
-fn as_datetime_with_op<A: ArrayAccessor<Item = T::Native>, T: 
ArrowTemporalType, F>(
-    iter: ArrayIter<A>,
-    mut builder: PrimitiveBuilder<Int32Type>,
-    op: F,
-) -> Int32Array
-where
-    F: Fn(NaiveDateTime) -> i32,
-    i64: From<T::Native>,
-{
-    iter.into_iter().for_each(|value| {
-        if let Some(value) = value {
-            match as_datetime::<T>(i64::from(value)) {
-                Some(dt) => builder.append_value(op(dt)),
-                None => builder.append_null(),
-            }
-        } else {
-            builder.append_null();
-        }
-    });
+/// Returns function to extract relevant [`DatePart`] from a [`NaiveDateTime`].
+fn get_naive_date_time_part_extract_fn(part: DatePart) -> fn(NaiveDateTime) -> 
i32 {
+    match part {

Review Comment:
   Perhaps we could use 
https://docs.rs/chrono/latest/chrono/trait.Datelike.html and 
https://docs.rs/chrono/latest/chrono/trait.Timelike.html to reduce duplication 
with the method below



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