Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(pg): handle timestamps outside protobuf range in query results (BYT-9062) (#20623)

boojack1d29f47a0f38
diff --git a/backend/plugin/db/pg/query.go b/backend/plugin/db/pg/query.go
index 06d9656fc6..c12bf5dd1e 100644
--- a/backend/plugin/db/pg/query.go
+++ b/backend/plugin/db/pg/query.go
@@ -124,33 +124,62 @@ func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.
if scale == -1 {
scale = 6
}
- if typeName == "TIMESTAMP" {
- return &v1pb.RowValue{
- Kind: &v1pb.RowValue_TimestampValue{
- TimestampValue: &v1pb.RowValue_Timestamp{
- GoogleTimestamp: timestamppb.New(raw.Time),
- Accuracy: int32(scale),
- },
- },
- }
- }
- zone, offset := raw.Time.Zone()
- return &v1pb.RowValue{
- Kind: &v1pb.RowValue_TimestampTzValue{
- TimestampTzValue: &v1pb.RowValue_TimestampTZ{
- GoogleTimestamp: timestamppb.New(raw.Time),
- Zone: zone,
- Offset: int32(offset),
- Accuracy: int32(scale),
- },
- },
- }
+ return buildTimestamptzRowValue(typeName, raw.Time, int32(scale))
}
default:
}
return util.NullRowValue
}
+// buildTimestamptzRowValue converts a PostgreSQL timestamp/timestamptz value into a
+// RowValue. PostgreSQL supports dates outside the google.protobuf.Timestamp range
+// (0001-01-01 to 9999-12-31); for those, fall back to a string instead of crashing
+// response marshaling with "seconds out of range".
+func buildTimestamptzRowValue(typeName string, t time.Time, scale int32) *v1pb.RowValue {
+ ts := timestamppb.New(t)
+ if err := ts.CheckValid(); err != nil {
+ return util.BuildStringRowValue(formatPGTimestamp(t, typeName != "TIMESTAMP"))
+ }
+ if typeName == "TIMESTAMP" {
+ return &v1pb.RowValue{
+ Kind: &v1pb.RowValue_TimestampValue{
+ TimestampValue: &v1pb.RowValue_Timestamp{
+ GoogleTimestamp: ts,
+ Accuracy: scale,
+ },
+ },
+ }
+ }
+ zone, offset := t.Zone()
+ return &v1pb.RowValue{
+ Kind: &v1pb.RowValue_TimestampTzValue{
+ TimestampTzValue: &v1pb.RowValue_TimestampTZ{
+ GoogleTimestamp: ts,
+ Zone: zone,
+ Offset: int32(offset),
+ Accuracy: scale,
+ },
+ },
+ }
+}
+
+// formatPGTimestamp renders a time.Time using PostgreSQL's text representation rather
+// than RFC3339: a space separator, no zone for timestamp without time zone, and a " BC"
+// suffix for years before 1 AD (Go uses astronomical year numbering where year 0 == 1 BC).
+func formatPGTimestamp(t time.Time, withZone bool) string {
+ year := t.Year()
+ suffix := ""
+ if year <= 0 {
+ year = 1 - year
+ suffix = " BC"
+ }
+ s := fmt.Sprintf("%04d-%s", year, t.Format("01-02 15:04:05.999999999"))
+ if withZone {
+ s += t.Format("-07:00")
+ }
+ return s + suffix
+}
+
// Padding 0's to nanosecond precision to make sure it's always 6 digits.
// Since the data cannot be formatted into a time.Time, we need to pad it here.
// Accuracy is passed as argument since we cannot determine the precision of the data type using DecimalSize().
diff --git a/backend/plugin/db/pg/query_test.go b/backend/plugin/db/pg/query_test.go
index c955a5a770..e8a3ed069e 100644
--- a/backend/plugin/db/pg/query_test.go
+++ b/backend/plugin/db/pg/query_test.go
@@ -2,10 +2,70 @@ package pg
import (
"testing"
+ "time"
"github.com/stretchr/testify/require"
+ "google.golang.org/protobuf/encoding/protojson"
)
+func TestBuildTimestamptzRowValue(t *testing.T) {
+ tests := []struct {
+ name string
+ typeName string
+ input time.Time
+ wantString string // non-empty means we expect a string fallback with this value
+ }{
+ {
+ name: "valid timestamp",
+ typeName: "TIMESTAMP",
+ input: time.Date(2026, 5, 11, 12, 34, 56, 0, time.UTC),
+ },
+ {
+ name: "valid timestamptz",
+ typeName: "TIMESTAMPTZ",
+ input: time.Date(2026, 5, 11, 12, 34, 56, 0, time.FixedZone("", 2*60*60)),
+ },
+ {
+ name: "BC timestamp falls back to PG-formatted string",
+ typeName: "TIMESTAMP",
+ input: time.Date(0, 12, 31, 15, 0, 0, 0, time.UTC), // Go year 0 == 1 BC
+ wantString: "0001-12-31 15:00:00 BC",
+ },
+ {
+ name: "year 10000 timestamp omits zone",
+ typeName: "TIMESTAMP",
+ input: time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC),
+ wantString: "10000-01-01 00:00:00",
+ },
+ {
+ name: "year 10000 timestamptz keeps offset",
+ typeName: "TIMESTAMPTZ",
+ input: time.Date(10000, 5, 11, 12, 34, 56, 0, time.FixedZone("", 2*60*60)),
+ wantString: "10000-05-11 12:34:56+02:00",
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ got := buildTimestamptzRowValue(tc.typeName, tc.input, 6)
+
+ // The whole point of the fix: marshaling must never fail.
+ _, err := protojson.Marshal(got)
+ require.NoError(t, err)
+
+ if tc.wantString != "" {
+ require.Equal(t, tc.wantString, got.GetStringValue())
+ return
+ }
+ if tc.typeName == "TIMESTAMP" {
+ require.NotNil(t, got.GetTimestampValue())
+ } else {
+ require.NotNil(t, got.GetTimestampTzValue())
+ }
+ })
+ }
+}
+
func TestPadZeroes(t *testing.T) {
tests := []struct {
name string