Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cpp/src/arrow/compute/kernels/scalar_arithmetic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2536,12 +2536,31 @@ void RegisterScalarArithmetic(FunctionRegistry* registry) {
// ----------------------------------------------------------------------
auto add = MakeArithmeticFunction<Add>("add", &add_doc);
AddDecimalBinaryKernels<Add>("add", add.get());

// Add add(timestamp, duration) -> timestamp
for (auto unit : TimeUnit::values()) {
InputType in_type(match::TimestampTypeUnit(unit));
auto exec = ScalarBinary<TimestampType, DurationType, TimestampType, Add>::Exec;
DCHECK_OK(add->AddKernel({in_type, duration(unit)}, OutputType(FirstType),
std::move(exec)));
}

DCHECK_OK(registry->AddFunction(std::move(add)));

// ----------------------------------------------------------------------
auto add_checked =
MakeArithmeticFunctionNotNull<AddChecked>("add_checked", &add_checked_doc);
AddDecimalBinaryKernels<AddChecked>("add_checked", add_checked.get());

// Add add_checked(timestamp, duration) -> timestamp
for (auto unit : TimeUnit::values()) {
InputType in_type(match::TimestampTypeUnit(unit));
auto exec =
ScalarBinary<TimestampType, DurationType, TimestampType, AddChecked>::Exec;
DCHECK_OK(add_checked->AddKernel({in_type, duration(unit)}, OutputType(FirstType),
std::move(exec)));
}

DCHECK_OK(registry->AddFunction(std::move(add_checked)));

// ----------------------------------------------------------------------
Expand Down
28 changes: 28 additions & 0 deletions cpp/src/arrow/compute/kernels/scalar_temporal_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,34 @@ TEST_F(ScalarTemporalTest, TestTemporalDifference) {
}
}

TEST_F(ScalarTemporalTest, TestTemporalAddDateAndDuration) {
for (auto op : {"add", "add_checked"}) {
std::string milliseconds_between_date_and_time =
"[59000, 84203000, 3560000, 12800000, 3905000, 7810000, 11715000, 15620000, "
"19525000, 23430000, 27335000, 31240000, 35145000, 0, 0, 3723000, null]";
std::string microseconds_between_date_and_time =
"[59000000, 84203000000, 3560000000, 12800000000, 3905000000, 7810000000, "
"11715000000, 15620000000, 19525000000, 23430000000, 27335000000, 31240000000, "
"35145000000, 0, 0, 3723000000, null]";
auto dates32 = ArrayFromJSON(date32(), date32s);
auto dates64 = ArrayFromJSON(date64(), date64s);

auto durations_ms =
ArrayFromJSON(duration(TimeUnit::MILLI), milliseconds_between_date_and_time);
auto timestamps_ms =
ArrayFromJSON(timestamp(TimeUnit::MILLI), times_seconds_precision);
CheckScalarBinary(op, dates32, durations_ms, timestamps_ms);
CheckScalarBinary(op, dates64, durations_ms, timestamps_ms);

auto durations_us =
ArrayFromJSON(duration(TimeUnit::MICRO), microseconds_between_date_and_time);
auto timestamps_us =
ArrayFromJSON(timestamp(TimeUnit::MICRO), times_seconds_precision);
CheckScalarBinary(op, dates32, durations_us, timestamps_us);
CheckScalarBinary(op, dates64, durations_us, timestamps_us);
}
}

TEST_F(ScalarTemporalTest, TestTemporalSubtractDateAndDuration) {
for (auto op : {"subtract", "subtract_checked"}) {
std::string milliseconds_between_time_and_date =
Expand Down
4 changes: 2 additions & 2 deletions docs/source/cpp/compute.rst
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,9 @@ Mixed time resolution temporal inputs will be cast to finest input resolution.
+------------------+--------+----------------------------+----------------------------+-------+
| abs_checked | Unary | Numeric | Numeric | |
+------------------+--------+----------------------------+----------------------------+-------+
| add | Binary | Numeric | Numeric | \(1) |
| add | Binary | Numeric/Temporal | Numeric/Temporal | \(1) |
+------------------+--------+----------------------------+----------------------------+-------+
| add_checked | Binary | Numeric | Numeric | \(1) |
| add_checked | Binary | Numeric/Temporal | Numeric/Temporal | \(1) |
+------------------+--------+----------------------------+----------------------------+-------+
| divide | Binary | Numeric | Numeric | \(1) |
+------------------+--------+----------------------------+----------------------------+-------+
Expand Down