-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
walk.go
441 lines (368 loc) · 14.3 KB
/
walk.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
// Copyright 2016 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package sql
import (
"context"
"reflect"
"github.com/cockroachdb/errors"
)
// planObserver is the interface to implement by components that need
// to visit a planNode tree.
// Used mainly by EXPLAIN, but also for the collector of back-references
// for view definitions.
type planObserver struct {
// replaceNode is invoked upon entering a tree node. It can replace the
// current planNode in the tree by returning a non-nil planNode. Returning
// nil will continue the recursion and not modify the current node.
replaceNode func(ctx context.Context, nodeName string, plan planNode) (planNode, error)
// enterNode is invoked upon entering a tree node. It can return false to
// stop the recursion at this node.
enterNode func(ctx context.Context, nodeName string, plan planNode) (bool, error)
// leaveNode is invoked upon leaving a tree node.
leaveNode func(nodeName string, plan planNode) error
// followRowSourceToPlanNode controls whether the tree walker continues
// walking when it encounters a rowSourceToPlanNode, which indicates that the
// logical plan has been mutated for distribution. This should normally be
// set to false, as normally the planNodeToRowSource on the other end will
// take care of propagating signals via its own walker.
followRowSourceToPlanNode bool
}
// walkPlan performs a depth-first traversal of the plan given as
// argument, informing the planObserver of the node details at each
// level.
func walkPlan(ctx context.Context, plan planNode, observer planObserver) error {
v := makePlanVisitor(ctx, observer)
v.visit(plan)
return v.err
}
// planVisitor is the support structure for walkPlan().
type planVisitor struct {
observer planObserver
ctx context.Context
err error
}
// makePlanVisitor creates a planVisitor instance.
// ctx will be stored in the planVisitor and used when visiting planNode's and
// expressions..
func makePlanVisitor(ctx context.Context, observer planObserver) planVisitor {
return planVisitor{observer: observer, ctx: ctx}
}
// visit is the recursive function that supports walkPlan().
func (v *planVisitor) visit(plan planNode) planNode {
if v.err != nil {
return plan
}
name := nodeName(plan)
if v.observer.replaceNode != nil {
newNode, err := v.observer.replaceNode(v.ctx, name, plan)
if err != nil {
v.err = err
return plan
}
if newNode != nil {
return newNode
}
}
v.visitInternal(plan, name)
return plan
}
// visitConcrete is like visit, but provided for the case where a planNode is
// trying to recurse into a concrete planNode type, and not a planNode
// interface.
func (v *planVisitor) visitConcrete(plan planNode) {
if v.err != nil {
return
}
name := nodeName(plan)
v.visitInternal(plan, name)
}
func (v *planVisitor) visitInternal(plan planNode, name string) {
if v.err != nil {
return
}
recurse := true
if v.observer.enterNode != nil {
recurse, v.err = v.observer.enterNode(v.ctx, name, plan)
if v.err != nil {
return
}
}
if v.observer.leaveNode != nil {
defer func() {
if v.err != nil {
return
}
v.err = v.observer.leaveNode(name, plan)
}()
}
if !recurse {
return
}
switch n := plan.(type) {
case *valuesNode:
case *scanNode:
case *filterNode:
n.source.plan = v.visit(n.source.plan)
case *renderNode:
n.source.plan = v.visit(n.source.plan)
case *indexJoinNode:
n.input = v.visit(n.input)
case *lookupJoinNode:
n.input = v.visit(n.input)
case *vTableLookupJoinNode:
n.input = v.visit(n.input)
case *zigzagJoinNode:
case *applyJoinNode:
n.input.plan = v.visit(n.input.plan)
case *joinNode:
n.left.plan = v.visit(n.left.plan)
n.right.plan = v.visit(n.right.plan)
case *interleavedJoinNode:
case *invertedFilterNode:
n.input = v.visit(n.input)
case *invertedJoinNode:
n.input = v.visit(n.input)
case *limitNode:
n.plan = v.visit(n.plan)
case *max1RowNode:
n.plan = v.visit(n.plan)
case *distinctNode:
n.plan = v.visit(n.plan)
case *sortNode:
n.plan = v.visit(n.plan)
case *groupNode:
n.plan = v.visit(n.plan)
case *windowNode:
n.plan = v.visit(n.plan)
case *unionNode:
n.left = v.visit(n.left)
n.right = v.visit(n.right)
case *splitNode:
n.rows = v.visit(n.rows)
case *unsplitNode:
n.rows = v.visit(n.rows)
case *relocateNode:
n.rows = v.visit(n.rows)
case *insertNode, *insertFastPathNode:
if ins, ok := n.(*insertNode); ok {
ins.source = v.visit(ins.source)
}
case *upsertNode:
n.source = v.visit(n.source)
case *updateNode:
n.source = v.visit(n.source)
case *deleteNode:
n.source = v.visit(n.source)
case *deleteRangeNode:
case *serializeNode:
v.visitConcrete(n.source)
case *rowCountNode:
v.visitConcrete(n.source)
case *createTableNode:
if n.n.As() {
n.sourcePlan = v.visit(n.sourcePlan)
}
case *createViewNode:
case *setVarNode:
case *setClusterSettingNode:
case *delayedNode:
if n.plan != nil {
n.plan = v.visit(n.plan)
}
case *explainDistSQLNode:
// We check whether planNode is nil because the plan might be
// represented physically. We don't yet have a walker over such
// representation, so we simply short-circuit.
// TODO(yuzefovich): implement that walker and use it here.
if n.plan.main.planNode == nil {
return
}
n.plan.main.planNode = v.visit(n.plan.main.planNode)
case *explainVecNode:
// We check whether planNode is nil because the plan might be
// represented physically. We don't yet have a walker over such
// representation, so we simply short-circuit.
// TODO(yuzefovich): implement that walker and use it here.
if n.plan.main.planNode == nil {
return
}
n.plan.main.planNode = v.visit(n.plan.main.planNode)
case *ordinalityNode:
n.source = v.visit(n.source)
case *spoolNode:
n.source = v.visit(n.source)
case *saveTableNode:
n.source = v.visit(n.source)
case *showTraceReplicaNode:
n.plan = v.visit(n.plan)
case *cancelQueriesNode:
n.rows = v.visit(n.rows)
case *cancelSessionsNode:
n.rows = v.visit(n.rows)
case *controlJobsNode:
n.rows = v.visit(n.rows)
case *controlSchedulesNode:
n.rows = v.visit(n.rows)
case *setZoneConfigNode:
case *projectSetNode:
n.source = v.visit(n.source)
case *rowSourceToPlanNode:
if v.observer.followRowSourceToPlanNode && n.originalPlanNode != nil {
v.visit(n.originalPlanNode)
}
case *errorIfRowsNode:
n.plan = v.visit(n.plan)
case *scanBufferNode:
case *bufferNode:
n.plan = v.visit(n.plan)
case *recursiveCTENode:
n.initial = v.visit(n.initial)
case *exportNode:
n.source = v.visit(n.source)
}
}
// nodeName returns the name of the given planNode as string. The
// node's current state is taken into account, e.g. sortNode has
// either name "sort" or "nosort" depending on whether sorting is
// needed.
func nodeName(plan planNode) string {
// Some nodes have custom names depending on attributes.
switch n := plan.(type) {
case *scanNode:
if n.reverse {
return "revscan"
}
case *unionNode:
if n.emitAll {
return "append"
}
case *joinNode:
if len(n.mergeJoinOrdering) > 0 {
return "merge join"
}
if len(n.pred.leftEqualityIndices) == 0 {
return "cross join"
}
return "hash join"
}
name, ok := planNodeNames[reflect.TypeOf(plan)]
if !ok {
panic(errors.AssertionFailedf("name missing for type %T", plan))
}
return name
}
// planNodeNames is the mapping from node type to strings. The
// strings are constant and not precomputed so that the type names can
// be changed without changing the output of "EXPLAIN".
var planNodeNames = map[reflect.Type]string{
reflect.TypeOf(&alterDatabaseOwnerNode{}): "alter database owner",
reflect.TypeOf(&alterIndexNode{}): "alter index",
reflect.TypeOf(&alterSequenceNode{}): "alter sequence",
reflect.TypeOf(&alterSchemaNode{}): "alter schema",
reflect.TypeOf(&alterTableNode{}): "alter table",
reflect.TypeOf(&alterTableSetSchemaNode{}): "alter table set schema",
reflect.TypeOf(&alterTypeNode{}): "alter type",
reflect.TypeOf(&alterRoleNode{}): "alter role",
reflect.TypeOf(&applyJoinNode{}): "apply join",
reflect.TypeOf(&bufferNode{}): "buffer",
reflect.TypeOf(&cancelQueriesNode{}): "cancel queries",
reflect.TypeOf(&cancelSessionsNode{}): "cancel sessions",
reflect.TypeOf(&changePrivilegesNode{}): "change privileges",
reflect.TypeOf(&commentOnColumnNode{}): "comment on column",
reflect.TypeOf(&commentOnDatabaseNode{}): "comment on database",
reflect.TypeOf(&commentOnIndexNode{}): "comment on index",
reflect.TypeOf(&commentOnTableNode{}): "comment on table",
reflect.TypeOf(&controlJobsNode{}): "control jobs",
reflect.TypeOf(&controlSchedulesNode{}): "control schedules",
reflect.TypeOf(&createDatabaseNode{}): "create database",
reflect.TypeOf(&createExtensionNode{}): "create extension",
reflect.TypeOf(&createIndexNode{}): "create index",
reflect.TypeOf(&createSequenceNode{}): "create sequence",
reflect.TypeOf(&createSchemaNode{}): "create schema",
reflect.TypeOf(&createStatsNode{}): "create statistics",
reflect.TypeOf(&createTableNode{}): "create table",
reflect.TypeOf(&createTypeNode{}): "create type",
reflect.TypeOf(&CreateRoleNode{}): "create user/role",
reflect.TypeOf(&createViewNode{}): "create view",
reflect.TypeOf(&delayedNode{}): "virtual table",
reflect.TypeOf(&deleteNode{}): "delete",
reflect.TypeOf(&deleteRangeNode{}): "delete range",
reflect.TypeOf(&distinctNode{}): "distinct",
reflect.TypeOf(&dropDatabaseNode{}): "drop database",
reflect.TypeOf(&dropIndexNode{}): "drop index",
reflect.TypeOf(&dropSequenceNode{}): "drop sequence",
reflect.TypeOf(&dropSchemaNode{}): "drop schema",
reflect.TypeOf(&dropTableNode{}): "drop table",
reflect.TypeOf(&dropTypeNode{}): "drop type",
reflect.TypeOf(&DropRoleNode{}): "drop user/role",
reflect.TypeOf(&dropViewNode{}): "drop view",
reflect.TypeOf(&errorIfRowsNode{}): "error if rows",
reflect.TypeOf(&explainDistSQLNode{}): "explain distsql",
reflect.TypeOf(&explainPlanNode{}): "explain plan",
reflect.TypeOf(&explainVecNode{}): "explain vectorized",
reflect.TypeOf(&exportNode{}): "export",
reflect.TypeOf(&filterNode{}): "filter",
reflect.TypeOf(&GrantRoleNode{}): "grant role",
reflect.TypeOf(&groupNode{}): "group",
reflect.TypeOf(&hookFnNode{}): "plugin",
reflect.TypeOf(&indexJoinNode{}): "index join",
reflect.TypeOf(&insertNode{}): "insert",
reflect.TypeOf(&insertFastPathNode{}): "insert fast path",
reflect.TypeOf(&interleavedJoinNode{}): "interleaved join",
reflect.TypeOf(&invertedFilterNode{}): "inverted filter",
reflect.TypeOf(&invertedJoinNode{}): "inverted join",
reflect.TypeOf(&joinNode{}): "join",
reflect.TypeOf(&limitNode{}): "limit",
reflect.TypeOf(&lookupJoinNode{}): "lookup join",
reflect.TypeOf(&max1RowNode{}): "max1row",
reflect.TypeOf(&ordinalityNode{}): "ordinality",
reflect.TypeOf(&projectSetNode{}): "project set",
reflect.TypeOf(&recursiveCTENode{}): "recursive cte",
reflect.TypeOf(&refreshMaterializedViewNode{}): "refresh materialized view",
reflect.TypeOf(&relocateNode{}): "relocate",
reflect.TypeOf(&renameColumnNode{}): "rename column",
reflect.TypeOf(&renameDatabaseNode{}): "rename database",
reflect.TypeOf(&renameIndexNode{}): "rename index",
reflect.TypeOf(&renameTableNode{}): "rename table",
reflect.TypeOf(&reparentDatabaseNode{}): "TODO (rohany): fill out",
reflect.TypeOf(&renderNode{}): "render",
reflect.TypeOf(&RevokeRoleNode{}): "revoke role",
reflect.TypeOf(&rowCountNode{}): "count",
reflect.TypeOf(&rowSourceToPlanNode{}): "row source to plan node",
reflect.TypeOf(&saveTableNode{}): "save table",
reflect.TypeOf(&scanBufferNode{}): "scan buffer",
reflect.TypeOf(&scanNode{}): "scan",
reflect.TypeOf(&scatterNode{}): "scatter",
reflect.TypeOf(&scrubNode{}): "scrub",
reflect.TypeOf(&sequenceSelectNode{}): "sequence select",
reflect.TypeOf(&serializeNode{}): "run",
reflect.TypeOf(&setClusterSettingNode{}): "set cluster setting",
reflect.TypeOf(&setVarNode{}): "set",
reflect.TypeOf(&setZoneConfigNode{}): "configure zone",
reflect.TypeOf(&showFingerprintsNode{}): "show fingerprints",
reflect.TypeOf(&showTraceNode{}): "show trace for",
reflect.TypeOf(&showTraceReplicaNode{}): "replica trace",
reflect.TypeOf(&sortNode{}): "sort",
reflect.TypeOf(&splitNode{}): "split",
reflect.TypeOf(&unsplitNode{}): "unsplit",
reflect.TypeOf(&unsplitAllNode{}): "unsplit all",
reflect.TypeOf(&spoolNode{}): "spool",
reflect.TypeOf(&truncateNode{}): "truncate",
reflect.TypeOf(&unaryNode{}): "emptyrow",
reflect.TypeOf(&unionNode{}): "union",
reflect.TypeOf(&updateNode{}): "update",
reflect.TypeOf(&upsertNode{}): "upsert",
reflect.TypeOf(&valuesNode{}): "values",
reflect.TypeOf(&virtualTableNode{}): "virtual table values",
reflect.TypeOf(&vTableLookupJoinNode{}): "virtual table lookup join",
reflect.TypeOf(&windowNode{}): "window",
reflect.TypeOf(&zeroNode{}): "norows",
reflect.TypeOf(&zigzagJoinNode{}): "zigzag join",
}