Skip to content

Commit

Permalink
expression: fix cast unsigned integer to decimal (#7792)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeRushing authored and zz-jason committed Sep 27, 2018
1 parent eb0c663 commit bca8c27
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func (b *builtinCastIntAsDecimalSig) evalDecimal(row chunk.Row) (res *types.MyDe
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
if !mysql.HasUnsignedFlag(b.tp.Flag) {
if !mysql.HasUnsignedFlag(b.tp.Flag) && !mysql.HasUnsignedFlag(b.args[0].GetType().Flag) {
res = types.NewDecFromInt(val)
} else if b.inUnion && val < 0 {
res = &types.MyDecimal{}
Expand Down
20 changes: 20 additions & 0 deletions expression/builtin_cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,26 @@ func (s *testEvaluatorSuite) TestCast(c *C) {
c.Assert(terror.ErrorEqual(types.ErrOverflow, lastWarn.Err), IsTrue, Commentf("err %v", lastWarn.Err))
sc = origSc

// create table tt(a bigint unsigned);
// insert into tt values(18446744073709551615);
// select cast(a as decimal(65, 0)) from tt;
ft = &types.FieldType{
Tp: mysql.TypeNewDecimal,
Flag: mysql.BinaryFlag,
Charset: charset.CharsetBin,
Collate: charset.CollationBin,
Flen: 65,
Decimal: 0,
}
rt := types.NewFieldType(mysql.TypeLonglong)
rt.Flag = mysql.BinaryFlag | mysql.UnsignedFlag
f = BuildCastFunction(ctx, &Constant{Value: types.NewUintDatum(18446744073709551615), RetType: rt}, ft)
res, err = f.Eval(chunk.Row{})
c.Assert(err, IsNil)
u, err := res.GetMysqlDecimal().ToUint()
c.Assert(err, IsNil)
c.Assert(u == 18446744073709551615, IsTrue)

// cast(bad_string as decimal)
for _, s := range []string{"hello", ""} {
f = BuildCastFunction(ctx, &Constant{Value: types.NewDatum(s), RetType: types.NewFieldType(mysql.TypeDecimal)}, tp)
Expand Down

0 comments on commit bca8c27

Please sign in to comment.