Skip to content

fix: date_trunc truncates timestamps in their own unit at every granularity - #27976

Open
xangelix wants to merge 2 commits into
apache:mainfrom
xangelix:fix/date-trunc-outside-nanosecond-range
Open

xangelix wants to merge 2 commits into
apache:mainfrom
xangelix:fix/date-trunc-outside-nanosecond-range

Conversation

@xangelix

@xangelix xangelix commented Dec 2, 2026

Copy link
Copy Markdown

Which issue does this PR close?

Rationale for this change

date_trunc rejects valid second, millisecond and microsecond timestamps before 1677-09-21 or after 2262-04-11 at week, month, quarter and year, and at hour and day when the timestamp has a timezone, because it converts them to nanoseconds first. A single such value (for example the Windows FILETIME epoch, 1601-01-01) fails the whole query.

What changes are included in this PR?

  • date_trunc_coarse and _date_trunc_coarse_without_tz are generic over the timestamp type and truncate in its own unit. The divisors are constants for each unit (const { … }), so the nanosecond path compiles to the same arithmetic as before.
  • With a timezone, the value goes to and from chrono in its own unit (as_datetime_with_timezone::<T>, then timestamp, timestamp_millis, timestamp_micros or timestamp_nanos_opt).
  • general_date_trunc keeps its signature (the preimage code uses it) and dispatches to date_trunc_timestamp::<T>, which the array and scalar paths now call directly. Its results for values inside the nanosecond range are unchanged, including the second/minute rounding it applied after converting back from nanoseconds.
  • Timestamps chrono cannot represent are still rejected with the same message (for example date_trunc('week', arrow_cast(9223372036854775807, 'Timestamp(Second, None)')), already tested). The bounds are constants for each unit; nanoseconds need no check.
  • At chrono's limits, three places that could not be reached with nanoseconds would now panic, so they return the existing "out of range" errors instead: a local time outside chrono's range (naive_local), the 3-hour DST fallback, and a week that starts before chrono's first day (a Thursday).
  • The timezone path built its "out of range" error eagerly for every row (ok_or(exec_datafusion_err!(…))); it is now built only on failure.

What is the testing strategy for this PR?

  • datetime/timestamps.slt: µs, ms and s columns and scalars, without a timezone, in UTC, Europe/Berlin, America/New_York and +05:00, at 1601, 2023, 2262 and 9999, at every coarse granularity; a DST transition in each unit; chrono's first and last timestamps; and the errors just outside them. All but the DST case fail on main.
  • Rust unit tests in date_trunc.rs:
    • date_trunc_in_unit_matches_nanoseconds: inside the nanosecond range, every unit, granularity and timezone (including DST transitions in New York, Sao Paulo, Lord Howe and Berlin, and LMT offsets) gives what converting to nanoseconds did.
    • date_trunc_without_timezone_matches_utc: the integer arithmetic agrees with chrono's calendar in UTC over the whole of chrono's range, in every unit.
    • date_trunc_at_the_limits: i64 extremes, chrono's limits and the nanosecond limits in every unit, granularity and timezone: no panic, the same error as before outside chrono's range.
    • date_trunc_beyond_nanoseconds: expected values at 1601, and the errors at chrono's limits.

Benchmarks

The first commit extends the date_trunc benchmark (in datetime_expressions) to every time unit, with and without a timezone (America/New_York), at second, hour, day, week, month, quarter and year. The existing benchmarks keep their names and inputs. The second commit adds cases before 1677 (day, month and year, with and without a timezone), which fail on main.

main against this PR on two x86-64 machines (A and B), 1000 random rows within 30 years of 1970. Each machine ran main, PR, PR, main with nothing else scheduled; the table shows the second pair:

Criterion medians in µs on B, and Criterion's change estimate with its 95% confidence interval on each machine.

benchmark main (B) PR (B) change on A change on B
week_nanos 4.49 4.15 −19.1% [−19.2, −19.1] −7.5% [−7.6, −7.4]
month_nanos 8.64 8.09 −9.3% [−9.5, −9.2] −6.5% [−6.9, −6.3]
quarter_nanos 14.70 13.29 −7.0% [−7.1, −6.9] −9.9% [−10.1, −9.6]
year_nanos 10.53 10.18 −9.0% [−9.1, −9.0] −3.1% [−3.3, −2.8]
week_micros 4.95 3.92 −15.3% [−15.3, −15.2] −20.7% [−20.9, −20.6]
month_micros 9.42 8.39 −10.2% [−10.3, −10.1] −10.9% [−11.1, −10.8]
quarter_micros 14.71 14.43 +0.6% [+0.5, +0.8] −1.8% [−2.0, −1.6]
year_micros 11.76 10.72 +1.8% [+1.6, +1.9] −9.2% [−9.5, −9.0]
quarter_millis 14.55 14.13 +2.7% [+2.4, +3.2] −2.1% [−2.6, −1.5]
year_millis 10.97 11.17 +1.3% [+1.2, +1.4] +1.9% [+1.7, +2.1]
quarter_seconds 13.88 15.38 +2.5% [+2.3, +2.7] +10.5% [+10.0, +10.9]
year_seconds 10.73 11.40 +1.7% [+1.7, +1.8] +6.3% [+6.1, +6.6]
hour_nanos_tz 92.0 50.4 −60.7% [−61.0, −60.4] −45.3% [−45.4, −45.2]
month_micros_tz 95.7 52.2 −60.5% [−60.5, −60.4] −46.3% [−47.0, −45.7]
day_millis_tz 94.4 51.7 −59.9% [−60.4, −59.6] −45.2% [−45.3, −45.1]
year_seconds_tz 97.4 50.3 −61.3% [−61.4, −61.2] −48.4% [−48.5, −48.3]
day_nanos (unchanged path) 1.36 1.37 −0.7% [−0.7, −0.6] +0.7% [+0.5, +0.9]
year_micros_before_1677 error 10.47 (A)
  • With a timezone, every unit at every granularity from an hour up is 41–64% faster, because the error is no longer built for each row.
  • Without a timezone, week and month are 6–21% faster in every unit, and nanosecond quarter and year 3–10% faster.
  • Quarter and year in seconds are slower: +2–3% on A, +6–11% on B (up to 1.5 ns per row), and millisecond year +1–2%. The cause is in days_from_civil: on main, every input had gone through nanoseconds, so LLVM knew the year was between 1677 and 2262 and divided it by 400 with a 16-bit multiply and no sign correction. A second timestamp can now be any year chrono supports (±262,143), which needs the general signed division. Removing the range check or inlining the per-row function did not change it.
  • Second, hour and day without a timezone take a path this PR does not touch: within ±1%.

Are there any user-facing changes?

Queries that failed with "Timestamp … out of range" on these values now return the truncated timestamp. Nothing else changes.

AI Disclaimer

Claude Code helped with this one, as the co-author suggests. We're migrating away from duckdb into using pure datafusion, and our test suite we've been using for a while with duckdb has picked up some issues with datafusion we're hoping to get resolved upstream.

A little bit of perf magic sprinkled in here which seems to optimize very well with rustc 1.100. Haven't tested on older versions. Please note the slight performance decrease from days_from_civil. This seems well worth it anyways.

Thanks so much for the great support of this library.

xangelix and others added 2 commits December 2, 2026 22:48
Extend the date_trunc benchmark from nanosecond and second timestamps
without a timezone to every time unit, with and without a timezone, at
second, hour, day, week, month, quarter and year granularity. The
existing benchmarks keep their names and inputs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…larity

date_trunc converted second, millisecond and microsecond timestamps to
nanoseconds before truncating them to an hour or a day in a timezone, or
to a week, month, quarter or year. Valid timestamps before 1677-09-21 or
after 2262-04-11 failed with "Timestamp ... out of range", for example
the Windows FILETIME epoch, 1601-01-01, as a microsecond timestamp.

Truncate in the timestamp's own unit instead. The integer path is
generic over the timestamp type, so its divisors stay constants for each
unit, and the timezone path converts to and from chrono in that unit.
Timestamps chrono cannot represent are still rejected with the same
error. At chrono's limits, a local time outside its range, or a week
that starts before its first day, is an error rather than a panic.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

date_trunc fails on microsecond timestamps before 1677 or after 2262

1 participant