dragosmg commented on code in PR #12738:
URL: https://github.com/apache/arrow/pull/12738#discussion_r855901639
##########
r/R/dplyr-funcs-datetime.R:
##########
@@ -463,3 +463,56 @@ duration_from_chunks <- function(chunks) {
}
duration
}
+
+binding_as_date <- function(x,
+ format = NULL,
+ tryFormats = "%Y-%m-%d",
+ origin = "1970-01-01") {
+
+ 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 character
+ } else if (call_binding("is.character", x)) {
+ x <- binding_as_date_character(x, format, tryFormats)
+
+ # cast from numeric
+ } else if (call_binding("is.numeric", x)) {
+ x <- binding_as_date_numeric(x, origin)
+ }
+
+ build_expr("cast", x, options = cast_options(to_type = date32()))
+}
+
+binding_as_date_character <- function(x,
+ format = NULL,
+ tryFormats = "%Y-%m-%d") {
+ format <- format %||% tryFormats[[1]]
+ # unit = 0L is the identifier for seconds in valid_time32_units
+ build_expr("strptime", x, options = list(format = format, unit = 0L))
+}
+
+binding_as_date_numeric <- function(x,
+ origin = "1970-01-01") {
+
+ # 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
+ if (!call_binding("is.integer", x)) {
+ x <- build_expr("cast", x, options = cast_options(to_type = int32()))
+ }
+
+ if (origin != "1970-01-01") {
+ delta_in_sec <- call_binding("difftime", origin, "1970-01-01")
+ delta_in_sec <- build_expr("cast", delta_in_sec, options =
cast_options(to_type = int64()))
Review Comment:
Issues added as comments, but will also add them here:
* [ARROW-16253](https://issues.apache.org/jira/browse/ARROW-16253) - wrapper
function for the casting chain `double` -> `int64` -> `duration`
* [ARROW-15862](https://issues.apache.org/jira/browse/ARROW-15862) - support
in C++ casting from `int32` or `double` to `duration`
--
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]