Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support extract day and month in temporal.rs #1388

Merged
merged 5 commits into from
Mar 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
140 changes: 140 additions & 0 deletions arrow/src/compute/kernels/temporal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the timezone will impact hour, day, month, year and other time unit which are higher than hour.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example:
2020-01-01 07:00:00 UTC0
The format at UTC-8 is 2019-12-31 23:00:00 and the year, month, day, hour are all changed.
@Ted-Jiang

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already add timezone support

}
&DataType::Timestamp(_, Some(ref tz)) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

let mut scratch = Parsed::new();
extract_component_from_array!(
array,
b,
month,
value_as_datetime_with_tz,
tz,
scratch
)
}
dt => return_compute_error_with!("month 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
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, day, value_as_datetime)
}
&DataType::Timestamp(_, Some(ref tz)) => {
let mut scratch = Parsed::new();
extract_component_from_array!(
array,
b,
day,
value_as_datetime_with_tz,
tz,
scratch
)
}
dt => return_compute_error_with!("day 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
Expand Down Expand Up @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

#[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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

#[test]
fn test_temporal_array_date64_day() {
//1514764800000 -> 2018-01-01
//1550636625000 -> 2019-02-20
let a: PrimitiveArray<Date64Type> =
vec![Some(1514764800000), None, Some(1550636625000)].into();

let b = day(&a).unwrap();
assert_eq!(1, b.value(0));
assert!(!b.is_valid(1));
assert_eq!(20, b.value(2));
}

#[test]
fn test_temporal_array_date32_day() {
let a: PrimitiveArray<Date32Type> = vec![Some(0), None, Some(31)].into();

let b = day(&a).unwrap();
assert_eq!(1, b.value(0));
assert!(!b.is_valid(1));
assert_eq!(1, b.value(2));
}

#[test]
fn test_temporal_array_timestamp_micro_year() {
let a: TimestampMicrosecondArray =
Expand Down