thisisnic commented on code in PR #46206:
URL: https://github.com/apache/arrow/pull/46206#discussion_r2081677002


##########
r/R/dplyr-funcs-datetime.R:
##########
@@ -826,3 +827,54 @@ register_bindings_datetime_rounding <- function() {
     }
   )
 }
+
+register_bindings_hms <- function() {
+  numeric_to_time32 <- function(x) {
+    # The only numeric which can be cast to time32 is int32 so double cast to 
make sure
+    cast(cast(x, int32()), time32(unit = "s"))
+  }
+
+  register_binding(
+    "hms::hms",
+    function(seconds = 0, minutes = 0, hours = 0, days = 0) {
+      total_secs <- seconds +
+        Expression$create("multiply_checked", minutes, 60) +
+        Expression$create("multiply_checked", hours, 3600) +
+        Expression$create("multiply_checked", days, 86400)
+
+      return(numeric_to_time32(total_secs))
+    }
+  )
+
+  register_binding(
+    "hms::as_hms",
+    function(x = numeric()) {
+      datetime_to_time32 <- function(datetime) {
+        hour <- call_binding("hour", datetime)
+        min <- call_binding("minute", datetime)
+        sec <- call_binding("second", datetime)
+
+        return(call_binding("hms::hms", seconds = sec, minutes = min, hours = 
hour))
+      }
+
+      if (call_binding("is.POSIXct", x)) {
+        return(datetime_to_time32(x))
+      }
+
+      if (call_binding("is.numeric", x)) {
+        return(numeric_to_time32(x))
+      }
+
+      if (call_binding("is.character", x)) {
+        dash <- call_binding("gsub", ":", "-", x)
+        as_date_time_string <- call_binding("str_c", "1970-01-01", dash, sep = 
"-")
+        as_date_time <- Expression$create(
+          "strptime",
+          as_date_time_string,
+          options = list(format = "%Y-%m-%d-%H-%M-%S", unit = 0L)
+        )
+        return(datetime_to_time32(as_date_time))
+      }

Review Comment:
   At present, here's the two versions.
   
   hms:
   
   ```
   > tibble::tibble(x = "bad_string") %>% mutate(y = hms::as_hms(x)) %>% 
collect()
   Error in `mutate()`:
   ℹ In argument: `y = hms::as_hms(x)`.
   Caused by error in `abort_lossy_cast()`:
   ! Lossy cast from <character> to <hms> at position(s) 1
   Run `rlang::last_trace()` to see where the error occurred.
   ```
   
   This binding:
   ```
   > arrow_table(x = "bad_string") %>% mutate(y = hms::as_hms(x)) %>% collect()
   Error in `compute.arrow_dplyr_query()` at r/R/dplyr-collect.R:22:3:
   ! Invalid: Failed to parse string: '1970-01-01-bad_string' as a scalar of 
type timestamp[s]
   Run `rlang::last_trace()` to see where the error occurred.
   ```
   
   Messaging could be better as it's a bit misleading, but I couldn't think of 
a better way to handle it.  I tried a few things like 
`call_binding("stringr::str_detect", x "[^0-9:]")` but that didn't work, 
presumably because we can't get acccess to that information without evaluating 
it, and it's a scalar not an aggregate so wouldn't make sense anyway.



-- 
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-unsubscr...@arrow.apache.org

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

Reply via email to