Skip to content

Commit

Permalink
Improve error message for timestamp queries outside supported range (#…
Browse files Browse the repository at this point in the history
…5730)

* improved the error message

* added a test to test the overflow

* fixed the format arrow

* removed assert
  • Loading branch information
Abdi-29 committed May 16, 2024
1 parent 0c1e3b8 commit fa2ba9e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
28 changes: 28 additions & 0 deletions arrow-cast/src/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8057,6 +8057,34 @@ mod tests {
test_cast_string_to_decimal256_overflow(overflow_array);
}

#[test]
fn test_cast_outside_supported_range_for_nanoseconds() {
const EXPECTED_ERROR_MESSAGE: &str = "The dates that can be represented as nanoseconds have to be between 1677-09-21T00:12:44.0 and 2262-04-11T23:47:16.854775804";

let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);

let cast_options = CastOptions {
safe: false,
format_options: FormatOptions::default(),
};

let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
&array,
&None::<Arc<str>>,
&cast_options,
);

let err = result.unwrap_err();
assert_eq!(
err.to_string(),
format!(
"Cast error: Overflow converting {} to Nanosecond. {}",
array.value(0),
EXPECTED_ERROR_MESSAGE
)
);
}

#[test]
fn test_cast_date32_to_timestamp() {
let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
Expand Down
7 changes: 5 additions & 2 deletions arrow-cast/src/cast/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ fn cast_string_to_timestamp_impl<O: OffsetSizeTrait, T: ArrowTimestampType, Tz:
.map(|v| {
v.map(|v| {
let naive = string_to_datetime(tz, v)?.naive_utc();
T::make_value(naive).ok_or_else(|| {
ArrowError::CastError(format!(
T::make_value(naive).ok_or_else(|| match T::UNIT {
TimeUnit::Nanosecond => ArrowError::CastError(format!(
"Overflow converting {naive} to Nanosecond. The dates that can be represented as nanoseconds have to be between 1677-09-21T00:12:44.0 and 2262-04-11T23:47:16.854775804"
)),
_ => ArrowError::CastError(format!(
"Overflow converting {naive} to {:?}",
T::UNIT
))
Expand Down

0 comments on commit fa2ba9e

Please sign in to comment.