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 ))