From 34c0ef683b04c5efb3011fdfdc1b83930491903e Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Tue, 12 Sep 2023 14:16:39 +0800 Subject: [PATCH] expression: fix wrong result for unsigned non-const int cmp const duration (#46620) (#46699) close pingcap/tidb#45410 --- executor/cte_test.go | 2 -- expression/integration_test.go | 13 +++++++++++++ types/datum.go | 7 ++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/executor/cte_test.go b/executor/cte_test.go index 682c89ac925a1..94d912604e9a8 100644 --- a/executor/cte_test.go +++ b/executor/cte_test.go @@ -357,8 +357,6 @@ func TestCTEWithLimit(t *testing.T) { } func TestCTEPanic(t *testing.T) { - t.Parallel() - store, close := testkit.CreateMockStore(t) defer close() tk := testkit.NewTestKit(t, store) diff --git a/expression/integration_test.go b/expression/integration_test.go index 35e81c1245226..c72a36aba5f15 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -10610,3 +10610,16 @@ func (s *testIntegrationSuite) TestIfFunctionWithNull(c *C) { 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 (s *testIntegrationSuite) TestIssue45410(c *C) { + tk := testkit.NewTestKit(c, s.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 e15332f88fc50..6220ec96a75e0 100644 --- a/types/datum.go +++ b/types/datum.go @@ -1089,9 +1089,10 @@ func (d *Datum) convertToUint(sc *stmtctx.StatementContext, target *FieldType) ( case KindMysqlDuration: dec := d.GetMysqlDuration().ToNumber() err = dec.Round(dec, 0, ModeHalfEven) - 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)