This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new fa2ba9efc7c Improve error message for timestamp queries outside 
supported range (#5730)
fa2ba9efc7c is described below

commit fa2ba9efc7cecd5b02f81ae8b109c28a1963c1b3
Author: Abi <[email protected]>
AuthorDate: Thu May 16 16:32:29 2024 +0200

    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 171267f8054..ccbc8727f7d 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::<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
diff --git a/arrow-cast/src/cast/string.rs b/arrow-cast/src/cast/string.rs
index e9c1ff58d62..4b83a2a5e7d 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<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
                         ))

Reply via email to