From 7dc90fb9d2abe10ada4c844568678b75a92b3b3d Mon Sep 17 00:00:00 2001 From: Zhang Jian Date: Mon, 25 Feb 2019 14:48:44 +0800 Subject: [PATCH 1/2] planner: remove correlated column sort items (#9435) --- cmd/explaintest/r/topn_push_down.result | 29 +++++++++++++++++++++++++ cmd/explaintest/t/topn_push_down.test | 9 ++++++++ planner/core/rule_topn_push_down.go | 4 ++-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/cmd/explaintest/r/topn_push_down.result b/cmd/explaintest/r/topn_push_down.result index f91277a5a774e..e67b1dbd6837f 100644 --- a/cmd/explaintest/r/topn_push_down.result +++ b/cmd/explaintest/r/topn_push_down.result @@ -188,3 +188,32 @@ id count task operator info Projection_7 1.00 root 1 └─Limit_8 1.00 root offset:0, count:1 └─TableDual_11 1.00 root rows:1 +drop table if exists t1; +drop table if exists t2; +create table t1(a bigint, b bigint); +create table t2(a bigint, b bigint); +desc select * from t1 where t1.a in (select t2.a as a from t2 where t2.b > t1.b order by t1.b limit 1); +id count task operator info +Apply_15 9990.00 root semi join, inner:Selection_19, equal:[eq(test.t1.a, test.t2.a)] +├─TableReader_18 9990.00 root data:Selection_17 +│ └─Selection_17 9990.00 cop not(isnull(test.t1.a)) +│ └─TableScan_16 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo +└─Selection_19 0.80 root not(isnull(test.t2.a)) + └─Limit_20 1.00 root offset:0, count:1 + └─TableReader_26 1.00 root data:Limit_25 + └─Limit_25 1.00 cop offset:0, count:1 + └─Selection_24 1.00 cop gt(test.t2.b, test.t1.b) + └─TableScan_23 1.25 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo +desc select * from t1 where t1.a in (select a from (select t2.a as a, t1.b as b from t2 where t2.b > t1.b) x order by b limit 1); +id count task operator info +Apply_17 9990.00 root semi join, inner:Selection_21, equal:[eq(test.t1.a, x.a)] +├─TableReader_20 9990.00 root data:Selection_19 +│ └─Selection_19 9990.00 cop not(isnull(test.t1.a)) +│ └─TableScan_18 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo +└─Selection_21 0.80 root not(isnull(x.a)) + └─Projection_22 1.00 root test.t2.a, test.t1.b + └─Limit_23 1.00 root offset:0, count:1 + └─TableReader_29 1.00 root data:Limit_28 + └─Limit_28 1.00 cop offset:0, count:1 + └─Selection_27 1.00 cop gt(test.t2.b, test.t1.b) + └─TableScan_26 1.25 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo diff --git a/cmd/explaintest/t/topn_push_down.test b/cmd/explaintest/t/topn_push_down.test index 93f2d2cc239bd..e37f0e7512c2e 100644 --- a/cmd/explaintest/t/topn_push_down.test +++ b/cmd/explaintest/t/topn_push_down.test @@ -172,4 +172,13 @@ WHERE ORDER BY te.expect_time asc LIMIT 0, 5; +-- test order by constant desc select 1 as a from dual order by a limit 1; + +-- test order by correlated column +drop table if exists t1; +drop table if exists t2; +create table t1(a bigint, b bigint); +create table t2(a bigint, b bigint); +desc select * from t1 where t1.a in (select t2.a as a from t2 where t2.b > t1.b order by t1.b limit 1); +desc select * from t1 where t1.a in (select a from (select t2.a as a, t1.b as b from t2 where t2.b > t1.b) x order by b limit 1); diff --git a/planner/core/rule_topn_push_down.go b/planner/core/rule_topn_push_down.go index ccb56da194968..0c2074b9a743d 100644 --- a/planner/core/rule_topn_push_down.go +++ b/planner/core/rule_topn_push_down.go @@ -99,8 +99,8 @@ func (p *LogicalProjection) pushDownTopN(topN *LogicalTopN) LogicalPlan { // remove meaningless constant sort items. for i := len(topN.ByItems) - 1; i >= 0; i-- { - _, isConst := topN.ByItems[i].Expr.(*expression.Constant) - if isConst { + switch topN.ByItems[i].Expr.(type) { + case *expression.Constant, *expression.CorrelatedColumn: topN.ByItems = append(topN.ByItems[:i], topN.ByItems[i+1:]...) } } From d4db1db601bcca4c76541db89435eef75502c33a Mon Sep 17 00:00:00 2001 From: Jian Zhang Date: Mon, 25 Feb 2019 15:57:51 +0800 Subject: [PATCH 2/2] fix explain test --- cmd/explaintest/r/topn_push_down.result | 32 +++++++++++-------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/cmd/explaintest/r/topn_push_down.result b/cmd/explaintest/r/topn_push_down.result index e67b1dbd6837f..c297e422c6c8f 100644 --- a/cmd/explaintest/r/topn_push_down.result +++ b/cmd/explaintest/r/topn_push_down.result @@ -194,26 +194,22 @@ create table t1(a bigint, b bigint); create table t2(a bigint, b bigint); desc select * from t1 where t1.a in (select t2.a as a from t2 where t2.b > t1.b order by t1.b limit 1); id count task operator info -Apply_15 9990.00 root semi join, inner:Selection_19, equal:[eq(test.t1.a, test.t2.a)] -├─TableReader_18 9990.00 root data:Selection_17 -│ └─Selection_17 9990.00 cop not(isnull(test.t1.a)) -│ └─TableScan_16 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo -└─Selection_19 0.80 root not(isnull(test.t2.a)) +Apply_14 10000.00 root semi join, inner:Limit_17, equal:[eq(test.t1.a, test.t2.a)] +├─TableReader_16 10000.00 root data:TableScan_15 +│ └─TableScan_15 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo +└─Limit_17 1.00 root offset:0, count:1 + └─TableReader_23 1.00 root data:Limit_22 + └─Limit_22 1.00 cop offset:0, count:1 + └─Selection_21 1.00 cop gt(test.t2.b, test.t1.b) + └─TableScan_20 1.25 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo +desc select * from t1 where t1.a in (select a from (select t2.a as a, t1.b as b from t2 where t2.b > t1.b) x order by b limit 1); +id count task operator info +Apply_16 10000.00 root semi join, inner:Projection_19, equal:[eq(test.t1.a, x.a)] +├─TableReader_18 10000.00 root data:TableScan_17 +│ └─TableScan_17 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo +└─Projection_19 1.00 root test.t2.a, test.t1.b └─Limit_20 1.00 root offset:0, count:1 └─TableReader_26 1.00 root data:Limit_25 └─Limit_25 1.00 cop offset:0, count:1 └─Selection_24 1.00 cop gt(test.t2.b, test.t1.b) └─TableScan_23 1.25 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo -desc select * from t1 where t1.a in (select a from (select t2.a as a, t1.b as b from t2 where t2.b > t1.b) x order by b limit 1); -id count task operator info -Apply_17 9990.00 root semi join, inner:Selection_21, equal:[eq(test.t1.a, x.a)] -├─TableReader_20 9990.00 root data:Selection_19 -│ └─Selection_19 9990.00 cop not(isnull(test.t1.a)) -│ └─TableScan_18 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo -└─Selection_21 0.80 root not(isnull(x.a)) - └─Projection_22 1.00 root test.t2.a, test.t1.b - └─Limit_23 1.00 root offset:0, count:1 - └─TableReader_29 1.00 root data:Limit_28 - └─Limit_28 1.00 cop offset:0, count:1 - └─Selection_27 1.00 cop gt(test.t2.b, test.t1.b) - └─TableScan_26 1.25 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo