Skip to content

fix bucket() rounding up before 1970 - #7716

Merged
philrz merged 2 commits into
mainfrom
bucket-round-down
Nov 24, 2026
Merged

philrz merged 2 commits into
mainfrom
bucket-round-down

Conversation

@philrz

@philrz philrz commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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) gave 1970-01-01T00:00:00Z and bucket(-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 from CAST(... AS DATE), which uses bucket(), and from a DATE '...' literal that includes a time.

Trying it out

On current tip of main, commit 1563608:

$ super -version && super -c "
values 1969-12-31T23:30:00Z, 1970-01-01T00:30:00Z
| count() by hour:=bucket(this, 1h)
| sort hour"
Version: v0.4.0-116-g15636080b
{hour:1970-01-01T00:00:00Z,count:2}

$ super -c "SELECT CAST(TIMESTAMP '1969-07-20 20:17:40' AS DATE) AS moon_landing"
{moon_landing:1969-07-21T00:00:00Z}

On this branch, commit 7d20352:

$ super -version && super -c "
values 1969-12-31T23:30:00Z, 1970-01-01T00:30:00Z
| count() by hour:=bucket(this, 1h)
| sort hour"
Version: v0.4.0-117-g7d2035270
{hour:1969-12-31T23:00:00Z,count:1}
{hour:1970-01-01T00:00:00Z,count:1}

$ super -c "SELECT CAST(TIMESTAMP '1969-07-20 20:17:40' AS DATE) AS moon_landing"
{moon_landing:1969-07-20T00:00:00Z}

Testing

  • New stanzas in bucket.yaml cover a span that varies by row, a constant span applied to a column and to a constant, and a SQL cast to DATE. One more stanza covers the clamp at the earliest timestamp and shortest duration. timestamp-and-date.yaml adds a DATE literal before 1970. All of them fail on main.
  • make test-unit, make test-system and the book's mdtests pass. On 10M timestamps from the 2020s, bucket() takes the same time as on main.

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), DATE literals) 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, so bucket(3s, -2s) is still 2s. Postgres and DuckDB reject a negative span instead, but that would be a separate change. Rounding down can also go past the earliest timestamp super can 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, the bucket docs' statement from #7714 that "a 1h or 1d span starts each bucket on the hour or at midnight UTC" holds before 1970 too.

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.
@philrz philrz self-assigned this Nov 24, 2026
@philrz
philrz requested a review from a team November 24, 2026 17:05
@philrz
philrz merged commit 9fc2a27 into main Nov 24, 2026
2 checks passed
@philrz
philrz deleted the bucket-round-down branch November 24, 2026 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants