rok commented on code in PR #12154:
URL: https://github.com/apache/arrow/pull/12154#discussion_r844619826
##########
r/R/dplyr-funcs-datetime.R:
##########
@@ -166,6 +166,73 @@ register_bindings_datetime <- function() {
(inherits(x, "Expression") && x$type_id() %in% Type[c("TIMESTAMP")])
})
+ register_binding("round_date", function(x, unit = "second",
+ week_start =
getOption("lubridate.week.start", 7)) {
+ opts <- parse_period_unit(unit)
+ if (opts$unit == 7L) {
+ if (week_start == 7) { # Sunday
+ opts$week_starts_monday <- 0L
+ return(Expression$create("round_temporal", x, options = opts))
+
+ } else if (week_start == 1) { # Monday
+ opts$week_starts_monday <- 1L
+ return(Expression$create("round_temporal", x, options = opts))
+
+ } else { # other values of week_start
+
+ # create a duration object as an offset
+ shift <- build_expr(
+ "cast",
+ Scalar$create((as.integer(week_start) - 1L) * 86400L, int64()),
+ options = cast_options(to_type = duration(unit = "s"))
+ )
+
+ # add the offset, round, then subtract the offset
+ # [throws error: add_checked has no kernel for date32-array and
duration(s)-scalar]
+ interim <- build_expr("round_temporal", x + shift, options = opts)
+ return(interim - shift)
Review Comment:
Or try:
```suggestion
# create a duration object as an offset
shift <- build_expr(
"cast",
Scalar$create((as.integer(week_start) - 1L) * 86400000L, int64()),
options = cast_options(to_type = duration(unit = "ms"))
)
# add the offset, round, then subtract the offset
# [throws error: add_checked has no kernel for date32-array and
duration(s)-scalar]
interim <- build_expr("round_temporal", x + shift, options = opts)
return(interim - shift)
```
This should sidestep ARROW-16060 without the need to rebase.
--
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]