Skip to content

Commit 68c6968

Browse files
winoroszz-jason
authored andcommitted
plan: fix a bug in topn_push_down rule. (#6899) (#6923)
1 parent 6207c48 commit 68c6968

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

plan/logical_plan_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,11 @@ func (s *testPlanSuite) TestTopNPushDown(c *C) {
15781578
sql: "select * from t union all (select * from t s order by a) limit 5",
15791579
best: "UnionAll{DataScan(t)->Limit->Projection->DataScan(s)->TopN([s.a],0,5)->Projection}->Limit",
15801580
},
1581+
// Test `ByItem` containing column from both sides.
1582+
{
1583+
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",
1584+
best: "Join{DataScan(t1)->DataScan(t2)}(t1.e,t2.e)->TopN([ifnull(t1.b, t2.a)],0,5)->Projection->Projection",
1585+
},
15811586
}
15821587
for i, tt := range tests {
15831588
comment := Commentf("case:%v sql:%s", i, tt.sql)

plan/topn_push_down.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ func (p *LogicalJoin) pushDownTopNToChild(topN *LogicalTopN, idx int) LogicalPla
109109

110110
for _, by := range topN.ByItems {
111111
cols := expression.ExtractColumns(by.Expr)
112-
if len(p.children[1-idx].Schema().ColumnsIndices(cols)) != 0 {
113-
return p.children[idx].pushDownTopN(nil)
112+
for _, col := range cols {
113+
if p.children[1-idx].Schema().Contains(col) {
114+
return p.children[idx].pushDownTopN(nil)
115+
}
114116
}
115117
}
116118

0 commit comments

Comments
 (0)