-
Notifications
You must be signed in to change notification settings - Fork 0
/
expand_plan.go
585 lines (505 loc) · 16.9 KB
/
expand_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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// Copyright 2016 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 (
"fmt"
"math"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
)
// expandPlan finalizes type checking of placeholders and expands
// the query plan to its final form, including index selection and
// expansion of sub-queries. Returns an error if the initialization
// fails.
func (p *planner) expandPlan(ctx context.Context, plan planNode) (planNode, error) {
var err error
plan, err = doExpandPlan(ctx, p, noParams, plan)
if err != nil {
return plan, err
}
plan = simplifyOrderings(plan, nil)
return plan, nil
}
// expandParameters propagates the known row limit and desired ordering at
// a given level to the levels under it (upstream).
type expandParameters struct {
numRowsHint int64
desiredOrdering sqlbase.ColumnOrdering
}
var noParams = expandParameters{numRowsHint: math.MaxInt64, desiredOrdering: nil}
// doExpandPlan is the algorithm that supports expandPlan().
func doExpandPlan(
ctx context.Context, p *planner, params expandParameters, plan planNode,
) (planNode, error) {
var err error
switch n := plan.(type) {
case *createTableNode:
n.sourcePlan, err = doExpandPlan(ctx, p, noParams, n.sourcePlan)
case *updateNode:
n.run.rows, err = doExpandPlan(ctx, p, noParams, n.run.rows)
case *insertNode:
n.run.rows, err = doExpandPlan(ctx, p, noParams, n.run.rows)
case *deleteNode:
n.run.rows, err = doExpandPlan(ctx, p, noParams, n.run.rows)
case *explainDistSQLNode:
n.plan, err = doExpandPlan(ctx, p, noParams, n.plan)
if err != nil {
return plan, err
}
case *traceNode:
n.plan, err = doExpandPlan(ctx, p, noParams, n.plan)
if err != nil {
return plan, err
}
case *explainPlanNode:
if n.expanded {
n.plan, err = doExpandPlan(ctx, p, noParams, n.plan)
if err != nil {
return plan, err
}
// Trigger limit hint propagation, which would otherwise only occur
// during the plan's Start() phase. This may trigger additional
// optimizations (eg. in sortNode) which the user of EXPLAIN will be
// interested in.
setUnlimited(n.plan)
}
case *indexJoinNode:
// We ignore the return value because we know the scanNode is preserved.
_, err = doExpandPlan(ctx, p, params, n.index)
if err != nil {
return plan, err
}
// The row limit and desired ordering, if any, only propagates on
// the index side.
_, err = doExpandPlan(ctx, p, noParams, n.table)
case *unionNode:
n.right, err = doExpandPlan(ctx, p, params, n.right)
if err != nil {
return plan, err
}
n.left, err = doExpandPlan(ctx, p, params, n.left)
case *filterNode:
n.source.plan, err = doExpandPlan(ctx, p, params, n.source.plan)
case *joinNode:
n.left.plan, err = doExpandPlan(ctx, p, noParams, n.left.plan)
if err != nil {
return plan, err
}
n.right.plan, err = doExpandPlan(ctx, p, noParams, n.right.plan)
if err != nil {
return plan, err
}
n.mergeJoinOrdering = computeMergeJoinOrdering(
planOrdering(n.left.plan),
planOrdering(n.right.plan),
n.pred.leftEqualityIndices,
n.pred.rightEqualityIndices,
)
n.ordering = n.joinOrdering()
case *ordinalityNode:
// There may be too many columns in the required ordering. Filter them.
params.desiredOrdering = n.restrictOrdering(params.desiredOrdering)
n.source, err = doExpandPlan(ctx, p, params, n.source)
if err != nil {
return plan, err
}
// The source ordering may have been updated. Update the
// ordinality ordering accordingly.
n.optimizeOrdering()
case *limitNode:
// Estimate the limit parameters. We can't full eval them just yet,
// because evaluation requires running potential sub-queries, which
// cannot occur during expand.
n.estimateLimit()
params.numRowsHint = getLimit(n.count, n.offset)
n.plan, err = doExpandPlan(ctx, p, params, n.plan)
case *groupNode:
params.desiredOrdering = n.desiredOrdering
// Under a group node, there may be arbitrarily more rows
// than those required by the context.
params.numRowsHint = math.MaxInt64
n.plan, err = doExpandPlan(ctx, p, params, n.plan)
if len(n.desiredOrdering) > 0 {
match := planOrdering(n.plan).computeMatch(n.desiredOrdering)
if match == len(n.desiredOrdering) {
// We have a single MIN/MAX function and the underlying plan's
// ordering matches the function. We only need to retrieve one row.
// See desiredAggregateOrdering.
n.needOnlyOneRow = true
}
}
case *windowNode:
n.plan, err = doExpandPlan(ctx, p, noParams, n.plan)
case *sortNode:
if !n.ordering.IsPrefixOf(params.desiredOrdering) {
params.desiredOrdering = n.ordering
}
n.plan, err = doExpandPlan(ctx, p, params, n.plan)
if err != nil {
return plan, err
}
if s, ok := n.plan.(*sortNode); ok {
// (... ORDER BY x) ORDER BY y -> keep the outer sort
elideDoubleSort(n, s)
}
// Check to see if the requested ordering is compatible with the existing
// ordering.
match := planOrdering(n.plan).computeMatch(n.ordering)
n.needSort = (match < len(n.ordering))
case *distinctNode:
// TODO(radu/knz): perhaps we can propagate the DISTINCT
// clause as desired ordering for the source node.
n.plan, err = doExpandPlan(ctx, p, params, n.plan)
if err != nil {
return plan, err
}
ordering := planOrdering(n.plan)
if !ordering.isEmpty() {
n.columnsInOrder = make([]bool, len(planColumns(n.plan)))
ordering.constantCols.ForEach(func(colIdx uint32) {
n.columnsInOrder[colIdx] = true
})
for _, g := range ordering.ordering {
for col, ok := g.cols.Next(0); ok; col, ok = g.cols.Next(col + 1) {
n.columnsInOrder[col] = true
}
}
}
case *scanNode:
plan, err = expandScanNode(ctx, p, params, n)
case *renderNode:
plan, err = expandRenderNode(ctx, p, params, n)
case *delayedNode:
var newPlan planNode
newPlan, err = n.constructor(ctx, p)
if err != nil {
return plan, err
}
newPlan, err = doExpandPlan(ctx, p, params, newPlan)
if err != nil {
return plan, err
}
plan = newPlan
case *splitNode:
n.rows, err = doExpandPlan(ctx, p, noParams, n.rows)
case *testingRelocateNode:
n.rows, err = doExpandPlan(ctx, p, noParams, n.rows)
case *valuesNode:
case *alterTableNode:
case *cancelQueryNode:
case *controlJobNode:
case *copyNode:
case *createDatabaseNode:
case *createIndexNode:
case *createUserNode:
case *createViewNode:
case *dropDatabaseNode:
case *dropIndexNode:
case *dropTableNode:
case *dropViewNode:
case *dropUserNode:
case *zeroNode:
case *unaryNode:
case *hookFnNode:
case *valueGenerator:
case *setNode:
case *setClusterSettingNode:
case *showRangesNode:
case *showFingerprintsNode:
case *scatterNode:
case nil:
default:
panic(fmt.Sprintf("unhandled node type: %T", plan))
}
return plan, err
}
// elideDoubleSort removes the source sortNode because it is
// redundant.
func elideDoubleSort(parent, source *sortNode) {
parent.plan = source.plan
// Propagate renamed columns
mutSourceCols := planMutableColumns(parent.plan)
for i, col := range parent.columns {
mutSourceCols[i].Name = col.Name
}
}
func expandScanNode(
ctx context.Context, p *planner, params expandParameters, s *scanNode,
) (planNode, error) {
var analyzeOrdering analyzeOrderingFn
if len(params.desiredOrdering) > 0 {
analyzeOrdering = func(indexOrdering orderingInfo) (matchingCols, totalCols int) {
match := indexOrdering.computeMatch(params.desiredOrdering)
return match, len(params.desiredOrdering)
}
}
// If we have a reasonable limit, prefer an order matching index even if
// it is not covering.
var preferOrderMatchingIndex bool
if len(params.desiredOrdering) > 0 && params.numRowsHint <= 1000 {
preferOrderMatchingIndex = true
}
plan, err := p.selectIndex(ctx, s, analyzeOrdering, preferOrderMatchingIndex)
if err != nil {
return s, err
}
return plan, nil
}
func expandRenderNode(
ctx context.Context, p *planner, params expandParameters, r *renderNode,
) (planNode, error) {
params.desiredOrdering = translateOrdering(params.desiredOrdering, r)
var err error
r.source.plan, err = doExpandPlan(ctx, p, params, r.source.plan)
if err != nil {
return r, err
}
// Elide the render node if it renders its source as-is.
sourceCols := planColumns(r.source.plan)
if len(r.columns) == len(sourceCols) {
// We don't drop renderNodes which have a different number of
// columns than their sources, because some nodes currently assume
// the number of source columns doesn't change between
// instantiation and Start() (e.g. groupNode).
// TODO(knz): investigate this further and enable the optimization fully.
needRename := false
foundNonTrivialRender := false
for i, e := range r.render {
if r.columns[i].Omitted {
continue
}
if iv, ok := e.(*parser.IndexedVar); ok && i < len(sourceCols) && iv.Idx == i {
if sourceCols[i].Name != r.columns[i].Name {
// Pass-through with rename: SELECT k AS x, v AS y FROM kv ...
// We'll want to push the demanded names "x" and "y" to the
// source.
needRename = true
}
continue
}
foundNonTrivialRender = true
break
}
if !foundNonTrivialRender {
// Nothing special rendered, remove the render node entirely.
if needRename {
// If the render was renaming some columns, propagate the
// requested names.
mutSourceCols := planMutableColumns(r.source.plan)
for i, col := range r.columns {
mutSourceCols[i].Name = col.Name
}
}
return r.source.plan, nil
}
}
r.computeOrdering(planOrdering(r.source.plan))
return r, nil
}
// translateOrdering modifies a desired ordering on the output of the
// renderNode to a desired ordering on its its input.
//
// For example, it translates a desired ordering [@2 asc, @1 desc] for
// a render node that renders [@4, @3, @2] into a desired ordering [@3
// asc, @4 desc].
func translateOrdering(desiredDown sqlbase.ColumnOrdering, r *renderNode) sqlbase.ColumnOrdering {
var desiredUp sqlbase.ColumnOrdering
for _, colOrder := range desiredDown {
rendered := r.render[colOrder.ColIdx]
if _, ok := rendered.(parser.Datum); ok {
// Simple constants do not participate in ordering. Just ignore.
continue
}
if v, ok := rendered.(*parser.IndexedVar); ok {
// This is a simple render, so we can propagate the desired ordering.
// However take care of avoiding duplicate ordering requests in
// case there is more than one render for the same source column.
duplicate := false
for _, desiredOrderCol := range desiredUp {
if desiredOrderCol.ColIdx == v.Idx {
duplicate = true
break
}
}
if !duplicate {
desiredUp = append(desiredUp,
sqlbase.ColumnOrderInfo{ColIdx: v.Idx, Direction: colOrder.Direction})
}
continue
}
// Anything else and we can't propagate the desired order.
break
}
return desiredUp
}
// simplifyOrderings reduces the Ordering() guarantee of each node in the plan
// to that which is actually used by the parent(s). It also performs sortNode
// elision when possible.
//
// Simplification of orderings is useful for DistSQL, where maintaining
// orderings between parallel streams is not free.
//
// This determination cannot be done directly as part of the doExpandPlan
// recursion (using desiredOrdering) because some nodes (distinctNode) make use
// of whatever ordering the underlying node happens to provide.
func simplifyOrderings(plan planNode, usefulOrdering sqlbase.ColumnOrdering) planNode {
if plan == nil {
return nil
}
switch n := plan.(type) {
case *createTableNode:
n.sourcePlan = simplifyOrderings(n.sourcePlan, nil)
case *updateNode:
n.run.rows = simplifyOrderings(n.run.rows, nil)
case *insertNode:
n.run.rows = simplifyOrderings(n.run.rows, nil)
case *deleteNode:
n.run.rows = simplifyOrderings(n.run.rows, nil)
case *explainDistSQLNode:
n.plan = simplifyOrderings(n.plan, nil)
case *traceNode:
n.plan = simplifyOrderings(n.plan, nil)
case *explainPlanNode:
if n.expanded {
n.plan = simplifyOrderings(n.plan, nil)
}
case *indexJoinNode:
n.index.ordering.trim(usefulOrdering)
n.table.ordering = orderingInfo{}
case *unionNode:
n.right = simplifyOrderings(n.right, nil)
n.left = simplifyOrderings(n.left, nil)
case *filterNode:
n.source.plan = simplifyOrderings(n.source.plan, usefulOrdering)
case *joinNode:
// In DistSQL, we may take advantage of matching orderings on equality
// columns and use merge joins. Preserve the orderings in that case.
var usefulLeft, usefulRight sqlbase.ColumnOrdering
if len(n.mergeJoinOrdering) > 0 {
usefulLeft = make(sqlbase.ColumnOrdering, len(n.mergeJoinOrdering))
usefulRight = make(sqlbase.ColumnOrdering, len(n.mergeJoinOrdering))
for i, mergedCol := range n.mergeJoinOrdering {
usefulLeft[i].ColIdx = n.pred.leftEqualityIndices[mergedCol.ColIdx]
usefulRight[i].ColIdx = n.pred.rightEqualityIndices[mergedCol.ColIdx]
usefulLeft[i].Direction = mergedCol.Direction
usefulRight[i].Direction = mergedCol.Direction
}
}
n.ordering.trim(usefulOrdering)
n.left.plan = simplifyOrderings(n.left.plan, usefulLeft)
n.right.plan = simplifyOrderings(n.right.plan, usefulRight)
case *ordinalityNode:
n.ordering.trim(usefulOrdering)
n.source = simplifyOrderings(n.source, n.restrictOrdering(usefulOrdering))
case *limitNode:
n.plan = simplifyOrderings(n.plan, usefulOrdering)
case *groupNode:
if n.needOnlyOneRow {
n.plan = simplifyOrderings(n.plan, n.desiredOrdering)
} else {
n.plan = simplifyOrderings(n.plan, nil)
}
case *windowNode:
n.plan = simplifyOrderings(n.plan, nil)
case *sortNode:
if n.needSort {
// We could pass no ordering below, but a partial ordering can speed up
// the sort (and save memory), at least for DistSQL.
n.plan = simplifyOrderings(n.plan, n.ordering)
} else {
constantCols := planOrdering(n.plan).constantCols
// Normally we would pass n.ordering; but n.ordering could be a prefix of
// the useful ordering. Check for this, ignoring any constant columns.
sortOrder := make(sqlbase.ColumnOrdering, 0, len(n.ordering))
for _, c := range n.ordering {
if !constantCols.Contains(uint32(c.ColIdx)) {
sortOrder = append(sortOrder, c)
}
}
givenOrder := make(sqlbase.ColumnOrdering, 0, len(usefulOrdering))
for _, c := range usefulOrdering {
if !constantCols.Contains(uint32(c.ColIdx)) {
givenOrder = append(givenOrder, c)
}
}
if sortOrder.IsPrefixOf(givenOrder) {
n.plan = simplifyOrderings(n.plan, givenOrder)
} else {
n.plan = simplifyOrderings(n.plan, sortOrder)
}
}
if !n.needSort {
if len(n.columns) < len(planColumns(n.plan)) {
// No sorting required, but we have to strip off the extra render
// expressions we added. So keep the sort node.
// TODO(radu): replace with a renderNode
} else {
// Sort node fully disappears.
// Just be sure to propagate the column names.
mutSourceCols := planMutableColumns(n.plan)
for i, col := range n.columns {
mutSourceCols[i].Name = col.Name
}
plan = n.plan
}
}
case *distinctNode:
// distinctNode uses whatever order the underlying node presents (regardless
// of any ordering requirement on distinctNode itself).
sourceOrdering := planOrdering(n.plan)
n.plan = simplifyOrderings(n.plan, sourceOrdering.getColumnOrdering())
case *scanNode:
n.ordering.trim(usefulOrdering)
case *renderNode:
n.source.plan = simplifyOrderings(n.source.plan, translateOrdering(usefulOrdering, n))
// Recompute r.ordering using the source's simplified ordering.
// TODO(radu): in some cases there may be multiple possible n.orderings for
// a given source plan ordering; we should pass usefulOrdering to help make
// that choice (#13709).
n.computeOrdering(planOrdering(n.source.plan))
case *delayedNode:
n.plan = simplifyOrderings(n.plan, usefulOrdering)
case *splitNode:
n.rows = simplifyOrderings(n.rows, nil)
case *testingRelocateNode:
n.rows = simplifyOrderings(n.rows, nil)
case *valuesNode:
case *alterTableNode:
case *cancelQueryNode:
case *controlJobNode:
case *copyNode:
case *createDatabaseNode:
case *createIndexNode:
case *createUserNode:
case *createViewNode:
case *dropDatabaseNode:
case *dropIndexNode:
case *dropTableNode:
case *dropViewNode:
case *dropUserNode:
case *zeroNode:
case *unaryNode:
case *hookFnNode:
case *valueGenerator:
case *setNode:
case *setClusterSettingNode:
case *showRangesNode:
case *showFingerprintsNode:
case *scatterNode:
default:
panic(fmt.Sprintf("unhandled node type: %T", plan))
}
return plan
}