Skip to content

Commit

Permalink
plan: second step of removing parents (pingcap#5451)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanfei1991 authored and shenli committed Dec 20, 2017
1 parent 85fe9ff commit 076242f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 51 deletions.
36 changes: 0 additions & 36 deletions plan/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,6 @@ import (
"github.com/pingcap/tidb/expression/aggregation"
)

func setParents4FinalPlan(plan PhysicalPlan) {
allPlans := []PhysicalPlan{plan}
planMark := map[int]bool{}
planMark[plan.ID()] = true
for pID := 0; pID < len(allPlans); pID++ {
allPlans[pID].SetParents()
switch copPlan := allPlans[pID].(type) {
case *PhysicalTableReader:
setParents4FinalPlan(copPlan.tablePlan)
case *PhysicalIndexReader:
setParents4FinalPlan(copPlan.indexPlan)
case *PhysicalIndexLookUpReader:
setParents4FinalPlan(copPlan.indexPlan)
setParents4FinalPlan(copPlan.tablePlan)
}
for _, p := range allPlans[pID].Children() {
if !planMark[p.ID()] {
allPlans = append(allPlans, p.(PhysicalPlan))
planMark[p.ID()] = true
}
}
}

allPlans = allPlans[0:1]
planMark[plan.ID()] = false
for pID := 0; pID < len(allPlans); pID++ {
for _, p := range allPlans[pID].Children() {
p.SetParents(allPlans[pID])
if planMark[p.ID()] {
planMark[p.ID()] = false
allPlans = append(allPlans, p.(PhysicalPlan))
}
}
}
}

// ExplainInfo implements PhysicalPlan interface.
func (p *PhysicalLock) ExplainInfo() string {
return p.Lock.String()
Expand Down
3 changes: 1 addition & 2 deletions plan/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,6 @@ func (b *planBuilder) buildExplain(explain *ast.ExplainStmt) Plan {
return nil
}
}
setParents4FinalPlan(pp)
p := &Explain{StmtPlan: pp}
switch strings.ToLower(explain.Format) {
case ast.ExplainFormatROW:
Expand All @@ -1080,7 +1079,7 @@ func (b *planBuilder) buildExplain(explain *ast.ExplainStmt) Plan {
}
p.SetSchema(schema)
p.explainedPlans = map[int]bool{}
p.prepareRootTaskInfo(p.StmtPlan.(PhysicalPlan))
p.prepareRootTaskInfo(p.StmtPlan.(PhysicalPlan), "")
case ast.ExplainFormatDOT:
retFields := []string{"dot contents"}
schema := expression.NewSchema(make([]*expression.Column, 0, len(retFields))...)
Expand Down
24 changes: 11 additions & 13 deletions plan/plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,40 +357,38 @@ type Explain struct {

// prepareExplainInfo4DAGTask generates the following information for every plan:
// ["id", "parents", "task", "operator info"].
func (e *Explain) prepareExplainInfo4DAGTask(p PhysicalPlan, taskType string) {
parents := p.Parents()
parentIDs := make([]string, 0, len(parents))
for _, parent := range parents {
parentIDs = append(parentIDs, parent.ExplainID())
}
func (e *Explain) prepareExplainInfo4DAGTask(p PhysicalPlan, taskType string, parentID string) {
childrenIDs := make([]string, 0, len(p.Children()))
for _, ch := range p.Children() {
childrenIDs = append(childrenIDs, ch.ExplainID())
}
parentInfo := strings.Join(parentIDs, ",")
childrenInfo := strings.Join(childrenIDs, ",")
operatorInfo := p.ExplainInfo()
count := string(strconv.AppendFloat([]byte{}, p.statsInfo().count, 'f', -1, 64))
row := []string{p.ExplainID(), parentInfo, childrenInfo, taskType, operatorInfo, count}
row := []string{p.ExplainID(), parentID, childrenInfo, taskType, operatorInfo, count}
e.Rows = append(e.Rows, row)
}

// prepareCopTaskInfo generates explain information for cop-tasks.
// Only PhysicalTableReader, PhysicalIndexReader and PhysicalIndexLookUpReader have cop-tasks currently.
func (e *Explain) prepareCopTaskInfo(plans []PhysicalPlan) {
for _, p := range plans {
e.prepareExplainInfo4DAGTask(p, "cop")
for i, p := range plans {
var parentID string
if i+1 < len(plans) {
parentID = plans[i+1].ExplainID()
}
e.prepareExplainInfo4DAGTask(p, "cop", parentID)
}
}

// prepareRootTaskInfo generates explain information for root-tasks.
func (e *Explain) prepareRootTaskInfo(p PhysicalPlan) {
func (e *Explain) prepareRootTaskInfo(p PhysicalPlan, parentID string) {
e.explainedPlans[p.ID()] = true
for _, child := range p.Children() {
if e.explainedPlans[child.ID()] {
continue
}
e.prepareRootTaskInfo(child.(PhysicalPlan))
e.prepareRootTaskInfo(child.(PhysicalPlan), p.ExplainID())
}
switch copPlan := p.(type) {
case *PhysicalTableReader:
Expand All @@ -401,7 +399,7 @@ func (e *Explain) prepareRootTaskInfo(p PhysicalPlan) {
e.prepareCopTaskInfo(copPlan.IndexPlans)
e.prepareCopTaskInfo(copPlan.TablePlans)
}
e.prepareExplainInfo4DAGTask(p, "root")
e.prepareExplainInfo4DAGTask(p, "root", parentID)
}

func (e *Explain) prepareDotInfo(p PhysicalPlan) {
Expand Down

0 comments on commit 076242f

Please sign in to comment.