You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by th...@apache.org on 2022/05/11 09:44:47 UTC

[arrow] branch master updated: ARROW-16253: [R] Helper function for casting from float to duration via int64()

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 101f63ecd8 ARROW-16253: [R] Helper function for casting from float to duration via int64()
101f63ecd8 is described below

commit 101f63ecd84002395ac7314170da7e8ec2360bb4
Author: Dragoș Moldovan-Grünfeld <dr...@gmail.com>
AuthorDate: Wed May 11 10:44:33 2022 +0100

    ARROW-16253: [R] Helper function for casting from float to duration via int64()
    
    Closes #13055 from dragosmg/duration_casting_helper
    
    Authored-by: Dragoș Moldovan-Grünfeld <dr...@gmail.com>
    Signed-off-by: Nic Crane <th...@gmail.com>
---
 r/R/dplyr-datetime-helpers.R | 10 +++-------
 r/R/dplyr-funcs-datetime.R   | 12 +++++-------
 2 files changed, 8 insertions(+), 14 deletions(-)

diff --git a/r/R/dplyr-datetime-helpers.R b/r/R/dplyr-datetime-helpers.R
index 25c29319d8..1855a4a46e 100644
--- a/r/R/dplyr-datetime-helpers.R
+++ b/r/R/dplyr-datetime-helpers.R
@@ -143,13 +143,9 @@ binding_as_date_numeric <- function(x, origin = "1970-01-01") {
 
   if (origin != "1970-01-01") {
     delta_in_sec <- call_binding("difftime", origin, "1970-01-01")
-    # TODO: revisit once either of these issues is addressed:
-    #   https://issues.apache.org/jira/browse/ARROW-16253 (helper function for
-    #   casting from double to duration) or
-    #   https://issues.apache.org/jira/browse/ARROW-15862 (casting from int32
-    #   -> duration or double -> duration)
-    delta_in_sec <- build_expr("cast", delta_in_sec, options = cast_options(to_type = int64()))
-    delta_in_days <- (delta_in_sec / 86400L)$cast(int32())
+    # TODO revisit once https://issues.apache.org/jira/browse/ARROW-15862
+    # (casting from int32 -> duration or double -> duration) is addressed
+    delta_in_days <- (delta_in_sec$cast(int64()) / 86400L)$cast(int32())
     x <- build_expr("+", x, delta_in_days)
   }
 
diff --git a/r/R/dplyr-funcs-datetime.R b/r/R/dplyr-funcs-datetime.R
index 466272db46..5e78b94fe5 100644
--- a/r/R/dplyr-funcs-datetime.R
+++ b/r/R/dplyr-funcs-datetime.R
@@ -352,8 +352,8 @@ register_bindings_datetime_conversion <- function() {
 
     fraction <- decimal - y
     delta <- build_expr("floor", seconds * fraction)
-    delta <- delta$cast(int64())
-    start + delta$cast(duration("s"))
+    delta <- make_duration(delta, "s")
+    start + delta
   })
 }
 
@@ -413,9 +413,8 @@ register_bindings_duration <- function() {
 
     if (call_binding("is.character", x)) {
       x <- build_expr("strptime", x, options = list(format = format, unit = 0L))
-      # complex casting only due to cast type restrictions: time64 -> int64 -> duration(us)
-      # and then we cast to duration ("s") at the end
-      x <- x$cast(time64("us"))$cast(int64())$cast(duration("us"))
+      # we do a final cast to duration ("s") at the end
+      x <- make_duration(x$cast(time64("us")), unit = "us")
     }
 
     # numeric -> duration not supported in Arrow yet so we use int64() as an
@@ -460,8 +459,7 @@ register_bindings_duration_constructor <- function() {
       duration <- duration_from_chunks(chunks)
     }
 
-    duration <- build_expr("cast", duration, options = cast_options(to_type = int64()))
-    duration$cast(duration("s"))
+    make_duration(duration, "s")
   })
 }