From 225a8fb24d5af5f31d5cf53f4a151a51f1e6bcca Mon Sep 17 00:00:00 2001 From: H-ZeX Date: Sun, 28 Jul 2019 19:52:48 +0800 Subject: [PATCH 1/2] fix StrToInt unsigned bug Signed-off-by: H-ZeX --- expression/integration_test.go | 1 + types/convert.go | 6 +++++- types/convert_test.go | 36 ++++++++++++++++++++-------------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index 7ae1430955acd..ce77511e651c3 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -2145,6 +2145,7 @@ func (s *testIntegrationSuite) TestBuiltin(c *C) { result.Check(testkit.Rows("2017-01-01 00:00:00.00")) result = tk.MustQuery(`select cast(20170118.999 as datetime);`) result.Check(testkit.Rows("2017-01-18 00:00:00")) + tk.MustExec(`select convert(a2.a, unsigned int) from (select cast('"9223372036854775808"' as json) as a) as a2;`) tk.MustExec(`create table tb5(a bigint(64) unsigned, b double);`) tk.MustExec(`insert into tb5 (a, b) values (9223372036854776000, 9223372036854776000);`) diff --git a/types/convert.go b/types/convert.go index c42ebad12229d..1571343f08119 100644 --- a/types/convert.go +++ b/types/convert.go @@ -566,7 +566,11 @@ func ConvertJSONToInt(sc *stmtctx.StatementContext, j json.BinaryJSON, unsigned return int64(u), errors.Trace(err) case json.TypeCodeString: str := string(hack.String(j.GetString())) - return StrToInt(sc, str) + if !unsigned { + return StrToInt(sc, str) + } + u, err := StrToUint(sc, str) + return int64(u), errors.Trace(err) } return 0, errors.New("Unknown type code in JSON") } diff --git a/types/convert_test.go b/types/convert_test.go index 9d292632e518e..3411423942d2d 100644 --- a/types/convert_test.go +++ b/types/convert_test.go @@ -706,22 +706,24 @@ func (s *testTypeConvertSuite) TestGetValidInt(c *C) { tests := []struct { origin string valid string + signed bool warning bool }{ - {"100", "100", false}, - {"-100", "-100", false}, - {"1abc", "1", true}, - {"-1-1", "-1", true}, - {"+1+1", "+1", true}, - {"123..34", "123", true}, - {"123.23E-10", "123", true}, - {"1.1e1.3", "1", true}, - {"11e1.3", "11", true}, - {"1.", "1", true}, - {".1", "0", true}, - {"", "0", true}, - {"123e+", "123", true}, - {"123de", "123", true}, + {"100", "100", true, false}, + {"-100", "-100", true, false}, + {"9223372036854775808", "9223372036854775808", false, false}, + {"1abc", "1", true, true}, + {"-1-1", "-1", true, true}, + {"+1+1", "+1", true, true}, + {"123..34", "123", true, true}, + {"123.23E-10", "123", true, true}, + {"1.1e1.3", "1", true, true}, + {"11e1.3", "11", true, true}, + {"1.", "1", true, true}, + {".1", "0", true, true}, + {"", "0", true, true}, + {"123e+", "123", true, true}, + {"123de", "123", true, true}, } sc := new(stmtctx.StatementContext) sc.TruncateAsWarning = true @@ -731,7 +733,11 @@ func (s *testTypeConvertSuite) TestGetValidInt(c *C) { prefix, err := getValidIntPrefix(sc, tt.origin) c.Assert(err, IsNil) c.Assert(prefix, Equals, tt.valid) - _, err = strconv.ParseInt(prefix, 10, 64) + if tt.signed { + _, err = strconv.ParseInt(prefix, 10, 64) + } else { + _, err = strconv.ParseUint(prefix, 10, 64) + } c.Assert(err, IsNil) warnings := sc.GetWarnings() if tt.warning { From 1d8fc8a0977830e217416aa7ba1a5c3fc31936c7 Mon Sep 17 00:00:00 2001 From: H-ZeX Date: Mon, 29 Jul 2019 17:58:25 +0800 Subject: [PATCH 2/2] change MustExec to MustQuery Signed-off-by: H-ZeX --- expression/integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index ce77511e651c3..704da7561ed8a 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -2145,7 +2145,7 @@ func (s *testIntegrationSuite) TestBuiltin(c *C) { result.Check(testkit.Rows("2017-01-01 00:00:00.00")) result = tk.MustQuery(`select cast(20170118.999 as datetime);`) result.Check(testkit.Rows("2017-01-18 00:00:00")) - tk.MustExec(`select convert(a2.a, unsigned int) from (select cast('"9223372036854775808"' as json) as a) as a2;`) + tk.MustQuery(`select convert(a2.a, unsigned int) from (select cast('"9223372036854775808"' as json) as a) as a2;`) tk.MustExec(`create table tb5(a bigint(64) unsigned, b double);`) tk.MustExec(`insert into tb5 (a, b) values (9223372036854776000, 9223372036854776000);`)