lidavidm commented on a change in pull request #12139:
URL: https://github.com/apache/arrow/pull/12139#discussion_r795705061



##########
File path: cpp/src/arrow/compute/kernels/scalar_arithmetic.cc
##########
@@ -2090,6 +2146,34 @@ std::shared_ptr<ScalarFunction> 
MakeArithmeticFunctionFloatingPointNotNull(
   return func;
 }
 
+template <template <bool, int64_t> class Op>
+void AddArithmeticFunctionTimeDurations(std::shared_ptr<ScalarFunction> func) {
+  // Add subtract(time32, duration) -> time32
+  TimeUnit::type unit = TimeUnit::SECOND;
+  auto exec_1 = ScalarBinary<Time32Type, Time32Type, DurationType, Op<true, 
86400>>::Exec;
+  DCHECK_OK(func->AddKernel({match::Time32TypeUnit(unit), duration(unit)},
+                            OutputType(FirstType), std::move(exec_1)));
+
+  unit = TimeUnit::MILLI;
+  auto exec_2 =
+      ScalarBinary<Time32Type, Time32Type, DurationType, Op<true, 
86400000>>::Exec;
+  DCHECK_OK(func->AddKernel({match::Time32TypeUnit(unit), duration(unit)},
+                            OutputType(FirstType), std::move(exec_2)));
+
+  // Add subtract(time64, duration) -> time64
+  unit = TimeUnit::MICRO;
+  auto exec_3 =
+      ScalarBinary<Time64Type, Time64Type, DurationType, Op<false, 
86400000000>>::Exec;
+  DCHECK_OK(func->AddKernel({match::Time64TypeUnit(unit), duration(unit)},
+                            OutputType(FirstType), std::move(exec_3)));
+
+  unit = TimeUnit::NANO;
+  auto exec_4 =
+      ScalarBinary<Time64Type, Time64Type, DurationType, Op<false, 
86400000000000>>::Exec;
+  DCHECK_OK(func->AddKernel({match::Time64TypeUnit(unit), duration(unit)},
+                            OutputType(FirstType), std::move(exec_4)));

Review comment:
       nit, but is there any advantage to using the type matcher + computed 
output type vs the literal types?

##########
File path: cpp/src/arrow/compute/kernels/scalar_arithmetic.cc
##########
@@ -214,6 +214,62 @@ struct SubtractChecked {
   }
 };
 
+template <bool is_32bit, int64_t multiple>
+struct SubtractTimeDuration {
+  template <typename T, typename Arg0, typename Arg1>
+  static enable_if_t<is_32bit, T> Call(KernelContext*, Arg0 left, Arg1 right,
+                                       Status* st) {
+    T result = arrow::internal::SafeSignedSubtract(left, 
static_cast<T>(right));
+    if (result < 0) {
+      *st = Status::Invalid(result, " is not within the acceptable range of ", 
"[0, ",
+                            multiple, ") s");
+    }

Review comment:
       Since we could subtract a negative duration, we should also check if the 
output is larger than the maximum acceptable value.

##########
File path: cpp/src/arrow/compute/kernels/scalar_arithmetic.cc
##########
@@ -214,6 +214,62 @@ struct SubtractChecked {
   }
 };
 
+template <bool is_32bit, int64_t multiple>
+struct SubtractTimeDuration {
+  template <typename T, typename Arg0, typename Arg1>
+  static enable_if_t<is_32bit, T> Call(KernelContext*, Arg0 left, Arg1 right,
+                                       Status* st) {
+    T result = arrow::internal::SafeSignedSubtract(left, 
static_cast<T>(right));
+    if (result < 0) {
+      *st = Status::Invalid(result, " is not within the acceptable range of ", 
"[0, ",
+                            multiple, ") s");
+    }

Review comment:
       Alternatively - maybe this arithmetic should wrap around? Does that make 
any sense?




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