From 65aabc9be834cda10c5bb8ac8f1aad52513c9896 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Tue, 23 May 2023 11:03:38 +0800 Subject: [PATCH] planner, expression: fix wrong fieldtype after aggregationPushDownSolver (#43906) (#43974) close pingcap/tidb#43805 --- expression/integration_test.go | 11 +++++++++++ planner/core/rule_aggregation_push_down.go | 15 +++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index 5b577279dbe7f..02fb24248e425 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -10599,3 +10599,14 @@ func (s *testIntegrationSuite) TestIssue33397(c *C) { tk.MustExec("set @@tidb_enable_vectorized_expression = true;") tk.MustQuery("select compress(a) from t").Check(testkit.Rows("", "")) } + +func (s *testIntegrationSuite) TestIfFunctionWithNull(c *C) { + // issue 43805 + tk := testkit.NewTestKit(c, s.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")) +} diff --git a/planner/core/rule_aggregation_push_down.go b/planner/core/rule_aggregation_push_down.go index 98e9dec865dad..99fcec5b55a39 100644 --- a/planner/core/rule_aggregation_push_down.go +++ b/planner/core/rule_aggregation_push_down.go @@ -283,8 +283,8 @@ func (a *aggregationPushDownSolver) checkAnyCountAndSum(aggFuncs []*aggregation. } // TODO: -// 1. https://github.com/pingcap/tidb/issues/16355, push avg & distinct functions across join -// 2. remove this method and use splitPartialAgg instead for clean code. +// 1. https://github.com/pingcap/tidb/issues/16355, push avg & distinct functions across join +// 2. remove this method and use splitPartialAgg instead for clean code. func (a *aggregationPushDownSolver) makeNewAgg(ctx sessionctx.Context, aggFuncs []*aggregation.AggFuncDesc, gbyCols []*expression.Column, aggHints aggHintInfo, blockOffset int, nullGenerating bool) (*LogicalAggregation, error) { agg := LogicalAggregation{ @@ -482,6 +482,17 @@ func (a *aggregationPushDownSolver) aggPushDown(p LogicalPlan) (_ LogicalPlan, e newAggFuncsArgs = append(newAggFuncsArgs, newArgs) } } + for i, funcsArgs := range newAggFuncsArgs { + for j := range funcsArgs { + if agg.AggFuncs[i].Args[j].GetType().EvalType() != newAggFuncsArgs[i][j].GetType().EvalType() { + noSideEffects = false + break + } + } + if !noSideEffects { + break + } + } if noSideEffects { agg.GroupByItems = newGbyItems for i, aggFunc := range agg.AggFuncs {