Skip to content

Commit fa2ba9e

Browse files
authored
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
1 parent 0c1e3b8 commit fa2ba9e

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

arrow-cast/src/cast/mod.rs

+28
Original file line numberDiff line numberDiff line change
@@ -8057,6 +8057,34 @@ mod tests {
80578057
test_cast_string_to_decimal256_overflow(overflow_array);
80588058
}
80598059

8060+
#[test]
8061+
fn test_cast_outside_supported_range_for_nanoseconds() {
8062+
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";
8063+
8064+
let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
8065+
8066+
let cast_options = CastOptions {
8067+
safe: false,
8068+
format_options: FormatOptions::default(),
8069+
};
8070+
8071+
let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
8072+
&array,
8073+
&None::<Arc<str>>,
8074+
&cast_options,
8075+
);
8076+
8077+
let err = result.unwrap_err();
8078+
assert_eq!(
8079+
err.to_string(),
8080+
format!(
8081+
"Cast error: Overflow converting {} to Nanosecond. {}",
8082+
array.value(0),
8083+
EXPECTED_ERROR_MESSAGE
8084+
)
8085+
);
8086+
}
8087+
80608088
#[test]
80618089
fn test_cast_date32_to_timestamp() {
80628090
let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1

arrow-cast/src/cast/string.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,11 @@ fn cast_string_to_timestamp_impl<O: OffsetSizeTrait, T: ArrowTimestampType, Tz:
112112
.map(|v| {
113113
v.map(|v| {
114114
let naive = string_to_datetime(tz, v)?.naive_utc();
115-
T::make_value(naive).ok_or_else(|| {
116-
ArrowError::CastError(format!(
115+
T::make_value(naive).ok_or_else(|| match T::UNIT {
116+
TimeUnit::Nanosecond => ArrowError::CastError(format!(
117+
"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"
118+
)),
119+
_ => ArrowError::CastError(format!(
117120
"Overflow converting {naive} to {:?}",
118121
T::UNIT
119122
))

0 commit comments

Comments
 (0)