Skip to content

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

chore: add columnType to convertValue; add accuracy to timestamp display (#15107)

Vincent Huang10ca31d1dc1c
diff --git a/backend/plugin/db/clickhouse/query.go b/backend/plugin/db/clickhouse/query.go
index 212f514cc5..050f7efae7 100644
--- a/backend/plugin/db/clickhouse/query.go
+++ b/backend/plugin/db/clickhouse/query.go
@@ -113,7 +113,7 @@ func makeValueByTypeName(typeName string, columnType *sql.ColumnType) any {
return reflect.New(columnType.ScanType()).Interface()
}
-func convertValue(_ string, value any) *v1pb.RowValue {
+func convertValue(_ string, _ *sql.ColumnType, value any) *v1pb.RowValue {
// handle TUPLE ARRAY MAP
if v, ok := value.(*any); ok && v != nil {
value, err := json.Marshal(v)
diff --git a/backend/plugin/db/cockroachdb/query.go b/backend/plugin/db/cockroachdb/query.go
index 78bf9a4e67..3ab1ebd911 100644
--- a/backend/plugin/db/cockroachdb/query.go
+++ b/backend/plugin/db/cockroachdb/query.go
@@ -28,7 +28,7 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {
}
}
-func convertValue(typeName string, value any) *v1pb.RowValue {
+func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {
switch raw := value.(type) {
case *sql.NullString:
if raw.Valid {
@@ -72,11 +72,13 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
}
case *sql.NullTime:
if raw.Valid {
+ _, scale, _ := columnType.DecimalSize()
if typeName == "TIMESTAMP" {
return &v1pb.RowValue{
Kind: &v1pb.RowValue_TimestampValue{
TimestampValue: &v1pb.RowValue_Timestamp{
GoogleTimestamp: timestamppb.New(raw.Time),
+ Accuracy: int32(scale),
},
},
}
@@ -88,6 +90,7 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
GoogleTimestamp: timestamppb.New(raw.Time),
Zone: zone,
Offset: int32(offset),
+ Accuracy: int32(scale),
},
},
}
diff --git a/backend/plugin/db/dm/query.go b/backend/plugin/db/dm/query.go
index f7d76cea17..99377dcf8e 100644
--- a/backend/plugin/db/dm/query.go
+++ b/backend/plugin/db/dm/query.go
@@ -34,7 +34,7 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {
}
}
-func convertValue(typeName string, value any) *v1pb.RowValue {
+func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {
switch raw := value.(type) {
case *sql.NullString:
if raw.Valid {
@@ -78,11 +78,13 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
}
case *sql.NullTime:
if raw.Valid {
+ _, scale, _ := columnType.DecimalSize()
if typeName == "DATE" || typeName == "TIMESTAMPDTY" {
return &v1pb.RowValue{
Kind: &v1pb.RowValue_TimestampValue{
TimestampValue: &v1pb.RowValue_Timestamp{
GoogleTimestamp: timestamppb.New(raw.Time),
+ Accuracy: int32(scale),
},
},
}
@@ -99,6 +101,7 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
Kind: &v1pb.RowValue_TimestampValue{
TimestampValue: &v1pb.RowValue_Timestamp{
GoogleTimestamp: timestamppb.New(t),
+ Accuracy: int32(scale),
},
},
}
@@ -110,6 +113,7 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
GoogleTimestamp: timestamppb.New(raw.Time),
Zone: zone,
Offset: int32(offset),
+ Accuracy: int32(scale),
},
},
}
diff --git a/backend/plugin/db/mssql/query.go b/backend/plugin/db/mssql/query.go
index 932c089637..fea2e89408 100644
--- a/backend/plugin/db/mssql/query.go
+++ b/backend/plugin/db/mssql/query.go
@@ -61,7 +61,7 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {
return new(sql.NullString)
}
-func convertValue(typeName string, value any) *v1pb.RowValue {
+func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {
switch raw := value.(type) {
case *sql.NullString:
if raw.Valid {
@@ -117,11 +117,13 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
}
case *sql.NullTime:
if raw.Valid {
+ _, scale, _ := columnType.DecimalSize()
if typeName == "DATETIME" || typeName == "DATETIME2" || typeName == "SMALLDATETIME" {
return &v1pb.RowValue{
Kind: &v1pb.RowValue_TimestampValue{
TimestampValue: &v1pb.RowValue_Timestamp{
GoogleTimestamp: timestamppb.New(raw.Time),
+ Accuracy: int32(scale),
},
},
}
@@ -133,6 +135,7 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
GoogleTimestamp: timestamppb.New(raw.Time),
Zone: zone,
Offset: int32(offset),
+ Accuracy: int32(scale),
},
},
}
diff --git a/backend/plugin/db/mysql/query.go b/backend/plugin/db/mysql/query.go
index b025126309..4a6a03be2d 100644
--- a/backend/plugin/db/mysql/query.go
+++ b/backend/plugin/db/mysql/query.go
@@ -36,10 +36,11 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {
}
}
-func convertValue(typeName string, value any) *v1pb.RowValue {
+func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {
switch raw := value.(type) {
case *sql.NullString:
if raw.Valid {
+ _, scale, _ := columnType.DecimalSize()
if typeName == "TIMESTAMP" || typeName == "DATETIME" {
t, err := time.Parse(time.DateTime, raw.String)
if err != nil {
@@ -50,6 +51,7 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
Kind: &v1pb.RowValue_TimestampValue{
TimestampValue: &v1pb.RowValue_Timestamp{
GoogleTimestamp: timestamppb.New(t),
+ Accuracy: int32(scale),
},
},
}
diff --git a/backend/plugin/db/obo/query.go b/backend/plugin/db/obo/query.go
index 65de4b03b6..782be02306 100644
--- a/backend/plugin/db/obo/query.go
+++ b/backend/plugin/db/obo/query.go
@@ -34,7 +34,7 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {
}
}
-func convertValue(typeName string, value any) *v1pb.RowValue {
+func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {
switch raw := value.(type) {
case *sql.NullString:
if raw.Valid {
@@ -78,11 +78,13 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
}
case *sql.NullTime:
if raw.Valid {
+ _, scale, _ := columnType.DecimalSize()
if typeName == "DATE" || typeName == "TIMESTAMPDTY" {
return &v1pb.RowValue{
Kind: &v1pb.RowValue_TimestampValue{
TimestampValue: &v1pb.RowValue_Timestamp{
GoogleTimestamp: timestamppb.New(raw.Time),
+ Accuracy: int32(scale),
},
},
}
@@ -99,6 +101,7 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
Kind: &v1pb.RowValue_TimestampValue{
TimestampValue: &v1pb.RowValue_Timestamp{
GoogleTimestamp: timestamppb.New(t),
+ Accuracy: int32(scale),
},
},
}
@@ -110,6 +113,7 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
GoogleTimestamp: timestamppb.New(raw.Time),
Zone: zone,
Offset: int32(offset),
+ Accuracy: int32(scale),
},
},
}
diff --git a/backend/plugin/db/oracle/query.go b/backend/plugin/db/oracle/query.go
index a69d206b96..187a3fc8d9 100644
--- a/backend/plugin/db/oracle/query.go
+++ b/backend/plugin/db/oracle/query.go
@@ -46,7 +46,7 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {
}
}
-func convertValue(typeName string, value any) *v1pb.RowValue {
+func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {
switch raw := value.(type) {
case *sql.NullString:
if raw.Valid {
@@ -93,12 +93,14 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
// The go-ora driver retrieves the database timezone from the wire protocol and appends it to the timestamp.
// To ensure consistency with Oracle Date expectations, we should remove the timezone information.
// https://github.com/sijms/go-ora/blob/2962e725e7a756a667a546fb360ef09afd4c8bd0/v2/parameter.go#L616
+ _, scale, _ := columnType.DecimalSize()
if typeName == "DATE" || typeName == "TIMESTAMPDTY" {
timeStripped := time.Date(raw.Time.Year(), raw.Time.Month(), raw.Time.Day(), raw.Time.Hour(), raw.Time.Minute(), raw.Time.Second(), raw.Time.Nanosecond(), time.UTC)
return &v1pb.RowValue{
Kind: &v1pb.RowValue_TimestampValue{
TimestampValue: &v1pb.RowValue_Timestamp{
GoogleTimestamp: timestamppb.New(timeStripped),
+ Accuracy: int32(scale),
},
},
}
@@ -115,6 +117,7 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
Kind: &v1pb.RowValue_TimestampValue{
TimestampValue: &v1pb.RowValue_Timestamp{
GoogleTimestamp: timestamppb.New(t),
+ Accuracy: int32(scale),
},
},
}
@@ -126,6 +129,7 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
GoogleTimestamp: timestamppb.New(raw.Time),
Zone: zone,
Offset: int32(offset),
+ Accuracy: int32(scale),
},
},
}
diff --git a/backend/plugin/db/pg/query.go b/backend/plugin/db/pg/query.go
index b5501b5c06..83fe413bf9 100644
--- a/backend/plugin/db/pg/query.go
+++ b/backend/plugin/db/pg/query.go
@@ -30,7 +30,7 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {
}
}
-func convertValue(typeName string, value any) *v1pb.RowValue {
+func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {
switch raw := value.(type) {
case *sql.NullString:
if raw.Valid {
@@ -81,11 +81,13 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
},
}
}
+ _, scale, _ := columnType.DecimalSize()
if typeName == "TIMESTAMP" {
return &v1pb.RowValue{
Kind: &v1pb.RowValue_TimestampValue{
TimestampValue: &v1pb.RowValue_Timestamp{
GoogleTimestamp: timestamppb.New(raw.Time),
+ Accuracy: int32(scale),
},
},
}
@@ -97,6 +99,7 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
GoogleTimestamp: timestamppb.New(raw.Time),
Zone: zone,
Offset: int32(offset),
+ Accuracy: int32(scale),
},
},
}
diff --git a/backend/plugin/db/redshift/query.go b/backend/plugin/db/redshift/query.go
index 3d639921f6..05efdad048 100644
--- a/backend/plugin/db/redshift/query.go
+++ b/backend/plugin/db/redshift/query.go
@@ -28,7 +28,7 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {
}
}
-func convertValue(typeName string, value any) *v1pb.RowValue {
+func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {
switch raw := value.(type) {
case *sql.NullString:
if raw.Valid {
@@ -72,11 +72,13 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
}
case *sql.NullTime:
if raw.Valid {
+ _, scale, _ := columnType.DecimalSize()
if typeName == "TIMESTAMP" {
return &v1pb.RowValue{
Kind: &v1pb.RowValue_TimestampValue{
TimestampValue: &v1pb.RowValue_Timestamp{
GoogleTimestamp: timestamppb.New(raw.Time),
+ Accuracy: int32(scale),
},
},
}
@@ -88,6 +90,7 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
GoogleTimestamp: timestamppb.New(raw.Time),
Zone: zone,
Offset: int32(offset),
+ Accuracy: int32(scale),
},
},
}
diff --git a/backend/plugin/db/risingwave/query.go b/backend/plugin/db/risingwave/query.go
index f12d63062e..ac6c588b80 100644
--- a/backend/plugin/db/risingwave/query.go
+++ b/backend/plugin/db/risingwave/query.go
@@ -28,7 +28,7 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {
}
}
-func convertValue(typeName string, value any) *v1pb.RowValue {
+func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {
switch raw := value.(type) {
case *sql.NullString:
if raw.Valid {
@@ -72,11 +72,13 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
}
case *sql.NullTime:
if raw.Valid {
+ _, scale, _ := columnType.DecimalSize()
if typeName == "TIMESTAMP" {
return &v1pb.RowValue{
Kind: &v1pb.RowValue_TimestampValue{
TimestampValue: &v1pb.RowValue_Timestamp{
GoogleTimestamp: timestamppb.New(raw.Time),
+ Accuracy: int32(scale),
},
},
}
@@ -88,6 +90,7 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
GoogleTimestamp: timestamppb.New(raw.Time),
Zone: zone,
Offset: int32(offset),
+ Accuracy: int32(scale),
},
},
}
diff --git a/backend/plugin/db/starrocks/query.go b/backend/plugin/db/starrocks/query.go
index 6b4f4180ce..be93db3949 100644
--- a/backend/plugin/db/starrocks/query.go
+++ b/backend/plugin/db/starrocks/query.go
@@ -36,11 +36,12 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {
}
}
-func convertValue(typeName string, value any) *v1pb.RowValue {
+func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {
switch raw := value.(type) {
case *sql.NullString:
if raw.Valid {
if typeName == "TIMESTAMP" || typeName == "DATETIME" {
+ _, scale, _ := columnType.DecimalSize()
t, err := time.Parse(time.DateTime, raw.String)
if err != nil {
slog.Error("failed to parse time value", log.BBError(err))
@@ -50,6 +51,7 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
Kind: &v1pb.RowValue_TimestampValue{
TimestampValue: &v1pb.RowValue_Timestamp{
GoogleTimestamp: timestamppb.New(t),
+ Accuracy: int32(scale),
},
},
}
diff --git a/backend/plugin/db/tidb/query.go b/backend/plugin/db/tidb/query.go
index e67b36bca9..4a4941168f 100644
--- a/backend/plugin/db/tidb/query.go
+++ b/backend/plugin/db/tidb/query.go
@@ -37,10 +37,11 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {
}
}
-func convertValue(typeName string, value any) *v1pb.RowValue {
+func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {
switch raw := value.(type) {
case *sql.NullString:
if raw.Valid {
+ _, scale, _ := columnType.DecimalSize()
if typeName == "TIMESTAMP" || typeName == "DATETIME" {
t, err := time.Parse(time.DateTime, raw.String)
if err != nil {
@@ -51,6 +52,7 @@ func convertValue(typeName string, value any) *v1pb.RowValue {
Kind: &v1pb.RowValue_TimestampValue{
TimestampValue: &v1pb.RowValue_Timestamp{
GoogleTimestamp: timestamppb.New(t),
+ Accuracy: int32(scale),
},
},
}
diff --git a/backend/plugin/db/util/driverutil.go b/backend/plugin/db/util/driverutil.go
index 5d48f15461..ee37315536 100644
--- a/backend/plugin/db/util/driverutil.go
+++ b/backend/plugin/db/util/driverutil.go
@@ -45,7 +45,7 @@ var NullRowValue = &v1pb.RowValue{
},
}
-func RowsToQueryResult(rows *sql.Rows, valueMaker func(string, *sql.ColumnType) any, rowValueConverter func(string, any) *v1pb.RowValue, limit int64) (*v1pb.QueryResult, error) {
+func RowsToQueryResult(rows *sql.Rows, valueMaker func(string, *sql.ColumnType) any, rowValueConverter func(string, *sql.ColumnType, any) *v1pb.RowValue, limit int64) (*v1pb.QueryResult, error) {
columnNames, err := rows.Columns()
if err != nil {
return nil, err
@@ -79,7 +79,7 @@ func RowsToQueryResult(rows *sql.Rows, valueMaker func(string, *sql.ColumnType)
row := &v1pb.QueryRow{}
for i := 0; i < columnLength; i++ {
- row.Values = append(row.Values, rowValueConverter(columnTypeNames[i], values[i]))
+ row.Values = append(row.Values, rowValueConverter(columnTypeNames[i], columnTypes[i], values[i]))
}
result.Rows = append(result.Rows, row)
@@ -115,7 +115,7 @@ func MakeCommonValueByTypeName(typeName string, _ *sql.ColumnType) any {
}
}
-func ConvertCommonValue(_ string, value any) *v1pb.RowValue {
+func ConvertCommonValue(_ string, _ *sql.ColumnType, value any) *v1pb.RowValue {
switch raw := value.(type) {
case *sql.NullString:
if raw.Valid {
diff --git a/frontend/src/utils/v1/sql.ts b/frontend/src/utils/v1/sql.ts
index e1c2cbcb96..e2465f3db8 100644
--- a/frontend/src/utils/v1/sql.ts
+++ b/frontend/src/utils/v1/sql.ts
@@ -2,10 +2,10 @@ import dayjs from "dayjs";
import Long from "long";
import { getDateForPbTimestamp } from "@/types";
import { NullValue } from "@/types/proto/google/protobuf/struct";
-import type { Timestamp } from "@/types/proto/google/protobuf/timestamp";
import { Engine } from "@/types/proto/v1/common";
import {
RowValue,
+ type RowValue_Timestamp,
type RowValue_TimestampTZ,
} from "@/types/proto/v1/sql_service";
import { isNullOrUndefined } from "../util";
@@ -36,7 +36,7 @@ export const extractSQLRowValuePlain = (value: RowValue | undefined) => {
return binaryString.length === 0 ? "0" : binaryString;
}
if (value.timestampValue && value.timestampValue.googleTimestamp) {
- return formatTimestamp(value.timestampValue.googleTimestamp);
+ return formatTimestamp(value.timestampValue);
}
if (value.timestampTzValue && value.timestampTzValue.googleTimestamp) {
return formatTimestampWithTz(value.timestampTzValue);
@@ -45,12 +45,12 @@ export const extractSQLRowValuePlain = (value: RowValue | undefined) => {
return plainObject[key];
};
-const formatTimestamp = (timestamp: Timestamp) => {
- const fullDayjs = dayjs(getDateForPbTimestamp(timestamp)).utc();
- const microseconds = Math.floor(timestamp.nanos / 1000);
+const formatTimestamp = (timestamp: RowValue_Timestamp) => {
+ const fullDayjs = dayjs(getDateForPbTimestamp(timestamp.googleTimestamp)).utc();
+ const microseconds = Math.floor((timestamp.googleTimestamp?.nanos ?? 0) / Math.pow(10, 9 - timestamp.accuracy));
const formattedTimestamp =
microseconds > 0
- ? `${fullDayjs.format("YYYY-MM-DD HH:mm:ss")}.${microseconds.toString().padStart(6, "0")}`
+ ? `${fullDayjs.format("YYYY-MM-DD HH:mm:ss")}.${microseconds.toString().padStart(timestamp.accuracy, "0")}`
: `${fullDayjs.format("YYYY-MM-DD HH:mm:ss")}`;
return formattedTimestamp;
};
@@ -66,13 +66,13 @@ const formatTimestampWithTz = (timestampTzValue: RowValue_TimestampTZ) => {
hourOffset > 0
? `+${timezoneOffsetPrefix}${hourOffset}`
: `-${timezoneOffsetPrefix}${Math.abs(hourOffset)}`;
-
+ timestampTzValue.accuracy = (timestampTzValue.accuracy === 0) ? 6 : timestampTzValue.accuracy;
const microseconds = Math.floor(
- (timestampTzValue.googleTimestamp?.nanos ?? 0) / 1000
+ (timestampTzValue.googleTimestamp?.nanos ?? 0) / Math.pow(10, 9 - timestampTzValue.accuracy)
);
const formattedTimestamp =
microseconds > 0
- ? `${fullDayjs.format("YYYY-MM-DD HH:mm:ss")}.${microseconds.toString().padStart(6, "0")}${timezoneOffset}`
+ ? `${fullDayjs.format("YYYY-MM-DD HH:mm:ss")}.${microseconds.toString().padStart(timestampTzValue.accuracy, "0")}${timezoneOffset}`
: `${fullDayjs.format("YYYY-MM-DD HH:mm:ss")}${timezoneOffset}`;
return formattedTimestamp;
};