Skip to content

Commit

Permalink
Merge pull request pingcap#915 from pingcap/coocood/plan-accept
Browse files Browse the repository at this point in the history
optimizer/plan: fix Accept method for plans.
  • Loading branch information
qiuyesuifeng committed Feb 20, 2016
2 parents 3eeae36 + 6188d96 commit 6bb3ce2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 20 deletions.
48 changes: 48 additions & 0 deletions optimizer/plan/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,51 @@ func (s *testPlanSuite) TestMultiColumnIndex(c *C) {
c.Assert(idxScan.Ranges[0].LowVal, HasLen, ca.usedColumnCount)
}
}

func (s *testPlanSuite) TestVisitCount(c *C) {
sqls := []string{
"select t1.c1, t2.c2 from t1, t2",
"select * from t1 left join t2 on t1.c1 = t2.c1",
"select * from t1 group by t1.c1 having sum(t1.c2) = 1",
"select * from t1 where t1.c1 > 2 order by t1.c2 limit 100",
"insert t1 values (1), (2)",
"delete from t1 where false",
"truncate table t1",
"do 1",
"show databases",
}
for _, sql := range sqls {
stmt, err := parser.ParseOneStmt(sql, "", "")
c.Assert(err, IsNil, Commentf(sql))
ast.SetFlag(stmt)
mockJoinResolve(c, stmt)
b := &planBuilder{}
p := b.build(stmt)
c.Assert(b.err, IsNil)
visitor := &countVisitor{}
for i := 0; i < 5; i++ {
visitor.skipAt = i
visitor.enterCount = 0
visitor.leaveCount = 0
p.Accept(visitor)
c.Assert(visitor.enterCount, Equals, visitor.leaveCount, Commentf(sql))
}
}
}

type countVisitor struct {
skipAt int
enterCount int
leaveCount int
}

func (s *countVisitor) Enter(in Plan) (Plan, bool) {
skip := s.skipAt == s.enterCount
s.enterCount++
return in, skip
}

func (s *countVisitor) Leave(in Plan) (Plan, bool) {
s.leaveCount++
return in, true
}
40 changes: 20 additions & 20 deletions optimizer/plan/plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ type JoinOuter struct {
func (p *JoinOuter) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*JoinOuter)
var ok bool
Expand Down Expand Up @@ -188,7 +188,7 @@ func (p *JoinInner) String() string {
func (p *JoinInner) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*JoinInner)
for i, in := range p.Inners {
Expand All @@ -212,7 +212,7 @@ type SelectLock struct {
func (p *SelectLock) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*SelectLock)
var ok bool
Expand All @@ -238,7 +238,7 @@ type SelectFields struct {
func (p *SelectFields) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*SelectFields)
if p.src != nil {
Expand Down Expand Up @@ -270,7 +270,7 @@ type Sort struct {
func (p *Sort) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*Sort)
var ok bool
Expand Down Expand Up @@ -300,7 +300,7 @@ type Limit struct {
func (p *Limit) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*Limit)
var ok bool
Expand Down Expand Up @@ -330,7 +330,7 @@ type Union struct {
func (p *Union) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(p)
return v.Leave(p)
}
p = np.(*Union)
for i, sel := range p.Selects {
Expand All @@ -352,7 +352,7 @@ type Distinct struct {
func (p *Distinct) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(p)
return v.Leave(p)
}
p = np.(*Distinct)
var ok bool
Expand Down Expand Up @@ -383,7 +383,7 @@ type Prepare struct {
func (p *Prepare) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*Prepare)
return v.Leave(p)
Expand All @@ -402,7 +402,7 @@ type Execute struct {
func (p *Execute) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*Execute)
return v.Leave(p)
Expand All @@ -419,7 +419,7 @@ type Deallocate struct {
func (p *Deallocate) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*Deallocate)
return v.Leave(p)
Expand All @@ -436,7 +436,7 @@ type Aggregate struct {
func (p *Aggregate) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*Aggregate)
if p.src != nil {
Expand Down Expand Up @@ -472,7 +472,7 @@ type Having struct {
func (p *Having) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*Having)
var ok bool
Expand Down Expand Up @@ -502,7 +502,7 @@ type Update struct {
func (p *Update) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*Update)
var ok bool
Expand All @@ -526,7 +526,7 @@ type Delete struct {
func (p *Delete) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*Delete)
var ok bool
Expand All @@ -551,7 +551,7 @@ type Filter struct {
func (p *Filter) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*Filter)
var ok bool
Expand Down Expand Up @@ -589,7 +589,7 @@ type Show struct {
func (p *Show) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*Show)
return v.Leave(p)
Expand All @@ -606,7 +606,7 @@ type Simple struct {
func (p *Simple) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*Simple)
return v.Leave(p)
Expand All @@ -631,7 +631,7 @@ type Insert struct {
func (p *Insert) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*Insert)
if p.SelectPlan != nil {
Expand All @@ -655,7 +655,7 @@ type DDL struct {
func (p *DDL) Accept(v Visitor) (Plan, bool) {
np, skip := v.Enter(p)
if skip {
v.Leave(np)
return v.Leave(np)
}
p = np.(*DDL)
return v.Leave(p)
Expand Down

0 comments on commit 6bb3ce2

Please sign in to comment.