From 10a4f67e90133466c33080fd3dfdcef418c76157 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Tue, 12 Sep 2023 12:37:40 +0800 Subject: [PATCH] expression: fix wrong result for unsigned non-const int cmp const duration (#46620) (#46702) close pingcap/tidb#45410 --- expression/integration_test.go | 14 ++++++++++++++ types/datum.go | 7 ++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index 31be5cf384c0d..e16210a1e7bc3 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -7948,3 +7948,17 @@ 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 TestIssue45410(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + // 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")) +} diff --git a/types/datum.go b/types/datum.go index ab29115487ccc..c88cd3b2725a8 100644 --- a/types/datum.go +++ b/types/datum.go @@ -1198,9 +1198,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)