Skip to content

Commit

Permalink
fix timestamp format
Browse files Browse the repository at this point in the history
  • Loading branch information
VPolka committed Oct 11, 2024
1 parent 04da8e8 commit a577944
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
14 changes: 10 additions & 4 deletions internal/dst_table/gen_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.uber.org/zap"
"sort"
"strings"
"time"
)

type UpdatingData struct {
Expand Down Expand Up @@ -223,11 +224,16 @@ func ConvertToYDBValue(v json.RawMessage, t ydb_types.Type) (ydb_types.Value, er
}
return nil, fmt.Errorf("ConvertToYDBValue: unmurshal to date value: %w", err)
case ydb_types.TypeTimestamp:
var value uint64
if err = json.Unmarshal(v, &value); err == nil {
return ydb_types.TimestampValue(value), nil
var value_str string
if err = json.Unmarshal(v, &value_str); err != nil {
return nil, fmt.Errorf("ConvertToYDBValue: timestamp: unmurshal to string value: %w", err)
}
format := "2006-01-02T15:04:05.000000Z"
value, err := time.Parse(format, value_str)
if err != nil {
return nil, fmt.Errorf("ConvertToYDBValue: timestamp: error to time parse: %w", err)
}
return nil, fmt.Errorf("ConvertToYDBValue: unmurshal to timestamp value: %w", err)
return ydb_types.TimestampValue(uint64(value.UnixMicro())), nil
case ydb_types.TypeInterval:
var value int64
if err = json.Unmarshal(v, &value); err == nil {
Expand Down
15 changes: 15 additions & 0 deletions internal/dst_table/gen_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func GetTestTableMetaInfo() TableMetaInfo {
"value2": options.Column{"value2", ydb_types.TypeUint64, ""},
"value3": options.Column{"value3", ydb_types.Optional(ydb_types.TypeDouble), ""},
"value4": options.Column{"value4", ydb_types.Optional(ydb_types.TypeString), ""},
"value5": options.Column{"value5", ydb_types.Optional(ydb_types.TypeTimestamp), ""},
}
return result
}
Expand Down Expand Up @@ -163,3 +164,17 @@ func TestGenOnlyEraseQuery(t *testing.T) {
"]}"
assert.Equal(t, expectedParams, query.Parameters.String())
}

func TestGenQueryWithTimestamp(t *testing.T) {
ctx := context.Background()
txData1, _ := reader.ParseTxData(ctx, []byte("{\"update\":{\"value1\":\"15\", \"value5\":\"1970-01-01T00:00:01.000001Z\"},\"key\":[15,\"15\"],\"ts\":[18446744073709551615,18446744073709551615]}"), 0)
query, err := GenQueryFromUpdateTx(ctx, GetTestTableMetaInfo(), []UpdatingData{CreateData(txData1)}, 0, 1)
require.Nil(t, err)
assert.Equal(t, "UPSERT INTO path (key1, key2, value1, value5) SELECT key1, key2, value1, value5 FROM AS_TABLE($p_1_0);\n", query.Statement)
assert.Equal(t, len(query.Params), 1)
assert.Equal(t, query.Params[0].Name(), "$p_1_0")
expectedParams := "[" +
"<|`key1`:15,`key2`:\"15\",`value1`:\"15\",`value5`:Just(Timestamp(\"1970-01-01T00:00:01.000001Z\"))|>" +
"]"
assert.Equal(t, expectedParams, query.Params[0].Value().Yql())
}

0 comments on commit a577944

Please sign in to comment.