From 1e59f2d0373fca287d11931054661b95c47a2d75 Mon Sep 17 00:00:00 2001 From: Zhang Jian Date: Mon, 18 Feb 2019 10:57:35 +0800 Subject: [PATCH] planner: remove constant sort items after substitution (#9333) --- cmd/explaintest/r/topn_push_down.result | 5 +++++ cmd/explaintest/t/topn_push_down.test | 2 ++ planner/core/rule_topn_push_down.go | 8 ++++++++ 3 files changed, 15 insertions(+) diff --git a/cmd/explaintest/r/topn_push_down.result b/cmd/explaintest/r/topn_push_down.result index 65992f23cfadd..fca50edb85a4d 100644 --- a/cmd/explaintest/r/topn_push_down.result +++ b/cmd/explaintest/r/topn_push_down.result @@ -183,3 +183,8 @@ Projection_12 0.00 root te.expect_time │ └─TableScan_31 10.00 cop table:te, keep order:false, stats:pseudo └─IndexReader_135 10.00 root index:IndexScan_134 └─IndexScan_134 10.00 cop table:p, index:relate_id, range: decided by [tr.id], keep order:false, stats:pseudo +desc select 1 as a from dual order by a limit 1; +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 diff --git a/cmd/explaintest/t/topn_push_down.test b/cmd/explaintest/t/topn_push_down.test index 5ae400aa49227..93f2d2cc239bd 100644 --- a/cmd/explaintest/t/topn_push_down.test +++ b/cmd/explaintest/t/topn_push_down.test @@ -171,3 +171,5 @@ WHERE te.expect_time BETWEEN '2018-04-23 00:00:00.0' AND '2018-04-23 23:59:59.0' ORDER BY te.expect_time asc LIMIT 0, 5; + +desc select 1 as a from dual order by a limit 1; diff --git a/planner/core/rule_topn_push_down.go b/planner/core/rule_topn_push_down.go index 0a79d54cb02d2..ccb56da194968 100644 --- a/planner/core/rule_topn_push_down.go +++ b/planner/core/rule_topn_push_down.go @@ -96,6 +96,14 @@ func (p *LogicalProjection) pushDownTopN(topN *LogicalTopN) LogicalPlan { for _, by := range topN.ByItems { by.Expr = expression.ColumnSubstitute(by.Expr, p.schema, p.Exprs) } + + // remove meaningless constant sort items. + for i := len(topN.ByItems) - 1; i >= 0; i-- { + _, isConst := topN.ByItems[i].Expr.(*expression.Constant) + if isConst { + topN.ByItems = append(topN.ByItems[:i], topN.ByItems[i+1:]...) + } + } } p.children[0] = p.children[0].pushDownTopN(topN) return p