This is an automated email from the ASF dual-hosted git repository.
alamb 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 fc4044f35 Feature add weekday temporal kernel (#1891)
fc4044f35 is described below
commit fc4044f35d4aa67e706c6d3f61a9f24bab5346be
Author: Remco Verhoef <[email protected]>
AuthorDate: Fri Jun 17 12:52:16 2022 +0200
Feature add weekday temporal kernel (#1891)
* add weekday temporal kernel
* add test
* fmt
* fix test
* use correct weekday numbers
---
arrow/src/compute/kernels/temporal.rs | 41 +++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/arrow/src/compute/kernels/temporal.rs
b/arrow/src/compute/kernels/temporal.rs
index 2482b7b75..f731ff5ce 100644
--- a/arrow/src/compute/kernels/temporal.rs
+++ b/arrow/src/compute/kernels/temporal.rs
@@ -267,6 +267,34 @@ where
Ok(b.finish())
}
+/// Extracts the day of week of a given temporal array as an array of integers
+pub fn weekday<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, weekday, value_as_datetime)
+ }
+ &DataType::Timestamp(_, Some(ref tz)) => {
+ let mut scratch = Parsed::new();
+ extract_component_from_array!(
+ array,
+ b,
+ weekday,
+ value_as_datetime_with_tz,
+ tz,
+ scratch
+ )
+ }
+ dt => return_compute_error_with!("weekday does not support", dt),
+ }
+
+ Ok(b.finish())
+}
+
/// Extracts the day of a given temporal array as an array of integers
pub fn day<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
where
@@ -548,6 +576,19 @@ mod tests {
assert_eq!(1, b.value(0));
}
+ #[test]
+ fn test_temporal_array_date64_weekday() {
+ //1514764800000 -> 2018-01-01 (Monday)
+ //1550636625000 -> 2019-02-20 (Wednesday)
+ let a: PrimitiveArray<Date64Type> =
+ vec![Some(1514764800000), None, Some(1550636625000)].into();
+
+ let b = weekday(&a).unwrap();
+ assert_eq!(0, b.value(0));
+ assert!(!b.is_valid(1));
+ assert_eq!(2, b.value(2));
+ }
+
#[test]
fn test_temporal_array_date64_day() {
//1514764800000 -> 2018-01-01