-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathselect_plan.go
More file actions
175 lines (153 loc) · 3.79 KB
/
select_plan.go
File metadata and controls
175 lines (153 loc) · 3.79 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package planner
import (
"encoding/json"
"errors"
"strings"
"planner/builder"
"router"
"xcontext"
"github.com/xelabs/go-mysqlstack/sqlparser"
"github.com/xelabs/go-mysqlstack/sqlparser/depends/common"
"github.com/xelabs/go-mysqlstack/xlog"
)
var (
_ Plan = &SelectPlan{}
)
// SelectPlan represents select plan.
type SelectPlan struct {
log *xlog.Log
// router
router *router.Router
// select ast
node *sqlparser.Select
// database
database string
// raw query
RawQuery string
// type
typ PlanType
Root builder.PlanNode
}
// NewSelectPlan used to create SelectPlan.
func NewSelectPlan(log *xlog.Log, database string, query string, node *sqlparser.Select, router *router.Router) *SelectPlan {
return &SelectPlan{
log: log,
node: node,
router: router,
database: database,
RawQuery: query,
typ: PlanTypeSelect,
}
}
// Build used to build distributed querys.
// For now, we don't support subquery in select.
func (p *SelectPlan) Build() error {
var err error
// Check subquery.
if hasSubquery(p.node) {
return errors.New("unsupported: subqueries.in.select")
}
p.Root, err = builder.BuildNode(p.log, p.router, p.database, p.node)
return err
}
// Type returns the type of the plan.
func (p *SelectPlan) Type() PlanType {
return p.typ
}
// JSON returns the plan info.
func (p *SelectPlan) JSON() string {
type limit struct {
Offset int
Limit int
}
type join struct {
Type string
Strategy string
}
type explain struct {
RawQuery string `json:",omitempty"`
Project string `json:",omitempty"`
Partitions []xcontext.QueryTuple `json:",omitempty"`
Join *join `json:",omitempty"`
Aggregate []string `json:",omitempty"`
GatherMerge []string `json:",omitempty"`
HashGroupBy []string `json:",omitempty"`
Limit *limit `json:",omitempty"`
}
var joins *join
if j, ok := p.Root.(*builder.JoinNode); ok {
joins = &join{}
switch j.Strategy {
case builder.Cartesian:
joins.Strategy = "Cartesian Join"
case builder.SortMerge:
joins.Strategy = "Sort Merge Join"
case builder.NestLoop:
joins.Strategy = "Nested Loop Join"
}
if j.IsLeftJoin {
joins.Type = "LEFT JOIN"
} else {
if j.Strategy == builder.Cartesian {
joins.Type = "CROSS JOIN"
} else {
joins.Type = "INNER JOIN"
}
}
}
// Aggregate.
var aggregate []string
var hashGroup []string
var gatherMerge []string
var lim *limit
for _, sub := range p.Root.Children() {
switch sub.Type() {
case builder.ChildTypeAggregate:
plan := sub.(*builder.AggregatePlan)
for _, aggr := range plan.NormalAggregators() {
aggregate = append(aggregate, aggr.Field)
}
for _, aggr := range plan.GroupAggregators() {
hashGroup = append(hashGroup, aggr.Field)
}
case builder.ChildTypeOrderby:
plan := sub.(*builder.OrderByPlan)
for _, order := range plan.OrderBys {
field := order.Field
if order.Table != "" {
field = strings.Join([]string{order.Table, order.Field}, ".")
}
gatherMerge = append(gatherMerge, field)
}
case builder.ChildTypeLimit:
plan := sub.(*builder.LimitPlan)
lim = &limit{Offset: plan.Offset, Limit: plan.Limit}
}
}
exp := &explain{Project: builder.GetProject(p.Root),
RawQuery: p.RawQuery,
Partitions: p.Root.GetQuery(),
Join: joins,
Aggregate: aggregate,
GatherMerge: gatherMerge,
HashGroupBy: hashGroup,
Limit: lim,
}
bout, err := json.MarshalIndent(exp, "", "\t")
if err != nil {
return err.Error()
}
return common.BytesToString(bout)
}
// Size returns the memory size.
func (p *SelectPlan) Size() int {
size := len(p.RawQuery)
return size
}