From 7bec4c891fa67249070deade763da3a168542e0f Mon Sep 17 00:00:00 2001 From: wshwsh12 <793703860@qq.com> Date: Wed, 17 May 2023 15:28:18 +0800 Subject: [PATCH 1/4] fix --- expression/builtin_cast.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/expression/builtin_cast.go b/expression/builtin_cast.go index a15da8a53cb72..2d5ae991bcdc4 100644 --- a/expression/builtin_cast.go +++ b/expression/builtin_cast.go @@ -2137,6 +2137,11 @@ func WrapWithCastAsInt(ctx sessionctx.Context, expr Expression) Expression { tp.SetDecimal(0) types.SetBinChsClnFlag(tp) tp.AddFlag(expr.GetType().GetFlag() & mysql.UnsignedFlag) + + if expr.GetType().GetType() == mysql.TypeNull { + tp.SetType(mysql.TypeLong) + tp.AddFlag(mysql.UnsignedFlag) + } return BuildCastFunction(ctx, expr, tp) } From 61b6caf36a1f451fffecd2261e6ea70d34dd7f1d Mon Sep 17 00:00:00 2001 From: wshwsh12 <793703860@qq.com> Date: Wed, 17 May 2023 15:34:47 +0800 Subject: [PATCH 2/4] add ut --- expression/integration_test/integration_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/expression/integration_test/integration_test.go b/expression/integration_test/integration_test.go index c422a17c788d9..b4bdcb5ed3af9 100644 --- a/expression/integration_test/integration_test.go +++ b/expression/integration_test/integration_test.go @@ -7933,3 +7933,16 @@ func TestAesDecryptionVecEvalWithZeroChunk(t *testing.T) { tk.MustExec("insert into test values(aes_encrypt('a', 'x'), aes_encrypt('b', 'x'))") tk.MustQuery("SELECT * FROM test WHERE CAST(AES_DECRYPT(name1, 'x') AS CHAR) = '00' AND CAST(AES_DECRYPT(name2, 'x') AS CHAR) = '1'").Check(testkit.Rows()) } + +func TestIfFunctionWithNull(t *testing.T) { + // issue 43805 + store := testkit.CreateMockStore(t) + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("drop table if exists ordres;") + tk.MustExec("CREATE TABLE orders (id bigint(20) unsigned NOT NULL ,account_id bigint(20) unsigned NOT NULL DEFAULT '0' ,loan bigint(20) unsigned NOT NULL DEFAULT '0' ,stage_num int(20) unsigned NOT NULL DEFAULT '0' ,apply_time bigint(20) unsigned NOT NULL DEFAULT '0' ,PRIMARY KEY (id) /*T![clustered_index] CLUSTERED */,KEY idx_orders_account_id (account_id),KEY idx_orders_apply_time (apply_time));") + tk.MustExec("insert into orders values (20, 210802010000721168, 20000 , 2 , 1682484268727), (22, 210802010000721168, 35100 , 4 , 1650885615002);") + 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() + 18000), from_unixtime(apply_time/1000 + 18000)) as apply_to_now_days from orders) t1;").Sort().Check( + testkit.Rows("20000 35100")) +} From 2225cf8f845e28168d1853a2054f2b254004bab3 Mon Sep 17 00:00:00 2001 From: wshwsh12 <793703860@qq.com> Date: Wed, 17 May 2023 15:43:17 +0800 Subject: [PATCH 3/4] fix --- expression/builtin_cast.go | 5 ----- planner/core/rule_aggregation_push_down.go | 7 +++++++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/expression/builtin_cast.go b/expression/builtin_cast.go index 2d5ae991bcdc4..a15da8a53cb72 100644 --- a/expression/builtin_cast.go +++ b/expression/builtin_cast.go @@ -2137,11 +2137,6 @@ func WrapWithCastAsInt(ctx sessionctx.Context, expr Expression) Expression { tp.SetDecimal(0) types.SetBinChsClnFlag(tp) tp.AddFlag(expr.GetType().GetFlag() & mysql.UnsignedFlag) - - if expr.GetType().GetType() == mysql.TypeNull { - tp.SetType(mysql.TypeLong) - tp.AddFlag(mysql.UnsignedFlag) - } return BuildCastFunction(ctx, expr, tp) } diff --git a/planner/core/rule_aggregation_push_down.go b/planner/core/rule_aggregation_push_down.go index 24aef4161a8ec..77e4ceee63808 100644 --- a/planner/core/rule_aggregation_push_down.go +++ b/planner/core/rule_aggregation_push_down.go @@ -544,6 +544,13 @@ func (a *aggregationPushDownSolver) aggPushDown(p LogicalPlan, opt *logicalOptim newAggFuncsArgs = append(newAggFuncsArgs, newArgs) } } + for i, funcsArgs := range oldAggFuncsArgs { + for j := range funcsArgs { + if oldAggFuncsArgs[i][j].GetType().EvalType() != newAggFuncsArgs[i][j].GetType().EvalType() { + noSideEffects = false + } + } + } if noSideEffects { agg.GroupByItems = newGbyItems for i, aggFunc := range agg.AggFuncs { From d6e83df98d8d0e4bc6cccef9d930623b8ef760e2 Mon Sep 17 00:00:00 2001 From: wshwsh12 <793703860@qq.com> Date: Thu, 18 May 2023 16:49:19 +0800 Subject: [PATCH 4/4] address comments --- planner/core/rule_aggregation_push_down.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/planner/core/rule_aggregation_push_down.go b/planner/core/rule_aggregation_push_down.go index 77e4ceee63808..e3ecc5a79cf1e 100644 --- a/planner/core/rule_aggregation_push_down.go +++ b/planner/core/rule_aggregation_push_down.go @@ -548,8 +548,12 @@ func (a *aggregationPushDownSolver) aggPushDown(p LogicalPlan, opt *logicalOptim for j := range funcsArgs { if oldAggFuncsArgs[i][j].GetType().EvalType() != newAggFuncsArgs[i][j].GetType().EvalType() { noSideEffects = false + break } } + if !noSideEffects { + break + } } if noSideEffects { agg.GroupByItems = newGbyItems