From a962fb581052332da61d973ac4fc93bcacbaf28a Mon Sep 17 00:00:00 2001 From: DiadkoShmek <207617272+DiadkoShmek@users.noreply.github.com> Date: Wed, 4 Nov 2026 15:59:18 +0200 Subject: [PATCH] GH-55394: [C++][Compute] Check overflow when negating duration Signed-off-by: DiadkoShmek <207617272+DiadkoShmek@users.noreply.github.com> --- .../compute/kernels/scalar_arithmetic.cc | 2 +- .../compute/kernels/scalar_temporal_test.cc | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc b/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc index 53d4751b8c4f..fa770ccd238f 100644 --- a/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc +++ b/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc @@ -1815,7 +1815,7 @@ void RegisterScalarArithmetic(FunctionRegistry* registry) { // Add neg_checked(duration) -> duration for (auto unit : TimeUnit::values()) { - auto exec = ArithmeticExec(duration(unit)); + auto exec = ArithmeticExec(duration(unit)); DCHECK_OK( negate_checked->AddKernel({duration(unit)}, OutputType(duration(unit)), exec)); } diff --git a/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc b/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc index 86a81ffdd384..392d1283af33 100644 --- a/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc +++ b/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc @@ -3874,6 +3874,25 @@ TEST_F(ScalarTemporalTest, TestDurationTemporalWeekMultipleOverflow) { } } +TEST_F(ScalarTemporalTest, DurationNegateCheckedOverflow) { + for (auto unit : TimeUnit::values()) { + auto type = duration(unit); + auto overflow = ArrayFromJSON(type, "[-9223372036854775808]"); + ASSERT_RAISES(Invalid, CallFunction("negate_checked", {overflow})); + ASSERT_OK_AND_ASSIGN(auto scalar, overflow->GetScalar(0)); + ASSERT_RAISES(Invalid, CallFunction("negate_checked", {scalar})); + + auto values = ArrayFromJSON(type, + "[-9223372036854775807, 9223372036854775807, " + "null, -1, 0, 1]"); + auto expected = ArrayFromJSON(type, + "[9223372036854775807, -9223372036854775807, " + "null, 1, 0, -1]"); + CheckScalarUnary("negate_checked", values, expected); + CheckScalarUnary("negate", overflow, overflow); + } +} + TEST_F(ScalarTemporalTest, DurationUnaryArithmetics) { auto arr = ArrayFromJSON(duration(TimeUnit::SECOND), "[2, -1, null, 3, 0]"); CheckScalarUnary("negate", arr,