Skip to content

Commit 8669364

Browse files
committed
added TypeAwareConversion
1 parent fc441ea commit 8669364

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

enginetest/queries/script_queries.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9393,7 +9393,6 @@ where
93939393
},
93949394
},
93959395
{
9396-
Skip: true,
93979396
Name: "set conversion to strings",
93989397
Dialect: "mysql",
93999398
SetUpScript: []string{
@@ -9429,6 +9428,7 @@ where
94299428
},
94309429
},
94319430
{
9431+
Skip: true,
94329432
Query: "select s from t where s like 'a%' order by s;",
94339433
Expected: []sql.Row{
94349434
{"abc"},
@@ -9518,6 +9518,7 @@ where
95189518
},
95199519
},
95209520
{
9521+
Skip: true,
95219522
Query: "select s, cast(s as char) from t order by s;",
95229523
Expected: []sql.Row{
95239524
{"abc", "abc"},
@@ -9526,6 +9527,7 @@ where
95269527
},
95279528
},
95289529
{
9530+
Skip: true,
95299531
Query: "select s, cast(s as binary) from t order by s;",
95309532
Expected: []sql.Row{
95319533
{"abc", []uint8("abc")},

sql/expression/case.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (c *Case) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
136136
}
137137
// When unable to convert to the type of the case, return the original value
138138
// A common error here is "Out of bounds value for decimal type"
139-
if ret, _, err := t.Convert(ctx, bval); err == nil {
139+
if ret, err := types.TypeAwareConversion(ctx, bval, b.Value.Type(), t); err == nil {
140140
return ret, nil
141141
}
142142
return bval, nil
@@ -150,7 +150,7 @@ func (c *Case) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
150150
}
151151
// When unable to convert to the type of the case, return the original value
152152
// A common error here is "Out of bounds value for decimal type"
153-
if ret, _, err := t.Convert(ctx, val); err == nil {
153+
if ret, err := types.TypeAwareConversion(ctx, val, c.Else.Type(), t); err == nil {
154154
return ret, nil
155155
}
156156
return val, nil

sql/types/conversion.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,3 +735,17 @@ func GeneralizeTypes(a, b sql.Type) sql.Type {
735735
// TODO: decide if we want to make this VarChar to match MySQL, match VarChar length to max of two types
736736
return LongText
737737
}
738+
739+
func TypeAwareConversion(ctx *sql.Context, val interface{}, originalType sql.Type, convertedType sql.Type) (interface{}, error) {
740+
var converted interface{}
741+
var err error
742+
if IsTextOnly(convertedType) {
743+
converted, _, err = ConvertToCollatedString(ctx, val, originalType)
744+
} else {
745+
converted, _, err = convertedType.Convert(ctx, val)
746+
}
747+
if err != nil {
748+
return nil, err
749+
}
750+
return converted, nil
751+
}

0 commit comments

Comments
 (0)