diff --git a/types/convert.go b/types/convert.go index a3e5b572e0add..6ae428d7242c0 100644 --- a/types/convert.go +++ b/types/convert.go @@ -246,7 +246,7 @@ func convertDecimalStrToUint(sc *stmtctx.StatementContext, str string, upperBoun if intStr == "" { intStr = "0" } - if sc.ShouldClipToZero() && intStr[0] == '-' { + if intStr[0] == '-' { return 0, overflow(str, tp) } @@ -255,8 +255,7 @@ func convertDecimalStrToUint(sc *stmtctx.StatementContext, str string, upperBoun round++ } - upperBound -= round - upperStr := strconv.FormatUint(upperBound, 10) + upperStr := strconv.FormatUint(upperBound-round, 10) if len(intStr) > len(upperStr) || (len(intStr) == len(upperStr) && intStr > upperStr) { return upperBound, overflow(str, tp) diff --git a/types/convert_test.go b/types/convert_test.go index b28560dfbdf75..db9cb2ba691fd 100644 --- a/types/convert_test.go +++ b/types/convert_test.go @@ -1275,8 +1275,9 @@ func TestConvertDecimalStrToUint(t *testing.T) { {"9223372036854775807.4999", 9223372036854775807, true}, {"18446744073709551614.55", 18446744073709551615, true}, {"18446744073709551615.344", 18446744073709551615, true}, - {"18446744073709551615.544", 0, false}, + {"18446744073709551615.544", 18446744073709551615, false}, {"-111.111", 0, false}, + {"-10000000000000000000.0", 0, false}, } for _, ca := range cases { result, err := convertDecimalStrToUint(&stmtctx.StatementContext{}, ca.input, math.MaxUint64, 0) @@ -1284,7 +1285,15 @@ func TestConvertDecimalStrToUint(t *testing.T) { require.Error(t, err) } else { require.NoError(t, err) - require.Equal(t, ca.result, result) } + require.Equal(t, ca.result, result, "input=%v", ca.input) } + + result, err := convertDecimalStrToUint(&stmtctx.StatementContext{}, "-99.0", math.MaxUint8, 0) + require.Error(t, err) + require.Equal(t, uint64(0), result) + + result, err = convertDecimalStrToUint(&stmtctx.StatementContext{}, "-100.0", math.MaxUint8, 0) + require.Error(t, err) + require.Equal(t, uint64(0), result) }