Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
25 changes: 19 additions & 6 deletions datafusion/spark/src/function/datetime/date_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::sync::Arc;
use arrow::array::ArrayRef;
use arrow::compute;
use arrow::datatypes::{DataType, Date32Type};
use arrow::error::ArrowError;
use datafusion_common::cast::{
as_date32_array, as_int16_array, as_int32_array, as_int8_array,
};
Expand Down Expand Up @@ -96,26 +97,38 @@ fn spark_date_add(args: &[ArrayRef]) -> Result<ArrayRef> {
let result = match days_arg.data_type() {
DataType::Int8 => {
let days_array = as_int8_array(days_arg)?;
compute::binary::<_, _, _, Date32Type>(
compute::try_binary::<_, _, _, Date32Type>(
date_array,
days_array,
|date, days| date + days as i32,
|date, days| {
date.checked_add(days as i32).ok_or_else(|| {
ArrowError::ArithmeticOverflow("date_add".to_string())
})
},
)?
}
DataType::Int16 => {
let days_array = as_int16_array(days_arg)?;
compute::binary::<_, _, _, Date32Type>(
compute::try_binary::<_, _, _, Date32Type>(
date_array,
days_array,
|date, days| date + days as i32,
|date, days| {
date.checked_add(days as i32).ok_or_else(|| {
ArrowError::ArithmeticOverflow("date_add".to_string())
})
},
)?
}
DataType::Int32 => {
let days_array = as_int32_array(days_arg)?;
compute::binary::<_, _, _, Date32Type>(
compute::try_binary::<_, _, _, Date32Type>(
date_array,
days_array,
|date, days| date + days,
|date, days| {
date.checked_add(days).ok_or_else(|| {
ArrowError::ArithmeticOverflow("date_add".to_string())
})
},
)?
}
_ => {
Expand Down
25 changes: 19 additions & 6 deletions datafusion/spark/src/function/datetime/date_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::sync::Arc;
use arrow::array::ArrayRef;
use arrow::compute;
use arrow::datatypes::{DataType, Date32Type};
use arrow::error::ArrowError;
use datafusion_common::cast::{
as_date32_array, as_int16_array, as_int32_array, as_int8_array,
};
Expand Down Expand Up @@ -90,26 +91,38 @@ fn spark_date_sub(args: &[ArrayRef]) -> Result<ArrayRef> {
let result = match days_arg.data_type() {
DataType::Int8 => {
let days_array = as_int8_array(days_arg)?;
compute::binary::<_, _, _, Date32Type>(
compute::try_binary::<_, _, _, Date32Type>(
date_array,
days_array,
|date, days| date - days as i32,
|date, days| {
date.checked_sub(days as i32).ok_or_else(|| {
ArrowError::ArithmeticOverflow("date_sub".to_string())
})
},
)?
}
DataType::Int16 => {
let days_array = as_int16_array(days_arg)?;
compute::binary::<_, _, _, Date32Type>(
compute::try_binary::<_, _, _, Date32Type>(
date_array,
days_array,
|date, days| date - days as i32,
|date, days| {
date.checked_sub(days as i32).ok_or_else(|| {
ArrowError::ArithmeticOverflow("date_sub".to_string())
})
},
)?
}
DataType::Int32 => {
let days_array = as_int32_array(days_arg)?;
compute::binary::<_, _, _, Date32Type>(
compute::try_binary::<_, _, _, Date32Type>(
date_array,
days_array,
|date, days| date - days,
|date, days| {
date.checked_sub(days).ok_or_else(|| {
ArrowError::ArithmeticOverflow("date_sub".to_string())
})
},
)?
}
_ => {
Expand Down
18 changes: 18 additions & 0 deletions datafusion/sqllogictest/test_files/spark/datetime/date_add.slt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ SELECT date_sub('2016-07-30'::date, 0::int);
----
2016-07-30

query error DataFusion error: Arrow error: Arithmetic overflow:
SELECT date_add('2016-07-30'::date, 2147483647::int);

query D
SELECT date_sub('2016-07-30'::date, 2147483647::int);
----
ERROR: Cast error: Failed to convert -2147466635 to temporal for Date32

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ERROR: Cast error: Failed to convert -2147466635 to temporal for Date32 is it a result returned by date_sub 🤔 ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No. This error is unrelated to the changes in this PR, but I thought it was good to add the test.

I was expecting to see an overflow though. I will try and track down the root cause.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added a test to show negative overflow


query D
SELECT date_add('2016-07-30'::date, 100000::int);
----
2290-05-15

query D
SELECT date_sub('2016-07-30'::date, 100000::int);
----
1742-10-15

# Test with negative day values (should subtract days)
query D
SELECT date_add('2016-07-30'::date, -5::int);
Expand Down