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/03/28 20:28:52 UTC

[GitHub] [arrow] dragosmg commented on a change in pull request #12677: ARROW-15800 [R] bindings for `lubridate::as_date()` and `lubridate::as_datetime()` - abandoned

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



##########
File path: r/R/dplyr-funcs-datetime.R
##########
@@ -239,66 +303,108 @@ register_bindings_duration <- function() {
                                        tz = "UTC") {
     call_binding("make_datetime", year, month, day, hour, min, sec, tz)
   })
-  register_binding("difftime", function(time1,
-                                        time2,
-                                        tz,
-                                        units = "secs") {
-    if (units != "secs") {
-      abort("`difftime()` with units other than `secs` not supported in Arrow")
-    }
+  register_binding("as.Date", function(x,
+                                       format = NULL,
+                                       tryFormats = "%Y-%m-%d",
+                                       origin = "1970-01-01",
+                                       tz = "UTC") {
 
-    if (!missing(tz)) {
-      warn("`tz` argument is not supported in Arrow, so it will be ignored")
+    # 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")
     }
 
-    # 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")))
+    # 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.instant", time2)) {
-      time2 <- build_expr("cast", time2, options = cast_options(to_type = timestamp(timezone = "UTC")))
-    }
+    if (call_binding("is.Date", x)) {
+      return(x)
 
-    # we need to go build the subtract expression instead of `time1 - time2` to
-    # prevent complaints when we try to subtract an R object from an Expression
-    subtract_output <- build_expr("-", time1, time2)
-    build_expr("cast", subtract_output, options = cast_options(to_type = duration("s")))
+      # 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))
+
+      # 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(), but for
+      # integer-like values we can go via int32()
+      # https://issues.apache.org/jira/browse/ARROW-15798
+      # TODO revisit if arrow decides to support double -> date casting
+      x <- build_expr("cast", x, options = cast_options(to_type = int32()))
+    }
+    build_expr("cast", x, options = cast_options(to_type = date32()))
   })
-  register_binding("as.difftime", function(x,
-                                           format = "%X",
-                                           units = "secs") {
-    # windows doesn't seem to like "%X"
-    if (format == "%X" & tolower(Sys.info()[["sysname"]]) == "windows") {
-      format <- "%H:%M:%S"
+  register_binding("as_date", function(x,
+                                       format = NULL,
+                                       origin = "1970-01-01",
+                                       tz = "UTC") {
+    # 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")
     }
 
-    if (units != "secs") {
-      abort("`as.difftime()` with units other than 'secs' not supported in Arrow")
+    # assume format is ISO if unspecified (to align with lubridate::as_date)
+    if (is.null(format)) {
+      format <- "%Y-%m-%d"
     }
 
-    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"))
-    }
+    if (call_binding("is.Date", x)) {
+      return(x)
 
-    # numeric -> duration not supported in Arrow yet so we use int64() as an
-    # intermediate step
-    # TODO revisit if https://issues.apache.org/jira/browse/ARROW-15862 results
-    # in numeric -> duration support
+      # cast from POSIXct
+    } else if (call_binding("is.POSIXct", x)) {
+      # this is where as_date() differs from as.Date()
+      if (!missing(tz)) {
+        x <- build_expr("cast", x, options = cast_options(to_type = timestamp(timezone = tz)))
+      }
+      # POSIXct is of type double -> we need this to prevent going down the
+      # "double" branch
+      x <- x
+
+      # cast from character
+    } else if (call_binding("is.character", x)) {
+      # unit = 0L is the identifier for seconds in valid_time32_units
+      x <- build_expr("strptime", x, options = list(format = format, unit = 0L))
 
+      # 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(), but for
+      # integer-like values we can go via int32()
+      # https://issues.apache.org/jira/browse/ARROW-15798
+      # TODO revisit if arrow decides to support double -> date casting
+      x <- build_expr("cast", x, options = cast_options(to_type = int32()))
+    }
+    build_expr("cast", x, options = cast_options(to_type = date32()))
+  })

Review comment:
       @jonkeane You're 100% right. I hadn't realised how much all the rebasing and moving around have complicated this PR. I have decided to start over at #12738. 




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