-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexplainTreeBuilder.go
54 lines (45 loc) · 986 Bytes
/
explainTreeBuilder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package db
import (
"fmt"
)
type QueryPlanNode struct {
ID string
ParentID string
NotUsed string
Detail string
Children []*QueryPlanNode
}
func BuildQueryPlanTree(rows [][]string) (*QueryPlanNode, error) {
var nodes []*QueryPlanNode
nodeMap := make(map[string]*QueryPlanNode)
for _, row := range rows {
id := row[0]
parentId := row[1]
notUsed := row[2]
detail := row[3]
node := &QueryPlanNode{
ID: id,
ParentID: parentId,
NotUsed: notUsed,
Detail: detail,
}
nodes = append(nodes, node)
nodeMap[id] = node
}
root := &QueryPlanNode{}
for _, node := range nodes {
if node.ParentID == "0" {
root = node
} else {
parent := nodeMap[node.ParentID]
parent.Children = append(parent.Children, node)
}
}
return root, nil
}
func PrintQueryPlanTree(node *QueryPlanNode, indent string) {
fmt.Printf("%s%s\n", indent, node.Detail)
for _, child := range node.Children {
PrintQueryPlanTree(child, indent+" ")
}
}