-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
mutation.go
1336 lines (1225 loc) · 45.7 KB
/
mutation.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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the CockroachDB Software License
// included in the /LICENSE file.
package execbuilder
import (
"bytes"
"fmt"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/lexbase"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
"github.com/cockroachdb/cockroach/pkg/sql/opt/exec"
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/row"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util/intsets"
"github.com/cockroachdb/errors"
)
func (b *Builder) buildMutationInput(
mutExpr, inputExpr memo.RelExpr, colList opt.ColList, p *memo.MutationPrivate,
) (_ execPlan, outputCols colOrdMap, err error) {
toLock, err := b.shouldApplyImplicitLockingToMutationInput(mutExpr)
if err != nil {
return execPlan{}, colOrdMap{}, err
}
if toLock != 0 {
if b.forceForUpdateLocking.Contains(int(toLock)) {
return execPlan{}, colOrdMap{}, errors.AssertionFailedf(
"unexpectedly found table %v in forceForUpdateLocking set", toLock,
)
}
b.forceForUpdateLocking.Add(int(toLock))
defer func() {
b.forceForUpdateLocking.Remove(int(toLock))
}()
}
input, inputCols, err := b.buildRelational(inputExpr)
if err != nil {
return execPlan{}, colOrdMap{}, err
}
// TODO(mgartner/radu): This can incorrectly append columns in a FK cascade
// update that are never used during execution. See issue #57097.
if p.WithID != 0 {
// The input might have extra columns that are used only by FK or unique
// checks; make sure we don't project them away.
cols := inputExpr.Relational().OutputCols.Copy()
for _, c := range colList {
cols.Remove(c)
}
for c, ok := cols.Next(0); ok; c, ok = cols.Next(c + 1) {
colList = append(colList, c)
}
}
input, inputCols, err = b.ensureColumns(
input, inputCols, inputExpr, colList,
inputExpr.ProvidedPhysical().Ordering, true, /* reuseInputCols */
)
if err != nil {
return execPlan{}, colOrdMap{}, err
}
if p.WithID != 0 {
label := fmt.Sprintf("buffer %d", p.WithID)
bufferNode, err := b.factory.ConstructBuffer(input.root, label)
if err != nil {
return execPlan{}, colOrdMap{}, err
}
b.addBuiltWithExpr(p.WithID, inputCols, bufferNode)
input.root = bufferNode
}
return input, inputCols, nil
}
func (b *Builder) buildInsert(ins *memo.InsertExpr) (_ execPlan, outputCols colOrdMap, err error) {
if ep, cols, ok, err := b.tryBuildFastPathInsert(ins); err != nil || ok {
return ep, cols, err
}
// Construct list of columns that only contains columns that need to be
// inserted (e.g. delete-only mutation columns don't need to be inserted).
colList := make(opt.ColList, 0, len(ins.InsertCols)+len(ins.CheckCols)+len(ins.PartialIndexPutCols))
colList = appendColsWhenPresent(colList, ins.InsertCols)
colList = appendColsWhenPresent(colList, ins.CheckCols)
colList = appendColsWhenPresent(colList, ins.PartialIndexPutCols)
input, _, err := b.buildMutationInput(ins, ins.Input, colList, &ins.MutationPrivate)
if err != nil {
return execPlan{}, colOrdMap{}, err
}
// Construct the Insert node.
tab := b.mem.Metadata().Table(ins.Table)
insertOrds := ordinalSetFromColList(ins.InsertCols)
checkOrds := ordinalSetFromColList(ins.CheckCols)
returnOrds := ordinalSetFromColList(ins.ReturnCols)
node, err := b.factory.ConstructInsert(
input.root,
tab,
ins.ArbiterIndexes,
ins.ArbiterConstraints,
insertOrds,
returnOrds,
checkOrds,
ins.UniqueWithTombstoneIndexes,
b.allowAutoCommit && len(ins.UniqueChecks) == 0 &&
len(ins.FKChecks) == 0 && len(ins.FKCascades) == 0 && ins.AfterTriggers == nil,
)
if err != nil {
return execPlan{}, colOrdMap{}, err
}
// Construct the output column map.
ep := execPlan{root: node}
if ins.NeedResults() {
outputCols = b.mutationOutputColMap(ins)
}
if err := b.buildUniqueChecks(ins.UniqueChecks); err != nil {
return execPlan{}, colOrdMap{}, err
}
if err := b.buildFKChecks(ins.FKChecks); err != nil {
return execPlan{}, colOrdMap{}, err
}
if err := b.buildAfterTriggers(ins.WithID, ins.AfterTriggers); err != nil {
return execPlan{}, colOrdMap{}, err
}
return ep, outputCols, nil
}
// tryBuildFastPathInsert attempts to construct an insert using the fast path,
// checking all required conditions. See exec.Factory.ConstructInsertFastPath.
func (b *Builder) tryBuildFastPathInsert(
ins *memo.InsertExpr,
) (_ execPlan, outputCols colOrdMap, ok bool, _ error) {
// Conditions from ConstructFastPathInsert:
//
// - there are no other mutations in the statement, and the output of the
// insert is not processed through side-effecting expressions (i.e. we can
// auto-commit);
//
// This condition was taken into account in build().
if !b.allowInsertFastPath {
return execPlan{}, colOrdMap{}, false, nil
}
// If there are unique checks required, there must be the same number of fast
// path unique checks.
if len(ins.UniqueChecks) != len(ins.FastPathUniqueChecks) {
return execPlan{}, colOrdMap{}, false, nil
}
// Do not attempt the fast path if there are any triggers.
if ins.AfterTriggers != nil {
return execPlan{}, colOrdMap{}, false, nil
}
insInput := ins.Input
values, ok := insInput.(*memo.ValuesExpr)
// Values expressions containing subqueries or UDFs, or having a size larger
// than the max mutation batch size are disallowed.
if !ok || !memo.ValuesLegalForInsertFastPath(values) {
return execPlan{}, colOrdMap{}, false, nil
}
md := b.mem.Metadata()
tab := md.Table(ins.Table)
uniqChecks := make([]exec.InsertFastPathCheck, len(ins.UniqueChecks))
for i := range ins.FastPathUniqueChecks {
c := &ins.FastPathUniqueChecks[i]
if len(c.DatumsFromConstraint) == 0 {
// We need at least one DatumsFromConstraint in order to perform
// uniqueness checks during fast-path insert. Even if DatumsFromConstraint
// contains no Datums, that case indicates that all values to check come
// from the input row.
return execPlan{}, colOrdMap{}, false, nil
}
execFastPathCheck := &uniqChecks[i]
// Set up the execbuilder structure from the elements built during
// exploration.
execFastPathCheck.ReferencedTable = md.Table(c.ReferencedTableID)
execFastPathCheck.ReferencedIndex = execFastPathCheck.ReferencedTable.Index(c.ReferencedIndexOrdinal)
execFastPathCheck.CheckOrdinal = c.CheckOrdinal
// If there is a unique index with implicit partitioning columns, the fast
// path can write tombstones to lock the row in all partitions.
locking, err := b.buildLocking(ins.Table, c.Locking)
if err != nil {
return execPlan{}, colOrdMap{}, false, err
}
execFastPathCheck.Locking = locking
execFastPathCheck.InsertCols = make([]exec.TableColumnOrdinal, len(c.InsertCols))
for j, insertCol := range c.InsertCols {
execFastPathCheck.InsertCols[j] = exec.TableColumnOrdinal(md.ColumnMeta(insertCol).Table.ColumnOrdinal(insertCol))
}
datumsFromConstraintSpec := c.DatumsFromConstraint
execFastPathCheck.DatumsFromConstraint = make([]tree.Datums, len(datumsFromConstraintSpec))
for j, row := range datumsFromConstraintSpec {
execFastPathCheck.DatumsFromConstraint[j] = make(tree.Datums, tab.ColumnCount())
tuple := row.(*memo.TupleExpr)
if len(c.InsertCols) != len(tuple.Elems) {
panic(errors.AssertionFailedf("expected %d tuple elements in insert fast path uniqueness check, found %d", len(c.InsertCols), len(tuple.Elems)))
}
for k := 0; k < len(tuple.Elems); k++ {
var constDatum tree.Datum
switch e := tuple.Elems[k].(type) {
case *memo.ConstExpr:
constDatum = e.Value
case *memo.TrueExpr:
constDatum = tree.DBoolTrue
case *memo.FalseExpr:
constDatum = tree.DBoolFalse
default:
return execPlan{}, colOrdMap{}, false, nil
}
execFastPathCheck.DatumsFromConstraint[j][execFastPathCheck.InsertCols[k]] = constDatum
}
}
uniqCheck := &ins.UniqueChecks[i]
// TODO(mgartner): We shouldn't keep references to md, uniqCheck, and
// execFastPathCheck. Ideally, the query plan for the constraint would
// produce the constraint column names as output columns. Then, the
// error message could be constructed without needing to access the
// catalog or metadata.
execFastPathCheck.MkErr = func(values tree.Datums) error {
return mkFastPathUniqueCheckErr(md, uniqCheck, values, execFastPathCheck.ReferencedIndex)
}
}
// - there are no self-referencing foreign keys;
// - all FK checks can be performed using direct lookups into unique indexes.
fkChecks := make([]exec.InsertFastPathCheck, len(ins.FKChecks))
for i := range ins.FKChecks {
c := &ins.FKChecks[i]
if md.Table(c.ReferencedTable).ID() == md.Table(ins.Table).ID() {
// Self-referencing FK.
return execPlan{}, colOrdMap{}, false, nil
}
fk := tab.OutboundForeignKey(c.FKOrdinal)
lookupJoin, isLookupJoin := c.Check.(*memo.LookupJoinExpr)
if !isLookupJoin || lookupJoin.JoinType != opt.AntiJoinOp {
// Not a lookup anti-join.
return execPlan{}, colOrdMap{}, false, nil
}
// TODO(rytaft): see if we can remove the requirement that LookupExpr is
// empty.
if len(lookupJoin.On) > 0 || len(lookupJoin.LookupExpr) > 0 ||
len(lookupJoin.KeyCols) != fk.ColumnCount() {
return execPlan{}, colOrdMap{}, false, nil
}
inputExpr := lookupJoin.Input
// Ignore any select (used to deal with NULLs).
if sel, isSelect := inputExpr.(*memo.SelectExpr); isSelect {
inputExpr = sel.Input
}
withScan, isWithScan := inputExpr.(*memo.WithScanExpr)
if !isWithScan {
return execPlan{}, colOrdMap{}, false, nil
}
if withScan.With != ins.WithID {
return execPlan{}, colOrdMap{}, false, nil
}
locking, err := b.buildLocking(lookupJoin.Table, lookupJoin.Locking)
if err != nil {
return execPlan{}, colOrdMap{}, false, err
}
out := &fkChecks[i]
out.InsertCols = make([]exec.TableColumnOrdinal, len(lookupJoin.KeyCols))
for j, keyCol := range lookupJoin.KeyCols {
// The keyCol comes from the WithScan operator. We must find the matching
// column in the mutation input.
var withColOrd, inputColOrd int
withColOrd, ok = withScan.OutCols.Find(keyCol)
if !ok {
return execPlan{}, colOrdMap{}, false, errors.AssertionFailedf("cannot find column %d", keyCol)
}
inputCol := withScan.InCols[withColOrd]
inputColOrd, ok = ins.InsertCols.Find(inputCol)
if !ok {
return execPlan{}, colOrdMap{}, false, errors.AssertionFailedf("cannot find column %d", inputCol)
}
out.InsertCols[j] = exec.TableColumnOrdinal(inputColOrd)
}
out.ReferencedTable = md.Table(lookupJoin.Table)
out.ReferencedIndex = out.ReferencedTable.Index(lookupJoin.Index)
out.MatchMethod = fk.MatchMethod()
out.Locking = locking
out.MkErr = func(values tree.Datums) error {
if len(values) != len(out.InsertCols) {
return errors.AssertionFailedf("invalid FK violation values")
}
// This is a little tricky. The column ordering might not match between
// the FK reference and the index we're looking up. We have to reshuffle
// the values to fix that.
fkVals := make(tree.Datums, len(values))
for i := range fkVals {
parentOrd := fk.ReferencedColumnOrdinal(out.ReferencedTable, i)
for j := 0; j < out.ReferencedIndex.KeyColumnCount(); j++ {
if out.ReferencedIndex.Column(j).Ordinal() == parentOrd {
fkVals[i] = values[j]
break
}
}
if fkVals[i] == nil {
return errors.AssertionFailedf("invalid column mapping")
}
}
return mkFKCheckErr(md, c, fkVals)
}
}
colList := make(opt.ColList, 0, len(ins.InsertCols)+len(ins.CheckCols)+len(ins.PartialIndexPutCols))
colList = appendColsWhenPresent(colList, ins.InsertCols)
colList = appendColsWhenPresent(colList, ins.CheckCols)
colList = appendColsWhenPresent(colList, ins.PartialIndexPutCols)
rows, err := b.buildValuesRows(values)
if err != nil {
return execPlan{}, colOrdMap{}, false, err
}
// We may need to rearrange the columns.
rows, err = rearrangeColumns(values.Cols, rows, colList)
if err != nil {
return execPlan{}, colOrdMap{}, false, err
}
// Construct the InsertFastPath node.
insertOrds := ordinalSetFromColList(ins.InsertCols)
checkOrds := ordinalSetFromColList(ins.CheckCols)
returnOrds := ordinalSetFromColList(ins.ReturnCols)
node, err := b.factory.ConstructInsertFastPath(
rows,
tab,
insertOrds,
returnOrds,
checkOrds,
fkChecks,
uniqChecks,
ins.UniqueWithTombstoneIndexes,
b.allowAutoCommit,
)
if err != nil {
return execPlan{}, colOrdMap{}, false, err
}
// Construct the output column map.
ep := execPlan{root: node}
if ins.NeedResults() {
outputCols = b.mutationOutputColMap(ins)
}
return ep, outputCols, true, nil
}
// rearrangeColumns rearranges the columns in a matrix of TypedExpr values.
//
// Each column in inRows corresponds to a column in inCols. The values in the
// columns are rearranged so that they correspond to wantedCols. Note that
// wantedCols can contain the same column multiple times, in which case the
// values will be duplicated.
//
// Returns an error if wantedCols contains a column that isn't part of inCols.
func rearrangeColumns(
inCols opt.ColList, inRows [][]tree.TypedExpr, wantedCols opt.ColList,
) (outRows [][]tree.TypedExpr, _ error) {
if inCols.Equals(wantedCols) {
// Nothing to do.
return inRows, nil
}
outRows = makeTypedExprMatrix(len(inRows), len(wantedCols))
for i, wanted := range wantedCols {
j, ok := inCols.Find(wanted)
if !ok {
return nil, errors.AssertionFailedf("no column %d in input", wanted)
}
for rowIdx := range inRows {
outRows[rowIdx][i] = inRows[rowIdx][j]
}
}
return outRows, nil
}
func (b *Builder) buildUpdate(upd *memo.UpdateExpr) (_ execPlan, outputCols colOrdMap, err error) {
// Currently, the execution engine requires one input column for each fetch
// and update expression, so use ensureColumns to map and reorder columns so
// that they correspond to target table columns. For example:
//
// UPDATE xyz SET x=1, y=1
//
// Here, the input has just one column (because the constant is shared), and
// so must be mapped to two separate update columns.
//
// TODO(andyk): Using ensureColumns here can result in an extra Render.
// Upgrade execution engine to not require this.
cnt := len(upd.FetchCols) + len(upd.UpdateCols) + len(upd.PassthroughCols) +
len(upd.CheckCols) + len(upd.PartialIndexPutCols) + len(upd.PartialIndexDelCols)
colList := make(opt.ColList, 0, cnt)
colList = appendColsWhenPresent(colList, upd.FetchCols)
colList = appendColsWhenPresent(colList, upd.UpdateCols)
// The RETURNING clause of the Update can refer to the columns
// in any of the FROM tables. As a result, the Update may need
// to passthrough those columns so the projection above can use
// them.
if upd.NeedResults() {
colList = append(colList, upd.PassthroughCols...)
}
colList = appendColsWhenPresent(colList, upd.CheckCols)
colList = appendColsWhenPresent(colList, upd.PartialIndexPutCols)
colList = appendColsWhenPresent(colList, upd.PartialIndexDelCols)
input, _, err := b.buildMutationInput(upd, upd.Input, colList, &upd.MutationPrivate)
if err != nil {
return execPlan{}, colOrdMap{}, err
}
// Construct the Update node.
md := b.mem.Metadata()
tab := md.Table(upd.Table)
fetchColOrds := ordinalSetFromColList(upd.FetchCols)
updateColOrds := ordinalSetFromColList(upd.UpdateCols)
returnColOrds := ordinalSetFromColList(upd.ReturnCols)
checkOrds := ordinalSetFromColList(upd.CheckCols)
// Construct the result columns for the passthrough set.
var passthroughCols colinfo.ResultColumns
if upd.NeedResults() {
for _, passthroughCol := range upd.PassthroughCols {
colMeta := b.mem.Metadata().ColumnMeta(passthroughCol)
passthroughCols = append(passthroughCols, colinfo.ResultColumn{Name: colMeta.Alias, Typ: colMeta.Type})
}
}
node, err := b.factory.ConstructUpdate(
input.root,
tab,
fetchColOrds,
updateColOrds,
returnColOrds,
checkOrds,
passthroughCols,
upd.UniqueWithTombstoneIndexes,
b.allowAutoCommit && len(upd.UniqueChecks) == 0 &&
len(upd.FKChecks) == 0 && len(upd.FKCascades) == 0 && upd.AfterTriggers == nil,
)
if err != nil {
return execPlan{}, colOrdMap{}, err
}
if err := b.buildUniqueChecks(upd.UniqueChecks); err != nil {
return execPlan{}, colOrdMap{}, err
}
if err := b.buildFKChecks(upd.FKChecks); err != nil {
return execPlan{}, colOrdMap{}, err
}
if err := b.buildFKCascades(upd.WithID, upd.FKCascades); err != nil {
return execPlan{}, colOrdMap{}, err
}
if err := b.buildAfterTriggers(upd.WithID, upd.AfterTriggers); err != nil {
return execPlan{}, colOrdMap{}, err
}
// Construct the output column map.
ep := execPlan{root: node}
if upd.NeedResults() {
outputCols = b.mutationOutputColMap(upd)
}
return ep, outputCols, nil
}
func (b *Builder) buildUpsert(ups *memo.UpsertExpr) (_ execPlan, outputCols colOrdMap, err error) {
// Currently, the execution engine requires one input column for each insert,
// fetch, and update expression, so use ensureColumns to map and reorder
// columns so that they correspond to target table columns. For example:
//
// INSERT INTO xyz (x, y) VALUES (1, 1)
// ON CONFLICT (x) DO UPDATE SET x=2, y=2
//
// Here, both insert values and update values come from the same input column
// (because the constants are shared), and so must be mapped to separate
// output columns.
//
// If CanaryCol = 0, then this is the "blind upsert" case, which uses a KV
// "Put" to insert new rows or blindly overwrite existing rows. Existing rows
// do not need to be fetched or separately updated (i.e. ups.FetchCols and
// ups.UpdateCols are both empty).
//
// TODO(andyk): Using ensureColumns here can result in an extra Render.
// Upgrade execution engine to not require this.
cnt := len(ups.InsertCols) + len(ups.FetchCols) + len(ups.UpdateCols) + len(ups.CheckCols) +
len(ups.PartialIndexPutCols) + len(ups.PartialIndexDelCols) + 1
colList := make(opt.ColList, 0, cnt)
colList = appendColsWhenPresent(colList, ups.InsertCols)
colList = appendColsWhenPresent(colList, ups.FetchCols)
colList = appendColsWhenPresent(colList, ups.UpdateCols)
if ups.CanaryCol != 0 {
colList = append(colList, ups.CanaryCol)
}
colList = appendColsWhenPresent(colList, ups.CheckCols)
colList = appendColsWhenPresent(colList, ups.PartialIndexPutCols)
colList = appendColsWhenPresent(colList, ups.PartialIndexDelCols)
input, inputCols, err := b.buildMutationInput(ups, ups.Input, colList, &ups.MutationPrivate)
if err != nil {
return execPlan{}, colOrdMap{}, err
}
// Construct the Upsert node.
md := b.mem.Metadata()
tab := md.Table(ups.Table)
canaryCol := exec.NodeColumnOrdinal(-1)
if ups.CanaryCol != 0 {
canaryCol, err = getNodeColumnOrdinal(inputCols, ups.CanaryCol)
if err != nil {
return execPlan{}, colOrdMap{}, err
}
}
insertColOrds := ordinalSetFromColList(ups.InsertCols)
fetchColOrds := ordinalSetFromColList(ups.FetchCols)
updateColOrds := ordinalSetFromColList(ups.UpdateCols)
returnColOrds := ordinalSetFromColList(ups.ReturnCols)
checkOrds := ordinalSetFromColList(ups.CheckCols)
node, err := b.factory.ConstructUpsert(
input.root,
tab,
ups.ArbiterIndexes,
ups.ArbiterConstraints,
canaryCol,
insertColOrds,
fetchColOrds,
updateColOrds,
returnColOrds,
checkOrds,
ups.UniqueWithTombstoneIndexes,
b.allowAutoCommit && len(ups.UniqueChecks) == 0 &&
len(ups.FKChecks) == 0 && len(ups.FKCascades) == 0 && ups.AfterTriggers == nil,
)
if err != nil {
return execPlan{}, colOrdMap{}, err
}
if err := b.buildUniqueChecks(ups.UniqueChecks); err != nil {
return execPlan{}, colOrdMap{}, err
}
if err := b.buildFKChecks(ups.FKChecks); err != nil {
return execPlan{}, colOrdMap{}, err
}
if err := b.buildFKCascades(ups.WithID, ups.FKCascades); err != nil {
return execPlan{}, colOrdMap{}, err
}
if err := b.buildAfterTriggers(ups.WithID, ups.AfterTriggers); err != nil {
return execPlan{}, colOrdMap{}, err
}
// If UPSERT returns rows, they contain all non-mutation columns from the
// table, in the same order they're defined in the table. Each output column
// value is taken from an insert, fetch, or update column, depending on the
// result of the UPSERT operation for that row.
ep := execPlan{root: node}
if ups.NeedResults() {
outputCols = b.mutationOutputColMap(ups)
}
return ep, outputCols, nil
}
func (b *Builder) buildDelete(del *memo.DeleteExpr) (_ execPlan, outputCols colOrdMap, err error) {
// Check for the fast-path delete case that can use a range delete.
if ep, ok, err := b.tryBuildDeleteRange(del); err != nil || ok {
return ep, colOrdMap{}, err
}
// Ensure that order of input columns matches order of target table columns.
//
// TODO(andyk): Using ensureColumns here can result in an extra Render.
// Upgrade execution engine to not require this.
colList := make(opt.ColList, 0, len(del.FetchCols)+len(del.PassthroughCols)+len(del.PartialIndexDelCols))
colList = appendColsWhenPresent(colList, del.FetchCols)
// The RETURNING clause of the Delete can refer to the columns in any of the
// USING tables. As a result, the Update may need to passthrough those
// columns so the projection above can use them.
if del.NeedResults() {
colList = append(colList, del.PassthroughCols...)
}
colList = appendColsWhenPresent(colList, del.PartialIndexDelCols)
input, _, err := b.buildMutationInput(del, del.Input, colList, &del.MutationPrivate)
if err != nil {
return execPlan{}, colOrdMap{}, err
}
// Construct the Delete node.
md := b.mem.Metadata()
tab := md.Table(del.Table)
fetchColOrds := ordinalSetFromColList(del.FetchCols)
returnColOrds := ordinalSetFromColList(del.ReturnCols)
// Construct the result columns for the passthrough set.
var passthroughCols colinfo.ResultColumns
if del.NeedResults() {
passthroughCols = make(colinfo.ResultColumns, 0, len(del.PassthroughCols))
for _, passthroughCol := range del.PassthroughCols {
colMeta := b.mem.Metadata().ColumnMeta(passthroughCol)
passthroughCols = append(passthroughCols, colinfo.ResultColumn{Name: colMeta.Alias, Typ: colMeta.Type})
}
}
node, err := b.factory.ConstructDelete(
input.root,
tab,
fetchColOrds,
returnColOrds,
passthroughCols,
b.allowAutoCommit && len(del.FKChecks) == 0 &&
len(del.FKCascades) == 0 && del.AfterTriggers == nil,
)
if err != nil {
return execPlan{}, colOrdMap{}, err
}
if err := b.buildFKChecks(del.FKChecks); err != nil {
return execPlan{}, colOrdMap{}, err
}
if err := b.buildFKCascades(del.WithID, del.FKCascades); err != nil {
return execPlan{}, colOrdMap{}, err
}
if err := b.buildAfterTriggers(del.WithID, del.AfterTriggers); err != nil {
return execPlan{}, colOrdMap{}, err
}
// Construct the output column map.
ep := execPlan{root: node}
if del.NeedResults() {
outputCols = b.mutationOutputColMap(del)
}
return ep, outputCols, nil
}
// tryBuildDeleteRange attempts to construct a fast DeleteRange execution for a
// logical Delete operator, checking all required conditions. See
// exec.Factory.ConstructDeleteRange.
func (b *Builder) tryBuildDeleteRange(del *memo.DeleteExpr) (_ execPlan, ok bool, _ error) {
// If rows need to be returned from the Delete operator (i.e. RETURNING
// clause), no fast path is possible, because row values must be fetched.
if del.NeedResults() {
return execPlan{}, false, nil
}
// Check for simple Scan input operator without a limit; anything else is not
// supported by a range delete.
if scan, ok := del.Input.(*memo.ScanExpr); !ok || scan.HardLimit != 0 {
return execPlan{}, false, nil
}
tab := b.mem.Metadata().Table(del.Table)
if tab.DeletableIndexCount() > 1 {
// Any secondary index prevents fast path, because separate delete batches
// must be formulated to delete rows from them.
return execPlan{}, false, nil
}
// We can use the fast path if we don't need to buffer the input to the
// delete operator (for foreign key checks/cascades).
if del.WithID != 0 {
return execPlan{}, false, nil
}
ep, err := b.buildDeleteRange(del)
if err != nil {
return execPlan{}, false, err
}
if err := b.buildFKChecks(del.FKChecks); err != nil {
return execPlan{}, false, err
}
if err := b.buildFKCascades(del.WithID, del.FKCascades); err != nil {
return execPlan{}, false, err
}
if err := b.buildAfterTriggers(del.WithID, del.AfterTriggers); err != nil {
return execPlan{}, false, err
}
return ep, true, nil
}
// buildDeleteRange constructs a DeleteRange operator that deletes contiguous
// rows in the primary index; the caller must have already checked the
// conditions which allow use of DeleteRange.
func (b *Builder) buildDeleteRange(del *memo.DeleteExpr) (execPlan, error) {
// tryBuildDeleteRange has already validated that input is a Scan operator.
scan := del.Input.(*memo.ScanExpr)
tab := b.mem.Metadata().Table(scan.Table)
needed, _ := b.getColumns(scan.Cols, scan.Table)
autoCommit := false
if b.allowAutoCommit {
// Permitting autocommit in DeleteRange is very important, because DeleteRange
// is used for simple deletes from primary indexes like
// DELETE FROM t WHERE key = 1000
// When possible, we need to make this a 1pc transaction for performance
// reasons. At the same time, we have to be careful, because DeleteRange
// returns all of the keys that it deleted - so we have to set a limit on the
// DeleteRange request. But, trying to set autocommit and a limit on the
// request doesn't work properly if the limit is hit. So, we permit autocommit
// here if we can guarantee that the number of returned keys is finite and
// relatively small.
//
// Mutations only allow auto-commit if there are no FK checks or cascades.
if maxRows, ok := b.indexConstraintMaxResults(&scan.ScanPrivate, scan.Relational()); ok {
if maxKeys := maxRows * uint64(tab.FamilyCount()); maxKeys <= row.TableTruncateChunkSize {
autoCommit = true
}
}
if len(del.FKChecks) > 0 || len(del.FKCascades) > 0 || del.AfterTriggers != nil {
autoCommit = false
}
}
root, err := b.factory.ConstructDeleteRange(
tab,
needed,
scan.Constraint,
autoCommit,
)
if err != nil {
return execPlan{}, err
}
return execPlan{root: root}, nil
}
// appendColsWhenPresent appends non-zero column IDs from the src list into the
// dst list, and returns the possibly grown list.
func appendColsWhenPresent(dst opt.ColList, src opt.OptionalColList) opt.ColList {
for _, col := range src {
if col != 0 {
dst = append(dst, col)
}
}
return dst
}
// ordinalSetFromColList returns the set of ordinal positions of each non-zero
// column ID in the given list. This is used with mutation operators, which
// maintain lists that correspond to the target table, with zero column IDs
// indicating columns that are not involved in the mutation.
func ordinalSetFromColList(colList opt.OptionalColList) intsets.Fast {
var res intsets.Fast
for i, col := range colList {
if col != 0 {
res.Add(i)
}
}
return res
}
// mutationOutputColMap constructs a ColMap for the execPlan that maps from the
// opt.ColumnID of each output column to the ordinal position of that column in
// the result.
func (b *Builder) mutationOutputColMap(mutation memo.RelExpr) colOrdMap {
private := mutation.Private().(*memo.MutationPrivate)
tab := mutation.Memo().Metadata().Table(private.Table)
outCols := mutation.Relational().OutputCols
colMap := b.colOrdsAlloc.Alloc()
ord := 0
for i, n := 0, tab.ColumnCount(); i < n; i++ {
colID := private.Table.ColumnID(i)
// System columns should not be included in mutations.
if outCols.Contains(colID) && tab.Column(i).Kind() != cat.System {
colMap.Set(colID, ord)
ord++
}
}
// The output columns of the mutation will also include all
// columns it allowed to pass through.
for _, colID := range private.PassthroughCols {
if colID != 0 {
colMap.Set(colID, ord)
ord++
}
}
return colMap
}
// checkContainsLocking sets PlanFlagCheckContainsLocking based on whether we
// found locking while building a check query plan.
func (b *Builder) checkContainsLocking(mainContainsLocking bool) {
if b.flags.IsSet(exec.PlanFlagContainsLocking) {
b.flags.Set(exec.PlanFlagCheckContainsLocking)
}
if mainContainsLocking {
b.flags.Set(exec.PlanFlagContainsLocking)
}
}
// buildUniqueChecks builds uniqueness check queries. These check queries are
// used to enforce UNIQUE WITHOUT INDEX constraints.
//
// The checks consist of queries that will only return rows if a constraint is
// violated. Those queries are each wrapped in an ErrorIfRows operator, which
// will throw an appropriate error in case the inner query returns any rows.
func (b *Builder) buildUniqueChecks(checks memo.UniqueChecksExpr) error {
defer b.checkContainsLocking(b.flags.IsSet(exec.PlanFlagContainsLocking))
b.flags.Unset(exec.PlanFlagContainsLocking)
md := b.mem.Metadata()
for i := range checks {
c := &checks[i]
// Construct the query that returns uniqueness violations.
query, queryCols, err := b.buildRelational(c.Check)
if err != nil {
return err
}
// Wrap the query in an error node.
mkErr := func(row tree.Datums) error {
keyVals := make(tree.Datums, len(c.KeyCols))
for i, col := range c.KeyCols {
ord, err := getNodeColumnOrdinal(queryCols, col)
if err != nil {
return err
}
keyVals[i] = row[ord]
}
return mkUniqueCheckErr(md, c, keyVals)
}
node, err := b.factory.ConstructErrorIfRows(query.root, mkErr)
if err != nil {
return err
}
b.checks = append(b.checks, node)
}
return nil
}
func (b *Builder) buildFKChecks(checks memo.FKChecksExpr) error {
defer b.checkContainsLocking(b.flags.IsSet(exec.PlanFlagContainsLocking))
b.flags.Unset(exec.PlanFlagContainsLocking)
md := b.mem.Metadata()
for i := range checks {
c := &checks[i]
// Construct the query that returns FK violations.
query, queryCols, err := b.buildRelational(c.Check)
if err != nil {
return err
}
// Wrap the query in an error node.
mkErr := func(row tree.Datums) error {
keyVals := make(tree.Datums, len(c.KeyCols))
for i, col := range c.KeyCols {
ord, err := getNodeColumnOrdinal(queryCols, col)
if err != nil {
return err
}
keyVals[i] = row[ord]
}
return mkFKCheckErr(md, c, keyVals)
}
node, err := b.factory.ConstructErrorIfRows(query.root, mkErr)
if err != nil {
return err
}
b.checks = append(b.checks, node)
}
return nil
}
// mkUniqueCheckErr generates a user-friendly error describing a uniqueness
// violation. The keyVals are the values that correspond to the
// cat.UniqueConstraint columns.
func mkUniqueCheckErr(md *opt.Metadata, c *memo.UniqueChecksItem, keyVals tree.Datums) error {
tabMeta := md.TableMeta(c.Table)
uc := tabMeta.Table.Unique(c.CheckOrdinal)
constraintName := uc.Name()
var msg, details bytes.Buffer
// Generate an error of the form:
// ERROR: duplicate key value violates unique constraint "foo"
// DETAIL: Key (k)=(2) already exists.
msg.WriteString("duplicate key value violates unique constraint ")
lexbase.EncodeEscapedSQLIdent(&msg, constraintName)
details.WriteString("Key (")
for i := 0; i < uc.ColumnCount(); i++ {
if i > 0 {
details.WriteString(", ")
}
col := tabMeta.Table.Column(uc.ColumnOrdinal(tabMeta.Table, i))
details.WriteString(string(col.ColName()))
}
details.WriteString(")=(")
for i, d := range keyVals {
if i > 0 {
details.WriteString(", ")
}
details.WriteString(d.String())
}
details.WriteString(") already exists.")
return errors.WithDetail(
pgerror.WithConstraintName(
pgerror.Newf(pgcode.UniqueViolation, "%s", msg.String()),
constraintName,
),
details.String(),
)
}
// mkUniqueCheckErrWithoutColNames is a simpler version of mkUniqueCheckErr that
// omits column names from the error details.
func mkUniqueCheckErrWithoutColNames(
md *opt.Metadata, c *memo.UniqueChecksItem, keyVals tree.Datums,
) error {
tabMeta := md.TableMeta(c.Table)
uc := tabMeta.Table.Unique(c.CheckOrdinal)
constraintName := uc.Name()
var msg, details bytes.Buffer
// Generate an error of the form:
// ERROR: duplicate key value violates unique constraint "foo"
// DETAIL: Key (2) already exists.
msg.WriteString("duplicate key value violates unique constraint ")
lexbase.EncodeEscapedSQLIdent(&msg, constraintName)
details.WriteString("Key (")
for i, d := range keyVals {
if i > 0 {
details.WriteString(", ")
}
details.WriteString(d.String())
}
details.WriteString(") already exists.")
return errors.WithDetail(
pgerror.WithConstraintName(
pgerror.Newf(pgcode.UniqueViolation, "%s", msg.String()),
constraintName,
),
details.String(),
)
}
// mkFastPathUniqueCheckErr is a wrapper for mkUniqueCheckErr in the insert fast
// path flow, which reorders the keyVals row according to the ordering of the
// key columns in index `idx`. This is needed because mkUniqueCheckErr assumes
// the ordering of columns in `keyVals` matches the ordering of columns in
// `cat.UniqueConstraint.ColumnOrdinal(tabMeta.Table, i)`.
func mkFastPathUniqueCheckErr(
md *opt.Metadata, c *memo.UniqueChecksItem, keyVals tree.Datums, idx cat.Index,
) error {
tabMeta := md.TableMeta(c.Table)
uc := tabMeta.Table.Unique(c.CheckOrdinal)
newKeyVals := make(tree.Datums, 0, uc.ColumnCount())
for i := 0; i < uc.ColumnCount(); i++ {
ord := uc.ColumnOrdinal(tabMeta.Table, i)
found := false
for j := 0; j < idx.KeyColumnCount() && j < len(keyVals); j++ {
keyCol := idx.Column(j)
keyColOrd := keyCol.Column.Ordinal()
if ord == keyColOrd {
newKeyVals = append(newKeyVals, keyVals[j])
found = true
break
}
}
if !found {
// The unique constraint columns could not be matched to the index
// key columns. This can happen when the index columns are computed
// columns that map to the unique constraint columns (see #126988).
// When this happens, produce a simpler error message.
return mkUniqueCheckErrWithoutColNames(md, c, keyVals)
}
}
return mkUniqueCheckErr(md, c, newKeyVals)
}
// mkFKCheckErr generates a user-friendly error describing a foreign key
// violation. The keyVals are the values that correspond to the
// cat.ForeignKeyConstraint columns.