Skip to content

GH-50395: [C++] Support duration inputs in temporal rounding - #50675

Open
snigdhachoppac wants to merge 4 commits into
apache:mainfrom
snigdhachoppac:feat/duration-temporal-rounding
Open

GH-50395: [C++] Support duration inputs in temporal rounding#50675
snigdhachoppac wants to merge 4 commits into
apache:mainfrom
snigdhachoppac:feat/duration-temporal-rounding

Conversation

@snigdhachoppac

@snigdhachoppac snigdhachoppac commented Jul 28, 2026

Copy link
Copy Markdown

Rationale for this change

The ceil_temporal, floor_temporal, and round_temporal functions currently support date, time, and timestamp inputs, but not duration inputs.

What changes are included in this PR?

  • Add kernel registration for duration values with second, millisecond, microsecond, and nanosecond resolutions
  • Support rounding duration inputs using physical units through day
  • Treat week as seven physical days for duration inputs
  • Reject ambiguous calendar units such as month, quarter, and year
  • Reject calendar_based_origin for duration inputs
  • Add focused C++ tests covering all four duration resolutions, positive and negative values, null propagation, day and week rounding, and unsupported calendar behavior

Are these changes tested?

Yes.

  • arrow-compute-scalar-temporal-test: 55 tests passed
  • Applicable pre-commit C++ formatting and lint checks passed

Are there any user-facing changes?

Yes. Users can now pass duration values to ceil_temporal, floor_temporal, and round_temporal for supported physical units.

AI assistance

I used ChatGPT to help navigate the codebase and draft the initial implementation. I reviewed, revised, and tested the changes locally.

@snigdhachoppac
snigdhachoppac requested a review from pitrou as a code owner July 28, 2026 06:05
@github-actions github-actions Bot added the awaiting review Awaiting review label Jul 28, 2026
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #50395 has been automatically assigned in GitHub to PR creator.

@rok rok left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this @snigdhachoppac.

While these tests look visually ok, but I'd like to have a deeper verification of correctness. Could you please add a pandas integration tests in test_compute.py? See test_round_temporal as an example.

Thank you for sharing this PR was at least in part generated with ChatGPT.
Feel free to say no to this request: could you please share prompts you used to generate this PR? As a project we're dealing with a large inflow of generated PRs we have to triage and review and knowing which prompts were used to generate them could help us review said PR.

Comment thread cpp/src/arrow/compute/kernels/scalar_temporal_test.cc

CheckScalarUnary(
"ceil_temporal", day_values,
ArrayFromJSON(duration(TimeUnit::SECOND), "[0, 86400, 86400, 86400, 0, 0, null]"),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check values in these tests manually?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I derived them from fixed-duration semantics and the existing tie-breaking behavior, but I’ll independently verify each expected value while updating the tests.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot of downstream users that will potentially use this functionality. We want to be as sure about these as possible. So derived + human review + agent review would be great.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I manually re-derived the expected values using fixed-duration boundaries, including negative values and halfway cases. I also added a pandas integration test that independently compares Arrow’s results against pandas for nanoseconds, microseconds, milliseconds, seconds, minutes, hours, days, and weeks. All 8 parameterized integration cases and the full 55-test C++ temporal suite pass locally.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A philosophical question perhaps: how can verify that you, the physical person, did manually verify these and it's not just your agent trying to convince me?

@github-actions github-actions Bot added awaiting changes Awaiting changes and removed awaiting review Awaiting review labels Jul 28, 2026
@snigdhachoppac

Copy link
Copy Markdown
Author

Thanks for the review. I’ll add the pandas integration test, expand the C++ coverage to minutes and hours, and re-check the expected values before pushing an update.

I mainly used ChatGPT for codebase navigation, debugging, and drafting an initial test structure. I inspected the relevant implementation, adjusted the scope based on the issue discussion, reviewed the changes, and ran the targeted and full test suites locally. Some prompts I used include:
“Where are ceil_temporal, floor_temporal, and round_temporal registered in Arrow?”
“How are duration types represented in the C++ compute kernels?”
“Can you help me interpret this build/test failure and identify the relevant test target?”

@github-actions github-actions Bot added Component: Python awaiting change review Awaiting change review and removed awaiting changes Awaiting changes labels Jul 28, 2026
@snigdhachoppac

Copy link
Copy Markdown
Author

I pushed the requested updates with explicit minute/hour C++ coverage and a pandas integration test modeled after test_round_temporal. The full C++ temporal suite passes with 55 tests, and all 8 pandas integration cases pass.

@snigdhachoppac
snigdhachoppac requested a review from rok July 28, 2026 07:24
if (options.unit == CalendarUnit::WEEK) {
RoundTemporalOptions duration_options = options;
duration_options.unit = CalendarUnit::DAY;
duration_options.multiple *= 7;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pitrou is assigning multiple here ok?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, probably? There is a slight risk of overflow and we may want to guard for that, but that sounds extremely unlikely as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snigdhachoppac could you add an overflow guard such as this one?

int64_t product = 1;
for (const auto dim : shape) {
if (MultiplyWithOverflow(product, dim, &product)) {
return Status::Invalid(
"Product of tensor shape dimensions would not fit in 64-bit integer");
}
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a checked multiplication using MultiplyWithOverflow before converting week multiples to days, along with regression coverage for ceil_temporal, floor_temporal, and round_temporal. The targeted tests, full 56-test temporal C++ suite, and pandas integration tests pass locally.

@rok

rok commented Jul 28, 2026

Copy link
Copy Markdown
Member

As far as I'm concerned this PR could be merged, but I would like a second review. @pitrou could you take a look please?

@github-actions github-actions Bot added awaiting changes Awaiting changes awaiting merge Awaiting merge and removed awaiting change review Awaiting change review awaiting changes Awaiting changes labels Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants