Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions r/R/dplyr-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,20 @@ get_stringr_pattern_options <- function(pattern) {
contains_regex <- function(string) {
grepl("[.\\|()[{^$*+?]", string)
}

nse_funcs$strptime <- function(x, format = "%Y-%m-%d %H:%M:%S", tz = NULL, unit = "ms") {
# Arrow uses unit for time parsing, strptime() does not.
# Arrow has no default option for strptime (format, unit),
# we suggest following format = "%Y-%m-%d %H:%M:%S", unit = MILLI/1L/"ms",
# (ARROW-12809)

# ParseTimestampStrptime currently ignores the timezone information (ARROW-12820).
# Stop if tz is provided.
if (is.character(tz)) {
arrow_not_supported("Time zone argument")
}

unit <- make_valid_time_unit(unit, c(valid_time64_units, valid_time32_units))

Expression$create("strptime", x, options = list(format = format, unit = unit))
}
7 changes: 7 additions & 0 deletions r/src/compute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ std::shared_ptr<arrow::compute::FunctionOptions> make_compute_options(
max_replacements);
}

if (func_name == "strptime") {
using Options = arrow::compute::StrptimeOptions;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does StrptimeOptions have a Defaults() method in C++? If so, we should call it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand correctly that in scalar_string.cc line 1744 would suggest StrptimeOptions do not have Defaults() in C++?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah looks like it. I made ARROW-12809 to evaluate whether that's correct, but for the purposes of the PR, it's fine.

return std::make_shared<Options>(
cpp11::as_cpp<std::string>(options["format"]),
cpp11::as_cpp<arrow::TimeUnit::type>(options["unit"]));
}

if (func_name == "split_pattern" || func_name == "split_pattern_regex") {
using Options = arrow::compute::SplitPatternOptions;
int64_t max_splits = -1;
Expand Down
75 changes: 75 additions & 0 deletions r/tests/testthat/test-dplyr-string-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,78 @@ test_that("edge cases in string detection and replacement", {
tibble(x = c("ABC"))
)
})

test_that("strptime", {

t_string <- tibble(x = c("2018-10-07 19:04:05", NA))
t_stamp <- tibble(x = c(lubridate::ymd_hms("2018-10-07 19:04:05"), NA))

expect_equal(
t_string %>%
Table$create() %>%
mutate(
x = strptime(x)
) %>%
collect(),
t_stamp,
check.tzone = FALSE
)

expect_equal(
t_string %>%
Table$create() %>%
mutate(
x = strptime(x, format = "%Y-%m-%d %H:%M:%S")
) %>%
collect(),
t_stamp,
check.tzone = FALSE
)

expect_equal(
t_string %>%
Table$create() %>%
mutate(
x = strptime(x, format = "%Y-%m-%d %H:%M:%S", unit = "ns")
) %>%
collect(),
t_stamp,
check.tzone = FALSE
)

expect_equal(
t_string %>%
Table$create() %>%
mutate(
x = strptime(x, format = "%Y-%m-%d %H:%M:%S", unit = "s")
) %>%
collect(),
t_stamp,
check.tzone = FALSE
)

tstring <- tibble(x = c("08-05-2008", NA))
tstamp <- tibble(x = c(strptime("08-05-2008", format = "%m-%d-%Y"), NA))

expect_equal(
tstring %>%
Table$create() %>%
mutate(
x = strptime(x, format = "%m-%d-%Y")
) %>%
collect(),
tstamp,
check.tzone = FALSE
)

})

test_that("errors in strptime", {
# Error when tz is passed

x <- Expression$field_ref("x")
expect_error(
nse_funcs$strptime(x, tz = "PDT"),
'Time zone argument not supported by Arrow'
)
})