Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: remove constant sort items after substitution #9333

Merged
merged 5 commits into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/explaintest/r/topn_push_down.result
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions cmd/explaintest/t/topn_push_down.test
Original file line number Diff line number Diff line change
Expand Up @@ -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;
10 changes: 10 additions & 0 deletions planner/core/rule_topn_push_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
eurekaka marked this conversation as resolved.
Show resolved Hide resolved
if isConst {
numItems := len(topN.ByItems)
copy(topN.ByItems[i:numItems-1], topN.ByItems[i+1:])
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
topN.ByItems = topN.ByItems[:numItems-1]
}
}
}
p.children[0] = p.children[0].pushDownTopN(topN)
return p
Expand Down