From d2ff6e6ea032539c48de392111f50cff813fd391 Mon Sep 17 00:00:00 2001 From: Rok Date: Wed, 9 Feb 2022 20:06:58 +0100 Subject: [PATCH] TemporalAddDuration --- .../arrow/compute/kernels/scalar_arithmetic.cc | 16 ++++++++++++++++ .../compute/kernels/scalar_temporal_test.cc | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc b/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc index 9459e397332..c248ee4caf7 100644 --- a/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc +++ b/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc @@ -1568,6 +1568,7 @@ ArrayKernelExec ArithmeticExecFromOp(detail::GetTypeId get_id) { return KernelGenerator::Exec; case Type::UINT32: return KernelGenerator::Exec; + case Type::DURATION: case Type::INT64: case Type::TIMESTAMP: return KernelGenerator::Exec; @@ -2545,6 +2546,13 @@ void RegisterScalarArithmetic(FunctionRegistry* registry) { std::move(exec))); } + // Add add(duration, duration) -> duration + for (auto unit : TimeUnit::values()) { + InputType in_type(match::DurationTypeUnit(unit)); + auto exec = ArithmeticExecFromOp(Type::DURATION); + DCHECK_OK(add->AddKernel({in_type, in_type}, duration(unit), std::move(exec))); + } + DCHECK_OK(registry->AddFunction(std::move(add))); // ---------------------------------------------------------------------- @@ -2561,6 +2569,14 @@ void RegisterScalarArithmetic(FunctionRegistry* registry) { std::move(exec))); } + // Add add(duration, duration) -> duration + for (auto unit : TimeUnit::values()) { + InputType in_type(match::DurationTypeUnit(unit)); + auto exec = ArithmeticExecFromOp(Type::DURATION); + DCHECK_OK( + add_checked->AddKernel({in_type, in_type}, duration(unit), std::move(exec))); + } + DCHECK_OK(registry->AddFunction(std::move(add_checked))); // ---------------------------------------------------------------------- diff --git a/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc b/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc index fdd2876304f..1f18d71b3cf 100644 --- a/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc +++ b/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc @@ -1004,6 +1004,22 @@ TEST_F(ScalarTemporalTest, TestTemporalAddDateAndDuration) { } } +TEST_F(ScalarTemporalTest, TestTemporalAddDuration) { + for (auto op : {"add", "add_checked"}) { + for (auto u : TimeUnit::values()) { + auto unit = duration(u); + CheckScalarBinary(op, ArrayFromJSON(unit, times_s), + ArrayFromJSON(unit, seconds_between_time), + ArrayFromJSON(unit, times_s2)); + } + + auto seconds_1 = ArrayFromJSON(duration(TimeUnit::SECOND), R"([1, null])"); + auto milliseconds_2k = ArrayFromJSON(duration(TimeUnit::MILLI), R"([2000, null])"); + auto milliseconds_3k = ArrayFromJSON(duration(TimeUnit::MILLI), R"([3000, null])"); + CheckScalarBinary(op, seconds_1, milliseconds_2k, milliseconds_3k); + } +} + TEST_F(ScalarTemporalTest, TestTemporalSubtractDateAndDuration) { for (auto op : {"subtract", "subtract_checked"}) { std::string milliseconds_between_time_and_date =