You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "ThilakShekharShriyan (via GitHub)" <gi...@apache.org> on 2026/10/09 08:00:04 UTC
[PR] fix: bin date_bin month strides outside chrono's range [datafusion]
ThilakShekharShriyan opened a new pull request, #26153:
URL: https://github.com/apache/datafusion/pull/26153
## Which issue does this PR close?
Closes #25855.
## Rationale for this change
`date_bin` with a month stride returned `NULL` for timestamps that sit outside `DateTime<Utc>`, even when the month start still fits in the output type. Second, millisecond, and microsecond timestamps can be wider than chrono's range. A month bin should be `NULL` only when that bin itself does not fit.
## What changes are included in this PR?
The narrow month path is unchanged and still uses chrono for timestamps that fit in `i64` nanoseconds.
The wide path, used when the nanosecond conversion overflows, now bins on the civil calendar with the same day-count conversion `date_trunc` already uses. It keeps the existing stride rule, including negative strides, and clamps a missing day to the end of the month the way `checked_add_months` does. When the candidate is after the source, it steps back one stride from the origin rather than from the already-clamped date.
A result that does not fit the output unit is still `NULL`. That covers `Timestamp(Second)::MIN` and a nanosecond source with a stride too large for `i64` nanoseconds.
## What is the testing strategy for this PR?
- `date_bin_errors.slt` covers the issue: `10_000_000_000_000` seconds bins to month start `9999998294400`, and the negative input bins to `-10000001059200`.
- The same file updates the large millisecond stride from issue #20219. That bin fits in milliseconds (`-4306016287785600000`) and used to be `NULL` only because chrono overflowed. The nanosecond form of that stride still expects `NULL`.
- A unit test checks that the wide path matches the chrono path for in-range values, including month-end clamping, a leap day, negative strides, and dates before the epoch.
## Are there any user-facing changes?
Yes. Month-stride `date_bin` now returns the month start for timestamps outside chrono's range when that instant fits the output type. Values that already fit in `i64` nanoseconds are unchanged. No public API change.
Made with [Cursor](https://cursor.com)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: bin date_bin month strides outside chrono's range [datafusion]
Posted by "ThilakShekharShriyan (via GitHub) via github" <gi...@datafusion.apache.org>.
ThilakShekharShriyan commented on PR #26153:
URL: https://github.com/apache/datafusion/pull/26153#issuecomment-6700138761
@adriangb ^^
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: bin date_bin month strides outside chrono's range [datafusion]
Posted by "mhilton (via GitHub)" <gi...@apache.org>.
mhilton commented on code in PR #26153:
URL: https://github.com/apache/datafusion/pull/26153#discussion_r4292979009
##########
datafusion/functions/src/datetime/date_bin.rs:
##########
@@ -463,24 +464,85 @@ fn date_bin_months_interval(stride_months: i64, source: i64, origin: i64) -> Res
}
}
-// `date_bin_months_interval` with i128 nanoseconds, limited by the range of
-// `DateTime<Utc>` instead of i64 nanoseconds.
+// Compared in field order, which is chronological.
+#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
+struct CivilTimestamp {
+ year: i64,
Review Comment:
Since this has a relatively limited scope, I think it's fine to leave these types as signed, but a comment saying what are legal and illegal combinations of signed and unsigned components would be helpful.
##########
datafusion/functions/src/datetime/date_bin.rs:
##########
@@ -1642,10 +1752,10 @@ mod tests {
ScalarValue::new_interval_mdn(0, 0, 3),
ScalarValue::TimestampNanosecond(Some(i64::MIN), None),
),
- // Month bins are limited by the range of `DateTime<Utc>`.
+ // The month bin of Timestamp(Second)::MIN does not fit (#25855).
Review Comment:
I personally think that we should define `date_bin` to use `i64::MIN` for any value that would be less then `i64::MIN` to ensure that it always preserves sort order. However that is not really part of this PR.
##########
datafusion/functions/src/datetime/date_trunc.rs:
##########
@@ -644,7 +644,7 @@ const DAYS_PER_ERA: i64 = 146_097;
/// This is a port of Howard Hinnant's `civil_from_days`, which documents the
/// derivation of the constants and the March-based year used below:
/// <https://howardhinnant.github.io/date_algorithms.html#civil_from_days>
-fn civil_from_days(days: i64) -> (i64, i64, i64) {
+pub(super) fn civil_from_days(days: i64) -> (i64, i64, i64) {
Review Comment:
Since this is now being used outside of `date_trunc` maybe it should be moved to some shared module.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: bin date_bin month strides outside chrono's range [datafusion]
Posted by "Omega359 (via GitHub)" <gi...@apache.org>.
Omega359 commented on PR #26153:
URL: https://github.com/apache/datafusion/pull/26153#issuecomment-6372798432
@adriangb you seem to review and/or do work related to date/time stuff so perhaps you can review this PR or at least fire off the workflow? Thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: bin date_bin month strides outside chrono's range [datafusion]
Posted by "Omega359 (via GitHub)" <gi...@apache.org>.
Omega359 commented on PR #26153:
URL: https://github.com/apache/datafusion/pull/26153#issuecomment-6351797743
I had a look at this PR and I found that having two implementations for month-bin semantics wasn't ideal and that perhaps a bit of a refactor would be worth it. Refactor was done by claude and reviewed by myself PR @ https://github.com/ThilakShekharShriyan/datafusion/pull/1
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: bin date_bin month strides outside chrono's range [datafusion]
Posted by "github-actions[bot] (via GitHub) via github" <gi...@datafusion.apache.org>.
github-actions[bot] commented on PR #26153:
URL: https://github.com/apache/datafusion/pull/26153#issuecomment-6700096325
Marked as draft after 14 days without activity: merge conflicts.
Please address these items and mark **Ready for review** when ready.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org