This is an automated email from the ASF dual-hosted git repository.

tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new 6a37d9f5f feat(compute): Support doy (day of year) for temporal (#2067)
6a37d9f5f is described below

commit 6a37d9f5ff69be0d5c8d73300e79e08c93803df1
Author: Dmitry Patsura <[email protected]>
AuthorDate: Mon Jul 18 20:20:19 2022 +0300

    feat(compute): Support doy (day of year) for temporal (#2067)
    
    * feat(compute): Support doy (day of year) for temporal
    
    * Update arrow/src/compute/kernels/temporal.rs
    
    Co-authored-by: Raphael Taylor-Davies 
<[email protected]>
    
    Co-authored-by: Raphael Taylor-Davies 
<[email protected]>
---
 arrow/src/compute/kernels/temporal.rs | 51 ++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)

diff --git a/arrow/src/compute/kernels/temporal.rs 
b/arrow/src/compute/kernels/temporal.rs
index efb828430..b5cbda140 100644
--- a/arrow/src/compute/kernels/temporal.rs
+++ b/arrow/src/compute/kernels/temporal.rs
@@ -342,7 +342,7 @@ where
                 scratch
             )
         }
-        dt => return_compute_error_with!("weekday does not support", dt),
+        dt => return_compute_error_with!("num_days_from_sunday does not 
support", dt),
     }
 
     Ok(b.finish())
@@ -376,6 +376,35 @@ where
     Ok(b.finish())
 }
 
+/// Extracts the day of year of a given temporal array as an array of integers
+/// The day of year that ranges from 1 to 366
+pub fn doy<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+    T: ArrowTemporalType + ArrowNumericType,
+    i64: std::convert::From<T::Native>,
+{
+    let mut b = Int32Builder::new(array.len());
+    match array.data_type() {
+        &DataType::Date32 | &DataType::Date64 | &DataType::Timestamp(_, None) 
=> {
+            extract_component_from_array!(array, b, ordinal, value_as_datetime)
+        }
+        &DataType::Timestamp(_, Some(ref tz)) => {
+            let mut scratch = Parsed::new();
+            extract_component_from_array!(
+                array,
+                b,
+                ordinal,
+                value_as_datetime_with_tz,
+                tz,
+                scratch
+            )
+        }
+        dt => return_compute_error_with!("doy does not support", dt),
+    }
+
+    Ok(b.finish())
+}
+
 /// Extracts the minutes of a given temporal array as an array of integers
 pub fn minute<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
 where
@@ -685,6 +714,26 @@ mod tests {
         assert_eq!(1, b.value(2));
     }
 
+    #[test]
+    fn test_temporal_array_date64_doy() {
+        //1483228800000 -> 2017-01-01 (Sunday)
+        //1514764800000 -> 2018-01-01
+        //1550636625000 -> 2019-02-20
+        let a: PrimitiveArray<Date64Type> = vec![
+            Some(1483228800000),
+            Some(1514764800000),
+            None,
+            Some(1550636625000),
+        ]
+        .into();
+
+        let b = doy(&a).unwrap();
+        assert_eq!(1, b.value(0));
+        assert_eq!(1, b.value(1));
+        assert!(!b.is_valid(2));
+        assert_eq!(51, b.value(3));
+    }
+
     #[test]
     fn test_temporal_array_timestamp_micro_year() {
         let a: TimestampMicrosecondArray =

Reply via email to