From fe09892107ef805afd2f49d31edf9a90302baa62 Mon Sep 17 00:00:00 2001 From: AilinKid <314806019@qq.com> Date: Wed, 3 Jan 2024 12:29:29 +0800 Subject: [PATCH] fix agg push down rule mistake order by item inside agg function Signed-off-by: AilinKid <314806019@qq.com> --- pkg/planner/core/rule_aggregation_push_down.go | 2 +- tests/integrationtest/r/expression/issues.result | 15 +++++++++++++++ tests/integrationtest/t/expression/issues.test | 8 +++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/pkg/planner/core/rule_aggregation_push_down.go b/pkg/planner/core/rule_aggregation_push_down.go index 5c5b4c0db664a..c3dcf84312fce 100644 --- a/pkg/planner/core/rule_aggregation_push_down.go +++ b/pkg/planner/core/rule_aggregation_push_down.go @@ -608,7 +608,7 @@ func (a *aggregationPushDownSolver) aggPushDown(p LogicalPlan, opt *logicalOptim break } } - for j, item := range newAggOrderItems { + for j, item := range newAggOrderItems[i] { if item == nil { continue } diff --git a/tests/integrationtest/r/expression/issues.result b/tests/integrationtest/r/expression/issues.result index 0463b4e6c7fc0..ca2c8e2dcd66d 100644 --- a/tests/integrationtest/r/expression/issues.result +++ b/tests/integrationtest/r/expression/issues.result @@ -3177,3 +3177,18 @@ a b update test.t set b = 0 where (a, b) in (('a', 1), (null, 0)); SHOW WARNINGS; Level Code Message +drop table if exists test.t; +create table if not exists test.ast (i varchar(20)); +create table if not exists test.acc (j varchar(20), k varchar(20), l varchar(20), m varchar(20)); +explain format='brief' with t as(select i, (case when b.j = '20001' then b.l else b.k end) an from test.ast a inner join test.acc b on (a.i = b.m) and a.i = 'astp2019121731703151'), t1 as (select i, group_concat(an order by an separator '; ') an from t group by i) select * from t1; +id estRows task access object operator info +Projection 8.00 root test.ast.i, Column#32 +└─HashAgg 8.00 root group by:Column#37, funcs:group_concat(Column#34 order by Column#35 separator "; ")->Column#32, funcs:firstrow(Column#36)->test.ast.i + └─Projection 100.00 root case(eq(test.acc.j, 20001), test.acc.l, test.acc.k)->Column#34, case(eq(test.acc.j, 20001), test.acc.l, test.acc.k)->Column#35, test.ast.i->Column#36, test.ast.i->Column#37 + └─HashJoin 100.00 root CARTESIAN inner join + ├─TableReader(Build) 10.00 root data:Selection + │ └─Selection 10.00 cop[tikv] eq(test.ast.i, "astp2019121731703151") + │ └─TableFullScan 10000.00 cop[tikv] table:a keep order:false, stats:pseudo + └─TableReader(Probe) 10.00 root data:Selection + └─Selection 10.00 cop[tikv] eq("astp2019121731703151", test.acc.m) + └─TableFullScan 10000.00 cop[tikv] table:b keep order:false, stats:pseudo diff --git a/tests/integrationtest/t/expression/issues.test b/tests/integrationtest/t/expression/issues.test index 09512a12fbb8b..4c74962b84244 100644 --- a/tests/integrationtest/t/expression/issues.test +++ b/tests/integrationtest/t/expression/issues.test @@ -2154,4 +2154,10 @@ create table test.t (a varchar(10), b tinyint(1)); insert into test.t values ("abc", 1); select * from test.t where (a, b) in (('a', 1), (null, 0)); update test.t set b = 0 where (a, b) in (('a', 1), (null, 0)); -SHOW WARNINGS; \ No newline at end of file +SHOW WARNINGS; + +# TestIssue49986 +drop table if exists test.t; +create table if not exists test.ast (i varchar(20)); +create table if not exists test.acc (j varchar(20), k varchar(20), l varchar(20), m varchar(20)); +explain format='brief' with t as(select i, (case when b.j = '20001' then b.l else b.k end) an from test.ast a inner join test.acc b on (a.i = b.m) and a.i = 'astp2019121731703151'), t1 as (select i, group_concat(an order by an separator '; ') an from t group by i) select * from t1;