From 57d06c161efec0f58b0453555fdbe5c5aa7eb8fd Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Thu, 2 Nov 2017 14:41:27 +0800 Subject: [PATCH] plan: selection to dual directly if condition is always false. (#4980) --- plan/logical_plan_builder.go | 7 ++++++- plan/logical_plan_test.go | 6 +++++- plan/physical_plan_test.go | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/plan/logical_plan_builder.go b/plan/logical_plan_builder.go index 7699fe8e10..3a16ad102f 100644 --- a/plan/logical_plan_builder.go +++ b/plan/logical_plan_builder.go @@ -407,8 +407,13 @@ func (b *planBuilder) buildSelection(p LogicalPlan, where ast.ExprNode, AggMappe for _, item := range cnfItems { if con, ok := item.(*expression.Constant); ok { ret, err := expression.EvalBool(expression.CNFExprs{con}, nil, b.ctx) - if ret || err != nil { + if err != nil || ret { continue + } else { + // If there is condition which is always false, return dual plan directly. + dual := TableDual{}.init(b.allocator, b.ctx) + dual.SetSchema(p.Schema().Clone()) + return dual } } expressions = append(expressions, item) diff --git a/plan/logical_plan_test.go b/plan/logical_plan_test.go index 859bc18196..dea6fb7874 100644 --- a/plan/logical_plan_test.go +++ b/plan/logical_plan_test.go @@ -549,7 +549,7 @@ func (s *testPlanSuite) TestPredicatePushDown(c *C) { }, { sql: "select * from (select a, sum(b) as s from t group by a having 1 = 0) k where a > 1", - best: "DataScan(t)->Selection->Aggr(sum(test.t.b),firstrow(test.t.a))->Selection->Projection->Projection", + best: "Dual->Selection->Projection", }, { sql: "select a, count(a) cnt from t group by a having cnt < 1", @@ -678,6 +678,10 @@ func (s *testPlanSuite) TestPlanBuilder(c *C) { sql: "select substr(\"abc\", 1)", plan: "Dual->Projection", }, + { + sql: "select * from t t1, t t2 where 1 = 0", + plan: "Dual->Projection", + }, } for _, ca := range tests { comment := Commentf("for %s", ca.sql) diff --git a/plan/physical_plan_test.go b/plan/physical_plan_test.go index 7fd54df6d7..909307600c 100644 --- a/plan/physical_plan_test.go +++ b/plan/physical_plan_test.go @@ -418,7 +418,7 @@ func (s *testPlanSuite) TestCBO(c *C) { }, { sql: "select count(*) from t t1 having 1 = 0", - best: "Dual->HashAgg->Selection", + best: "Dual", }, { sql: "select sum(a.b), sum(b.b) from t a join t b on a.c = b.c group by a.d order by a.d",