From 85713bd2a6ff64f9ab80ab485030c399c07e84ee Mon Sep 17 00:00:00 2001 From: Shenghui Wu <793703860@qq.com> Date: Wed, 6 Sep 2023 09:30:41 +0800 Subject: [PATCH] This is an automated cherry-pick of #46620 Signed-off-by: ti-chi-bot --- .../integration_test/integration_test.go | 46 +++++++++++++++++++ types/datum.go | 7 +-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/expression/integration_test/integration_test.go b/expression/integration_test/integration_test.go index 7f3537f53b56a..585aa70bcfb04 100644 --- a/expression/integration_test/integration_test.go +++ b/expression/integration_test/integration_test.go @@ -7946,3 +7946,49 @@ 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")) } +<<<<<<< HEAD +======= + +func TestIssue41733AndIssue45410(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")) + + // Issue 45410 + tk.MustExec("create database testIssue45410") + defer tk.MustExec("drop database testIssue45410") + tk.MustExec("use testIssue45410") + + tk.MustExec("DROP TABLE IF EXISTS t1;") + tk.MustExec("CREATE TABLE t1 (c1 TINYINT(1) UNSIGNED NOT NULL );") + tk.MustExec("INSERT INTO t1 VALUES (0);") + tk.MustQuery("SELECT c1>=CAST('-787360724' AS TIME) FROM t1;").Check(testkit.Rows("1")) +} +>>>>>>> ca696229234 (expression: fix wrong result for unsigned non-const int cmp const duration (#46620)) diff --git a/types/datum.go b/types/datum.go index d03aa62464fda..49ab5b0dd570b 100644 --- a/types/datum.go +++ b/types/datum.go @@ -1214,9 +1214,10 @@ func (d *Datum) convertToUint(sc *stmtctx.StatementContext, target *FieldType) ( case KindMysqlDuration: dec := d.GetMysqlDuration().ToNumber() err = dec.Round(dec, 0, ModeHalfUp) - ival, err1 := dec.ToInt() - if err1 == nil { - val, err = ConvertIntToUint(sc, ival, upperBound, tp) + var err1 error + val, err1 = ConvertDecimalToUint(sc, dec, upperBound, tp) + if err == nil { + err = err1 } case KindMysqlDecimal: val, err = ConvertDecimalToUint(sc, d.GetMysqlDecimal(), upperBound, tp)