Repository navigation
fix bucket() rounding up before 1970 - #7716
Merged
Merged
Conversation
bucket() truncated toward zero, so timestamps before 1970 and negative durations were rounded up to the next bucket instead of down to their own, and SQL dates before 1970 came out a day late. Round down in nano.Duration.Trunc and have nano.Ts.Trunc use it, clamping a result that would fall before the earliest timestamp. Add a pre-1970 example to the bucket docs.
mattnibs
reviewed
Nov 24, 2026
mattnibs
approved these changes
Nov 24, 2026
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.
bucket()truncated toward zero instead of rounding down. So timestamps before 1970 and negative durations were rounded up to the next bucket boundary rather than down to the start of their own bucket, e.g.,bucket(1969-12-31T23:30:00Z, 1h)gave1970-01-01T00:00:00Zandbucket(-3s, 2s)gave-2s. That made the bucket starting at zero twice as wide as the others, so counts by bucket that cross 1970 were wrong. SQL dates before 1970 came out a day late the same way, both fromCAST(... AS DATE), which usesbucket(), and from aDATE '...'literal that includes a time.Trying it out
On current tip of
main, commit 1563608:On this branch, commit 7d20352:
Testing
bucket.yamlcover a span that varies by row, a constant span applied to a column and to a constant, and a SQL cast toDATE. One more stanza covers the clamp at the earliest timestamp and shortest duration.timestamp-and-date.yamladds aDATEliteral before 1970. All of them fail onmain.make test-unit,make test-systemand the book's mdtests pass. On 10M timestamps from the 2020s,bucket()takes the same time as onmain.I found this while checking the function docs against
main(#7714). It's not a regression:bucket()has rounded toward zero since the first zq commit. Postgres (date_bin,CAST(... AS DATE),DATEliterals) and DuckDB (time_bucket) round down, and give the same results as this branch for the commands above. As before, the sign of the span doesn't matter, sobucket(3s, -2s)is still2s. Postgres and DuckDB reject a negative span instead, but that would be a separate change. Rounding down can also go past the earliest timestampsupercan represent (1677-09-21T00:12:43.145224192Z), e.g.,bucket(1700-01-01T00:00:00Z, 100y). Such a result is clamped to that earliest timestamp instead of wrapping around to 2254. An error would be the other option. With this fix, thebucketdocs' statement from #7714 that "a1hor1dspan starts each bucket on the hour or at midnight UTC" holds before 1970 too.