Repository navigation
Conversation
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>
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?
date_truncfails on microsecond timestamps before 1677 or after 2262 #27952.Rationale for this change
date_truncrejects valid second, millisecond and microsecond timestamps before 1677-09-21 or after 2262-04-11 atweek,month,quarterandyear, and athouranddaywhen 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_coarseand_date_trunc_coarse_without_tzare 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.as_datetime_with_timezone::<T>, thentimestamp,timestamp_millis,timestamp_microsortimestamp_nanos_opt).general_date_trunckeeps its signature (the preimage code uses it) and dispatches todate_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.date_trunc('week', arrow_cast(9223372036854775807, 'Timestamp(Second, None)')), already tested). The bounds are constants for each unit; nanoseconds need no check.naive_local), the 3-hour DST fallback, and a week that starts before chrono's first day (a Thursday).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 onmain.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:i64extremes, 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_truncbenchmark (indatetime_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 onmain.mainagainst this PR on two x86-64 machines (A and B), 1000 random rows within 30 years of 1970. Each machine ranmain, PR, PR,mainwith 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.
week_nanosmonth_nanosquarter_nanosyear_nanosweek_microsmonth_microsquarter_microsyear_microsquarter_millisyear_millisquarter_secondsyear_secondshour_nanos_tzmonth_micros_tzday_millis_tzyear_seconds_tzday_nanos(unchanged path)year_micros_before_1677days_from_civil: onmain, 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.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.