From 7d20352706aaf61e051e0e9f9acba3e11525f8dc Mon Sep 17 00:00:00 2001 From: Phil Rzewski Date: Tue, 24 Nov 2026 08:47:24 -0800 Subject: [PATCH 1/2] fix bucket() rounding up before 1970 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. --- book/src/super-sql/functions/time/bucket.md | 15 +++++ compiler/ztests/sql/timestamp-and-date.yaml | 3 +- pkg/nano/duration.go | 9 ++- pkg/nano/time.go | 2 +- runtime/ztests/expr/function/bucket.yaml | 61 +++++++++++++++++++++ 5 files changed, 87 insertions(+), 3 deletions(-) diff --git a/book/src/super-sql/functions/time/bucket.md b/book/src/super-sql/functions/time/bucket.md index 3d69b5361c..549e239ebc 100644 --- a/book/src/super-sql/functions/time/bucket.md +++ b/book/src/super-sql/functions/time/bucket.md @@ -83,3 +83,18 @@ values bucket(this, 15m) 15m 0s ``` + +--- + +_Timestamps before 1970 round down to the start of their bucket too_ + +```mdtest-spq +# spq +values {ts, day:bucket(ts, 1d)} +# input +{ts:1969-07-20T20:17:40Z} +{ts:1969-12-31T23:30:00Z} +# expected output +{ts:1969-07-20T20:17:40Z,day:1969-07-20T00:00:00Z} +{ts:1969-12-31T23:30:00Z,day:1969-12-31T00:00:00Z} +``` diff --git a/compiler/ztests/sql/timestamp-and-date.yaml b/compiler/ztests/sql/timestamp-and-date.yaml index 6279232f4e..c4a2884c75 100644 --- a/compiler/ztests/sql/timestamp-and-date.yaml +++ b/compiler/ztests/sql/timestamp-and-date.yaml @@ -1,4 +1,4 @@ -spq: values TIMESTAMP '2012-10-02 12:01:01', DATE '2012-10-02 12:01:01' +spq: values TIMESTAMP '2012-10-02 12:01:01', DATE '2012-10-02 12:01:01', DATE '1969-07-20 20:17:40' input: | null @@ -6,3 +6,4 @@ input: | output: | 2012-10-02T12:01:01Z 2012-10-02T00:00:00Z + 1969-07-20T00:00:00Z diff --git a/pkg/nano/duration.go b/pkg/nano/duration.go index 1e34c897fb..0c87adc282 100644 --- a/pkg/nano/duration.go +++ b/pkg/nano/duration.go @@ -103,7 +103,14 @@ func writeFixedPoint(b *strings.Builder, ns, scale int64) { } func (d Duration) Trunc(bin Duration) Duration { - return d / bin * bin + r := d % bin + if r < 0 { + r += max(bin, -bin) + if d < math.MinInt64+r { + return math.MinInt64 + } + } + return d - r } func (d Duration) MarshalJSON() ([]byte, error) { diff --git a/pkg/nano/time.go b/pkg/nano/time.go index 914a617226..d38236529f 100644 --- a/pkg/nano/time.go +++ b/pkg/nano/time.go @@ -82,7 +82,7 @@ func (t Ts) Time() time.Time { } func (t Ts) Trunc(bin Duration) Ts { - return Ts(Duration(t) / bin * bin) + return Ts(Duration(t).Trunc(bin)) } func (t Ts) Midnight() Ts { diff --git a/runtime/ztests/expr/function/bucket.yaml b/runtime/ztests/expr/function/bucket.yaml index e1772a2fa2..de387fa134 100644 --- a/runtime/ztests/expr/function/bucket.yaml +++ b/runtime/ztests/expr/function/bucket.yaml @@ -41,3 +41,64 @@ spq: SELECT CAST(TIMESTAMP '2020-05-26 15:27:47' AS DATE) AS d output: | {d:2020-05-26T00:00:00Z} + +--- + +# Timestamps before 1970 and negative durations round down, not toward zero, +# whatever the sign of the span. +spq: values bucket(t, bin) + +input: | + {t:1969-12-31T23:30:00Z,bin:1h} + {t:1969-07-20T20:17:40Z,bin:1d} + {t:-3s,bin:2s} + {t:-4s,bin:2s} + {t:-3s,bin:-2s} + {t:3s,bin:-2s} + +output: | + 1969-12-31T23:00:00Z + 1969-07-20T00:00:00Z + -4s + -4s + -4s + 2s + +--- + +# A constant span rounds column values and constants down before 1970 too. +spq: values bucket(this, 1h), bucket(1969-12-31T23:30:00Z, 1h) + +input: | + 1969-12-31T23:59:59Z + -1s + +output: | + 1969-12-31T23:00:00Z + 1969-12-31T23:00:00Z + -1h + 1969-12-31T23:00:00Z + +--- + +# A SQL cast to DATE gives the right day before 1970. +spq: SELECT CAST(TIMESTAMP '1969-07-20 20:17:40' AS DATE) AS d + +output: | + {d:1969-07-20T00:00:00Z} + +--- + +# A bucket that would start before the earliest timestamp or shortest +# duration starts there instead of wrapping around. +spq: values bucket(this, 100y) + +input: | + 1700-01-01T00:00:00Z + 1770-02-18T00:00:00Z + -292y171d23h47m16.854775807s + +output: | + 1677-09-21T00:12:43.145224192Z + 1770-02-18T00:00:00Z + -292y171d23h47m16.854775808s From 828d93932993ad41a7ce7d0675e5c2cfc21a8c87 Mon Sep 17 00:00:00 2001 From: Phil Rzewski Date: Tue, 24 Nov 2026 10:24:24 -0800 Subject: [PATCH 2/2] remove the pre-1970 example from the bucket docs --- book/src/super-sql/functions/time/bucket.md | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/book/src/super-sql/functions/time/bucket.md b/book/src/super-sql/functions/time/bucket.md index 549e239ebc..3d69b5361c 100644 --- a/book/src/super-sql/functions/time/bucket.md +++ b/book/src/super-sql/functions/time/bucket.md @@ -83,18 +83,3 @@ values bucket(this, 15m) 15m 0s ``` - ---- - -_Timestamps before 1970 round down to the start of their bucket too_ - -```mdtest-spq -# spq -values {ts, day:bucket(ts, 1d)} -# input -{ts:1969-07-20T20:17:40Z} -{ts:1969-12-31T23:30:00Z} -# expected output -{ts:1969-07-20T20:17:40Z,day:1969-07-20T00:00:00Z} -{ts:1969-12-31T23:30:00Z,day:1969-12-31T00:00:00Z} -```