dragosmg commented on a change in pull request #12433: URL: https://github.com/apache/arrow/pull/12433#discussion_r816601524
########## File path: r/tests/testthat/test-dplyr-funcs-datetime.R ########## @@ -801,3 +801,94 @@ test_that("dst extracts daylight savings time correctly", { test_df ) }) + +test_that("date works in arrow", { + # https://issues.apache.org/jira/browse/ARROW-13168 + skip_on_os("windows") + # this date is specific since lubridate::date() is different from base::as.Date() + # since as.Date returns the UTC date and date() doesn't + test_df <- tibble( + a = as.POSIXct(c("2012-03-26 23:12:13", NA), tz = "America/New_York")) + r_date_object <- lubridate::ymd_hms("2012-03-26 23:12:13") + + # we can't (for now) use namespacing, so we need to make sure lubridate::date() + # and not base::date() is being used + # TODO remove once https://issues.apache.org/jira/browse/ARROW-14575 is done + date <- lubridate::date + compare_dplyr_binding( + .input %>% + mutate(a_date = date(a)) %>% + collect(), + test_df + ) + + compare_dplyr_binding( + .input %>% + mutate(a_date_base = as.Date(a)) %>% + collect(), + test_df + ) + + compare_dplyr_binding( + .input %>% + mutate(b = date(r_date_object)) %>% + collect(), + test_df + ) + + compare_dplyr_binding( + .input %>% + mutate(b_base = as.Date(r_date_object)) %>% + collect(), + test_df + ) +}) + +test_that("date() errors with unsupported inputs", { + test_df <- tibble::tibble( + posixct_var = as.POSIXct("2022-02-25 00:00:01", tz = "Europe/London"), + date_var = as.Date("2022-02-25"), + character_ymd_var = "2022-02-25 00:00:01", + character_ydm_var = "2022/25/02 00:00:01", + integer_var = 32L, + double_var = 34.56, + boolean_var = TRUE + ) + + # date from integer supported in arrow (similar to base::as.Date()), but in + # Arrow it assumes a fixed origin "1970-01-01" + expect_equal( + test_df %>% + arrow_table() %>% + select(integer_var) %>% + mutate(date_int = date(integer_var)) %>% + collect(), + tibble(integer_var = 32L, + date_int = as.Date("1970-02-02")) + ) Review comment: Done. Moved it to the non-error behaviour. That is a case where `lubridate::date()` would error without a value for `origin`. Technically we could decide to error when origin is not present and mimic the lubridate behaviour closer (I think that was my initial intention, hence that test being in the error {testthat} block) ``` r lubridate::date(32L) #> Error in as.POSIXlt.numeric(x, tz = tz(x)): 'origin' must be supplied ``` <sup>Created on 2022-03-01 by the [reprex package](https://reprex.tidyverse.org) (v2.0.1)</sup> -- 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