types: fix convert float to uint (#42063)

close pingcap/tidb#41733
This commit is contained in:
jiyfhust
2023-07-28 18:43:34 +08:00
committed by GitHub
parent 086952ede8
commit 075a47e176
3 changed files with 39 additions and 12 deletions

View File

@ -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

View File

@ -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"))
}

View File

@ -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.