plan: selection to dual directly if condition is always false. (#4980)

This commit is contained in:
Yiding Cui
2017-11-02 14:41:27 +08:00
committed by Han Fei
parent 5ad1eb8e24
commit 57d06c161e
3 changed files with 12 additions and 3 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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",