Repository navigation
Conversation
Jefffrey
reviewed
Nov 22, 2026
| /// * `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 |
Contributor
There was a problem hiding this comment.
matching the
Date32cast
this part is a bit confusing to state here, since its not clear what its referring to; is it cast to date32? from what?
Contributor
Author
There was a problem hiding this comment.
Fixed — the sentence now reads "as casting a Timestamp to Date32 already does", which names both the source and target types.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
Casting a
Timestampto 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:The
TimestamptoDate32cast has floored since #4211, so the two paths disagreed about which interval an instant belongs to.Date64toTimestamp(Second)had the same problem.This matters for date bucketing: a second bucket derived from a
Timestampcould disagree with the same bucket derived fromDate32, and a timestamp just before the epoch was pushed into the following second.What changes are included in this PR?
Timestampto a coarserTimestampunit now usesdiv_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.Date64toTimestamp(Second)now usesdiv_euclid.cast_with_optionsdocumentation now states that casting aTimestampto a coarser unit floors, and is explicit that it matches casting aTimestamptoDate32.Casts to a coarser
Time32orTime64unit are unaffected because times are not negative.Durationis also left unchanged in this PR: a duration is an elapsed amount rather than an instant, so flooring is a separate question.Date64toDate32already floors since #12258; this PR does not change it, but the new test asserts thatDate64toTimestamp(Second)agrees with it.Are these changes tested?
test_cast_timestamp_to_coarser_unit_floorschecks the positive and negative boundaries forTimestamptoTimestampacross nanosecond, microsecond, millisecond and second targets, and checks that theDate32cast agrees on the day boundary.test_cast_date64_negative_floorschecksDate64toDate32andDate64toTimestamp(Second)for values just before and after the epoch, including a whole day, and asserts both agree with theTimestamptoDate32cast for the same instants.test_cast_timestamp_and_duration_to_coarser_unitpinned truncation toward zero for both casts. It now expectsTimestampto floor andDurationto keep truncating, so the two behaviours stay distinct.The full
arrow-castsuite passes (79 unit tests and 370 tests in thecastintegration test), as doesarrow'scasttest. Clippy with--all-features --all-targets -- -D warningsandcargo fmt --all -- --checkpass.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.
This aligns with the existing
TimestamptoDate32behaviour, but callers with pre-epoch data will observe different values. Timestamps at or after the epoch are unchanged.Durationcasts 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.