fix: prevent next_day panic on far-future start dates - #24194
Merged
Conversation
Spark's `next_day` computed its result by adding the day offset to a `chrono::NaiveDate`. When the next occurrence lands past `NaiveDate::MAX` (epoch day 95026236) — which happens for start dates in the last week of the representable `Date32` range — the `NaiveDate + TimeDelta` add panics with `NaiveDate + TimeDelta overflowed`. Spark's `DateTimeUtils.getNextDateForDayOfWeek` is pure integer arithmetic on the epoch day and always produces a value. This change computes the result on the epoch day directly (`days + delta`, with `checked_add`) so those start dates return a value instead of panicking. Closes apache#23891. Co-authored-by: Claude Code
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #24194 +/- ##
==========================================
+ Coverage 80.86% 81.05% +0.18%
==========================================
Files 1101 1106 +5
Lines 375446 382289 +6843
Branches 375446 382289 +6843
==========================================
+ Hits 303592 309848 +6256
- Misses 53761 54123 +362
- Partials 18093 18318 +225 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Jefffrey
approved these changes
Aug 9, 2026
Member
Author
|
Thanks @Jefffrey for review |
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
datafusion-spark'snext_daycomputed its result by adding the day offset to achrono::NaiveDate:When the next occurrence of the requested weekday lands past
chrono::NaiveDate::MAX(epoch day95026236) — which happens for start dates in the last week of the representableDate32range — theNaiveDate + TimeDeltaadd panics withNaiveDate + TimeDelta overflowed. (The linked issue reports NULL; the scalar path in fact panics.)Spark's
DateTimeUtils.getNextDateForDayOfWeekis pure integer arithmetic on the epoch day and always produces a value:What changes are included in this PR?
days + delta, viachecked_add) instead of building aNaiveDatefor the result, so a next occurrence pastNaiveDate::MAXreturns a value instead of panicking. The weekday/offset logic is unchanged, so results for all in-range dates are identical.Scope note: a start day beyond
NaiveDate::MAX(> 95026236) still returns NULL, because the weekday is derived viaNaiveDate. Extending fullInt-range parity (computing the weekday arithmetically too) is a larger change left as a follow-up; this PR fixes the panic, which is the concrete reported harm.Are these changes tested?
Yes.
next_day_handles_far_future_start_datescovering the two cases from the issue (95026236/Mon,95026230/Tue), each of which panicked before this change.spark/datetime/next_day.sltasserting the same two cases (casting theDate32result toInt32, since these dates are past the printable range).Are there any user-facing changes?
next_dayno longer panics for far-future start dates; it returns the correct epoch day. No API changes.