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

plan: fix a bug in topn_push_down rule. (#6899) #6923

Merged
merged 1 commit into from Jun 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions plan/logical_plan_test.go
Expand Up @@ -1578,6 +1578,11 @@ func (s *testPlanSuite) TestTopNPushDown(c *C) {
sql: "select * from t union all (select * from t s order by a) limit 5",
best: "UnionAll{DataScan(t)->Limit->Projection->DataScan(s)->TopN([s.a],0,5)->Projection}->Limit",
},
// Test `ByItem` containing column from both sides.
{
sql: "select ifnull(t1.b, t2.a) from t t1 left join t t2 on t1.e=t2.e order by ifnull(t1.b, t2.a) limit 5",
best: "Join{DataScan(t1)->DataScan(t2)}(t1.e,t2.e)->TopN([ifnull(t1.b, t2.a)],0,5)->Projection->Projection",
},
}
for i, tt := range tests {
comment := Commentf("case:%v sql:%s", i, tt.sql)
Expand Down
6 changes: 4 additions & 2 deletions plan/topn_push_down.go
Expand Up @@ -109,8 +109,10 @@ func (p *LogicalJoin) pushDownTopNToChild(topN *LogicalTopN, idx int) LogicalPla

for _, by := range topN.ByItems {
cols := expression.ExtractColumns(by.Expr)
if len(p.children[1-idx].Schema().ColumnsIndices(cols)) != 0 {
return p.children[idx].pushDownTopN(nil)
for _, col := range cols {
if p.children[1-idx].Schema().Contains(col) {
return p.children[idx].pushDownTopN(nil)
}
}
}

Expand Down