fix decimal normalization, revert sql time normalization

This commit is contained in:
Andy Arthur
2022-05-31 11:02:16 -07:00
parent f05aa5611a
commit 9b4e2e5b3e
2 changed files with 32 additions and 25 deletions

View File

@@ -76,31 +76,20 @@ func TestSingleQuery(t *testing.T) {
// Convenience test for debugging a single query. Unskip and set to the desired query.
func TestSingleScript(t *testing.T) {
t.Skip()
var scripts = []queries.ScriptTest{
{
Name: "Create table with BIT, ENUM, and SET types",
Name: "Create table with TIME type",
SetUpScript: []string{
"create table my_types (" +
"pk int primary key, " +
"c0 bit(64), " +
"c1 set('a','b','c'), " +
"c2 enum('x','y','z'));",
"create table my_types (pk int primary key, c0 time);",
},
Assertions: []queries.ScriptTestAssertion{
{
Query: "insert into my_types values " +
"(1, b'010101', 'a,b', 'x')," +
"(2, b'101010', 'b,c', 'z');",
Expected: []sql.Row{{sql.OkResult{RowsAffected: 2, InsertID: 0}}},
Query: "INSERT INTO my_types VALUES (1, '11:22:33.444444');",
Expected: []sql.Row{{sql.OkResult{RowsAffected: 1, InsertID: 0}}},
},
{
Query: "select * from my_types",
Expected: []sql.Row{
{int64(1), uint64(21), "a,b", "x"},
{int64(2), uint64(42), "b,c", "z"},
},
Query: "UPDATE my_types SET c0='11:22' WHERE pk=1;",
Expected: []sql.Row{{sql.OkResult{RowsAffected: 1, Info: plan.UpdateInfo{Matched: 1, Updated: 1, Warnings: 0}}}},
},
},
},

View File

@@ -22,7 +22,9 @@ import (
"github.com/dolthub/go-mysql-server/sql"
"github.com/shopspring/decimal"
"github.com/dolthub/dolt/go/libraries/doltcore/schema/typeinfo"
geo "github.com/dolthub/dolt/go/store/geometry"
"github.com/dolthub/dolt/go/store/types"
"github.com/dolthub/dolt/go/store/val"
)
@@ -35,9 +37,7 @@ func DenormalizeRow(sch sql.Schema, row sql.Row) (sql.Row, error) {
}
switch typ := sch[i].Type.(type) {
case sql.DecimalType:
row[i], err = decimal.NewFromString(row[i].(string))
case sql.TimeType:
row[i] = typ.Unmarshal(row[i].(int64))
row[i] = row[i].(decimal.Decimal).String()
case sql.EnumType:
row[i], err = typ.Unmarshal(int64(row[i].(uint16)))
case sql.SetType:
@@ -60,9 +60,7 @@ func NormalizeRow(sch sql.Schema, row sql.Row) (sql.Row, error) {
}
switch typ := sch[i].Type.(type) {
case sql.DecimalType:
row[i] = row[i].(decimal.Decimal).String()
case sql.TimeType:
row[i], err = typ.Marshal(row[i])
row[i], err = decimal.NewFromString(row[i].(string))
case sql.EnumType:
var v int64
v, err = typ.Marshal(row[i])
@@ -111,7 +109,11 @@ func GetField(td val.TupleDesc, i int, tup val.Tuple) (v interface{}, err error)
case val.DateEnc:
v, ok = td.GetDate(i, tup)
case val.TimeEnc:
v, ok = td.GetSqlTime(i, tup)
var t int64
t, ok = td.GetSqlTime(i, tup)
if ok {
v, err = deserializeTime(t)
}
case val.DatetimeEnc:
v, ok = td.GetDatetime(i, tup)
case val.EnumEnc:
@@ -184,7 +186,11 @@ func PutField(tb *val.TupleBuilder, i int, v interface{}) error {
case val.DateEnc:
tb.PutDate(i, v.(time.Time))
case val.TimeEnc:
tb.PutSqlTime(i, v.(int64))
t, err := serializeTime(v)
if err != nil {
return err
}
tb.PutSqlTime(i, t)
case val.DatetimeEnc:
tb.PutDatetime(i, v.(time.Time))
case val.EnumEnc:
@@ -302,3 +308,15 @@ func convJson(v interface{}) (buf []byte, err error) {
}
return json.Marshal(v.(sql.JSONDocument).Val)
}
func deserializeTime(v int64) (interface{}, error) {
return typeinfo.TimeType.ConvertNomsValueToValue(types.Int(v))
}
func serializeTime(v interface{}) (int64, error) {
i, err := typeinfo.TimeType.ConvertValueToNomsValue(nil, nil, v)
if err != nil {
return 0, err
}
return int64(i.(types.Int)), nil
}