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

Arrow Arithmetic: Subtract timestamps #4244

Merged
merged 3 commits into from
May 19, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 224 additions & 2 deletions arrow-arith/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,13 +1096,18 @@ pub fn subtract_dyn(left: &dyn Array, right: &dyn Array) -> Result<ArrayRef, Arr
let res = math_checked_op(l, r, TimestampSecondType::subtract_month_day_nano)?;
Ok(Arc::new(res.with_timezone_opt(l.timezone())))
}
DataType::Timestamp(TimeUnit::Second, _) => {
let r = right.as_primitive::<TimestampSecondType>();
let op = <TimestampSecondType as ArrowPrimitiveType>::Native::wrapping_sub;
let res: PrimitiveArray<DurationSecondType> = binary(l, r, op)?;
mr-brobot marked this conversation as resolved.
Show resolved Hide resolved
Ok(Arc::new(res))
}
_ => Err(ArrowError::CastError(format!(
"Cannot perform arithmetic operation between array of type {} and array of type {}",
left.data_type(), right.data_type()
))),
}
}

DataType::Timestamp(TimeUnit::Microsecond, _) => {
let l = left.as_primitive::<TimestampMicrosecondType>();
match right.data_type() {
Expand All @@ -1121,6 +1126,12 @@ pub fn subtract_dyn(left: &dyn Array, right: &dyn Array) -> Result<ArrayRef, Arr
let res = math_checked_op(l, r, TimestampMicrosecondType::subtract_month_day_nano)?;
Ok(Arc::new(res.with_timezone_opt(l.timezone())))
}
DataType::Timestamp(TimeUnit::Microsecond, _) => {
let r = right.as_primitive::<TimestampMicrosecondType>();
let op = <TimestampMicrosecondType as ArrowPrimitiveType>::Native::wrapping_sub;
let res: PrimitiveArray<DurationMicrosecondType> = binary(l, r, op)?;
Ok(Arc::new(res))
}
_ => Err(ArrowError::CastError(format!(
"Cannot perform arithmetic operation between array of type {} and array of type {}",
left.data_type(), right.data_type()
Expand All @@ -1145,13 +1156,18 @@ pub fn subtract_dyn(left: &dyn Array, right: &dyn Array) -> Result<ArrayRef, Arr
let res = math_checked_op(l, r, TimestampMillisecondType::subtract_month_day_nano)?;
Ok(Arc::new(res.with_timezone_opt(l.timezone())))
}
DataType::Timestamp(TimeUnit::Millisecond, _) => {
let r = right.as_primitive::<TimestampMillisecondType>();
let op = <TimestampMillisecondType as ArrowPrimitiveType>::Native::wrapping_sub;
let res: PrimitiveArray<DurationMillisecondType> = binary(l, r, op)?;
Ok(Arc::new(res))
}
_ => Err(ArrowError::CastError(format!(
"Cannot perform arithmetic operation between array of type {} and array of type {}",
left.data_type(), right.data_type()
))),
}
}

DataType::Timestamp(TimeUnit::Nanosecond, _) => {
let l = left.as_primitive::<TimestampNanosecondType>();
match right.data_type() {
Expand All @@ -1170,6 +1186,12 @@ pub fn subtract_dyn(left: &dyn Array, right: &dyn Array) -> Result<ArrayRef, Arr
let res = math_checked_op(l, r, TimestampNanosecondType::subtract_month_day_nano)?;
Ok(Arc::new(res.with_timezone_opt(l.timezone())))
}
DataType::Timestamp(TimeUnit::Nanosecond, _) => {
let r = right.as_primitive::<TimestampNanosecondType>();
let op = <TimestampNanosecondType as ArrowPrimitiveType>::Native::wrapping_sub;
let res: PrimitiveArray<DurationNanosecondType> = binary(l, r, op)?;
Ok(Arc::new(res))
}
_ => Err(ArrowError::CastError(format!(
"Cannot perform arithmetic operation between array of type {} and array of type {}",
left.data_type(), right.data_type()
Expand Down Expand Up @@ -1256,6 +1278,66 @@ pub fn subtract_dyn_checked(
))),
}
}
DataType::Timestamp(TimeUnit::Second, _) => {
let l = left.as_primitive::<TimestampSecondType>();
match right.data_type() {
DataType::Timestamp(TimeUnit::Second, _) => {
let r = right.as_primitive::<TimestampSecondType>();
let op = <TimestampSecondType as ArrowPrimitiveType>::Native::sub_checked;
let res: PrimitiveArray<DurationSecondType> = try_binary(l, r, op)?;
Ok(Arc::new(res))
}
_ => Err(ArrowError::CastError(format!(
"Cannot perform arithmetic operation between array of type {} and array of type {}",
left.data_type(), right.data_type()
))),
}
}
DataType::Timestamp(TimeUnit::Microsecond, _) => {
let l = left.as_primitive::<TimestampMicrosecondType>();
match right.data_type() {
DataType::Timestamp(TimeUnit::Microsecond, _) => {
let r = right.as_primitive::<TimestampMicrosecondType>();
let op = <TimestampMicrosecondType as ArrowPrimitiveType>::Native::sub_checked;
let res: PrimitiveArray<DurationMicrosecondType> = try_binary(l, r, op)?;
Ok(Arc::new(res))
}
_ => Err(ArrowError::CastError(format!(
"Cannot perform arithmetic operation between array of type {} and array of type {}",
left.data_type(), right.data_type()
))),
}
}
DataType::Timestamp(TimeUnit::Millisecond, _) => {
let l = left.as_primitive::<TimestampMillisecondType>();
match right.data_type() {
DataType::Timestamp(TimeUnit::Millisecond, _) => {
let r = right.as_primitive::<TimestampMillisecondType>();
let op = <TimestampMillisecondType as ArrowPrimitiveType>::Native::sub_checked;
let res: PrimitiveArray<DurationMillisecondType> = try_binary(l, r, op)?;
Ok(Arc::new(res))
}
_ => Err(ArrowError::CastError(format!(
"Cannot perform arithmetic operation between array of type {} and array of type {}",
left.data_type(), right.data_type()
))),
}
}
DataType::Timestamp(TimeUnit::Nanosecond, _) => {
let l = left.as_primitive::<TimestampNanosecondType>();
match right.data_type() {
DataType::Timestamp(TimeUnit::Nanosecond, _) => {
let r = right.as_primitive::<TimestampNanosecondType>();
let op = <TimestampNanosecondType as ArrowPrimitiveType>::Native::sub_checked;
let res: PrimitiveArray<DurationNanosecondType> = try_binary(l, r, op)?;
Ok(Arc::new(res))
}
_ => Err(ArrowError::CastError(format!(
"Cannot perform arithmetic operation between array of type {} and array of type {}",
left.data_type(), right.data_type()
))),
}
}
_ => {
downcast_primitive_array!(
(left, right) => {
Expand Down Expand Up @@ -4649,4 +4731,144 @@ mod tests {
]);
assert_eq!(&expected, result);
}

#[test]
fn test_timestamp_second_subtract_timestamp() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we could extract a generic function typed on the timestamp and duration types, this would avoid the duplication?

let a = TimestampSecondArray::from(vec![0, 2, 4, 6, 8]);
let b = TimestampSecondArray::from(vec![1, 2, 3, 4, 5]);
let expected = DurationSecondArray::from(vec![-1, 0, 1, 2, 3]);

// unchecked
let result = subtract_dyn(&a, &b).unwrap();
let result = result.as_primitive::<DurationSecondType>();
assert_eq!(&expected, result);

// checked
let result = subtract_dyn_checked(&a, &b).unwrap();
let result = result.as_primitive::<DurationSecondType>();
assert_eq!(&expected, result);
}

#[test]
fn test_timestamp_second_subtract_timestamp_overflow() {
let a = TimestampSecondArray::from(vec![
<TimestampSecondType as ArrowPrimitiveType>::Native::MAX,
]);
let b = TimestampSecondArray::from(vec![
<TimestampSecondType as ArrowPrimitiveType>::Native::MIN,
]);

// unchecked
let result = subtract_dyn(&a, &b);
assert!(!&result.is_err());

// checked
let result = subtract_dyn_checked(&a, &b);
assert!(&result.is_err());
}

#[test]
fn test_timestamp_microsecond_subtract_timestamp() {
let a = TimestampMicrosecondArray::from(vec![0, 2, 4, 6, 8]);
let b = TimestampMicrosecondArray::from(vec![1, 2, 3, 4, 5]);
let expected = DurationMicrosecondArray::from(vec![-1, 0, 1, 2, 3]);

// unchecked
let result = subtract_dyn(&a, &b).unwrap();
let result = result.as_primitive::<DurationMicrosecondType>();
assert_eq!(&expected, result);

// checked
let result = subtract_dyn_checked(&a, &b).unwrap();
let result = result.as_primitive::<DurationMicrosecondType>();
assert_eq!(&expected, result);
}

#[test]
fn test_timestamp_microsecond_subtract_timestamp_overflow() {
let a = TimestampMicrosecondArray::from(vec![
<TimestampMicrosecondType as ArrowPrimitiveType>::Native::MAX,
]);
let b = TimestampMicrosecondArray::from(vec![
<TimestampMicrosecondType as ArrowPrimitiveType>::Native::MIN,
]);

// unchecked
let result = subtract_dyn(&a, &b);
assert!(!&result.is_err());

// checked
let result = subtract_dyn_checked(&a, &b);
assert!(&result.is_err());
}

#[test]
fn test_timestamp_millisecond_subtract_timestamp() {
let a = TimestampMillisecondArray::from(vec![0, 2, 4, 6, 8]);
let b = TimestampMillisecondArray::from(vec![1, 2, 3, 4, 5]);
let expected = DurationMillisecondArray::from(vec![-1, 0, 1, 2, 3]);

// unchecked
let result = subtract_dyn(&a, &b).unwrap();
let result = result.as_primitive::<DurationMillisecondType>();
assert_eq!(&expected, result);

// checked
let result = subtract_dyn_checked(&a, &b).unwrap();
let result = result.as_primitive::<DurationMillisecondType>();
assert_eq!(&expected, result);
}

#[test]
fn test_timestamp_millisecond_subtract_timestamp_overflow() {
let a = TimestampMillisecondArray::from(vec![
<TimestampMillisecondType as ArrowPrimitiveType>::Native::MAX,
]);
let b = TimestampMillisecondArray::from(vec![
<TimestampMillisecondType as ArrowPrimitiveType>::Native::MIN,
]);

// unchecked
let result = subtract_dyn(&a, &b);
assert!(!&result.is_err());

// checked
let result = subtract_dyn_checked(&a, &b);
assert!(&result.is_err());
}

#[test]
fn test_timestamp_nanosecond_subtract_timestamp() {
let a = TimestampNanosecondArray::from(vec![0, 2, 4, 6, 8]);
let b = TimestampNanosecondArray::from(vec![1, 2, 3, 4, 5]);
let expected = DurationNanosecondArray::from(vec![-1, 0, 1, 2, 3]);

// unchecked
let result = subtract_dyn(&a, &b).unwrap();
let result = result.as_primitive::<DurationNanosecondType>();
assert_eq!(&expected, result);

// checked
let result = subtract_dyn_checked(&a, &b).unwrap();
let result = result.as_primitive::<DurationNanosecondType>();
assert_eq!(&expected, result);
}

#[test]
fn test_timestamp_nanosecond_subtract_timestamp_overflow() {
let a = TimestampNanosecondArray::from(vec![
<TimestampNanosecondType as ArrowPrimitiveType>::Native::MAX,
]);
let b = TimestampNanosecondArray::from(vec![
<TimestampNanosecondType as ArrowPrimitiveType>::Native::MIN,
]);

// unchecked
let result = subtract_dyn(&a, &b);
assert!(!&result.is_err());

// checked
let result = subtract_dyn_checked(&a, &b);
assert!(&result.is_err());
}
}