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 2022/04/05 08:55:17 UTC

[GitHub] [arrow] AlenkaF commented on a diff in pull request #12738: ARROW-15800 [R] Implement bindings for `lubridate::as_date()` and `lubridate::as_datetime()`

AlenkaF commented on code in PR #12738:
URL: https://github.com/apache/arrow/pull/12738#discussion_r842529031


##########
r/R/dplyr-funcs-type.R:
##########
@@ -82,44 +82,40 @@ register_bindings_type_cast <- function() {
                                        tryFormats = "%Y-%m-%d",
                                        origin = "1970-01-01",
                                        tz = "UTC") {
+    binding_as_date(
+      x = x,
+      format = format,
+      tryFormats = tryFormats,
+      origin = origin,
+      tz = tz,
+      base = TRUE
+    )
+  })
 
-    # the origin argument will be better supported once we implement temporal
-    # arithmetic (https://issues.apache.org/jira/browse/ARROW-14947)
-    # TODO revisit once the above has been sorted
-    if (call_binding("is.numeric", x) & origin != "1970-01-01") {
-      abort("`as.Date()` with an `origin` different than '1970-01-01' is not supported in Arrow")
-    }
-
-    # this could be improved with tryFormats once strptime returns NA and we
-    # can use coalesce - https://issues.apache.org/jira/browse/ARROW-15659
-    # TODO revisit once https://issues.apache.org/jira/browse/ARROW-15659 is done
-    if (is.null(format) && length(tryFormats) > 1) {
-      abort("`as.Date()` with multiple `tryFormats` is not supported in Arrow")
-    }
-
-    if (call_binding("is.Date", x)) {
-      return(x)
-
-    # cast from POSIXct
-    } else if (call_binding("is.POSIXct", x)) {
-      # base::as.Date() first converts to the desired timezone and then extracts
-      # the date, which is why we need to go through timestamp() first
-      x <- build_expr("cast", x, options = cast_options(to_type = timestamp(timezone = tz)))
-
-    # cast from character
-    } else if (call_binding("is.character", x)) {
-      format <- format %||% tryFormats[[1]]
-      # unit = 0L is the identifier for seconds in valid_time32_units
-      x <- build_expr("strptime", x, options = list(format = format, unit = 0L))
+  register_binding("as_date", function(x,
+                                       format = NULL,
+                                       origin = "1970-01-01",
+                                       tz = "UTC") {
+    binding_as_date(
+      x = x,
+      format = format,
+      origin = origin,
+      tz = tz,
+      base = FALSE
+    )
+  })
 
-    # cast from numeric
-    } else if (call_binding("is.numeric", x) & !call_binding("is.integer", x)) {
-      # Arrow does not support direct casting from double to date32()
-      # https://issues.apache.org/jira/browse/ARROW-15798
-      # TODO revisit if arrow decides to support double -> date casting
-      abort("`as.Date()` with double/float is not supported in Arrow")
+  register_binding("as_datetime", function(x,
+                                           origin = "1970-01-01",
+                                           tz = "UTC") {

Review Comment:
   I would think `as_date` accepts any `tz` and `as_datetime` has a default `tz` set to UTC?
   
   Also, how come `as_datetime` doesn't accept `format` - I am wondering how come you did not use `binding_as_date` for `as_datetime` also?



##########
r/R/dplyr-funcs-datetime.R:
##########
@@ -263,11 +263,11 @@ register_bindings_duration <- function() {
     # cast to timestamp if time1 and time2 are not dates or timestamp expressions
     # (the subtraction of which would output a `duration`)
     if (!call_binding("is.instant", time1)) {
-      time1 <- build_expr("cast", time1, options = cast_options(to_type = timestamp(timezone = "UTC")))
+      time1 <- build_expr("cast", time1, options = cast_options(to_type = timestamp()))
     }
 
     if (!call_binding("is.instant", time2)) {
-      time2 <- build_expr("cast", time2, options = cast_options(to_type = timestamp(timezone = "UTC")))
+      time2 <- build_expr("cast", time2, options = cast_options(to_type = timestamp()))

Review Comment:
   Are this changes necessary for this PR or is this a part of something else?



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