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

github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/main by this push:
     new fdf885971b fix(arrow-json): reject out-of-range float timestamps 
instead of saturating (#12007)
fdf885971b is described below

commit fdf885971bad1abd393b728f946ded4e7b7ef26b
Author: hsdfat <[email protected]>
AuthorDate: Wed Nov 4 05:26:55 2026 +0000

    fix(arrow-json): reject out-of-range float timestamps instead of saturating 
(#12007)
    
    # Which issue does this PR close?
    
    - None; small bug fix.
    
    # Rationale for this change
    
    When a JSON number for a `Timestamp` column doesn't parse as `i64`, the
    reader falls back to `f64` and casts with `as i64`, which saturates.
    `{"time": 1e30}` read as `Timestamp(Second)` silently becomes
    `i64::MAX`, and displaying the batch then fails with `Cast error: Failed
    to convert 9223372036854775807 to timestamp`.
    
    The primitive decoder (Int64, Date32, Time, Duration) uses
    `NumCast::from` for the same fallback. So an out-of-range float is a
    "failed to parse" error there, or null with
    `with_ignore_type_conflicts(true)`. Timestamps did neither.
    
    # What changes are included in this PR?
    
    The timestamp decoder uses the same `NumCast::from` fallback as
    `primitive_array.rs`. In-range values, including fractional ones that
    truncate, are unchanged.
    
    # Are these changes tested?
    
    Yes. `test_timestamp_float_out_of_range` covers ±1e30, 2^63, 1e999
    (error, or null when ignoring type conflicts) and -2^63 (still
    `i64::MIN`).
    
    # Are there any significant user-facing changes?
    
    Out-of-range float timestamps now produce an error (or null with
    `with_ignore_type_conflicts`) instead of a saturated `i64::MAX` /
    `i64::MIN`, matching the other numeric types.
    
    ---
    
    AI disclosure: the bug was found and the patch and test drafted with AI
    assistance (Claude); I reviewed every line, verified the failing test
    before/after the fix, and ran fmt/clippy/tests for the crate.
---
 arrow-json/src/reader/mod.rs             | 46 ++++++++++++++++++++++++++++++++
 arrow-json/src/reader/timestamp_array.rs |  7 +++--
 2 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/arrow-json/src/reader/mod.rs b/arrow-json/src/reader/mod.rs
index d8f5027a50..816b346a08 100644
--- a/arrow-json/src/reader/mod.rs
+++ b/arrow-json/src/reader/mod.rs
@@ -2290,6 +2290,52 @@ mod tests {
         assert_eq!(i64.values(), &[i64::MAX, i64::MIN, 900000]);
     }
 
+    #[test]
+    fn test_timestamp_float_out_of_range() {
+        let schema = Arc::new(Schema::new(vec![Field::new(
+            "time",
+            DataType::Timestamp(TimeUnit::Second, None),
+            true,
+        )]));
+
+        // -2^63 is the smallest representable value
+        let batch = ReaderBuilder::new(schema.clone())
+            .build(Cursor::new(
+                r#"{"time": -9.223372036854775808e18}"#.as_bytes(),
+            ))
+            .unwrap()
+            .next()
+            .unwrap()
+            .unwrap();
+        let col = batch.column(0).as_primitive::<TimestampSecondType>();
+        assert_eq!(col.value(0), i64::MIN);
+
+        for buf in [
+            r#"{"time": 1e30}"#,
+            r#"{"time": -1e30}"#,
+            r#"{"time": 9.223372036854775808e18}"#,
+            r#"{"time": 1e999}"#,
+        ] {
+            let err = ReaderBuilder::new(schema.clone())
+                .build(Cursor::new(buf.as_bytes()))
+                .unwrap()
+                .next()
+                .unwrap()
+                .unwrap_err()
+                .to_string();
+            assert!(err.contains("failed to parse"), "{buf}: {err}");
+
+            let batch = ReaderBuilder::new(schema.clone())
+                .with_ignore_type_conflicts(true)
+                .build(Cursor::new(buf.as_bytes()))
+                .unwrap()
+                .next()
+                .unwrap()
+                .unwrap();
+            assert!(batch.column(0).is_null(0), "{buf}");
+        }
+    }
+
     #[test]
     fn test_strict_mode_no_missing_columns_in_schema() {
         let buf = r#"
diff --git a/arrow-json/src/reader/timestamp_array.rs 
b/arrow-json/src/reader/timestamp_array.rs
index 7a3937565f..f0fd2dd84e 100644
--- a/arrow-json/src/reader/timestamp_array.rs
+++ b/arrow-json/src/reader/timestamp_array.rs
@@ -24,6 +24,7 @@ use arrow_array::types::ArrowTimestampType;
 use arrow_cast::parse::string_to_datetime;
 use arrow_schema::{ArrowError, DataType, TimeUnit};
 use chrono::TimeZone;
+use num_traits::NumCast;
 
 use crate::reader::tape::{Tape, TapeElement};
 use crate::reader::{ArrayDecoder, DecoderContext};
@@ -87,8 +88,10 @@ where
                     let s = tape.get_string(idx);
                     let b = s.as_bytes();
                     lexical_core::parse::<i64>(b)
-                        .or_else(|_| lexical_core::parse::<f64>(b).map(|x| x 
as i64))
-                        .map_err(|_| {
+                        .ok()
+                        // a float outside the i64 range must not saturate to 
i64::MIN/MAX
+                        .or_else(|| 
lexical_core::parse::<f64>(b).ok().and_then(NumCast::from))
+                        .ok_or_else(|| {
                             ArrowError::JsonError(format!(
                                 "failed to parse {s} as {}",
                                 self.data_type

Reply via email to