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

Add support for adding intervals to dates #2031

Merged
merged 10 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion arrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ csv_crate = { version = "1.1", default-features = false, optional = true, packag
regex = { version = "1.5.6", default-features = false, features = ["std", "unicode"] }
lazy_static = { version = "1.4", default-features = false }
packed_simd = { version = "0.3", default-features = false, optional = true, package = "packed_simd_2" }
chrono = { version = "0.4", default-features = false, features = ["clock"] }
chrono = { version = "0.4", default-features = false, features = ["std", "clock"] }
avantgardnerio marked this conversation as resolved.
Show resolved Hide resolved
chrono-tz = {version = "0.6", default-features = false, optional = true}
chronoutil = "0.2.3"
avantgardnerio marked this conversation as resolved.
Show resolved Hide resolved
flatbuffers = { version = "2.1.2", default-features = false, features = ["thiserror"], optional = true }
hex = { version = "0.4", default-features = false, features = ["std"] }
comfy-table = { version = "6.0", optional = true, default-features = false }
Expand Down
216 changes: 207 additions & 9 deletions arrow/src/compute/kernels/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ use crate::compute::kernels::arity::unary;
use crate::compute::unary_dyn;
use crate::compute::util::combine_option_bitmap;
use crate::datatypes;
use crate::datatypes::{ArrowNumericType, DataType};
use crate::datatypes::{
ArrowNumericType, DataType, Date32Type, Date64Type, IntervalDayTimeType,
IntervalMonthDayNanoType, IntervalUnit, IntervalYearMonthType,
};
use crate::datatypes::{
Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type,
UInt32Type, UInt64Type, UInt8Type,
Expand All @@ -55,14 +58,15 @@ use std::sync::Arc;
/// # Errors
///
/// This function errors if the arrays have different lengths
pub fn math_op<T, F>(
left: &PrimitiveArray<T>,
right: &PrimitiveArray<T>,
pub fn math_op<LT, RT, F>(
avantgardnerio marked this conversation as resolved.
Show resolved Hide resolved
left: &PrimitiveArray<LT>,
right: &PrimitiveArray<RT>,
op: F,
) -> Result<PrimitiveArray<T>>
) -> Result<PrimitiveArray<LT>>
where
T: ArrowNumericType,
F: Fn(T::Native, T::Native) -> T::Native,
LT: ArrowNumericType,
RT: ArrowNumericType,
F: Fn(LT::Native, RT::Native) -> LT::Native,
{
if left.len() != right.len() {
return Err(ArrowError::ComputeError(
Expand All @@ -87,7 +91,7 @@ where

let data = unsafe {
ArrayData::new_unchecked(
T::DATA_TYPE,
LT::DATA_TYPE,
left.len(),
None,
null_bit_buffer,
Expand All @@ -96,7 +100,7 @@ where
vec![],
)
};
Ok(PrimitiveArray::<T>::from(data))
Ok(PrimitiveArray::<LT>::from(data))
}

/// Helper function for operations where a valid `0` on the right array should
Expand Down Expand Up @@ -774,6 +778,110 @@ pub fn add_dyn(left: &dyn Array, right: &dyn Array) -> Result<ArrayRef> {
DataType::Dictionary(_, _) => {
typed_dict_math_op!(left, right, |a, b| a + b, math_op_dict)
}
DataType::Date32 => {
let l = left
.as_any()
.downcast_ref::<PrimitiveArray<Date32Type>>()
.ok_or_else(|| {
ArrowError::CastError(format!(
"Left array cannot be cast to Date32Type",
))
})?;
avantgardnerio marked this conversation as resolved.
Show resolved Hide resolved
match right.data_type() {
DataType::Interval(IntervalUnit::YearMonth) => {
let r = right
.as_any()
.downcast_ref::<PrimitiveArray<IntervalYearMonthType>>()
.ok_or_else(|| {
ArrowError::CastError(format!(
"Right array cannot be cast to IntervalYearMonthType",
))
})?;
let res = math_op(l, r, |a, b| Date32Type::add_year_months(a, b))?;
return Ok(Arc::new(res));
}
DataType::Interval(IntervalUnit::DayTime) => {
let r = right
.as_any()
.downcast_ref::<PrimitiveArray<IntervalDayTimeType>>()
.ok_or_else(|| {
ArrowError::CastError(format!(
"Right array cannot be cast to IntervalDayTimeType",
))
})?;
let res = math_op(l, r, |a, b| Date32Type::add_day_time(a, b))?;
return Ok(Arc::new(res));
}
DataType::Interval(IntervalUnit::MonthDayNano) => {
let r = right
.as_any()
.downcast_ref::<PrimitiveArray<IntervalMonthDayNanoType>>()
.ok_or_else(|| {
ArrowError::CastError(format!(
"Right array cannot be cast to IntervalMonthDayNanoType",
))
})?;
let res = math_op(l, r, |a, b| Date32Type::add_month_day_nano(a, b))?;
return Ok(Arc::new(res));
}
t => Err(ArrowError::CastError(format!(
"Cannot perform arithmetic operation on arrays of type {}",
t
))),
avantgardnerio marked this conversation as resolved.
Show resolved Hide resolved
}
}
DataType::Date64 => {
let l = left
.as_any()
.downcast_ref::<PrimitiveArray<Date64Type>>()
.ok_or_else(|| {
ArrowError::CastError(format!(
"Left array cannot be cast to Date64Type",
))
})?;
match right.data_type() {
DataType::Interval(IntervalUnit::YearMonth) => {
let r = right
.as_any()
.downcast_ref::<PrimitiveArray<IntervalYearMonthType>>()
.ok_or_else(|| {
ArrowError::CastError(format!(
"Right array cannot be cast to IntervalYearMonthType",
))
})?;
let res = math_op(l, r, |a, b| Date64Type::add_year_months(a, b))?;
return Ok(Arc::new(res));
}
DataType::Interval(IntervalUnit::DayTime) => {
let r = right
.as_any()
.downcast_ref::<PrimitiveArray<IntervalDayTimeType>>()
.ok_or_else(|| {
ArrowError::CastError(format!(
"Right array cannot be cast to IntervalDayTimeType",
))
})?;
let res = math_op(l, r, |a, b| Date64Type::add_day_time(a, b))?;
return Ok(Arc::new(res));
}
DataType::Interval(IntervalUnit::MonthDayNano) => {
let r = right
.as_any()
.downcast_ref::<PrimitiveArray<IntervalMonthDayNanoType>>()
.ok_or_else(|| {
ArrowError::CastError(format!(
"Right array cannot be cast to IntervalMonthDayNanoType",
))
})?;
let res = math_op(l, r, |a, b| Date64Type::add_month_day_nano(a, b))?;
return Ok(Arc::new(res));
}
t => Err(ArrowError::CastError(format!(
"Cannot perform arithmetic operation on arrays of type {}",
t
))),
avantgardnerio marked this conversation as resolved.
Show resolved Hide resolved
}
}
_ => typed_math_op!(left, right, |a, b| a + b, math_op),
}
}
Expand Down Expand Up @@ -1055,6 +1163,8 @@ where
mod tests {
use super::*;
use crate::array::Int32Array;
use crate::datatypes::Date64Type;
use chrono::NaiveDate;

#[test]
fn test_primitive_array_add() {
Expand All @@ -1068,6 +1178,94 @@ mod tests {
assert_eq!(17, c.value(4));
}

#[test]
fn test_date32_month_add() {
let a = Date32Array::from(vec![Date32Type::from_naive_date(
NaiveDate::from_ymd(2000, 01, 01),
)]);
let b = IntervalYearMonthArray::from(vec![IntervalYearMonthType::from(1, 2)]);
let c = add_dyn(&a, &b).unwrap();
let c = c.as_any().downcast_ref::<Date32Array>().unwrap();
assert_eq!(
c.value(0),
Date32Type::from_naive_date(NaiveDate::from_ymd(2001, 03, 01))
);
}

#[test]
fn test_date32_day_time_add() {
let a = Date32Array::from(vec![Date32Type::from_naive_date(
NaiveDate::from_ymd(2000, 01, 01),
)]);
let b = IntervalDayTimeArray::from(vec![IntervalDayTimeType::from(1, 2)]);
let c = add_dyn(&a, &b).unwrap();
let c = c.as_any().downcast_ref::<Date32Array>().unwrap();
assert_eq!(
c.value(0),
Date32Type::from_naive_date(NaiveDate::from_ymd(2000, 01, 02))
);
}

#[test]
fn test_date32_month_day_nano_add() {
let a = Date32Array::from(vec![Date32Type::from_naive_date(
NaiveDate::from_ymd(2000, 01, 01),
)]);
let b = IntervalMonthDayNanoArray::from(vec![IntervalMonthDayNanoType::from(
1, 2, 3,
)]);
let c = add_dyn(&a, &b).unwrap();
let c = c.as_any().downcast_ref::<Date32Array>().unwrap();
assert_eq!(
c.value(0),
Date32Type::from_naive_date(NaiveDate::from_ymd(2000, 02, 03))
);
}

#[test]
fn test_date64_month_add() {
let a = Date64Array::from(vec![Date64Type::from_naive_date(
NaiveDate::from_ymd(2000, 01, 01),
)]);
let b = IntervalYearMonthArray::from(vec![IntervalYearMonthType::from(1, 2)]);
let c = add_dyn(&a, &b).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

it is so great to see add_dyn used like this ❤️

let c = c.as_any().downcast_ref::<Date64Array>().unwrap();
assert_eq!(
c.value(0),
Date64Type::from_naive_date(NaiveDate::from_ymd(2001, 03, 01))
);
}

#[test]
fn test_date64_day_time_add() {
let a = Date64Array::from(vec![Date64Type::from_naive_date(
NaiveDate::from_ymd(2000, 01, 01),
)]);
let b = IntervalDayTimeArray::from(vec![IntervalDayTimeType::from(1, 2)]);
let c = add_dyn(&a, &b).unwrap();
let c = c.as_any().downcast_ref::<Date64Array>().unwrap();
assert_eq!(
c.value(0),
Date64Type::from_naive_date(NaiveDate::from_ymd(2000, 01, 02))
);
}

#[test]
fn test_date64_month_day_nano_add() {
let a = Date64Array::from(vec![Date64Type::from_naive_date(
NaiveDate::from_ymd(2000, 01, 01),
)]);
let b = IntervalMonthDayNanoArray::from(vec![IntervalMonthDayNanoType::from(
1, 2, 3,
)]);
let c = add_dyn(&a, &b).unwrap();
let c = c.as_any().downcast_ref::<Date64Array>().unwrap();
assert_eq!(
c.value(0),
Date64Type::from_naive_date(NaiveDate::from_ymd(2000, 02, 03))
);
}

#[test]
fn test_primitive_array_add_dyn() {
let a = Int32Array::from(vec![Some(5), Some(6), Some(7), Some(8), Some(9)]);
Expand Down
Loading