Skip to content

Commit

Permalink
plan: fix a bug in topn_push_down rule. (#6899) (#6923)
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros authored and zz-jason committed Jun 28, 2018
1 parent 6207c48 commit 68c6968
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions plan/logical_plan_test.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 68c6968

Please sign in to comment.