From fa2ba9efc7cecd5b02f81ae8b109c28a1963c1b3 Mon Sep 17 00:00:00 2001 From: Abi <76049938+Abdi-29@users.noreply.github.com> Date: Thu, 16 May 2024 16:32:29 +0200 Subject: [PATCH] Improve error message for timestamp queries outside supported range (#5730) * improved the error message * added a test to test the overflow * fixed the format arrow * removed assert --- arrow-cast/src/cast/mod.rs | 28 ++++++++++++++++++++++++++++ arrow-cast/src/cast/string.rs | 7 +++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/arrow-cast/src/cast/mod.rs b/arrow-cast/src/cast/mod.rs index 171267f80543..ccbc8727f7d9 100644 --- a/arrow-cast/src/cast/mod.rs +++ b/arrow-cast/src/cast/mod.rs @@ -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::( + &array, + &None::>, + &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 diff --git a/arrow-cast/src/cast/string.rs b/arrow-cast/src/cast/string.rs index e9c1ff58d62f..4b83a2a5e7da 100644 --- a/arrow-cast/src/cast/string.rs +++ b/arrow-cast/src/cast/string.rs @@ -112,8 +112,11 @@ fn cast_string_to_timestamp_impl 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 ))