From 93d0bae1616cf429bc8013e0f15b7d648734036a Mon Sep 17 00:00:00 2001 From: H-ZeX Date: Thu, 15 Aug 2019 12:16:16 +0800 Subject: [PATCH] fix some bugs Signed-off-by: H-ZeX --- expression/builtin_cast.go | 4 ++-- expression/integration_test.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/expression/builtin_cast.go b/expression/builtin_cast.go index be3b5273330b3..8b3723d3357e8 100644 --- a/expression/builtin_cast.go +++ b/expression/builtin_cast.go @@ -463,7 +463,7 @@ func (b *builtinCastIntAsRealSig) evalReal(row chunk.Row) (res float64, isNull b } if !mysql.HasUnsignedFlag(b.tp.Flag) && !mysql.HasUnsignedFlag(b.args[0].GetType().Flag) { res = float64(val) - } else if b.inUnion && val < 0 { + } else if b.inUnion && !mysql.HasUnsignedFlag(b.args[0].GetType().Flag) && val < 0 { res = 0 } else { // recall that, int to float is different from uint to float @@ -489,7 +489,7 @@ func (b *builtinCastIntAsDecimalSig) evalDecimal(row chunk.Row) (res *types.MyDe } if !mysql.HasUnsignedFlag(b.tp.Flag) && !mysql.HasUnsignedFlag(b.args[0].GetType().Flag) { res = types.NewDecFromInt(val) - } else if b.inUnion && val < 0 { + } else if b.inUnion && !mysql.HasUnsignedFlag(b.args[0].GetType().Flag) && val < 0 { res = &types.MyDecimal{} } else { res = types.NewDecFromUint(uint64(val)) diff --git a/expression/integration_test.go b/expression/integration_test.go index 59bdae56242d7..193df367ef288 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -2226,6 +2226,22 @@ func (s *testIntegrationSuite) TestBuiltin(c *C) { result.Check(testkit.Rows("9223372036854775808 9223372036854775808", "9223372036854775808 9223372036854775808")) tk.MustExec(`drop table tb5;`) + // test builtinCastIntAsDecimalSig + tk.MustExec(`drop table if exists tb5`) + tk.MustExec(`create table tb5 (a decimal(65), b bigint(64) unsigned);`) + tk.MustExec(`insert into tb5 (a, b) values (9223372036854775808, 9223372036854775808);`) + result = tk.MustQuery(`select cast(b as decimal(64)) from tb5 union all select b from tb5;`) + result.Check(testkit.Rows("9223372036854775808", "9223372036854775808")) + tk.MustExec(`drop table tb5`) + + // test builtinCastIntAsRealSig + tk.MustExec(`drop table if exists tb5`) + tk.MustExec(`create table tb5 (a bigint(64) unsigned, b double(64, 10));`) + tk.MustExec(`insert into tb5 (a, b) values (9223372036854775808, 9223372036854775808);`) + result = tk.MustQuery(`select a from nb1 where a = b union all select b from nb1;`) + result.Check(testkit.Rows("9223372036854776000", "9223372036854776000")) + tk.MustExec(`drop table tb5`) + // Test corner cases of cast string as datetime result = tk.MustQuery(`select cast("170102034" as datetime);`) result.Check(testkit.Rows("2017-01-02 03:04:00"))