Skip to content
Open
Changes from all 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
73 changes: 63 additions & 10 deletions arrow-cast/src/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1710,17 +1710,33 @@ pub fn cast_with_options(
.as_primitive::<Date32Type>()
.unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
)),
(Date64, Date32) => Ok(Arc::new(
array
.as_primitive::<Date64Type>()
.unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
)),
(Date64, Date32) => {
let array = array.as_primitive::<Date64Type>();
let result = if cast_options.safe {
array.unary_opt::<_, Date32Type>(|x| i32::try_from(x / MILLISECONDS_IN_DAY).ok())
} else {
array.try_unary::<_, Date32Type, _>(|x| {
i32::try_from(x / MILLISECONDS_IN_DAY).map_err(|_| {
ArrowError::CastError(format!(
"Cannot cast Date64 value {x} to Date32 without overflow"
))
})
})?
};
Ok(Arc::new(result))
}

(Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
array
.as_primitive::<Time32SecondType>()
.unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
)),
(Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => {
let array = array.as_primitive::<Time32SecondType>();
let result = if cast_options.safe {
array.unary_opt::<_, Time32MillisecondType>(|x| x.checked_mul(MILLISECONDS as i32))
} else {
array.try_unary::<_, Time32MillisecondType, _>(|x| {
x.mul_checked(MILLISECONDS as i32)
})?
};
Ok(Arc::new(result))
}
(Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
array
.as_primitive::<Time32SecondType>()
Expand Down Expand Up @@ -5239,6 +5255,26 @@ mod tests {
assert!(c.is_null(2));
}

#[test]
fn test_cast_date64_to_date32_overflow() {
let a = Date64Array::from(vec![i64::MAX]);
let array = Arc::new(a) as ArrayRef;

let b = cast(&array, &DataType::Date32).unwrap();
let c = b.as_primitive::<Date32Type>();
assert!(c.is_null(0));

let options = CastOptions {
safe: false,
..Default::default()
};
let err = cast_with_options(&array, &DataType::Date32, &options).unwrap_err();
assert!(
err.to_string().contains("Cannot cast Date64 value"),
"{err}"
);
}

#[test]
fn test_cast_string_to_integral_overflow() {
let str = Arc::new(StringArray::from(vec![
Expand Down Expand Up @@ -13841,6 +13877,23 @@ mod tests {
assert_eq!(c.value(3), 43_200_000_000);
}

#[test]
fn test_cast_time32_second_to_time32_millisecond_overflow() {
let array = Time32SecondArray::from(vec![i32::MAX]);

let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
let c = b.as_primitive::<Time32MillisecondType>();
assert!(c.is_null(0));

let options = CastOptions {
safe: false,
..Default::default()
};
let err = cast_with_options(&array, &DataType::Time32(TimeUnit::Millisecond), &options)
.unwrap_err();
assert!(err.to_string().contains("Overflow"), "{err}");
}

#[test]
fn test_cast_string_to_time32_second_to_int64() {
// Mimic: select arrow_cast('03:12:44'::time, 'Time32(Second)')::bigint;
Expand Down
Loading