You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/03/03 18:25:34 UTC

[GitHub] [arrow-rs] alamb commented on a change in pull request #1388: Add extract month and day in temporal.rs

alamb commented on a change in pull request #1388:
URL: https://github.com/apache/arrow-rs/pull/1388#discussion_r818946091



##########
File path: arrow/src/compute/kernels/temporal.rs
##########
@@ -301,6 +357,90 @@ mod tests {
         assert_eq!(2012, b.value(2));
     }
 
+    #[test]
+    fn test_temporal_array_date64_month() {
+        //1514764800000 -> 2018-01-01
+        //1550636625000 -> 2019-02-20
+        let a: PrimitiveArray<Date64Type> =
+            vec![Some(1514764800000), None, Some(1550636625000)].into();
+
+        let b = month(&a).unwrap();
+        assert_eq!(1, b.value(0));
+        assert!(!b.is_valid(1));
+        assert_eq!(2, b.value(2));
+    }
+
+    #[test]
+    fn test_temporal_array_date32_month() {
+        let a: PrimitiveArray<Date32Type> = vec![Some(1), None, Some(31)].into();
+
+        let b = month(&a).unwrap();
+        assert_eq!(1, b.value(0));
+        assert!(!b.is_valid(1));
+        assert_eq!(2, b.value(2));
+    }
+
+    #[test]
+    fn test_temporal_array_timestamp_month_with_timezone() {
+        use std::sync::Arc;
+
+        // 24 * 60 * 60 = 8640
+        let a = Arc::new(TimestampSecondArray::from_vec(
+            vec![86400 * 31],
+            Some("+00:00".to_string()),
+        ));
+        let b = month(&a).unwrap();
+        assert_eq!(2, b.value(0));
+        let a = Arc::new(TimestampSecondArray::from_vec(
+            vec![86400 * 31],
+            Some("-10:00".to_string()),
+        ));
+        let b = month(&a).unwrap();
+        assert_eq!(1, b.value(0));
+    }
+
+    #[test]
+    fn test_temporal_array_timestamp_day_with_timezone() {
+        use std::sync::Arc;
+
+        // 24 * 60 * 60 = 8640
+        let a = Arc::new(TimestampSecondArray::from_vec(
+            vec![86400],
+            Some("+00:00".to_string()),
+        ));
+        let b = day(&a).unwrap();
+        assert_eq!(2, b.value(0));
+        let a = Arc::new(TimestampSecondArray::from_vec(
+            vec![86400],
+            Some("-10:00".to_string()),
+        ));
+        let b = day(&a).unwrap();
+        assert_eq!(1, b.value(0));

Review comment:
       👍 

##########
File path: arrow/src/compute/kernels/temporal.rs
##########
@@ -169,6 +169,62 @@ where
     Ok(b.finish())
 }
 
+/// Extracts the month of a given temporal array as an array of integers
+pub fn month<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, month, value_as_datetime)
+        }
+        &DataType::Timestamp(_, Some(ref tz)) => {

Review comment:
       👍 

##########
File path: arrow/src/compute/kernels/temporal.rs
##########
@@ -301,6 +357,90 @@ mod tests {
         assert_eq!(2012, b.value(2));
     }
 
+    #[test]
+    fn test_temporal_array_date64_month() {
+        //1514764800000 -> 2018-01-01
+        //1550636625000 -> 2019-02-20
+        let a: PrimitiveArray<Date64Type> =
+            vec![Some(1514764800000), None, Some(1550636625000)].into();
+
+        let b = month(&a).unwrap();
+        assert_eq!(1, b.value(0));
+        assert!(!b.is_valid(1));
+        assert_eq!(2, b.value(2));
+    }
+
+    #[test]
+    fn test_temporal_array_date32_month() {
+        let a: PrimitiveArray<Date32Type> = vec![Some(1), None, Some(31)].into();
+
+        let b = month(&a).unwrap();
+        assert_eq!(1, b.value(0));
+        assert!(!b.is_valid(1));
+        assert_eq!(2, b.value(2));
+    }
+
+    #[test]
+    fn test_temporal_array_timestamp_month_with_timezone() {
+        use std::sync::Arc;
+
+        // 24 * 60 * 60 = 8640
+        let a = Arc::new(TimestampSecondArray::from_vec(
+            vec![86400 * 31],
+            Some("+00:00".to_string()),
+        ));
+        let b = month(&a).unwrap();
+        assert_eq!(2, b.value(0));
+        let a = Arc::new(TimestampSecondArray::from_vec(
+            vec![86400 * 31],
+            Some("-10:00".to_string()),
+        ));
+        let b = month(&a).unwrap();
+        assert_eq!(1, b.value(0));

Review comment:
       👍 




-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org