dragosmg commented on a change in pull request #12506:
URL: https://github.com/apache/arrow/pull/12506#discussion_r833113113



##########
File path: r/R/dplyr-funcs-datetime.R
##########
@@ -189,6 +189,65 @@ register_bindings_datetime <- function() {
   })
 }
 
+register_bindings_duration <- function() {
+  register_binding("difftime", function(time1,
+                                        time2,
+                                        tz,
+                                        units = c("auto", "secs", "mins",
+                                                  "hours", "days", "weeks")) {
+    units <- match.arg(units)
+    if (units != "secs") {
+      abort("`difftime()` with units other than seconds not supported in 
Arrow")
+    }
+
+    if (!missing(tz)) {
+      warn("`tz` is an optional argument to `difftime()` in R and will not be 
used in Arrow")
+    }
+
+    time1 <- build_expr("cast", time1, options = cast_options(to_type = 
timestamp()))
+    time2 <- build_expr("cast", time2, options = cast_options(to_type = 
timestamp()))
+
+    build_expr("cast", time1 - time2, options = cast_options(to_type = 
duration("s")))
+  })
+
+  register_binding("as.difftime", function(x,
+                                           format = "%X",
+                                           units = "auto") {
+    # windows doesn't seem to like "%X"
+    if (format == "%X" & tolower(Sys.info()[["sysname"]]) == "windows") {
+      format <- "%H:%M:%S"
+    }
+
+    if (units != "secs") {
+      abort("`as.difftime()` with units other than seconds not supported in 
Arrow")
+    }
+
+    if (call_binding("is.character", x)) {
+      x <- build_expr("strptime", x, options = list(format = format, unit = 
0L))
+      y <- build_expr("strptime", "0:0:0", options = list(format = "%H:%M:%S", 
unit = 0L))
+      diff_x_y <- call_binding("difftime", x, y, units = "secs")
+      return(diff_x_y)
+    }

Review comment:
       I've done a bit more thinking and digging around this. It turns out 
{arrow} maps `time32()` to R's `difftime`. So, in theory, we don't need `y` and 
we could simply cast `x` to `time32()`. In practice it's not as straightforward 
since `x` is both `hms` and `difftime`:
   
   ``` r
   library(arrow, warn.conflicts = FALSE)
   library(dplyr, warn.conflicts = FALSE)
   library(lubridate, warn.conflicts = FALSE)
   
   test_df <- tibble(
     hms_string = c("0:7:45", "12:34:56"),
     hm_string = c("7:45", "12:34"),
     int = c(30L, 75L),
     integerish_dbl = c(31, 76),
     dbl = c(31.2, 76.4)
   )
   
   test_df %>%
     mutate(hms_difftime = as.difftime(hms_string, units = "secs"))
   #> # A tibble: 2 × 6
   #>   hms_string hm_string   int integerish_dbl   dbl hms_difftime
   #>   <chr>      <chr>     <int>          <dbl> <dbl> <drtn>      
   #> 1 0:7:45     7:45         30             31  31.2   465 secs  
   #> 2 12:34:56   12:34        75             76  76.4 45296 secs
   
   test_df %>%
     arrow_table() %>%
     mutate(hms_difftime = as.difftime(hms_string, units = "secs")) %>%
     collect()
   #> # A tibble: 2 × 6
   #>   hms_string hm_string   int integerish_dbl   dbl hms_difftime
   #>   <chr>      <chr>     <int>          <dbl> <dbl> <drtn>      
   #> 1 0:7:45     7:45         30             31  31.2   465 secs  
   #> 2 12:34:56   12:34        75             76  76.4 45296 secs
   
   compare_dplyr_binding(
       .input %>%
         mutate(hms_difftime = as.difftime(hms_string, units = "secs")) %>%
         collect(),
       test_df
     )
   #> Error: `object` (`actual`) not equal to `expected` (`expected`).
   #>
   #> `class(actual$hms_difftime)`: "hms" "difftime"
   #> `class(expected$hms_difftime)`: "difftime"
   ```
   
   <sup>Created on 2022-03-23 by the [reprex 
package](https://reprex.tidyverse.org) (v2.0.1)</sup>
   
   We could simply ignore attributes but that doesn't feel right. 




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to