From 5b91a1a630ab32945d6adb541504a507caeb582a Mon Sep 17 00:00:00 2001 From: Zhi Qi Date: Tue, 28 Feb 2023 17:52:42 +0800 Subject: [PATCH 1/3] clip to zero when convert negative to uint --- types/convert.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/convert.go b/types/convert.go index a3e5b572e0add..e98a98276e838 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) } From d1696112a450f59b87be90687c81f324caa1ebf0 Mon Sep 17 00:00:00 2001 From: Zhi Qi Date: Wed, 1 Mar 2023 11:57:12 +0800 Subject: [PATCH 2/3] update tests --- types/convert.go | 3 +-- types/convert_test.go | 5 +++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/types/convert.go b/types/convert.go index e98a98276e838..6ae428d7242c0 100644 --- a/types/convert.go +++ b/types/convert.go @@ -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..85cd4d71e2b12 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,7 @@ 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) } } From 74dc5630b3c8b21bf178105a8bf556ba6976ada0 Mon Sep 17 00:00:00 2001 From: Zhi Qi Date: Wed, 1 Mar 2023 13:13:42 +0800 Subject: [PATCH 3/3] add more tests --- types/convert_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/types/convert_test.go b/types/convert_test.go index 85cd4d71e2b12..db9cb2ba691fd 100644 --- a/types/convert_test.go +++ b/types/convert_test.go @@ -1288,4 +1288,12 @@ func TestConvertDecimalStrToUint(t *testing.T) { } 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) }