Skip to content
Merged
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
77 changes: 67 additions & 10 deletions arrow-cast/src/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2137,18 +2137,30 @@ pub fn cast_with_options(
cast_with_options(&array, to_type, cast_options)
}
(Date32, Timestamp(TimeUnit::Microsecond, _)) => {
let array = array
.as_primitive::<Date32Type>()
.unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);

cast_with_options(&array, to_type, cast_options)
let date_array = array.as_primitive::<Date32Type>();
let converted = if cast_options.safe {
date_array.unary_opt::<_, TimestampMicrosecondType>(|x| {
(x as i64).checked_mul(MICROSECONDS_IN_DAY)
})
} else {
date_array.try_unary::<_, TimestampMicrosecondType, _>(|x| {
(x as i64).mul_checked(MICROSECONDS_IN_DAY)
})?
};
cast_with_options(&converted, to_type, cast_options)
}
(Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
let array = array
.as_primitive::<Date32Type>()
.unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);

cast_with_options(&array, to_type, cast_options)
let date_array = array.as_primitive::<Date32Type>();
let converted = if cast_options.safe {
date_array.unary_opt::<_, TimestampNanosecondType>(|x| {
(x as i64).checked_mul(NANOSECONDS_IN_DAY)
})
} else {
date_array.try_unary::<_, TimestampNanosecondType, _>(|x| {
(x as i64).mul_checked(NANOSECONDS_IN_DAY)
})?
};
cast_with_options(&converted, to_type, cast_options)
}

(_, Duration(unit)) if from_type.is_numeric() => {
Expand Down Expand Up @@ -11412,6 +11424,51 @@ mod tests {
assert!(c.is_null(2));
}

#[test]
fn test_cast_date32_to_timestamp_us_overflow() {
const MAX_DAYS_MICROS: i32 = (i64::MAX / MICROSECONDS_IN_DAY) as i32;
let a = Date32Array::from(vec![Some(MAX_DAYS_MICROS), Some(MAX_DAYS_MICROS + 1), None]);
let array = Arc::new(a) as ArrayRef;
let err = cast_with_options(
&array,
&DataType::Timestamp(TimeUnit::Microsecond, None),
&CastOptions {
safe: false,
format_options: FormatOptions::default(),
},
);
assert!(err.is_err());

let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
let c = b.as_primitive::<TimestampMicrosecondType>();
assert_eq!(MAX_DAYS_MICROS as i64 * MICROSECONDS_IN_DAY, c.value(0));
assert!(c.is_null(1));
assert!(c.is_null(2));
}

#[test]
fn test_cast_date32_to_timestamp_ns_overflow() {
// 2262-04-11, 2062-04-12
let upper_limit = 106_751;
let a = Date32Array::from(vec![Some(upper_limit), Some(upper_limit + 1), None]);
let array = Arc::new(a) as ArrayRef;
let err = cast_with_options(
&array,
&DataType::Timestamp(TimeUnit::Nanosecond, None),
&CastOptions {
safe: false,
format_options: FormatOptions::default(),
},
);
assert!(err.is_err());

let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
let c = b.as_primitive::<TimestampNanosecondType>();
assert_eq!(upper_limit as i64 * NANOSECONDS_IN_DAY, c.value(0));
assert!(c.is_null(1));
assert!(c.is_null(2));
}

#[test]
fn test_timezone_cast() {
let a = StringArray::from(vec![
Expand Down
Loading