rok commented on code in PR #12154:
URL: https://github.com/apache/arrow/pull/12154#discussion_r844596724


##########
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:
   With this test:
   ```diff
   diff --git a/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc 
b/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc
   index f8314163c..8b662fd57 100644
   --- a/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc
   +++ b/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc
   @@ -1076,6 +1076,24 @@ TEST_F(ScalarTemporalTest, 
TestTemporalAddDateAndDuration) {
            ArrayFromJSON(timestamp(TimeUnit::MICRO), times_seconds_precision);
        CheckScalarBinaryCommutative(op, dates32, durations_us, timestamps_us);
        CheckScalarBinaryCommutative(op, dates64, durations_us, timestamps_us);
   +
   +    const char* floor_1_day =
   +        R"(["1970-01-01", "2000-02-29", "1899-01-01", "2033-05-18", 
"2020-01-01",
   +          "2019-12-31", "2019-12-30", "2009-12-31", "2010-01-01", 
"2010-01-03",
   +          "2010-01-04", "2006-01-01", "2005-12-31", "2008-12-28", 
"2008-12-29",
   +          "2012-01-01", null])";
   +    EXPECT_THAT(
   +        CallFunction(op, {dates32, 
ScalarFromJSON(duration(TimeUnit::SECOND), "0")}),
   +        ResultWith(ArrayFromJSON(timestamp(TimeUnit::SECOND), 
floor_1_day)));
   +    EXPECT_THAT(
   +        CallFunction(op, {ScalarFromJSON(duration(TimeUnit::MILLI), "0"), 
dates32}),
   +        ResultWith(ArrayFromJSON(timestamp(TimeUnit::MILLI), floor_1_day)));
   +    EXPECT_THAT(
   +        CallFunction(op, {dates64, 
ScalarFromJSON(duration(TimeUnit::MICRO), "0")}),
   +        ResultWith(ArrayFromJSON(timestamp(TimeUnit::MICRO), floor_1_day)));
   +    EXPECT_THAT(
   +        CallFunction(op, {ScalarFromJSON(duration(TimeUnit::NANO), "0"), 
dates64}),
   +        ResultWith(ArrayFromJSON(timestamp(TimeUnit::NANO), floor_1_day)));
      }
    }
   ```
   
   I get:
   ```
   NotImplemented: Function 'add' has no kernel matching input types 
(array[date32[day]], scalar[duration[s]])
   /Users/rok/Documents/repos/arrow/cpp/src/arrow/compute/function.cc:231  
DispatchBest(&inputs), whose error "NotImplemented: Function 'add' has no 
kernel matching input types (array[date32[day]], scalar[duration[s]])
   ```
   
   Which is probably due to 
[ARROW-16060](https://issues.apache.org/jira/browse/ARROW-16060), so if you 
rebase this will probably be fixed.



-- 
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]

Reply via email to