diff --git a/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc b/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc index 86a81ffdd38..a45a00a2442 100644 --- a/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc +++ b/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc @@ -2475,6 +2475,13 @@ TEST_F(ScalarTemporalTest, TestCeilTemporal) { CheckScalarUnary(op, unit, times, unit, ceil_15_months, &round_to_15_months); CheckScalarUnary(op, unit, times, unit, ceil_15_quarters, &round_to_15_quarters); CheckScalarUnary(op, unit, times, unit, ceil_15_years, &round_to_15_years); + + for (const int multiple : {0, -1}) { + RoundTemporalOptions invalid_options(multiple, CalendarUnit::SECOND); + EXPECT_RAISES_WITH_MESSAGE_THAT( + Invalid, ::testing::HasSubstr("Rounding multiple must be positive"), + CallFunction(op, {ArrayFromJSON(unit, times)}, &invalid_options)); + } } TEST_F(ScalarTemporalTestStrictCeil, TestCeilTemporalStrictCeil) { @@ -3052,6 +3059,13 @@ TEST_F(ScalarTemporalTest, TestFloorTemporal) { CheckScalarUnary(op, unit, times, unit, floor_15_months, &round_to_15_months); CheckScalarUnary(op, unit, times, unit, floor_15_quarters, &round_to_15_quarters); CheckScalarUnary(op, unit, times, unit, floor_15_years, &round_to_15_years); + + for (const int multiple : {0, -1}) { + RoundTemporalOptions invalid_options(multiple, CalendarUnit::SECOND); + EXPECT_RAISES_WITH_MESSAGE_THAT( + Invalid, ::testing::HasSubstr("Rounding multiple must be positive"), + CallFunction(op, {ArrayFromJSON(unit, times)}, &invalid_options)); + } } TEST_F(ScalarTemporalTestMultipleSinceGreaterUnit, FloorUTC) { @@ -3444,6 +3458,13 @@ TEST_F(ScalarTemporalTest, TestRoundTemporal) { CheckScalarUnary(op, unit, times, unit, round_15_months, &round_to_15_months); CheckScalarUnary(op, unit, times, unit, round_15_quarters, &round_to_15_quarters); CheckScalarUnary(op, unit, times, unit, round_15_years, &round_to_15_years); + + for (const int multiple : {0, -1}) { + RoundTemporalOptions invalid_options(multiple, CalendarUnit::SECOND); + EXPECT_RAISES_WITH_MESSAGE_THAT( + Invalid, ::testing::HasSubstr("Rounding multiple must be positive"), + CallFunction(op, {ArrayFromJSON(unit, times)}, &invalid_options)); + } } TEST_F(ScalarTemporalTest, TestCeilFloorRoundTemporalBrussels) { diff --git a/cpp/src/arrow/compute/kernels/scalar_temporal_unary.cc b/cpp/src/arrow/compute/kernels/scalar_temporal_unary.cc index 74a29081ae8..cab9deb8ad5 100644 --- a/cpp/src/arrow/compute/kernels/scalar_temporal_unary.cc +++ b/cpp/src/arrow/compute/kernels/scalar_temporal_unary.cc @@ -80,6 +80,15 @@ using StrptimeState = OptionsWrapper; using AssumeTimezoneState = OptionsWrapper; using RoundTemporalState = OptionsWrapper; +Result> Init(KernelContext* ctx, + const KernelInitArgs& args) { + ARROW_ASSIGN_OR_RAISE(auto state, RoundTemporalState::Init(ctx, args)); + if (RoundTemporalState::Get(*state).multiple <= 0) { + return Status::Invalid("Rounding multiple must be positive"); + } + return state; +} + const std::shared_ptr& IsoCalendarType() { static auto type = struct_({field("iso_year", int64()), field("iso_week", int64()), field("iso_day_of_week", int64())}); @@ -2050,19 +2059,19 @@ void RegisterScalarTemporalUnary(FunctionRegistry* registry) { UnaryTemporalFactory:: Make( "floor_temporal", OutputType(FirstType), floor_temporal_doc, - &default_round_temporal_options, RoundTemporalState::Init); + &default_round_temporal_options, Init); DCHECK_OK(registry->AddFunction(std::move(floor_temporal))); auto ceil_temporal = UnaryTemporalFactory:: Make( "ceil_temporal", OutputType(FirstType), ceil_temporal_doc, - &default_round_temporal_options, RoundTemporalState::Init); + &default_round_temporal_options, Init); DCHECK_OK(registry->AddFunction(std::move(ceil_temporal))); auto round_temporal = UnaryTemporalFactory:: Make( "round_temporal", OutputType(FirstType), round_temporal_doc, - &default_round_temporal_options, RoundTemporalState::Init); + &default_round_temporal_options, Init); DCHECK_OK(registry->AddFunction(std::move(round_temporal))); } diff --git a/python/pyarrow/tests/test_compute.py b/python/pyarrow/tests/test_compute.py index 0e0260808d6..d62669b4aac 100644 --- a/python/pyarrow/tests/test_compute.py +++ b/python/pyarrow/tests/test_compute.py @@ -2982,6 +2982,12 @@ def test_round_temporal(unit): ts_zoned = ts.dt.tz_localize("UTC").dt.tz_convert(timezone) _check_temporal_rounding(ts_zoned, values, unit) + for fn in (pc.ceil_temporal, pc.floor_temporal, pc.round_temporal): + for multiple in (0, -1): + with pytest.raises(pa.ArrowInvalid, + match="Rounding multiple must be positive"): + fn(pa.array(ts), multiple=multiple, unit=unit) + @pytest.mark.parametrize( ("unit", "base_frequency", "round_frequency"),