You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by tu...@apache.org on 2023/05/31 21:11:16 UTC

[arrow-rs] branch master updated: fix date conversion if timestamp below unixtimestamp (#4323)

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

tustvold 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 d1fde40b7 fix date conversion if timestamp below unixtimestamp (#4323)
d1fde40b7 is described below

commit d1fde40b7854500d540d1671ba36c151d14071e6
Author: comphead <co...@users.noreply.github.com>
AuthorDate: Wed May 31 14:11:10 2023 -0700

    fix date conversion if timestamp below unixtimestamp (#4323)
    
    * fix date conversion if timestamp below unixtimestamp
    
    * comments
---
 arrow-cast/src/cast.rs | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

diff --git a/arrow-cast/src/cast.rs b/arrow-cast/src/cast.rs
index 2b286bfa9..9652047c7 100644
--- a/arrow-cast/src/cast.rs
+++ b/arrow-cast/src/cast.rs
@@ -1853,7 +1853,7 @@ pub fn cast_with_options(
                 if time_array.is_null(i) {
                     b.append_null();
                 } else {
-                    b.append_value((time_array.value(i) / from_size) as i32);
+                    b.append_value(num::integer::div_floor::<i64>(time_array.value(i), from_size) as i32);
                 }
             }
 
@@ -9172,4 +9172,41 @@ mod tests {
         );
         assert!(casted_array.is_err());
     }
+
+    #[test]
+    fn test_cast_below_unixtimestamp() {
+        let valid = StringArray::from(vec![
+            "1900-01-03 23:59:59",
+            "1969-12-31 00:00:01",
+            "1989-12-31 00:00:01",
+        ]);
+
+        let array = Arc::new(valid) as ArrayRef;
+        let casted_array = cast_with_options(
+            &array,
+            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
+            &CastOptions {
+                safe: false,
+                format_options: FormatOptions::default(),
+            },
+        )
+        .unwrap();
+
+        let ts_array = casted_array
+            .as_primitive::<TimestampNanosecondType>()
+            .values()
+            .iter()
+            .map(|ts| ts / 1_000_000)
+            .collect::<Vec<_>>();
+
+        let array =
+            TimestampMillisecondArray::from(ts_array).with_timezone("UTC".to_string());
+        let casted_array = cast(&array, &DataType::Date32).unwrap();
+        let date_array = casted_array.as_primitive::<Date32Type>();
+        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
+        let string_array = casted_array.as_string::<i32>();
+        assert_eq!("1900-01-03", string_array.value(0));
+        assert_eq!("1969-12-31", string_array.value(1));
+        assert_eq!("1989-12-31", string_array.value(2));
+    }
 }