Skip to content

fix(arrow-cast): floor when casting before the epoch to a coarser temporal unit - #12352

Open
lichuang wants to merge 1 commit into
apache:mainfrom
lichuang:issue-11602
Open

lichuang wants to merge 1 commit into
apache:mainfrom
lichuang:issue-11602

Conversation

@lichuang

@lichuang lichuang commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Casting a Timestamp to a coarser unit divides with /, which truncates toward zero. For an instant before the Unix epoch that rounds up to a later unit interval instead of staying inside the interval that contains it:

1969-12-31T23:59:59.999  ->  Timestamp(Second)  1970-01-01T00:00:00
1969-12-31T23:59:59.999  ->  Date32             1969-12-31

The Timestamp to Date32 cast has floored since #4211, so the two paths disagreed about which interval an instant belongs to. Date64 to Timestamp(Second) had the same problem.

This matters for date bucketing: a second bucket derived from a Timestamp could disagree with the same bucket derived from Date32, and a timestamp just before the epoch was pushed into the following second.

What changes are included in this PR?

  • Timestamp to a coarser Timestamp unit now uses div_euclid, so it floors. The constant-divisor lookup upstream added for this cast is preserved, so the compiler can still replace each division with a multiply and shifts.
  • Date64 to Timestamp(Second) now uses div_euclid.
  • The cast_with_options documentation now states that casting a Timestamp to a coarser unit floors, and is explicit that it matches casting a Timestamp to Date32.

Casts to a coarser Time32 or Time64 unit are unaffected because times are not negative. Duration is also left unchanged in this PR: a duration is an elapsed amount rather than an instant, so flooring is a separate question.

Date64 to Date32 already floors since #12258; this PR does not change it, but the new test asserts that Date64 to Timestamp(Second) agrees with it.

Are these changes tested?

  • test_cast_timestamp_to_coarser_unit_floors checks the positive and negative boundaries for Timestamp to Timestamp across nanosecond, microsecond, millisecond and second targets, and checks that the Date32 cast agrees on the day boundary.
  • test_cast_date64_negative_floors checks Date64 to Date32 and Date64 to Timestamp(Second) for values just before and after the epoch, including a whole day, and asserts both agree with the Timestamp to Date32 cast for the same instants.
  • test_cast_timestamp_and_duration_to_coarser_unit pinned truncation toward zero for both casts. It now expects Timestamp to floor and Duration to keep truncating, so the two behaviours stay distinct.

The full arrow-cast suite passes (79 unit tests and 370 tests in the cast integration test), as does arrow's cast test. Clippy with --all-features --all-targets -- -D warnings and cargo fmt --all -- --check pass.

Are there any significant user-facing changes?

Yes, one behaviour change: for timestamps before the Unix epoch, casting to a coarser unit now floors instead of truncating toward zero.

-1 ms    ->  -1 s      (was 0 s)
-1500 ms ->  -2 s      (was -1 s)

This aligns with the existing Timestamp to Date32 behaviour, but callers with pre-epoch data will observe different values. Timestamps at or after the epoch are unchanged. Duration casts are unchanged.

AI usage disclosure

This change and its tests were prepared with AI assistance. I reviewed the implementation and verified the tests, Clippy, and formatting locally.

@boring-cyborg boring-cyborg Bot added the arrow-kernels Crates that transform arrow data (e.g. compute, cast) label Nov 20, 2026
Comment thread arrow-cast/src/cast/mod.rs Outdated
/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval.
/// Casting a `Timestamp` to a coarser unit floors, so an instant before the Unix
/// epoch stays in the coarser interval that contains it, matching the `Date32` cast

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

matching the Date32 cast

this part is a bit confusing to state here, since its not clear what its referring to; is it cast to date32? from what?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed — the sentence now reads "as casting a Timestamp to Date32 already does", which names both the source and target types.

@Jefffrey Jefffrey added the bug Squash those bugs label Nov 22, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow-kernels Crates that transform arrow data (e.g. compute, cast) bug Squash those bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should casting a timestamp to a coarser unit floor instead of truncating toward zero?

2 participants