Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: fix cast unsigned integer to decimal #7792

Merged
merged 2 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it's more likely checking cast(cast(arg as decimal) as unsigned).
Maybe ToString is better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not like cast(cast(arg as decimal) as unsigned) , because when cast(arg as decimal) returns signed, res.GetMysqlDecimal().ToUint() will return range exceed error.

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