You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/12/01 00:37:45 UTC

[GitHub] [arrow] andygrove commented on a change in pull request #8794: ARROW-10759: [Rust][DataFusion] Implement string to date cast

andygrove commented on a change in pull request #8794:
URL: https://github.com/apache/arrow/pull/8794#discussion_r532995916



##########
File path: rust/arrow/src/compute/kernels/cast.rs
##########
@@ -2720,6 +2743,42 @@ mod tests {
             .collect()
     }
 
+    #[test]
+    fn test_cast_utf8_to_date32() {
+        use chrono::{NaiveDate, NaiveTime};
+
+        let a = StringArray::from(vec![
+            "2000-01-01",
+            "2000-2-2",
+            "2000-00-00",
+            "2000-01-01T12:00:00",
+            "2000",
+        ]);
+        let array = Arc::new(a) as ArrayRef;
+        let b = cast(&array, &DataType::Date32(DateUnit::Day)).unwrap();
+        let c = b.as_any().downcast_ref::<Date32Array>().unwrap();
+
+        let zero_time = NaiveTime::from_hms(0, 0, 0);
+
+        let date_value = (NaiveDate::from_ymd(2000, 1, 1)
+            .and_time(zero_time)
+            .timestamp()
+            / SECONDS_IN_DAY) as i32;
+        assert_eq!(true, c.is_valid(0));
+        assert_eq!(date_value, c.value(0));
+
+        let date_value = (NaiveDate::from_ymd(2000, 2, 2)
+            .and_time(zero_time)
+            .timestamp()
+            / SECONDS_IN_DAY) as i32;
+        assert_eq!(true, c.is_valid(1));
+        assert_eq!(date_value, c.value(1));
+
+        assert_eq!(false, c.is_valid(2));
+        assert_eq!(false, c.is_valid(3));
+        assert_eq!(false, c.is_valid(4));

Review comment:
       This looks great. Could we get some tests added for invalid inputs as well?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org