Skip to content

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

chore: change date/time output to string (#15117)

Vincent Huang3632b46da066
diff --git a/backend/plugin/db/mssql/query.go b/backend/plugin/db/mssql/query.go
index fea2e89408..df7d65f4c3 100644
--- a/backend/plugin/db/mssql/query.go
+++ b/backend/plugin/db/mssql/query.go
@@ -6,6 +6,7 @@ import (
"log/slog"
"strconv"
"strings"
+ "time"
"github.com/antlr4-go/antlr/v4"
"github.com/pkg/errors"
@@ -47,7 +48,7 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {
// // Source values of type [time.Time] may be scanned into values of type
// *time.Time, *interface{}, *string, or *[]byte. When converting to
// the latter two, [time.RFC3339Nano] is used.
- case "SMALLDATETIME", "DATETIME", "DATETIME2", "DATE", "TIME":
+ case "TIME", "DATE", "SMALLDATETIME", "DATETIME", "DATETIME2":
return new(sql.NullTime)
case "DATETIMEOFFSET":
return new(sql.NullTime)
@@ -117,6 +118,20 @@ func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.
}
case *sql.NullTime:
if raw.Valid {
+ if columnType.DatabaseTypeName() == "TIME" {
+ return &v1pb.RowValue{
+ Kind: &v1pb.RowValue_StringValue{
+ StringValue: raw.Time.Format(time.TimeOnly),
+ },
+ }
+ }
+ if columnType.DatabaseTypeName() == "DATE" {
+ return &v1pb.RowValue{
+ Kind: &v1pb.RowValue_StringValue{
+ StringValue: raw.Time.Format(time.DateOnly),
+ },
+ }
+ }
_, scale, _ := columnType.DecimalSize()
if typeName == "DATETIME" || typeName == "DATETIME2" || typeName == "SMALLDATETIME" {
return &v1pb.RowValue{
diff --git a/backend/plugin/db/pg/query.go b/backend/plugin/db/pg/query.go
index 83fe413bf9..74671dc8fd 100644
--- a/backend/plugin/db/pg/query.go
+++ b/backend/plugin/db/pg/query.go
@@ -2,6 +2,8 @@ package pg
import (
"database/sql"
+ "strings"
+ "time"
"google.golang.org/protobuf/types/known/timestamppb"
@@ -21,6 +23,8 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {
return new(sql.NullInt64)
case "FLOAT", "DOUBLE", "FLOAT4", "FLOAT8":
return new(sql.NullFloat64)
+ case "DATE":
+ return new(sql.NullTime)
case "TIMESTAMP", "TIMESTAMPTZ":
return new(pgtype.Timestamptz)
case "BIT", "VARBIT":
@@ -34,6 +38,14 @@ func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.
switch raw := value.(type) {
case *sql.NullString:
if raw.Valid {
+ // TODO: Fix DatabaseTypeName for 1266, Object ID for TIME WITHOUT TIME ZONE
+ if columnType.DatabaseTypeName() == "TIME" || columnType.DatabaseTypeName() == "1266" || columnType.DatabaseTypeName() == "INTERVAL" {
+ return &v1pb.RowValue{
+ Kind: &v1pb.RowValue_StringValue{
+ StringValue: padZeroes(raw.String, 6),
+ },
+ }
+ }
return &v1pb.RowValue{
Kind: &v1pb.RowValue_StringValue{
StringValue: raw.String,
@@ -72,6 +84,17 @@ func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.
},
}
}
+ case *sql.NullTime:
+ if raw.Valid {
+ if columnType.DatabaseTypeName() == "DATE" {
+ return &v1pb.RowValue{
+ Kind: &v1pb.RowValue_StringValue{
+ StringValue: raw.Time.Format(time.DateOnly),
+ },
+ }
+ }
+ }
+
case *pgtype.Timestamptz:
if raw.Valid {
if raw.InfinityModifier != pgtype.Finite {
@@ -107,3 +130,25 @@ func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.
}
return util.NullRowValue
}
+
+// 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().
+func padZeroes(rawStr string, acc int) string {
+ dotIndex := strings.Index(rawStr, ".")
+ if dotIndex < 0 {
+ return rawStr
+ }
+ // End index is used to cut off the time zone information.
+ endIndex := len(rawStr)
+ if plusIndex := strings.Index(rawStr, "+"); plusIndex >= 0 {
+ endIndex = plusIndex
+ } else if minusIndex := strings.Index(rawStr, "-"); minusIndex >= 0 {
+ endIndex = minusIndex
+ }
+ decimalPart := rawStr[dotIndex+1 : endIndex]
+ if len(decimalPart) < acc {
+ rawStr = rawStr[:endIndex] + strings.Repeat("0", acc-len(decimalPart)) + rawStr[endIndex:]
+ }
+ return rawStr
+}
diff --git a/backend/plugin/db/pg/query_test.go b/backend/plugin/db/pg/query_test.go
new file mode 100644
index 0000000000..3ffe0c9a46
--- /dev/null
+++ b/backend/plugin/db/pg/query_test.go
@@ -0,0 +1,58 @@
+package pg
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestPadZeroes(t *testing.T) {
+ tests := []struct {
+ name string
+ rawStr string
+ acc int
+ expected string
+ }{
+ {
+ name: "No decimal part",
+ rawStr: "12:34:56",
+ acc: 6,
+ expected: "12:34:56",
+ },
+ {
+ name: "Already correct precision",
+ rawStr: "12:34:56.123456",
+ acc: 6,
+ expected: "12:34:56.123456",
+ },
+ {
+ name: "Less precision",
+ rawStr: "12:34:56.123",
+ acc: 6,
+ expected: "12:34:56.123000",
+ },
+ {
+ name: "More precision",
+ rawStr: "12:34:56.123456789",
+ acc: 6,
+ expected: "12:34:56.123456789",
+ },
+ {
+ name: "With timezone",
+ rawStr: "12:34:56.123+02:00",
+ acc: 6,
+ expected: "12:34:56.123000+02:00",
+ },
+ {
+ name: "With negative timezone",
+ rawStr: "12:34:56.123-02:00",
+ acc: 6,
+ expected: "12:34:56.123000-02:00",
+ },
+ }
+
+ for _, test := range tests {
+ got := padZeroes(test.rawStr, test.acc)
+ require.Equal(t, test.expected, got)
+ }
+}