Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.3k
ARROW-15800 [R] bindings for lubridate::as_date() and lubridate::as_datetime() - abandoned#12677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
713b2ed
added integerish test for `as.Date()`
dragosmg 256e97a
add integerish support for `as.Date()`
dragosmg ec99da3
replace `compare_dplyr_binding()` with `expect_error()`
dragosmg 7571fc0
added the `as_date()` binding
dragosmg 11e99bd
if `format` is unspecified, assume ISO
dragosmg 071d68d
unit tests for `as_date()`
dragosmg 30ac13d
update NEWS
dragosmg c9fb23a
moved the `as_date()` binding to _dplyr-funcs-datetime_
dragosmg 67c56b5
moved `as_date()` tests to _test-dplyr-funcs-datetime_
dragosmg 517af04
moved `as.Date()` & tests to dplyr-funcs-datetime
dragosmg c53a8c8
gymnastics with the location of the bindings (in prep for rebase)
dragosmg 08e629f
first pass at `as_datetime()`
dragosmg 14bf8ea
lint + reorg
dragosmg 7cd97c9
`as_datetime()` binding
dragosmg 453199c
improvements to `as_datetime()`
dragosmg 99cea4a
unit test first step
dragosmg 76803ee
update
dragosmg b496c12
moved datetime-related tests to `test-dplyr-funcs-datetime.R`
dragosmg a219939
move datetime bindings to ...-funcs-datetime
dragosmg 759c2fd
create a new set of bindings (`datetime_helpers`)
dragosmg 4888ab9
casting `x` and `delta` to `int64()` + updated unit tests
dragosmg 8d8b6ab
test with `Pacific/Marquesas` and skip on Windows
dragosmg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -191,6 +191,70 @@ register_bindings_datetime <- function() { | ||
| } | ||
| register_bindings_duration <- function() { | ||
| register_binding("difftime", function(time1, | ||
| time2, | ||
| tz, | ||
| units = "secs") { | ||
| if (units != "secs") { | ||
| abort("`difftime()` with units other than `secs` not supported in Arrow") | ||
| } | ||
| if (!missing(tz)) { | ||
| warn("`tz` argument is not supported in Arrow, so it will be ignored") | ||
| } | ||
| # cast to timestamp if time1 and time2 are not dates or timestamp expressions | ||
| # (the subtraction of which would output a `duration`) | ||
| if (!call_binding("is.instant", time1)) { | ||
| time1 <- build_expr("cast", time1, options = cast_options(to_type = timestamp())) | ||
| } | ||
| if (!call_binding("is.instant", time2)) { | ||
| time2 <- build_expr("cast", time2, options = cast_options(to_type = timestamp())) | ||
| } | ||
| # we need to go build the subtract expression instead of `time1 - time2` to | ||
| # prevent complaints when we try to subtract an R object from an Expression | ||
| subtract_output <- build_expr("-", time1, time2) | ||
| build_expr("cast", subtract_output, options = cast_options(to_type = duration("s"))) | ||
| }) | ||
| register_binding("as.difftime", function(x, | ||
| format = "%X", | ||
| units = "secs") { | ||
| # windows doesn't seem to like "%X" | ||
| if (format == "%X" & tolower(Sys.info()[["sysname"]]) == "windows") { | ||
| format <- "%H:%M:%S" | ||
| } | ||
| if (units != "secs") { | ||
| abort("`as.difftime()` with units other than 'secs' not supported in Arrow") | ||
| } | ||
| if (call_binding("is.character", x)) { | ||
| x <- build_expr("strptime", x, options = list(format = format, unit = 0L)) | ||
| # complex casting only due to cast type restrictions: time64 -> int64 -> duration(us) | ||
| # and then we cast to duration ("s") at the end | ||
| x <- x$cast(time64("us"))$cast(int64())$cast(duration("us")) | ||
| } | ||
| # numeric -> duration not supported in Arrow yet so we use int64() as an | ||
| # intermediate step | ||
| # TODO revisit if https://issues.apache.org/jira/browse/ARROW-15862 results | ||
| # in numeric -> duration support | ||
| if (call_binding("is.numeric", x)) { | ||
| # coerce x to be int64(). it should work for integer-like doubles and fail | ||
| # for pure doubles | ||
| # if we abort for all doubles, we risk erroring in cases in which | ||
| # coercion to int64() would work | ||
| x <- build_expr("cast", x, options = cast_options(to_type = int64())) | ||
| } | ||
| build_expr("cast", x, options = cast_options(to_type = duration(unit = "s"))) | ||
| }) | ||
| } | ||
| register_bindings_datetime_helpers <- function() { | ||
| register_binding("make_datetime", function(year = 1970L, | ||
| month = 1L, | ||
| day = 1L, | ||
| @@ -239,66 +303,108 @@ register_bindings_duration <- function() { | ||
| tz = "UTC") { | ||
| call_binding("make_datetime", year, month, day, hour, min, sec, tz) | ||
| }) | ||
| register_binding("difftime", function(time1, | ||
| time2, | ||
| tz, | ||
| units = "secs") { | ||
| if (units != "secs") { | ||
| abort("`difftime()` with units other than `secs` not supported in Arrow") | ||
| } | ||
| register_binding("as.Date", function(x, | ||
| format = NULL, | ||
| tryFormats = "%Y-%m-%d", | ||
| origin = "1970-01-01", | ||
| tz = "UTC") { | ||
| if (!missing(tz)) { | ||
| warn("`tz` argument is not supported in Arrow, so it will be ignored") | ||
| # 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") | ||
| } | ||
| # cast to timestamp if time1 and time2 are not dates or timestamp expressions | ||
| # (the subtraction of which would output a `duration`) | ||
| if (!call_binding("is.instant", time1)) { | ||
| time1 <- build_expr("cast", time1, options = cast_options(to_type = timestamp(timezone = "UTC"))) | ||
| # this could be improved with tryFormats once strptime returns NA and we | ||
| # can use coalesce - https://issues.apache.org/jira/browse/ARROW-15659 | ||
| # TODO revisit once https://issues.apache.org/jira/browse/ARROW-15659 is done | ||
| if (is.null(format) && length(tryFormats) > 1) { | ||
| abort("`as.Date()` with multiple `tryFormats` is not supported in Arrow") | ||
| } | ||
| if (!call_binding("is.instant", time2)) { | ||
| time2 <- build_expr("cast", time2, options = cast_options(to_type = timestamp(timezone = "UTC"))) | ||
| } | ||
| if (call_binding("is.Date", x)) { | ||
| return(x) | ||
| # we need to go build the subtract expression instead of `time1 - time2` to | ||
| # prevent complaints when we try to subtract an R object from an Expression | ||
| subtract_output <- build_expr("-", time1, time2) | ||
| build_expr("cast", subtract_output, options = cast_options(to_type = duration("s"))) | ||
| # cast from POSIXct | ||
| } else if (call_binding("is.POSIXct", x)) { | ||
| # base::as.Date() first converts to the desired timezone and then extracts | ||
| # the date, which is why we need to go through timestamp() first | ||
| x <- build_expr("cast", x, options = cast_options(to_type = timestamp(timezone = tz))) | ||
| # cast from character | ||
| } else if (call_binding("is.character", x)) { | ||
| format <- format %||% tryFormats[[1]] | ||
| # 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 | ||
| x <- build_expr("cast", x, options = cast_options(to_type = int32())) | ||
| } | ||
| build_expr("cast", x, options = cast_options(to_type = date32())) | ||
| }) | ||
| register_binding("as.difftime", function(x, | ||
| format = "%X", | ||
| units = "secs") { | ||
| # windows doesn't seem to like "%X" | ||
| if (format == "%X" & tolower(Sys.info()[["sysname"]]) == "windows") { | ||
| format <- "%H:%M:%S" | ||
| 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") | ||
| } | ||
| if (units != "secs") { | ||
| abort("`as.difftime()` with units other than 'secs' 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.character", x)) { | ||
| x <- build_expr("strptime", x, options = list(format = format, unit = 0L)) | ||
| # complex casting only due to cast type restrictions: time64 -> int64 -> duration(us) | ||
| # and then we cast to duration ("s") at the end | ||
| x <- x$cast(time64("us"))$cast(int64())$cast(duration("us")) | ||
| } | ||
| if (call_binding("is.Date", x)) { | ||
| return(x) | ||
| # numeric -> duration not supported in Arrow yet so we use int64() as an | ||
| # intermediate step | ||
| # TODO revisit if https://issues.apache.org/jira/browse/ARROW-15862 results | ||
| # in numeric -> duration support | ||
| # cast from POSIXct | ||
| } else if (call_binding("is.POSIXct", x)) { | ||
| # this is where as_date() differs from as.Date() | ||
| 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 | ||
| x <- build_expr("cast", x, options = cast_options(to_type = int32())) | ||
| } | ||
| build_expr("cast", x, options = cast_options(to_type = date32())) | ||
| }) | ||
| register_binding("as_datetime", function(x, | ||
| origin = "1970-01-01", | ||
| tz = "UTC") { | ||
| if (call_binding("is.numeric", x)) { | ||
| # coerce x to be int64(). it should work for integer-like doubles and fail | ||
| # for pure doubles | ||
| # if we abort for all doubles, we risk erroring in cases in which | ||
| # coercion to int64() would work | ||
| delta <- call_binding("difftime", origin, "1970-01-01") | ||
| delta <- build_expr("cast", delta, options = cast_options(to_type = int64())) | ||
| x <- build_expr("cast", x, options = cast_options(to_type = int64())) | ||
| output <- build_expr("+", x, delta) | ||
| output <- build_expr("cast", output, options = cast_options(to_type = timestamp())) | ||
| } else { | ||
| output <- build_expr("cast", x, options = cast_options(to_type = timestamp())) | ||
| } | ||
| build_expr("cast", x, options = cast_options(to_type = duration(unit = "s"))) | ||
| build_expr("assume_timezone", output, options = list(timezone = tz)) | ||
dragosmg marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.