forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plans.go
327 lines (274 loc) · 8.38 KB
/
plans.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package plan
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/util/auth"
"github.com/pingcap/tidb/util/types"
)
// ShowDDL is for showing DDL information.
type ShowDDL struct {
basePlan
}
// ShowDDLJobs is for showing DDL job list.
type ShowDDLJobs struct {
basePlan
}
// CheckTable is used for checking table data, built from the 'admin check table' statement.
type CheckTable struct {
basePlan
Tables []*ast.TableName
}
// CancelDDLJobs represents a cancel DDL jobs plan.
type CancelDDLJobs struct {
basePlan
JobIDs []int64
}
// SelectLock represents a select lock plan.
type SelectLock struct {
*basePlan
baseLogicalPlan
basePhysicalPlan
Lock ast.SelectLockType
}
// Prepare represents prepare plan.
type Prepare struct {
basePlan
Name string
SQLText string
}
// Execute represents prepare plan.
type Execute struct {
basePlan
Name string
UsingVars []expression.Expression
ExecID uint32
}
// Deallocate represents deallocate plan.
type Deallocate struct {
basePlan
Name string
}
// Show represents a show plan.
type Show struct {
*basePlan
baseLogicalPlan
basePhysicalPlan
Tp ast.ShowStmtType // Databases/Tables/Columns/....
DBName string
Table *ast.TableName // Used for showing columns.
Column *ast.ColumnName // Used for `desc table column`.
Flag int // Some flag parsed from sql, such as FULL.
Full bool
User *auth.UserIdentity // Used for show grants.
// Used by show variables
GlobalScope bool
}
// Set represents a plan for set stmt.
type Set struct {
basePlan
VarAssigns []*expression.VarAssignment
}
// Simple represents a simple statement plan which doesn't need any optimization.
type Simple struct {
basePlan
Statement ast.StmtNode
}
// InsertGeneratedColumns is for completing generated columns in Insert.
// We resolve generation expressions in plan, and eval those in executor.
type InsertGeneratedColumns struct {
Columns []*ast.ColumnName
Exprs []expression.Expression
OnDuplicates []*expression.Assignment
}
// Insert represents an insert plan.
type Insert struct {
*basePlan
baseLogicalPlan
basePhysicalPlan
Table table.Table
tableSchema *expression.Schema
Columns []*ast.ColumnName
Lists [][]expression.Expression
Setlist []*expression.Assignment
OnDuplicate []*expression.Assignment
IsReplace bool
Priority mysql.PriorityEnum
IgnoreErr bool
// NeedFillDefaultValue is true when expr in value list reference other column.
NeedFillDefaultValue bool
GenCols InsertGeneratedColumns
}
// AnalyzeColumnsTask is used for analyze columns.
type AnalyzeColumnsTask struct {
TableInfo *model.TableInfo
PKInfo *model.ColumnInfo
ColsInfo []*model.ColumnInfo
PushDown bool
}
// AnalyzeIndexTask is used for analyze index.
type AnalyzeIndexTask struct {
TableInfo *model.TableInfo
IndexInfo *model.IndexInfo
PushDown bool
}
// Analyze represents an analyze plan
type Analyze struct {
basePlan
ColTasks []AnalyzeColumnsTask
IdxTasks []AnalyzeIndexTask
}
// LoadData represents a loaddata plan.
type LoadData struct {
basePlan
IsLocal bool
Path string
Table *ast.TableName
Columns []*ast.ColumnName
FieldsInfo *ast.FieldsClause
LinesInfo *ast.LinesClause
GenCols InsertGeneratedColumns
}
// DDL represents a DDL statement plan.
type DDL struct {
basePlan
Statement ast.DDLNode
}
// Explain represents a explain plan.
type Explain struct {
basePlan
StmtPlan Plan
Rows [][]types.Datum
explainedPlans map[int]bool
}
func (e *Explain) prepareExplainInfo(p Plan, parent Plan) error {
for _, child := range p.Children() {
err := e.prepareExplainInfo(child, p)
if err != nil {
return errors.Trace(err)
}
}
explain, err := json.MarshalIndent(p, "", " ")
if err != nil {
return errors.Trace(err)
}
parentStr := ""
if parent != nil {
parentStr = parent.ExplainID()
}
row := types.MakeDatums(p.ExplainID(), string(explain), parentStr)
e.Rows = append(e.Rows, row)
return nil
}
// 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())
}
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 := p.statsProfile().count
row := types.MakeDatums(p.ExplainID(), parentInfo, 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")
}
}
// prepareRootTaskInfo generates explain information for root-tasks.
func (e *Explain) prepareRootTaskInfo(p PhysicalPlan) {
e.explainedPlans[p.ID()] = true
for _, child := range p.Children() {
if e.explainedPlans[child.ID()] {
continue
}
e.prepareRootTaskInfo(child.(PhysicalPlan))
}
switch copPlan := p.(type) {
case *PhysicalTableReader:
e.prepareCopTaskInfo(copPlan.TablePlans)
case *PhysicalIndexReader:
e.prepareCopTaskInfo(copPlan.IndexPlans)
case *PhysicalIndexLookUpReader:
e.prepareCopTaskInfo(copPlan.IndexPlans)
e.prepareCopTaskInfo(copPlan.TablePlans)
}
e.prepareExplainInfo4DAGTask(p, "root")
}
func (e *Explain) prepareDotInfo(p PhysicalPlan) {
buffer := bytes.NewBufferString("")
buffer.WriteString(fmt.Sprintf("\ndigraph %s {\n", p.ExplainID()))
e.prepareTaskDot(p, "root", buffer)
buffer.WriteString(fmt.Sprintln("}"))
row := types.MakeDatums(buffer.String())
e.Rows = append(e.Rows, row)
}
func (e *Explain) prepareTaskDot(p PhysicalPlan, taskTp string, buffer *bytes.Buffer) {
buffer.WriteString(fmt.Sprintf("subgraph cluster%v{\n", p.ID()))
buffer.WriteString("node [style=filled, color=lightgrey]\n")
buffer.WriteString("color=black\n")
buffer.WriteString(fmt.Sprintf("label = \"%s\"\n", taskTp))
if len(p.Children()) == 0 {
buffer.WriteString(fmt.Sprintf("\"%s\"\n}\n", p.ExplainID()))
return
}
copTasks := []Plan{}
pipelines := []string{}
for planQueue := []Plan{p}; len(planQueue) > 0; planQueue = planQueue[1:] {
curPlan := planQueue[0]
switch copPlan := curPlan.(type) {
case *PhysicalTableReader:
pipelines = append(pipelines, fmt.Sprintf("\"%s\" -> \"%s\"\n", copPlan.ExplainID(), copPlan.tablePlan.ExplainID()))
copTasks = append(copTasks, copPlan.tablePlan)
case *PhysicalIndexReader:
pipelines = append(pipelines, fmt.Sprintf("\"%s\" -> \"%s\"\n", copPlan.ExplainID(), copPlan.indexPlan.ExplainID()))
copTasks = append(copTasks, copPlan.indexPlan)
case *PhysicalIndexLookUpReader:
pipelines = append(pipelines, fmt.Sprintf("\"%s\" -> \"%s\"\n", copPlan.ExplainID(), copPlan.tablePlan.ExplainID()))
pipelines = append(pipelines, fmt.Sprintf("\"%s\" -> \"%s\"\n", copPlan.ExplainID(), copPlan.indexPlan.ExplainID()))
copTasks = append(copTasks, copPlan.tablePlan)
copTasks = append(copTasks, copPlan.indexPlan)
}
for _, child := range curPlan.Children() {
buffer.WriteString(fmt.Sprintf("\"%s\" -> \"%s\"\n", curPlan.ExplainID(), child.ExplainID()))
planQueue = append(planQueue, child)
}
}
buffer.WriteString("}\n")
for _, cop := range copTasks {
e.prepareTaskDot(cop.(PhysicalPlan), "cop", buffer)
}
for i := range pipelines {
buffer.WriteString(pipelines[i])
}
}