From 075a47e17652ffcc976f032fece82322bee0ead8 Mon Sep 17 00:00:00 2001 From: jiyfhust Date: Fri, 28 Jul 2023 18:43:34 +0800 Subject: [PATCH] types: fix convert float to uint (#42063) close pingcap/tidb#41733 --- .../integration_serial_test.go | 2 +- .../integration_test/integration_test.go | 33 +++++++++++++++++++ types/convert.go | 16 +++------ 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/expression/integration_serial_test/integration_serial_test.go b/expression/integration_serial_test/integration_serial_test.go index 4ef1c68606..8ffa5c1255 100644 --- a/expression/integration_serial_test/integration_serial_test.go +++ b/expression/integration_serial_test/integration_serial_test.go @@ -3060,7 +3060,7 @@ func TestBuiltin(t *testing.T) { _ = tk.MustQuery(`select * from tb5 where cast(a as unsigned int)=b;`) // TODO `obtained string = "[18446744073709552000 18446744073709551615]` // result.Check(testkit.Rows("18446744073709551616 18446744073709551615")) - tk.MustQuery("show warnings;").Check(testkit.Rows()) + tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1690 constant 1.8446744073709552e+19 overflows bigint")) tk.MustExec(`drop table tb5;`) // test builtinCastJSONAsIntSig diff --git a/expression/integration_test/integration_test.go b/expression/integration_test/integration_test.go index 652c4a75cb..b42aa4e360 100644 --- a/expression/integration_test/integration_test.go +++ b/expression/integration_test/integration_test.go @@ -7954,3 +7954,36 @@ func TestIfFunctionWithNull(t *testing.T) { tk.MustQuery("select min(if(apply_to_now_days <= 30,loan,null)) as min, max(if(apply_to_now_days <= 720,loan,null)) as max from (select loan, datediff(from_unixtime(unix_timestamp('2023-05-18 18:43:43') + 18000), from_unixtime(apply_time/1000 + 18000)) as apply_to_now_days from orders) t1;").Sort().Check( testkit.Rows("20000 35100")) } + +func TestIssue41733(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("create database testIssue41733") + defer tk.MustExec("drop database testIssue41733") + tk.MustExec("use testIssue41733") + + tk.MustExec("create table t_tiny (c0 TINYINT UNSIGNED)") + tk.MustExec("INSERT IGNORE INTO t_tiny(c0) VALUES (1E9)") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1264 Out of range value for column 'c0' at row 1")) + tk.MustQuery("select * from t_tiny;").Check(testkit.Rows("255")) + + tk.MustExec("create table t_small (c0 SMALLINT UNSIGNED)") + tk.MustExec("INSERT IGNORE INTO t_small(c0) VALUES (1E9)") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1264 Out of range value for column 'c0' at row 1")) + tk.MustQuery("select * from t_small;").Check(testkit.Rows("65535")) + + tk.MustExec("create table t_medium (c0 MEDIUMINT UNSIGNED)") + tk.MustExec("INSERT IGNORE INTO t_medium(c0) VALUES (1E9)") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1264 Out of range value for column 'c0' at row 1")) + tk.MustQuery("select * from t_medium;").Check(testkit.Rows("16777215")) + + tk.MustExec("create table t_int (c0 INT UNSIGNED)") + tk.MustExec("INSERT IGNORE INTO t_int(c0) VALUES (1E20)") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1264 Out of range value for column 'c0' at row 1")) + tk.MustQuery("select * from t_int;").Check(testkit.Rows("4294967295")) + + tk.MustExec("create table t_big (c0 BIGINT UNSIGNED)") + tk.MustExec("INSERT IGNORE INTO t_big(c0) VALUES (1E20)") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1264 Out of range value for column 'c0' at row 1")) + tk.MustQuery("select * from t_big;").Check(testkit.Rows("18446744073709551615")) +} diff --git a/types/convert.go b/types/convert.go index 862997c231..572333433b 100644 --- a/types/convert.go +++ b/types/convert.go @@ -20,6 +20,7 @@ package types import ( "math" + "math/big" "strconv" "strings" @@ -175,18 +176,11 @@ func ConvertFloatToUint(sc *stmtctx.StatementContext, fval float64, upperBound u return uint64(int64(val)), overflow(val, tp) } - ubf := float64(upperBound) - // Because math.MaxUint64 can not be represented precisely in iee754(64bit), - // so `float64(math.MaxUint64)` will make a num bigger than math.MaxUint64, - // which can not be represented by 64bit integer. - // So `uint64(float64(math.MaxUint64))` is undefined behavior. - if val == ubf { - return math.MaxUint64, nil + ret, acc := new(big.Float).SetFloat64(val).Uint64() + if acc == big.Below || ret > upperBound { + return upperBound, overflow(val, tp) } - if val > ubf { - return math.MaxUint64, overflow(val, tp) - } - return uint64(val), nil + return ret, nil } // convertScientificNotation converts a decimal string with scientific notation to a normal decimal string.