Skip to content

Commit 2e0d32f

Browse files
authored
fix ConvertJSONToInt unsigned bug (#11483) (#11551)
1 parent bcf1aac commit 2e0d32f

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

expression/integration_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,7 @@ func (s *testIntegrationSuite) TestBuiltin(c *C) {
21732173
result.Check(testkit.Rows("2017-01-01 00:00:00.00"))
21742174
result = tk.MustQuery(`select cast(20170118.999 as datetime);`)
21752175
result.Check(testkit.Rows("2017-01-18 00:00:00"))
2176+
tk.MustQuery(`select convert(a2.a, unsigned int) from (select cast('"9223372036854775808"' as json) as a) as a2;`)
21762177

21772178
tk.MustExec(`create table tb5(a bigint(64) unsigned, b double);`)
21782179
tk.MustExec(`insert into tb5 (a, b) values (9223372036854776000, 9223372036854776000);`)

types/convert.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,11 @@ func ConvertJSONToInt(sc *stmtctx.StatementContext, j json.BinaryJSON, unsigned
566566
return int64(u), errors.Trace(err)
567567
case json.TypeCodeString:
568568
str := string(hack.String(j.GetString()))
569-
return StrToInt(sc, str)
569+
if !unsigned {
570+
return StrToInt(sc, str)
571+
}
572+
u, err := StrToUint(sc, str)
573+
return int64(u), errors.Trace(err)
570574
}
571575
return 0, errors.New("Unknown type code in JSON")
572576
}

types/convert_test.go

+21-15
Original file line numberDiff line numberDiff line change
@@ -706,22 +706,24 @@ func (s *testTypeConvertSuite) TestGetValidInt(c *C) {
706706
tests := []struct {
707707
origin string
708708
valid string
709+
signed bool
709710
warning bool
710711
}{
711-
{"100", "100", false},
712-
{"-100", "-100", false},
713-
{"1abc", "1", true},
714-
{"-1-1", "-1", true},
715-
{"+1+1", "+1", true},
716-
{"123..34", "123", true},
717-
{"123.23E-10", "123", true},
718-
{"1.1e1.3", "1", true},
719-
{"11e1.3", "11", true},
720-
{"1.", "1", true},
721-
{".1", "0", true},
722-
{"", "0", true},
723-
{"123e+", "123", true},
724-
{"123de", "123", true},
712+
{"100", "100", true, false},
713+
{"-100", "-100", true, false},
714+
{"9223372036854775808", "9223372036854775808", false, false},
715+
{"1abc", "1", true, true},
716+
{"-1-1", "-1", true, true},
717+
{"+1+1", "+1", true, true},
718+
{"123..34", "123", true, true},
719+
{"123.23E-10", "123", true, true},
720+
{"1.1e1.3", "1", true, true},
721+
{"11e1.3", "11", true, true},
722+
{"1.", "1", true, true},
723+
{".1", "0", true, true},
724+
{"", "0", true, true},
725+
{"123e+", "123", true, true},
726+
{"123de", "123", true, true},
725727
}
726728
sc := new(stmtctx.StatementContext)
727729
sc.TruncateAsWarning = true
@@ -731,7 +733,11 @@ func (s *testTypeConvertSuite) TestGetValidInt(c *C) {
731733
prefix, err := getValidIntPrefix(sc, tt.origin)
732734
c.Assert(err, IsNil)
733735
c.Assert(prefix, Equals, tt.valid)
734-
_, err = strconv.ParseInt(prefix, 10, 64)
736+
if tt.signed {
737+
_, err = strconv.ParseInt(prefix, 10, 64)
738+
} else {
739+
_, err = strconv.ParseUint(prefix, 10, 64)
740+
}
735741
c.Assert(err, IsNil)
736742
warnings := sc.GetWarnings()
737743
if tt.warning {

0 commit comments

Comments
 (0)