Skip to content

Commit

Permalink
fix wrong flen in cast functions (#19020)
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 authored Aug 30, 2020
1 parent b7682ae commit a363130
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,10 @@ func BuildCastFunction(ctx sessionctx.Context, expr Expression, tp *types.FieldT
return expr
}

if tp.Flen == types.UnspecifiedLength {
tp.Flen = expr.GetType().Flen
}

var fc functionClass
switch tp.EvalType() {
case types.ETInt:
Expand Down
4 changes: 2 additions & 2 deletions expression/typeinfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ func (s *testInferTypeSuite) createTestCase4Constants() []typeInferTestCase {

func (s *testInferTypeSuite) createTestCase4Cast() []typeInferTestCase {
return []typeInferTestCase{
{"CAST(c_int_d AS BINARY)", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag, -1, -1}, // TODO: Flen should be 11.
{"CAST(c_int_d AS BINARY)", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag, 11, -1},
{"CAST(c_int_d AS BINARY(5))", mysql.TypeString, charset.CharsetBin, mysql.BinaryFlag, 5, -1},
{"CAST(c_int_d AS CHAR)", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, -1, -1}, // TODO: Flen should be 11.
{"CAST(c_int_d AS CHAR)", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, 11, -1},
{"CAST(c_int_d AS CHAR(5))", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, 5, -1},
{"CAST(c_int_d AS DATE)", mysql.TypeDate, charset.CharsetBin, mysql.BinaryFlag, 10, 0},
{"CAST(c_int_d AS DATETIME)", mysql.TypeDatetime, charset.CharsetBin, mysql.BinaryFlag, 19, 0},
Expand Down

0 comments on commit a363130

Please sign in to comment.