Skip to content
Open
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
21 changes: 21 additions & 0 deletions cpp/src/arrow/compute/kernels/scalar_temporal_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 12 additions & 3 deletions cpp/src/arrow/compute/kernels/scalar_temporal_unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ using StrptimeState = OptionsWrapper<StrptimeOptions>;
using AssumeTimezoneState = OptionsWrapper<AssumeTimezoneOptions>;
using RoundTemporalState = OptionsWrapper<RoundTemporalOptions>;

Result<std::unique_ptr<KernelState>> 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<DataType>& IsoCalendarType() {
static auto type = struct_({field("iso_year", int64()), field("iso_week", int64()),
field("iso_day_of_week", int64())});
Expand Down Expand Up @@ -2050,19 +2059,19 @@ void RegisterScalarTemporalUnary(FunctionRegistry* registry) {
UnaryTemporalFactory<FloorTemporal, TemporalComponentExtractRound, TimestampType>::
Make<WithDates, WithTimes, WithTimestamps, WithDurations>(
"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<CeilTemporal, TemporalComponentExtractRound, TimestampType>::
Make<WithDates, WithTimes, WithTimestamps, WithDurations>(
"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<RoundTemporal, TemporalComponentExtractRound, TimestampType>::
Make<WithDates, WithTimes, WithTimestamps, WithDurations>(
"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)));
}

Expand Down
6 changes: 6 additions & 0 deletions python/pyarrow/tests/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Loading