dragosmg commented on a change in pull request #12738: URL: https://github.com/apache/arrow/pull/12738#discussion_r837595683
########## File path: r/R/dplyr-funcs-type.R ########## @@ -114,14 +113,74 @@ register_bindings_type_cast <- function() { # 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() + # 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_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") + } + + # assume format is ISO if unspecified (to align with lubridate::as_date) + if (is.null(format)) { + format <- "%Y-%m-%d" + } + + if (call_binding("is.Date", x)) { + return(x) + + # cast from POSIXct + } else if (call_binding("is.POSIXct", x)) { + 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 - abort("`as.Date()` with double/float is not supported in Arrow") + x <- build_expr("cast", x, options = cast_options(to_type = int32())) } build_expr("cast", x, options = cast_options(to_type = date32())) }) Review comment: Done -- 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