You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "tustvold (via GitHub)" <gi...@apache.org> on 2023/04/07 13:07:21 UTC

[GitHub] [arrow-rs] tustvold opened a new pull request, #4034: Fix timestamp handling in cast kernel (#1936) (#4033)

tustvold opened a new pull request, #4034:
URL: https://github.com/apache/arrow-rs/pull/4034

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #1936
   Closes #4033
   
   # Rationale for this change
    
   <!--
   Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
   Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   -->
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are there any user-facing changes?
   
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!---
   If there are any breaking changes to public APIs, please add the `breaking change` label.
   -->
   


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-rs] tustvold merged pull request #4034: Fix timestamp handling in cast kernel (#1936) (#4033)

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold merged PR #4034:
URL: https://github.com/apache/arrow-rs/pull/4034


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-rs] alamb commented on pull request #4034: Fix timestamp handling in cast kernel (#1936) (#4033)

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #4034:
URL: https://github.com/apache/arrow-rs/pull/4034#issuecomment-1500315352

   Thank you @tustvold 


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #4034: Fix timestamp handling in cast kernel (#1936) (#4033)

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #4034:
URL: https://github.com/apache/arrow-rs/pull/4034#discussion_r1160689809


##########
arrow-cast/src/cast.rs:
##########
@@ -2653,59 +2653,67 @@ fn cast_string_to_time64nanosecond<Offset: OffsetSizeTrait>(
 }
 
 /// Casts generic string arrays to an ArrowTimestampType (TimeStampNanosecondArray, etc.)
-fn cast_string_to_timestamp<
-    Offset: OffsetSizeTrait,
-    TimestampType: ArrowTimestampType<Native = i64>,
->(
+fn cast_string_to_timestamp<O: OffsetSizeTrait, T: ArrowTimestampType>(
     array: &dyn Array,
     to_tz: &Option<Arc<str>>,
     cast_options: &CastOptions,
 ) -> Result<ArrayRef, ArrowError> {
-    let string_array = array
-        .as_any()
-        .downcast_ref::<GenericStringArray<Offset>>()
-        .unwrap();
-
-    let scale_factor = match TimestampType::UNIT {
-        TimeUnit::Second => 1_000_000_000,
-        TimeUnit::Millisecond => 1_000_000,
-        TimeUnit::Microsecond => 1_000,
-        TimeUnit::Nanosecond => 1,
+    let array = array.as_string::<O>();
+    let out: PrimitiveArray<T> = match to_tz {
+        Some(tz) => {
+            let tz: Tz = tz.as_ref().parse()?;

Review Comment:
   This is the fix for #1936



-- 
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.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #4034: Fix timestamp handling in cast kernel (#1936) (#4033)

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #4034:
URL: https://github.com/apache/arrow-rs/pull/4034#discussion_r1160689601


##########
arrow-array/src/types.rs:
##########
@@ -311,19 +311,43 @@ pub trait ArrowTimestampType: ArrowTemporalType<Native = i64> {
     fn get_time_unit() -> TimeUnit {
         Self::UNIT
     }
+
+    /// Creates a ArrowTimestampType::Native from the provided [`NaiveDateTime`]
+    ///
+    /// See [`DataType::Timestamp`] for more information on timezone handling
+    fn make_value(naive: NaiveDateTime) -> Option<i64>;

Review Comment:
   This is the fix for #4033



-- 
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.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #4034: Fix timestamp handling in cast kernel (#1936) (#4033)

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #4034:
URL: https://github.com/apache/arrow-rs/pull/4034#discussion_r1160692149


##########
arrow-cast/src/cast.rs:
##########
@@ -8079,24 +8095,45 @@ mod tests {
             let array = Arc::new(valid) as ArrayRef;
             let b = cast_with_options(
                 &array,
-                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz)),
+                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
                 &CastOptions { safe: false },
             )
             .unwrap();
 
-            let c = b
-                .as_any()
-                .downcast_ref::<TimestampNanosecondArray>()
-                .unwrap();
-            assert_eq!(1672574706789000000, c.value(0));
-            assert_eq!(1672571106789000000, c.value(1));
-            assert_eq!(1672574706789000000, c.value(2));
-            assert_eq!(1672574706789000000, c.value(3));
-            assert_eq!(1672518906000000000, c.value(4));
-            assert_eq!(1672518906000000000, c.value(5));
-            assert_eq!(1672545906789000000, c.value(6));
-            assert_eq!(1672545906000000000, c.value(7));
-            assert_eq!(1672531200000000000, c.value(8));
+            let tz = tz.as_ref().parse().unwrap();
+
+            let as_tz = |v: i64| {
+                as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap()
+            };
+
+            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
+            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
+
+            let values = b.as_primitive::<TimestampNanosecondType>().values();
+            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
+            let local_results: Vec<_> = values.iter().map(as_local).collect();
+
+            // Absolute timestamps should be parsed preserving the same UTC instant
+            assert_eq!(
+                &utc_results[..6],
+                &[
+                    "2023-01-01 12:05:06.789".to_string(),
+                    "2023-01-01 11:05:06.789".to_string(),
+                    "2023-01-01 12:05:06.789".to_string(),
+                    "2023-01-01 12:05:06.789".to_string(),
+                    "2022-12-31 20:35:06".to_string(),
+                    "2022-12-31 20:35:06".to_string(),
+                ]
+            );
+            // Non-absolute timestamps should be parsed preserving the same local instant

Review Comment:
   More background on this can be found in #1936, in particular https://github.com/apache/arrow-rs/issues/1936#issuecomment-1253830499



-- 
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.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

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