You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by al...@apache.org on 2021/06/24 14:48:27 UTC

[arrow-rs] branch active_release updated: implement second/minute helpers for temporal (#493) (#498)

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

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


The following commit(s) were added to refs/heads/active_release by this push:
     new 43b4f72  implement second/minute helpers for temporal (#493) (#498)
43b4f72 is described below

commit 43b4f721fc31c7c6b700aea8bf4de8800a7f49bc
Author: Andrew Lamb <an...@nerdnetworks.org>
AuthorDate: Thu Jun 24 10:48:21 2021 -0400

    implement second/minute helpers for temporal (#493) (#498)
    
    * implement second/minute helpers for temporal
    
    * Update arrow/src/compute/kernels/temporal.rs
    
    Co-authored-by: Daniël Heres <da...@gmail.com>
    
    * Update arrow/src/compute/kernels/temporal.rs
    
    Co-authored-by: Daniël Heres <da...@gmail.com>
    
    Co-authored-by: Daniël Heres <da...@gmail.com>
    
    Co-authored-by: Dmitry Patsura <za...@gmail.com>
    Co-authored-by: Daniël Heres <da...@gmail.com>
---
 arrow/src/compute/kernels/temporal.rs | 110 ++++++++++++++++++++++++++++++++++
 1 file changed, 110 insertions(+)

diff --git a/arrow/src/compute/kernels/temporal.rs b/arrow/src/compute/kernels/temporal.rs
index 18a267f..5db0dfb 100644
--- a/arrow/src/compute/kernels/temporal.rs
+++ b/arrow/src/compute/kernels/temporal.rs
@@ -100,6 +100,72 @@ where
     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
+    T: ArrowTemporalType + ArrowNumericType,
+    i64: std::convert::From<T::Native>,
+{
+    let mut b = Int32Builder::new(array.len());
+    match array.data_type() {
+        &DataType::Date64 | &DataType::Timestamp(_, _) => {
+            for i in 0..array.len() {
+                if array.is_null(i) {
+                    b.append_null()?;
+                } else {
+                    match array.value_as_datetime(i) {
+                        Some(dt) => b.append_value(dt.minute() as i32)?,
+                        None => b.append_null()?,
+                    }
+                }
+            }
+        }
+        dt => {
+            return {
+                Err(ArrowError::ComputeError(format!(
+                    "minute does not support type {:?}",
+                    dt
+                )))
+            }
+        }
+    }
+
+    Ok(b.finish())
+}
+
+/// Extracts the seconds of a given temporal array as an array of integers
+pub fn second<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::Date64 | &DataType::Timestamp(_, _) => {
+            for i in 0..array.len() {
+                if array.is_null(i) {
+                    b.append_null()?;
+                } else {
+                    match array.value_as_datetime(i) {
+                        Some(dt) => b.append_value(dt.second() as i32)?,
+                        None => b.append_null()?,
+                    }
+                }
+            }
+        }
+        dt => {
+            return {
+                Err(ArrowError::ComputeError(format!(
+                    "second does not support type {:?}",
+                    dt
+                )))
+            }
+        }
+    }
+
+    Ok(b.finish())
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
@@ -184,4 +250,48 @@ mod tests {
         assert!(!b.is_valid(1));
         assert_eq!(2024, b.value(2));
     }
+
+    #[test]
+    fn test_temporal_array_date64_minute() {
+        let a: PrimitiveArray<Date64Type> =
+            vec![Some(1514764800000), None, Some(1550636625000)].into();
+
+        let b = minute(&a).unwrap();
+        assert_eq!(0, b.value(0));
+        assert!(!b.is_valid(1));
+        assert_eq!(23, b.value(2));
+    }
+
+    #[test]
+    fn test_temporal_array_timestamp_micro_minute() {
+        let a: TimestampMicrosecondArray =
+            vec![Some(1612025847000000), None, Some(1722015847000000)].into();
+
+        let b = minute(&a).unwrap();
+        assert_eq!(57, b.value(0));
+        assert!(!b.is_valid(1));
+        assert_eq!(44, b.value(2));
+    }
+
+    #[test]
+    fn test_temporal_array_date64_second() {
+        let a: PrimitiveArray<Date64Type> =
+            vec![Some(1514764800000), None, Some(1550636625000)].into();
+
+        let b = second(&a).unwrap();
+        assert_eq!(0, b.value(0));
+        assert!(!b.is_valid(1));
+        assert_eq!(45, b.value(2));
+    }
+
+    #[test]
+    fn test_temporal_array_timestamp_micro_second() {
+        let a: TimestampMicrosecondArray =
+            vec![Some(1612025847000000), None, Some(1722015847000000)].into();
+
+        let b = second(&a).unwrap();
+        assert_eq!(27, b.value(0));
+        assert!(!b.is_valid(1));
+        assert_eq!(7, b.value(2));
+    }
 }