From 87a61e60d88933d725b58dd604a7b6731eb754c3 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 96a12f64ca641..8078203c22132 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 6ad19c98162927d242878f02c2d49a9b74100a1a 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 8078203c22132..33eecb82193f7 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 a36ab16f4b5ed..07f1f259a0bef 100644 --- a/types/convert_test.go +++ b/types/convert_test.go @@ -1276,8 +1276,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) @@ -1285,7 +1286,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 ce9ee234dcd6b8971d28cd99d1c03c6b634c11e5 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 07f1f259a0bef..cb13d1c285b2f 100644 --- a/types/convert_test.go +++ b/types/convert_test.go @@ -1289,4 +1289,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) }