forked from pingcap/tidb
/
flat_plan.go
498 lines (464 loc) · 17.2 KB
/
flat_plan.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright 2022 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package core
import (
"sort"
"github.com/twotigers93/tidb/kv"
"github.com/twotigers93/tidb/util/texttree"
)
// FlatPhysicalPlan provides an easier structure to traverse a plan and collect needed information.
// Note: Although it's named FlatPhysicalPlan, there also could be Insert, Delete and Update at the beginning of Main.
type FlatPhysicalPlan struct {
Main FlatPlanTree
CTEs []FlatPlanTree
// InExecute and InExplain are expected to handle some special cases. Usually you don't need to use them.
// InExecute means if the original plan tree contains Execute operator.
//
// Be careful when trying to use this, InExecute is true doesn't mean we are handling an EXECUTE statement.
// When collecting information from the plan in an EXECUTE statement, usually we directly use the plan
// in Execute.Plan, not Execute itself, so InExecute will be false.
//
// When will InExecute be true? When you're using "EXPLAIN FOR CONNECTION" to get the last plan of
// a connection (usually we will record Explain.TargetPlan for an EXPLAIN statement) and that plan
// is from an EXECUTE statement, we will collect from Execute itself, not directly from Execute.Plan,
// then InExecute will be true.
InExecute bool
// InExplain means if the original plan tree contains Explain operator.
InExplain bool
// The fields below are only used when building the FlatPhysicalPlan.
buildSideFirst bool
ctesToFlatten []*PhysicalCTE
}
// FlatPlanTree is a simplified plan tree.
// It arranges all operators in the tree as a slice, ordered by the order of traversing the tree, which means a
// depth-first traversal plus some special rule for some operators.
type FlatPlanTree []*FlatOperator
// GetSelectPlan skips Insert, Delete, and Update at the beginning of the FlatPlanTree and the foreign key check/cascade plan at the end of the FlatPlanTree.
// Note:
//
// It returns a reference to the original FlatPlanTree, please avoid modifying the returned value.
// The second return value is the offset. Because the returned FlatPlanTree is a part of the original slice, you need to minus them by the offset when using the returned FlatOperator.Depth and FlatOperator.ChildrenIdx.
func (e FlatPlanTree) GetSelectPlan() (FlatPlanTree, int) {
if len(e) == 0 {
return nil, 0
}
hasDML := false
for i, op := range e {
switch op.Origin.(type) {
case *Insert, *Delete, *Update:
hasDML = true
default:
if hasDML {
for j := i; j < len(e); j++ {
switch e[j].Origin.(type) {
case *FKCheck, *FKCascade:
// The later plans are belong to foreign key check/cascade plans, doesn't belong to select plan, just skip it.
return e[i:j], i
}
}
}
return e[i:], i
}
}
return nil, 0
}
// FlatOperator is a simplified operator.
// It contains a reference to the original operator and some usually needed information.
type FlatOperator struct {
// A reference to the original operator.
Origin Plan
// With ChildrenIdx and ChildrenEndIdx, we can locate every children subtrees of this operator in the FlatPlanTree.
// For example, the first children subtree is flatTree[ChildrenIdx[0] : ChildrenIdx[1]], the last children subtree
// is flatTree[ChildrenIdx[n-1] : ChildrenEndIdx+1].
// ChildrenIdx is the indexes of the children of this operator in the FlatPlanTree.
// It's ordered from small to large.
ChildrenIdx []int
// ChildrenEndIdx is the index of the last operator of children subtrees of this operator in the FlatPlanTree.
ChildrenEndIdx int
// NeedReverseDriverSide means if we need to reverse the order of children to keep build side before probe side.
//
// Specifically, it means if the below are all true:
// 1. this operator has two children
// 2. the first child's Label is the probe side and the second's is the build side.
//
// If you call FlattenPhysicalPlan with buildSideFirst true, NeedReverseDriverSide will be useless.
NeedReverseDriverSide bool
Depth uint32
Label OperatorLabel
IsRoot bool
StoreType kv.StoreType
// ReqType is only meaningful when IsRoot is false.
ReqType ReadReqType
// The below two fields are mainly for text tree formatting. See texttree.PrettyIdentifier().
TextTreeIndent string
IsLastChild bool
IsPhysicalPlan bool
}
// OperatorLabel acts as some additional information to the name, usually it means its relationship with its parent.
// It's useful for index join, apply, index lookup, cte and so on.
type OperatorLabel uint8
const (
// Empty means OperatorLabel is meaningless for this operator.
Empty OperatorLabel = iota
// BuildSide means this operator is at the build side of its parent
BuildSide
// ProbeSide means this operator is at the probe side of its parent
ProbeSide
// SeedPart means this operator is the seed part of its parent (a cte)
SeedPart
// RecursivePart means this operator is the recursive part of its parent (a cte)
RecursivePart
)
func (d OperatorLabel) String() string {
switch d {
case Empty:
return ""
case BuildSide:
return "(Build)"
case ProbeSide:
return "(Probe)"
case SeedPart:
return "(Seed Part)"
case RecursivePart:
return "(Recursive Part)"
}
return ""
}
type operatorCtx struct {
depth uint32
label OperatorLabel
isRoot bool
storeType kv.StoreType
reqType ReadReqType
indent string
isLastChild bool
}
// FlattenPhysicalPlan generates a FlatPhysicalPlan from a PhysicalPlan, Insert, Delete, Update, Explain or Execute.
func FlattenPhysicalPlan(p Plan, buildSideFirst bool) *FlatPhysicalPlan {
if p == nil {
return nil
}
res := &FlatPhysicalPlan{
buildSideFirst: buildSideFirst,
}
initInfo := &operatorCtx{
depth: 0,
label: Empty,
isRoot: true,
storeType: kv.TiDB,
indent: "",
isLastChild: true,
}
res.Main, _ = res.flattenRecursively(p, initInfo, nil)
flattenedCTEPlan := make(map[int]struct{}, len(res.ctesToFlatten))
// Note that ctesToFlatten may be modified during the loop, so we manually loop over it instead of using for...range.
for i := 0; i < len(res.ctesToFlatten); i++ {
cte := res.ctesToFlatten[i]
cteDef := (*CTEDefinition)(cte)
if _, ok := flattenedCTEPlan[cteDef.CTE.IDForStorage]; ok {
continue
}
cteExplained := res.flattenCTERecursively(cteDef, initInfo, nil)
res.CTEs = append(res.CTEs, cteExplained)
flattenedCTEPlan[cteDef.CTE.IDForStorage] = struct{}{}
}
return res
}
func (f *FlatPhysicalPlan) flattenSingle(p Plan, info *operatorCtx) *FlatOperator {
// Some operators are not initialized and given an ExplainID. So their explain IDs are "_0"
// (when in EXPLAIN FORMAT = 'brief' it will be ""), we skip such operators.
// Examples: Explain, Execute
if len(p.TP()) == 0 && p.ID() == 0 {
return nil
}
res := &FlatOperator{
Origin: p,
Label: info.label,
IsRoot: info.isRoot,
StoreType: info.storeType,
Depth: info.depth,
ReqType: info.reqType,
TextTreeIndent: info.indent,
IsLastChild: info.isLastChild,
}
if _, ok := p.(PhysicalPlan); ok {
res.IsPhysicalPlan = true
}
return res
}
// Note that info should not be modified in this method.
func (f *FlatPhysicalPlan) flattenRecursively(p Plan, info *operatorCtx, target FlatPlanTree) (res FlatPlanTree, idx int) {
idx = -1
flat := f.flattenSingle(p, info)
if flat != nil {
target = append(target, flat)
idx = len(target) - 1
}
childIdxs := make([]int, 0)
var childIdx int
childCtx := &operatorCtx{
depth: info.depth + 1,
isRoot: info.isRoot,
storeType: info.storeType,
reqType: info.reqType,
indent: texttree.Indent4Child(info.indent, info.isLastChild),
}
// For physical operators, we just enumerate their children and collect their information.
// Note that some physical operators are special, and they are handled below this part.
if physPlan, ok := p.(PhysicalPlan); ok {
label := make([]OperatorLabel, len(physPlan.Children()))
switch plan := physPlan.(type) {
case *PhysicalApply:
label[plan.InnerChildIdx] = ProbeSide
label[1-plan.InnerChildIdx] = BuildSide
case *PhysicalHashJoin:
if plan.UseOuterToBuild {
label[plan.InnerChildIdx] = ProbeSide
label[1-plan.InnerChildIdx] = BuildSide
} else {
label[plan.InnerChildIdx] = BuildSide
label[1-plan.InnerChildIdx] = ProbeSide
}
case *PhysicalMergeJoin:
if plan.JoinType == RightOuterJoin {
label[0] = BuildSide
label[1] = ProbeSide
} else {
label[0] = ProbeSide
label[1] = BuildSide
}
case *PhysicalIndexJoin:
label[plan.InnerChildIdx] = ProbeSide
label[1-plan.InnerChildIdx] = BuildSide
case *PhysicalIndexMergeJoin:
label[plan.InnerChildIdx] = ProbeSide
label[1-plan.InnerChildIdx] = BuildSide
case *PhysicalIndexHashJoin:
label[plan.InnerChildIdx] = ProbeSide
label[1-plan.InnerChildIdx] = BuildSide
}
children := make([]PhysicalPlan, len(physPlan.Children()))
copy(children, physPlan.Children())
if len(label) == 2 &&
label[0] == ProbeSide &&
label[1] == BuildSide {
if f.buildSideFirst {
// Put the build side before the probe side if buildSideFirst is true.
label[0], label[1] = label[1], label[0]
children[0], children[1] = children[1], children[0]
} else if flat != nil {
// Set NeedReverseDriverSide to true if buildSideFirst is false.
flat.NeedReverseDriverSide = true
}
}
for i := range children {
childCtx.label = label[i]
childCtx.isLastChild = i == len(children)-1
target, childIdx = f.flattenRecursively(children[i], childCtx, target)
childIdxs = append(childIdxs, childIdx)
}
}
// For part of physical operators and some special operators, we need some special logic to get their "children".
// For PhysicalCTE, we need to add the plan tree into flatTree.ctesToFlatten.
switch plan := p.(type) {
case *PhysicalTableReader:
childCtx.isRoot = false
childCtx.storeType = plan.StoreType
childCtx.reqType = plan.ReadReqType
childCtx.label = Empty
childCtx.isLastChild = true
target, childIdx = f.flattenRecursively(plan.tablePlan, childCtx, target)
childIdxs = append(childIdxs, childIdx)
case *PhysicalIndexReader:
childCtx.isRoot = false
childCtx.reqType = Cop
childCtx.storeType = kv.TiKV
childCtx.label = Empty
childCtx.isLastChild = true
target, childIdx = f.flattenRecursively(plan.indexPlan, childCtx, target)
childIdxs = append(childIdxs, childIdx)
case *PhysicalIndexLookUpReader:
childCtx.isRoot = false
childCtx.reqType = Cop
childCtx.storeType = kv.TiKV
childCtx.label = BuildSide
childCtx.isLastChild = false
target, childIdx = f.flattenRecursively(plan.indexPlan, childCtx, target)
childIdxs = append(childIdxs, childIdx)
childCtx.label = ProbeSide
childCtx.isLastChild = true
target, childIdx = f.flattenRecursively(plan.tablePlan, childCtx, target)
childIdxs = append(childIdxs, childIdx)
case *PhysicalIndexMergeReader:
childCtx.isRoot = false
childCtx.reqType = Cop
childCtx.storeType = kv.TiKV
for _, pchild := range plan.partialPlans {
childCtx.label = BuildSide
childCtx.isLastChild = false
target, childIdx = f.flattenRecursively(pchild, childCtx, target)
childIdxs = append(childIdxs, childIdx)
}
childCtx.label = ProbeSide
childCtx.isLastChild = true
target, childIdx = f.flattenRecursively(plan.tablePlan, childCtx, target)
childIdxs = append(childIdxs, childIdx)
case *PhysicalShuffleReceiverStub:
childCtx.isRoot = true
childCtx.label = Empty
childCtx.isLastChild = true
target, childIdx = f.flattenRecursively(plan.DataSource, childCtx, target)
childIdxs = append(childIdxs, childIdx)
case *PhysicalCTE:
// We shallow copy the PhysicalCTE here because we don't want the probeParents (see comments in PhysicalPlan
// for details) to affect the row count display of the independent CTE plan tree.
copiedCTE := *plan
copiedCTE.probeParents = nil
f.ctesToFlatten = append(f.ctesToFlatten, &copiedCTE)
case *Insert:
if plan.SelectPlan != nil {
childCtx.isRoot = true
childCtx.label = Empty
childCtx.isLastChild = len(plan.FKChecks) == 0 && len(plan.FKCascades) == 0
target, childIdx = f.flattenRecursively(plan.SelectPlan, childCtx, target)
childIdxs = append(childIdxs, childIdx)
}
target, childIdxs = f.flattenForeignKeyChecksAndCascades(childCtx, target, childIdxs, plan.FKChecks, plan.FKCascades, true)
case *Update:
if plan.SelectPlan != nil {
childCtx.isRoot = true
childCtx.label = Empty
childCtx.isLastChild = len(plan.FKChecks) == 0 && len(plan.FKCascades) == 0
target, childIdx = f.flattenRecursively(plan.SelectPlan, childCtx, target)
childIdxs = append(childIdxs, childIdx)
}
target, childIdxs = f.flattenForeignKeyChecksAndCascadesMap(childCtx, target, childIdxs, plan.FKChecks, plan.FKCascades)
case *Delete:
if plan.SelectPlan != nil {
childCtx.isRoot = true
childCtx.label = Empty
childCtx.isLastChild = len(plan.FKChecks) == 0 && len(plan.FKCascades) == 0
target, childIdx = f.flattenRecursively(plan.SelectPlan, childCtx, target)
childIdxs = append(childIdxs, childIdx)
}
target, childIdxs = f.flattenForeignKeyChecksAndCascadesMap(childCtx, target, childIdxs, plan.FKChecks, plan.FKCascades)
case *Execute:
f.InExecute = true
if plan.Plan != nil {
childCtx.isRoot = true
childCtx.indent = info.indent
childCtx.label = Empty
childCtx.isLastChild = true
target, childIdx = f.flattenRecursively(plan.Plan, childCtx, target)
childIdxs = append(childIdxs, childIdx)
}
case *Explain:
f.InExplain = true
// Explain is ignored in flattenSingle(). We start to explain its TargetPlan from a new operatorCtx.
if plan.TargetPlan != nil {
initInfo := &operatorCtx{
depth: 0,
label: Empty,
isRoot: true,
storeType: kv.TiDB,
indent: "",
isLastChild: true,
}
target, childIdx = f.flattenRecursively(plan.TargetPlan, initInfo, target)
childIdxs = append(childIdxs, childIdx)
}
case *FKCascade:
for i, child := range plan.CascadePlans {
childCtx.label = Empty
childCtx.isLastChild = i == len(plan.CascadePlans)-1
target, childIdx = f.flattenRecursively(child, childCtx, target)
childIdxs = append(childIdxs, childIdx)
}
}
if flat != nil {
flat.ChildrenIdx = childIdxs
flat.ChildrenEndIdx = len(target) - 1
}
return target, idx
}
func (f *FlatPhysicalPlan) flattenForeignKeyChecksAndCascadesMap(childCtx *operatorCtx, target FlatPlanTree, childIdxs []int, fkChecksMap map[int64][]*FKCheck, fkCascadesMap map[int64][]*FKCascade) (FlatPlanTree, []int) {
tids := make([]int64, 0, len(fkChecksMap))
for tid := range fkChecksMap {
tids = append(tids, tid)
}
// Sort by table id for explain result stable.
sort.Slice(tids, func(i, j int) bool {
return tids[i] < tids[j]
})
for i, tid := range tids {
target, childIdxs = f.flattenForeignKeyChecksAndCascades(childCtx, target, childIdxs, fkChecksMap[tid], nil, len(fkCascadesMap) == 0 && i == len(tids)-1)
}
tids = tids[:0]
for tid := range fkCascadesMap {
tids = append(tids, tid)
}
sort.Slice(tids, func(i, j int) bool {
return tids[i] < tids[j]
})
for i, tid := range tids {
target, childIdxs = f.flattenForeignKeyChecksAndCascades(childCtx, target, childIdxs, nil, fkCascadesMap[tid], i == len(tids)-1)
}
return target, childIdxs
}
func (f *FlatPhysicalPlan) flattenForeignKeyChecksAndCascades(childCtx *operatorCtx, target FlatPlanTree, childIdxs []int, fkChecks []*FKCheck, fkCascades []*FKCascade, isLast bool) (FlatPlanTree, []int) {
var childIdx int
for i, fkCheck := range fkChecks {
childCtx.isRoot = true
childCtx.label = Empty
childCtx.isLastChild = isLast && len(fkCascades) == 0 && i == len(fkChecks)-1
target, childIdx = f.flattenRecursively(fkCheck, childCtx, target)
childIdxs = append(childIdxs, childIdx)
}
for i, fkCascade := range fkCascades {
childCtx.isRoot = true
childCtx.label = Empty
childCtx.isLastChild = isLast && i == len(fkCascades)-1
target, childIdx = f.flattenRecursively(fkCascade, childCtx, target)
childIdxs = append(childIdxs, childIdx)
}
return target, childIdxs
}
func (f *FlatPhysicalPlan) flattenCTERecursively(cteDef *CTEDefinition, info *operatorCtx, target FlatPlanTree) FlatPlanTree {
flat := f.flattenSingle(cteDef, info)
if flat != nil {
target = append(target, flat)
}
childIdxs := make([]int, 0)
var childIdx int
childInfo := &operatorCtx{
depth: info.depth + 1,
label: SeedPart,
isRoot: true,
storeType: kv.TiDB,
indent: texttree.Indent4Child(info.indent, info.isLastChild),
isLastChild: cteDef.RecurPlan == nil,
}
target, childIdx = f.flattenRecursively(cteDef.SeedPlan, childInfo, target)
childIdxs = append(childIdxs, childIdx)
if cteDef.RecurPlan != nil {
childInfo.label = RecursivePart
childInfo.isLastChild = true
target, childIdx = f.flattenRecursively(cteDef.RecurPlan, childInfo, target)
childIdxs = append(childIdxs, childIdx)
}
if flat != nil {
flat.ChildrenIdx = childIdxs
}
return target
}