From e080c3770278f08d65284db2330cfbb2e4201bee Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Fri, 30 Mar 2018 13:27:57 +0800 Subject: [PATCH] plan: fix a bug when applying `top-n push down` rule. (#6187) --- plan/physical_plan_test.go | 4 ++++ plan/topn_push_down.go | 2 ++ 2 files changed, 6 insertions(+) diff --git a/plan/physical_plan_test.go b/plan/physical_plan_test.go index 49d1f9bd65..91dc562c93 100644 --- a/plan/physical_plan_test.go +++ b/plan/physical_plan_test.go @@ -184,6 +184,10 @@ func (s *testPlanSuite) TestDAGPlanBuilderSimpleCase(c *C) { sql: "select * from t use index(e_d_c_str_prefix) where t.e_str = b'1110000'", best: "IndexLookUp(Index(t.e_d_c_str_prefix)[[p,p]], Table(t))", }, + { + sql: "select * from (select * from t use index() order by b) t left join t t1 on t.a=t1.a limit 10", + best: "IndexJoin{TableReader(Table(t)->TopN([test.t.b],0,10))->TopN([test.t.b],0,10)->TableReader(Table(t))}(test.t.a,t1.a)->Limit", + }, } for _, tt := range tests { comment := Commentf("for %s", tt.sql) diff --git a/plan/topn_push_down.go b/plan/topn_push_down.go index b17060847d..fda6b32163 100644 --- a/plan/topn_push_down.go +++ b/plan/topn_push_down.go @@ -61,6 +61,8 @@ func (ls *LogicalSort) pushDownTopN(topN *LogicalTopN) LogicalPlan { } else if topN.isLimit() { topN.ByItems = ls.ByItems // If a Limit is pushed down, the LogicalSort should be converted to topN and be pushed again. + // Since the limit is combined with the sort operator. It's not a partial one any more. + topN.partial = false return ls.children[0].pushDownTopN(topN) } // If a TopN is pushed down, this sort is useless.