jonkeane commented on a change in pull request #12622:
URL: https://github.com/apache/arrow/pull/12622#discussion_r826215484
##########
File path: r/R/dplyr-funcs-datetime.R
##########
@@ -187,6 +187,27 @@ register_bindings_datetime <- function() {
register_binding("date", function(x) {
build_expr("cast", x, options = list(to_type = date32()))
})
+ register_binding("make_date", function(year = 1970L, month = 1L, day = 1L) {
+ x <- call_binding("paste", year, month, day, sep = "-")
+ x <- call_binding("strptime", x, format = "%Y-%m-%d", unit = "s")
+ build_expr("cast", x, options = cast_options(to_type = date32()))
+ })
+ register_binding("make_datetime", function(year = 1970L,
+ month = 1L,
+ day = 1L,
+ hour = 0L,
+ min = 0L,
+ sec = 0,
+ tz = NULL) {
+ x <- call_binding("paste", year, month, day, hour, min, sec, sep = "-")
+ # ParseTimestampStrptime currently ignores the timezone information
(ARROW-12820).
+ # Stop if tz is provided.
+ if (is.character(tz)) {
+ arrow_not_supported("Time zone argument")
+ }
Review comment:
I wonder if we should set the default for tz to be `"UTC"` to match the
signature of `make_datetime()` and then we can error here if it's not that
value (and ignore it below).
Or does that approach leave something else out?
##########
File path: r/tests/testthat/test-dplyr-funcs-datetime.R
##########
@@ -974,3 +974,50 @@ test_that("date() errors with unsupported inputs", {
regexp = "Unsupported cast from double to date32 using function
cast_date32"
)
})
+
+test_that("make_date & make_datetime", {
+ set.seed(12345)
+ test_df <- tibble(
+ year = sample(1969:2069, 12),
+ month = 1:12,
+ day = sample(1:28, 12, replace = TRUE),
+ hour = sample(0:23, 12, replace = TRUE),
+ min = sample(0:59, 12),
+ sec = sample(0:59, 12)
+ )
Review comment:
I think I see what you're aiming here for the random data on the tests,
but I wonder if that's going to end up being more frustrating than it's worth?
Especially since with the seed, they will be somewhat deterministic anyway, but
debugging that locally might end up harder)
It might be better to use a selection of boundary cases you expect might do
funny things e.g. `year = c(1999, 1969, 2069, NA)` and same for other values.
(`expand.grid` might be your friend here).
##########
File path: r/R/dplyr-funcs-datetime.R
##########
@@ -187,6 +187,27 @@ register_bindings_datetime <- function() {
register_binding("date", function(x) {
build_expr("cast", x, options = list(to_type = date32()))
})
+ register_binding("make_date", function(year = 1970L, month = 1L, day = 1L) {
+ x <- call_binding("paste", year, month, day, sep = "-")
+ x <- call_binding("strptime", x, format = "%Y-%m-%d", unit = "s")
+ build_expr("cast", x, options = cast_options(to_type = date32()))
+ })
+ register_binding("make_datetime", function(year = 1970L,
+ month = 1L,
+ day = 1L,
+ hour = 0L,
+ min = 0L,
+ sec = 0,
+ tz = NULL) {
+ x <- call_binding("paste", year, month, day, hour, min, sec, sep = "-")
+ # ParseTimestampStrptime currently ignores the timezone information
(ARROW-12820).
+ # Stop if tz is provided.
+ if (is.character(tz)) {
+ arrow_not_supported("Time zone argument")
+ }
+
+ build_expr("strptime", x, options = list(format = "%Y-%m-%d-%H-%M-%S", unit
= 0L))
+ })
Review comment:
I wonder if it's possible (and advisable) to have `make_date` be defined
as ~ `cast(make_datetime(year, month, day, hour = 0L, min = 0L, sec = 0))`?
That will save a tiny bit of code (maybe it's not actually worth it?)
--
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]