From 22a828b68ee312b4aa45de9dcbcb1aafa810ccf4 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Fri, 26 Jan 2024 13:24:21 +0800 Subject: [PATCH] planner: fix agg push down rule mistake order by item inside agg function (#50002) (#50018) close pingcap/tidb#49986 --- pkg/planner/core/rule_aggregation_push_down.go | 2 +- .../r/statistics/integration.result | 15 +++++++++++++++ .../integrationtest/t/statistics/integration.test | 7 +++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/pkg/planner/core/rule_aggregation_push_down.go b/pkg/planner/core/rule_aggregation_push_down.go index a1296d41f1dfd..6c79d0f37103b 100644 --- a/pkg/planner/core/rule_aggregation_push_down.go +++ b/pkg/planner/core/rule_aggregation_push_down.go @@ -606,7 +606,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/statistics/integration.result b/tests/integrationtest/r/statistics/integration.result index b287e35af0816..65d95fe413f03 100644 --- a/tests/integrationtest/r/statistics/integration.result +++ b/tests/integrationtest/r/statistics/integration.result @@ -49,3 +49,18 @@ Sort 4.00 root statistics__integration.t1.a, statistics__integration.t2.a │ └─TableFullScan 2.00 cop[tikv] table:t2 keep order:false └─TableReader(Probe) 4.00 root data:TableFullScan └─TableFullScan 4.00 cop[tikv] table:t1 keep order:false +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 12.50 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 12.50 root inner join, equal:[eq(test.ast.i, test.acc.m)] + ├─TableReader(Build) 10.00 root data:Selection + │ └─Selection 10.00 cop[tikv] eq(test.ast.i, "astp2019121731703151"), not(isnull(test.ast.i)) + │ └─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(test.acc.m, "astp2019121731703151"), not(isnull(test.acc.m)) + └─TableFullScan 10000.00 cop[tikv] table:b keep order:false, stats:pseudo diff --git a/tests/integrationtest/t/statistics/integration.test b/tests/integrationtest/t/statistics/integration.test index e444f3dac93ef..1d9e257b0a9fc 100644 --- a/tests/integrationtest/t/statistics/integration.test +++ b/tests/integrationtest/t/statistics/integration.test @@ -22,3 +22,10 @@ explain format = 'brief' select * from t2 left join t1 on t1.a=t2.a order by t1. explain format = 'brief' select * from t1 right join t2 on t1.a=t2.a order by t1.a, t2.a; explain format = 'brief' select * from t2 right join t1 on t1.a=t2.a order by t1.a, t2.a; +# 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; + +