-
Notifications
You must be signed in to change notification settings - Fork 1
/
plan.go
542 lines (509 loc) · 17.7 KB
/
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// Copyright 2015 The Cockroach Authors.
//
// 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 sql
import (
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
)
type planMaker interface {
// newPlan starts preparing the query plan for a single SQL
// statement.
//
// It performs as many early checks as possible on the structure of
// the SQL statement, including verifying permissions and type
// checking. The returned plan object is not ready to execute; the
// optimizePlan() method must be called first. See makePlan()
// below.
//
// This method should not be used directly; instead prefer makePlan()
// or prepare() below.
newPlan(
ctx context.Context, stmt parser.Statement, desiredTypes []parser.Type,
) (planNode, error)
// makePlan prepares the query plan for a single SQL statement. it
// calls newPlan() then optimizePlan() on the result. Execution must
// start by calling Start() first and then iterating using Next()
// and Values() in order to retrieve matching rows.
//
// makePlan starts preparing the query plan for a single SQL
// statement.
// It performs as many early checks as possible on the structure of
// the SQL statement, including verifying permissions and type checking.
// The returned plan object is ready to execute. Execution
// must start by calling Start() first and then iterating using
// Next() and Values() in order to retrieve matching
// rows.
makePlan(ctx context.Context, stmt Statement) (planNode, error)
// prepare does the same checks as makePlan but skips building some
// data structures necessary for execution, based on the assumption
// that the plan will never be run. A planNode built with prepare()
// will do just enough work to check the structural validity of the
// SQL statement and determine types for placeholders. However it is
// not appropriate to call optimizePlan(), Next() or Values() on a plan
// object created with prepare().
prepare(ctx context.Context, stmt parser.Statement) (planNode, error)
}
var _ planMaker = &planner{}
// runParams is a struct containing all parameters passed to planNode.Next() and
// planNode.Start().
type runParams struct {
// context.Context for this method call.
ctx context.Context
// planner associated with this planNode.
p *planner
}
// planNode defines the interface for executing a query or portion of a query.
//
// The following methods apply to planNodes and contain special cases
// for each type; they thus need to be extended when adding/removing
// planNode instances:
// - planMaker.newPlan()
// - planMaker.prepare()
// - planMaker.setNeededColumns() (needed_columns.go)
// - planMaker.expandPlan() (expand_plan.go)
// - planVisitor.visit() (walk.go)
// - planNodeNames (walk.go)
// - planMaker.optimizeFilters() (filter_opt.go)
// - setLimitHint() (limit_hint.go)
// - collectSpans() (plan_spans.go)
// - planOrdering() (plan_ordering.go)
// - planColumns() (plan_columns.go)
//
type planNode interface {
// Start begins the processing of the query/statement and starts
// performing side effects for data-modifying statements. Returns an
// error if initial processing fails.
//
// Note: Don't use directly. Use startPlan() instead.
//
// Available after optimizePlan() (or makePlan).
Start(params runParams) error
// Next performs one unit of work, returning false if an error is
// encountered or if there is no more work to do. For statements
// that return a result set, the Values() method will return one row
// of results each time that Next() returns true.
// See executor.go: forEachRow() for an example.
//
// Available after Start(). It is illegal to call Next() after it returns
// false. It is legal to call Next() even if the node implements
// planNodeFastPath and the FastPathResults() method returns true.
Next(params runParams) (bool, error)
// Values returns the values at the current row. The result is only valid
// until the next call to Next().
//
// Available after Next().
Values() parser.Datums
// Close terminates the planNode execution and releases its resources.
// This method should be called if the node has been used in any way (any
// methods on it have been called) after it was constructed. Note that this
// doesn't imply that Start() has been necessarily called.
Close(ctx context.Context)
}
// planNodeFastPath is implemented by nodes that can perform all their
// work during Start(), possibly affecting even multiple rows. For
// example, DELETE can do this.
type planNodeFastPath interface {
// FastPathResults returns the affected row count and true if the
// node has no result set and has already executed when Start() completes.
// Note that Next() must still be valid even if this method returns
// true, although it may have nothing left to do.
FastPathResults() (int, bool)
}
var _ planNode = &alterTableNode{}
var _ planNode = ©Node{}
var _ planNode = &createDatabaseNode{}
var _ planNode = &createIndexNode{}
var _ planNode = &createTableNode{}
var _ planNode = &createViewNode{}
var _ planNode = &delayedNode{}
var _ planNode = &deleteNode{}
var _ planNode = &distinctNode{}
var _ planNode = &dropDatabaseNode{}
var _ planNode = &dropIndexNode{}
var _ planNode = &dropTableNode{}
var _ planNode = &dropViewNode{}
var _ planNode = &zeroNode{}
var _ planNode = &unaryNode{}
var _ planNode = &explainDistSQLNode{}
var _ planNode = &explainPlanNode{}
var _ planNode = &traceNode{}
var _ planNode = &filterNode{}
var _ planNode = &groupNode{}
var _ planNode = &hookFnNode{}
var _ planNode = &indexJoinNode{}
var _ planNode = &insertNode{}
var _ planNode = &joinNode{}
var _ planNode = &limitNode{}
var _ planNode = &ordinalityNode{}
var _ planNode = &testingRelocateNode{}
var _ planNode = &renderNode{}
var _ planNode = &scanNode{}
var _ planNode = &scatterNode{}
var _ planNode = &showRangesNode{}
var _ planNode = &showFingerprintsNode{}
var _ planNode = &sortNode{}
var _ planNode = &splitNode{}
var _ planNode = &unionNode{}
var _ planNode = &updateNode{}
var _ planNode = &valueGenerator{}
var _ planNode = &valuesNode{}
var _ planNode = &windowNode{}
var _ planNode = &createUserNode{}
var _ planNode = &dropUserNode{}
var _ planNodeFastPath = &deleteNode{}
var _ planNodeFastPath = &dropUserNode{}
// makePlan implements the Planner interface.
func (p *planner) makePlan(ctx context.Context, stmt Statement) (planNode, error) {
plan, err := p.newPlan(ctx, stmt.AST, nil)
if err != nil {
return nil, err
}
if stmt.ExpectedTypes != nil {
if !stmt.ExpectedTypes.TypesEqual(planColumns(plan)) {
return nil, pgerror.NewError(pgerror.CodeFeatureNotSupportedError,
"cached plan must not change result type")
}
}
if err := p.semaCtx.Placeholders.AssertAllAssigned(); err != nil {
return nil, err
}
needed := allColumns(plan)
plan, err = p.optimizePlan(ctx, plan, needed)
if err != nil {
// Once the plan has undergone optimization, it may contain
// monitor-registered memory, even in case of error.
plan.Close(ctx)
return nil, err
}
if log.V(3) {
log.Infof(ctx, "statement %s compiled to:\n%s", stmt, planToString(ctx, plan))
}
return plan, nil
}
// startPlan starts the plan and all its sub-query nodes.
func (p *planner) startPlan(ctx context.Context, plan planNode) error {
if err := p.startSubqueryPlans(ctx, plan); err != nil {
return err
}
params := runParams{
ctx: ctx,
p: p,
}
if err := plan.Start(params); err != nil {
return err
}
// Trigger limit propagation through the plan and sub-queries.
setUnlimited(plan)
return nil
}
func (p *planner) maybePlanHook(ctx context.Context, stmt parser.Statement) (planNode, error) {
// TODO(dan): This iteration makes the plan dispatch no longer constant
// time. We could fix that with a map of `reflect.Type` but including
// reflection in such a primary codepath is unfortunate. Instead, the
// upcoming IR work will provide unique numeric type tags, which will
// elegantly solve this.
for _, planHook := range planHooks {
if fn, header, err := planHook(stmt, p); err != nil {
return nil, err
} else if fn != nil {
return &hookFnNode{f: fn, header: header}, nil
}
}
return nil, nil
}
// delegateQuery creates a plan for a given SQL query.
// In addition, the caller can specify an additional validation
// function (initialCheck) that will be ran and checked for errors
// during plan optimization. This is meant for checks that cannot be
// run during a SQL prepare operation.
func (p *planner) delegateQuery(
ctx context.Context,
name string,
sql string,
initialCheck func(ctx context.Context) error,
desiredTypes []parser.Type,
) (planNode, error) {
// Prepare the sub-plan.
stmt, err := parser.ParseOne(sql)
if err != nil {
return nil, err
}
plan, err := p.newPlan(ctx, stmt, desiredTypes)
if err != nil {
return nil, err
}
if initialCheck == nil {
return plan, nil
}
// To enable late calling into initialCheck, we use a delayedNode.
return &delayedNode{
name: name,
// The columns attribute cannot be a straight-up reference to the sub-plan's
// own columns, because they can be modified in-place by setNeededColumns().
columns: append(sqlbase.ResultColumns(nil), planColumns(plan)...),
// The delayed constructor's only responsibility is to call
// initialCheck() - the plan is already constructed.
constructor: func(ctx context.Context, _ *planner) (planNode, error) {
if err := initialCheck(ctx); err != nil {
return nil, err
}
return plan, nil
},
// Breaking with the common usage pattern of delayedNode, where
// the plan attribute is initially nil (the constructor creates
// it), here we prepopulate the field with the sub-plan created
// above. We do this instead of simply returning the newly created
// sub-plan in a constructor closure, to ensure the sub-plan is
// properly Close()d if the delayedNode is discarded before its
// constructor is called.
plan: plan,
}, nil
}
// newPlan constructs a planNode from a statement. This is used
// recursively by the various node constructors.
func (p *planner) newPlan(
ctx context.Context, stmt parser.Statement, desiredTypes []parser.Type,
) (planNode, error) {
tracing.AnnotateTrace()
// This will set the system DB trigger for transactions containing
// DDL statements that have no effect, such as
// `BEGIN; INSERT INTO ...; CREATE TABLE IF NOT EXISTS ...; COMMIT;`
// where the table already exists. This will generate some false
// refreshes, but that's expected to be quite rare in practice.
if stmt.StatementType() == parser.DDL {
if err := p.txn.SetSystemConfigTrigger(); err != nil {
return nil, errors.Wrap(err,
"schema change statement cannot follow a statement that has written in the same transaction")
}
}
if plan, err := p.maybePlanHook(ctx, stmt); plan != nil || err != nil {
return plan, err
}
switch n := stmt.(type) {
case *parser.AlterTable:
return p.AlterTable(ctx, n)
case *parser.BeginTransaction:
return p.BeginTransaction(n)
case *parser.CancelQuery:
return p.CancelQuery(ctx, n)
case *parser.CancelJob:
return p.CancelJob(ctx, n)
case CopyDataBlock:
return p.CopyData(ctx, n)
case *parser.CopyFrom:
return p.CopyFrom(ctx, n)
case *parser.CreateDatabase:
return p.CreateDatabase(n)
case *parser.CreateIndex:
return p.CreateIndex(ctx, n)
case *parser.CreateTable:
return p.CreateTable(ctx, n)
case *parser.CreateUser:
return p.CreateUser(ctx, n)
case *parser.CreateView:
return p.CreateView(ctx, n)
case *parser.Deallocate:
return p.Deallocate(ctx, n)
case *parser.Delete:
return p.Delete(ctx, n, desiredTypes)
case *parser.Discard:
return p.Discard(ctx, n)
case *parser.DropDatabase:
return p.DropDatabase(ctx, n)
case *parser.DropIndex:
return p.DropIndex(ctx, n)
case *parser.DropTable:
return p.DropTable(ctx, n)
case *parser.DropView:
return p.DropView(ctx, n)
case *parser.DropUser:
return p.DropUser(ctx, n)
case *parser.Execute:
return p.Execute(ctx, n)
case *parser.Explain:
return p.Explain(ctx, n)
case *parser.Grant:
return p.Grant(ctx, n)
case *parser.Insert:
return p.Insert(ctx, n, desiredTypes)
case *parser.ParenSelect:
return p.newPlan(ctx, n.Select, desiredTypes)
case *parser.PauseJob:
return p.PauseJob(ctx, n)
case *parser.TestingRelocate:
return p.TestingRelocate(ctx, n)
case *parser.RenameColumn:
return p.RenameColumn(ctx, n)
case *parser.RenameDatabase:
return p.RenameDatabase(ctx, n)
case *parser.RenameIndex:
return p.RenameIndex(ctx, n)
case *parser.RenameTable:
return p.RenameTable(ctx, n)
case *parser.ResumeJob:
return p.ResumeJob(ctx, n)
case *parser.Revoke:
return p.Revoke(ctx, n)
case *parser.Scatter:
return p.Scatter(ctx, n)
case *parser.Select:
return p.Select(ctx, n, desiredTypes)
case *parser.SelectClause:
return p.SelectClause(ctx, n, nil, nil, desiredTypes, publicColumns)
case *parser.SetClusterSetting:
return p.SetClusterSetting(ctx, n)
case *parser.SetVar:
return p.SetVar(ctx, n)
case *parser.SetTransaction:
return p.SetTransaction(n)
case *parser.SetDefaultIsolation:
return p.SetDefaultIsolation(n)
case *parser.ShowClusterSetting:
return p.ShowClusterSetting(ctx, n)
case *parser.ShowVar:
return p.ShowVar(ctx, n)
case *parser.ShowColumns:
return p.ShowColumns(ctx, n)
case *parser.ShowConstraints:
return p.ShowConstraints(ctx, n)
case *parser.ShowCreateTable:
return p.ShowCreateTable(ctx, n)
case *parser.ShowCreateView:
return p.ShowCreateView(ctx, n)
case *parser.ShowDatabases:
return p.ShowDatabases(ctx, n)
case *parser.ShowGrants:
return p.ShowGrants(ctx, n)
case *parser.ShowIndex:
return p.ShowIndex(ctx, n)
case *parser.ShowQueries:
return p.ShowQueries(ctx, n)
case *parser.ShowJobs:
return p.ShowJobs(ctx, n)
case *parser.ShowSessions:
return p.ShowSessions(ctx, n)
case *parser.ShowTables:
return p.ShowTables(ctx, n)
case *parser.ShowTrace:
return p.ShowTrace(ctx, n)
case *parser.ShowTransactionStatus:
return p.ShowTransactionStatus(ctx)
case *parser.ShowUsers:
return p.ShowUsers(ctx, n)
case *parser.ShowRanges:
return p.ShowRanges(ctx, n)
case *parser.ShowFingerprints:
return p.ShowFingerprints(ctx, n)
case *parser.Split:
return p.Split(ctx, n)
case *parser.Truncate:
if err := p.txn.SetSystemConfigTrigger(); err != nil {
return nil, err
}
return p.Truncate(ctx, n)
case *parser.UnionClause:
return p.UnionClause(ctx, n, desiredTypes)
case *parser.Update:
return p.Update(ctx, n, desiredTypes)
case *parser.ValuesClause:
return p.ValuesClause(ctx, n, desiredTypes)
default:
return nil, errors.Errorf("unknown statement type: %T", stmt)
}
}
// prepare constructs the logical plan for the statement. This is
// needed both to type placeholders and to inform pgwire of the types
// of the result columns. All statements that either support
// placeholders or have result columns must be handled here.
func (p *planner) prepare(ctx context.Context, stmt parser.Statement) (planNode, error) {
if plan, err := p.maybePlanHook(ctx, stmt); plan != nil || err != nil {
return plan, err
}
switch n := stmt.(type) {
case *parser.CancelQuery:
return p.CancelQuery(ctx, n)
case *parser.CancelJob:
return p.CancelJob(ctx, n)
case *parser.Delete:
return p.Delete(ctx, n, nil)
case *parser.Explain:
return p.Explain(ctx, n)
case *parser.Insert:
return p.Insert(ctx, n, nil)
case *parser.PauseJob:
return p.PauseJob(ctx, n)
case *parser.ResumeJob:
return p.ResumeJob(ctx, n)
case *parser.Select:
return p.Select(ctx, n, nil)
case *parser.SelectClause:
return p.SelectClause(ctx, n, nil, nil, nil, publicColumns)
case *parser.SetClusterSetting:
return p.SetClusterSetting(ctx, n)
case *parser.SetVar:
return p.SetVar(ctx, n)
case *parser.ShowClusterSetting:
return p.ShowClusterSetting(ctx, n)
case *parser.ShowVar:
return p.ShowVar(ctx, n)
case *parser.ShowCreateTable:
return p.ShowCreateTable(ctx, n)
case *parser.ShowCreateView:
return p.ShowCreateView(ctx, n)
case *parser.ShowColumns:
return p.ShowColumns(ctx, n)
case *parser.ShowDatabases:
return p.ShowDatabases(ctx, n)
case *parser.ShowGrants:
return p.ShowGrants(ctx, n)
case *parser.ShowIndex:
return p.ShowIndex(ctx, n)
case *parser.ShowConstraints:
return p.ShowConstraints(ctx, n)
case *parser.ShowQueries:
return p.ShowQueries(ctx, n)
case *parser.ShowJobs:
return p.ShowJobs(ctx, n)
case *parser.ShowSessions:
return p.ShowSessions(ctx, n)
case *parser.ShowTables:
return p.ShowTables(ctx, n)
case *parser.ShowTrace:
return p.ShowTrace(ctx, n)
case *parser.ShowUsers:
return p.ShowUsers(ctx, n)
case *parser.ShowTransactionStatus:
return p.ShowTransactionStatus(ctx)
case *parser.ShowRanges:
return p.ShowRanges(ctx, n)
case *parser.Split:
return p.Split(ctx, n)
case *parser.TestingRelocate:
return p.TestingRelocate(ctx, n)
case *parser.Scatter:
return p.Scatter(ctx, n)
case *parser.Update:
return p.Update(ctx, n, nil)
default:
// Other statement types do not have result columns and do not
// support placeholders so there is no need for any special
// handling here.
return nil, nil
}
}