diff --git a/cmd/explaintest/r/topn_push_down.result b/cmd/explaintest/r/topn_push_down.result index f91277a5a774e..c297e422c6c8f 100644 --- a/cmd/explaintest/r/topn_push_down.result +++ b/cmd/explaintest/r/topn_push_down.result @@ -188,3 +188,28 @@ 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_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 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:]...) } }