Skip to content

Commit

Permalink
fix ConvertJSONToInt unsigned bug (pingcap#11483)
Browse files Browse the repository at this point in the history
Signed-off-by: H-ZeX <hzx20112012@gmail.com>
  • Loading branch information
H-ZeX committed Oct 30, 2019
1 parent 8d02779 commit ee49873
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
1 change: 1 addition & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2062,6 +2062,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.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);`)
Expand Down
7 changes: 6 additions & 1 deletion types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,12 @@ func ConvertJSONToInt(sc *stmtctx.StatementContext, j json.BinaryJSON, unsigned
u, err := ConvertFloatToUint(sc, f, bound, mysql.TypeLonglong)
return int64(u), errors.Trace(err)
case json.TypeCodeString:
return StrToInt(sc, hack.String(j.GetString()))
str := string(hack.String(j.GetString()))
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")
}
Expand Down
36 changes: 21 additions & 15 deletions types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,22 +733,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
Expand All @@ -758,7 +760,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 {
Expand Down

0 comments on commit ee49873

Please sign in to comment.