From 15c432fa4fb9157cbbb7780adbef9c6b59d2df30 Mon Sep 17 00:00:00 2001 From: Jian Zhang Date: Sun, 17 Feb 2019 11:36:06 +0800 Subject: [PATCH 1/3] planner: remove constant sort items after substitution --- cmd/explaintest/r/topn_push_down.result | 5 +++++ cmd/explaintest/t/topn_push_down.test | 2 ++ planner/core/rule_topn_push_down.go | 10 ++++++++++ 3 files changed, 17 insertions(+) diff --git a/cmd/explaintest/r/topn_push_down.result b/cmd/explaintest/r/topn_push_down.result index 244943aecec90..3cd5b58762586 100644 --- a/cmd/explaintest/r/topn_push_down.result +++ b/cmd/explaintest/r/topn_push_down.result @@ -184,3 +184,8 @@ Projection_13 0.00 root te.expect_time └─IndexReader_142 0.00 root index:Selection_141 └─Selection_141 0.00 cop not(isnull(p.relate_id)) └─IndexScan_140 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 f6bb9ce00215e..6678b8a27d97e 100644 --- a/planner/core/rule_topn_push_down.go +++ b/planner/core/rule_topn_push_down.go @@ -96,6 +96,16 @@ 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 { + numItems := len(topN.ByItems) + copy(topN.ByItems[i:numItems-1], topN.ByItems[i+1:]) + topN.ByItems = topN.ByItems[:numItems-1] + } + } } p.children[0] = p.children[0].pushDownTopN(topN) return p From 62683b10232fbd92430643711f36ed31d72e5dff Mon Sep 17 00:00:00 2001 From: Jian Zhang Date: Mon, 18 Feb 2019 10:28:55 +0800 Subject: [PATCH 2/3] address comment --- planner/core/rule_topn_push_down.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/planner/core/rule_topn_push_down.go b/planner/core/rule_topn_push_down.go index 6678b8a27d97e..1ea9699dedef5 100644 --- a/planner/core/rule_topn_push_down.go +++ b/planner/core/rule_topn_push_down.go @@ -101,9 +101,7 @@ func (p *LogicalProjection) pushDownTopN(topN *LogicalTopN) LogicalPlan { for i := len(topN.ByItems) - 1; i >= 0; i-- { _, isConst := topN.ByItems[i].Expr.(*expression.Constant) if isConst { - numItems := len(topN.ByItems) - copy(topN.ByItems[i:numItems-1], topN.ByItems[i+1:]) - topN.ByItems = topN.ByItems[:numItems-1] + topN.ByItems = append(topN.ByItems[:i], topN.ByItems[i+1:]) } } } From fc42294c0325bd0dbfeddafb325a42966d60097c Mon Sep 17 00:00:00 2001 From: Jian Zhang Date: Mon, 18 Feb 2019 10:30:46 +0800 Subject: [PATCH 3/3] fix build --- planner/core/rule_topn_push_down.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planner/core/rule_topn_push_down.go b/planner/core/rule_topn_push_down.go index 1ea9699dedef5..6412d85341998 100644 --- a/planner/core/rule_topn_push_down.go +++ b/planner/core/rule_topn_push_down.go @@ -101,7 +101,7 @@ func (p *LogicalProjection) pushDownTopN(topN *LogicalTopN) LogicalPlan { 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:]) + topN.ByItems = append(topN.ByItems[:i], topN.ByItems[i+1:]...) } } }