-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
alter_table.go
956 lines (877 loc) · 29.5 KB
/
alter_table.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
// 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 (
"bytes"
"context"
gojson "encoding/json"
"fmt"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/sql/coltypes"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/schemachange"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sem/types"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/sql/stats"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"golang.org/x/text/language"
)
type alterTableNode struct {
n *tree.AlterTable
tableDesc *MutableTableDescriptor
// statsData is populated with data for "alter table inject statistics"
// commands - the JSON stats expressions.
// It is parallel with n.Cmds (for the inject stats commands).
statsData map[int]tree.TypedExpr
}
// AlterTable applies a schema change on a table.
// Privileges: CREATE on table.
// notes: postgres requires CREATE on the table.
// mysql requires ALTER, CREATE, INSERT on the table.
func (p *planner) AlterTable(ctx context.Context, n *tree.AlterTable) (planNode, error) {
tableDesc, err := p.ResolveMutableTableDescriptor(ctx, &n.Table, !n.IfExists, requireTableDesc)
if err != nil {
return nil, err
}
if tableDesc == nil {
return newZeroNode(nil /* columns */), nil
}
if err := p.CheckPrivilege(ctx, tableDesc, privilege.CREATE); err != nil {
return nil, err
}
n.HoistAddColumnConstraints()
// See if there's any "inject statistics" in the query and type check the
// expressions.
statsData := make(map[int]tree.TypedExpr)
for i, cmd := range n.Cmds {
injectStats, ok := cmd.(*tree.AlterTableInjectStats)
if !ok {
continue
}
typedExpr, err := p.analyzeExpr(
ctx, injectStats.Stats,
nil, /* sources - no name resolution */
tree.IndexedVarHelper{},
types.JSON, true, /* requireType */
"INJECT STATISTICS" /* typingContext */)
if err != nil {
return nil, err
}
statsData[i] = typedExpr
}
return &alterTableNode{n: n, tableDesc: tableDesc, statsData: statsData}, nil
}
func (n *alterTableNode) startExec(params runParams) error {
// Commands can either change the descriptor directly (for
// alterations that don't require a backfill) or add a mutation to
// the list.
descriptorChanged := false
origNumMutations := len(n.tableDesc.Mutations)
var droppedViews []string
tn := &n.n.Table
for i, cmd := range n.n.Cmds {
switch t := cmd.(type) {
case *tree.AlterTableAddColumn:
d := t.ColumnDef
if d.HasFKConstraint() {
return pgerror.UnimplementedWithIssueError(32917,
"adding a REFERENCES constraint while also adding a column via ALTER not supported")
}
newDef, seqDbDesc, seqName, seqOpts, err := params.p.processSerialInColumnDef(params.ctx, d, tn)
if err != nil {
return err
}
if seqName != nil {
if err := doCreateSequence(params, n.n.String(), seqDbDesc, seqName, seqOpts); err != nil {
return err
}
}
d = newDef
col, idx, expr, err := sqlbase.MakeColumnDefDescs(d, ¶ms.p.semaCtx)
if err != nil {
return err
}
// If the new column has a DEFAULT expression that uses a sequence, add references between
// its descriptor and this column descriptor.
if d.HasDefaultExpr() {
changedSeqDescs, err := maybeAddSequenceDependencies(params.ctx, params.p, n.tableDesc, col, expr)
if err != nil {
return err
}
for _, changedSeqDesc := range changedSeqDescs {
if err := params.p.writeSchemaChange(params.ctx, changedSeqDesc, sqlbase.InvalidMutationID); err != nil {
return err
}
}
}
// We're checking to see if a user is trying add a non-nullable column without a default to a
// non empty table by scanning the primary index span with a limit of 1 to see if any key exists.
if !col.Nullable && (col.DefaultExpr == nil && !col.IsComputed()) {
kvs, err := params.p.txn.Scan(params.ctx, n.tableDesc.PrimaryIndexSpan().Key, n.tableDesc.PrimaryIndexSpan().EndKey, 1)
if err != nil {
return err
}
if len(kvs) > 0 {
return sqlbase.NewNonNullViolationError(col.Name)
}
}
_, dropped, err := n.tableDesc.FindColumnByName(d.Name)
if err == nil {
if dropped {
return fmt.Errorf("column %q being dropped, try again later", col.Name)
}
if t.IfNotExists {
continue
}
}
n.tableDesc.AddColumnMutation(*col, sqlbase.DescriptorMutation_ADD)
if idx != nil {
if err := n.tableDesc.AddIndexMutation(idx, sqlbase.DescriptorMutation_ADD); err != nil {
return err
}
}
if d.HasColumnFamily() {
err := n.tableDesc.AddColumnToFamilyMaybeCreate(
col.Name, string(d.Family.Name), d.Family.Create,
d.Family.IfNotExists)
if err != nil {
return err
}
}
case *tree.AlterTableAddConstraint:
info, err := n.tableDesc.GetConstraintInfo(params.ctx, nil)
if err != nil {
return err
}
inuseNames := make(map[string]struct{}, len(info))
for k := range info {
inuseNames[k] = struct{}{}
}
switch d := t.ConstraintDef.(type) {
case *tree.UniqueConstraintTableDef:
if d.PrimaryKey {
return fmt.Errorf("multiple primary keys for table %q are not allowed", n.tableDesc.Name)
}
idx := sqlbase.IndexDescriptor{
Name: string(d.Name),
Unique: true,
StoreColumnNames: d.Storing.ToStrings(),
}
if err := idx.FillColumns(d.Columns); err != nil {
return err
}
if d.PartitionBy != nil {
partitioning, err := CreatePartitioning(
params.ctx, params.p.ExecCfg().Settings,
params.EvalContext(), n.tableDesc, &idx, d.PartitionBy)
if err != nil {
return err
}
idx.Partitioning = partitioning
}
_, dropped, err := n.tableDesc.FindIndexByName(string(d.Name))
if err == nil {
if dropped {
return fmt.Errorf("index %q being dropped, try again later", d.Name)
}
}
if err := n.tableDesc.AddIndexMutation(&idx, sqlbase.DescriptorMutation_ADD); err != nil {
return err
}
case *tree.CheckConstraintTableDef:
// A previous command could have added a column which the new constraint uses,
// allocate IDs now.
if err != n.tableDesc.AllocateIDs() {
return err
}
ck, err := MakeCheckConstraint(params.ctx,
n.tableDesc, d, inuseNames, ¶ms.p.semaCtx, n.n.Table)
if err != nil {
return err
}
ck.Validity = sqlbase.ConstraintValidity_Validating
n.tableDesc.Checks = append(n.tableDesc.Checks, ck)
descriptorChanged = true
n.tableDesc.AddCheckValidationMutation(ck.Name)
case *tree.ForeignKeyConstraintTableDef:
for _, colName := range d.FromCols {
col, _, err := n.tableDesc.FindColumnByName(colName)
if err != nil {
return err
}
if err := col.CheckCanBeFKRef(); err != nil {
return err
}
}
affected := make(map[sqlbase.ID]*sqlbase.MutableTableDescriptor)
// If there are any FKs, we will need to update the table descriptor of the
// depended-on table (to register this table against its DependedOnBy field).
// This descriptor must be looked up uncached, and we'll allow FK dependencies
// on tables that were just added. See the comment at the start of
// the global-scope resolveFK().
// TODO(vivek): check if the cache can be used.
params.p.runWithOptions(resolveFlags{skipCache: true}, func() {
// Check whether the table is empty, and pass the result to resolveFK(). If
// the table is empty, then resolveFK will automatically add the necessary
// index for a fk constraint if the index does not exist.
kvs, scanErr := params.p.txn.Scan(params.ctx, n.tableDesc.PrimaryIndexSpan().Key, n.tableDesc.PrimaryIndexSpan().EndKey, 1)
if scanErr != nil {
err = scanErr
return
}
var tableState FKTableState
if len(kvs) == 0 {
tableState = EmptyTable
} else {
tableState = NonEmptyTable
}
err = params.p.resolveFK(params.ctx, n.tableDesc, d, affected, tableState)
})
if err != nil {
return err
}
descriptorChanged = true
for _, updated := range affected {
if err := params.p.writeSchemaChange(params.ctx, updated, sqlbase.InvalidMutationID); err != nil {
return err
}
}
default:
return fmt.Errorf("unsupported constraint: %T", t.ConstraintDef)
}
case *tree.AlterTableDropColumn:
if params.SessionData().SafeUpdates {
return pgerror.NewDangerousStatementErrorf("ALTER TABLE DROP COLUMN will remove all data in that column")
}
col, dropped, err := n.tableDesc.FindColumnByName(t.Column)
if err != nil {
if t.IfExists {
// Noop.
continue
}
return err
}
if dropped {
continue
}
// If the dropped column uses a sequence, remove references to it from that sequence.
if len(col.UsesSequenceIds) > 0 {
if err := removeSequenceDependencies(n.tableDesc, &col, params); err != nil {
return err
}
}
// You can't drop a column depended on by a view unless CASCADE was
// specified.
for _, ref := range n.tableDesc.DependedOnBy {
found := false
for _, colID := range ref.ColumnIDs {
if colID == col.ID {
found = true
break
}
}
if !found {
continue
}
err := params.p.canRemoveDependentViewGeneric(
params.ctx, "column", string(t.Column), n.tableDesc.ParentID, ref, t.DropBehavior,
)
if err != nil {
return err
}
viewDesc, err := params.p.getViewDescForCascade(
params.ctx, "column", string(t.Column), n.tableDesc.ParentID, ref.ID, t.DropBehavior,
)
if err != nil {
return err
}
droppedViews, err = params.p.removeDependentView(params.ctx, n.tableDesc, viewDesc)
if err != nil {
return err
}
}
if n.tableDesc.PrimaryIndex.ContainsColumnID(col.ID) {
return fmt.Errorf("column %q is referenced by the primary key", col.Name)
}
for _, idx := range n.tableDesc.AllNonDropIndexes() {
// We automatically drop indexes on that column that only
// index that column (and no other columns). If CASCADE is
// specified, we also drop other indices that refer to this
// column. The criteria to determine whether an index "only
// indexes that column":
//
// Assume a table created with CREATE TABLE foo (a INT, b INT).
// Then assume the user issues ALTER TABLE foo DROP COLUMN a.
//
// INDEX i1 ON foo(a) -> i1 deleted
// INDEX i2 ON foo(a) STORING(b) -> i2 deleted
// INDEX i3 ON foo(a, b) -> i3 not deleted unless CASCADE is specified.
// INDEX i4 ON foo(b) STORING(a) -> i4 not deleted unless CASCADE is specified.
// containsThisColumn becomes true if the index is defined
// over the column being dropped.
containsThisColumn := false
// containsOnlyThisColumn becomes false if the index also
// includes non-PK columns other than the one being dropped.
containsOnlyThisColumn := true
// Analyze the index.
for _, id := range idx.ColumnIDs {
if id == col.ID {
containsThisColumn = true
} else {
containsOnlyThisColumn = false
}
}
for _, id := range idx.ExtraColumnIDs {
if n.tableDesc.PrimaryIndex.ContainsColumnID(id) {
// All secondary indices necessary contain the PK
// columns, too. (See the comments on the definition of
// IndexDescriptor). The presence of a PK column in the
// secondary index should thus not be seen as a
// sufficient reason to reject the DROP.
continue
}
if id == col.ID {
containsThisColumn = true
}
}
// The loop above this comment is for the old STORING encoding. The
// loop below is for the new encoding (where the STORING columns are
// always in the value part of a KV).
for _, id := range idx.StoreColumnIDs {
if id == col.ID {
containsThisColumn = true
}
}
// Perform the DROP.
if containsThisColumn {
if containsOnlyThisColumn || t.DropBehavior == tree.DropCascade {
if err := params.p.dropIndexByName(
params.ctx, tn, tree.UnrestrictedName(idx.Name), n.tableDesc, false,
t.DropBehavior, ignoreIdxConstraint,
tree.AsStringWithFlags(n.n, tree.FmtAlwaysQualifyTableNames),
); err != nil {
return err
}
} else {
return fmt.Errorf("column %q is referenced by existing index %q", col.Name, idx.Name)
}
}
}
// Drop check constraints which reference the column.
validChecks := n.tableDesc.Checks[:0]
for _, check := range n.tableDesc.Checks {
if used, err := check.UsesColumn(n.tableDesc.TableDesc(), col.ID); err != nil {
return err
} else if used {
if check.Validity == sqlbase.ConstraintValidity_Validating {
return fmt.Errorf("referencing constraint %q in the middle of being added, try again later", check.Name)
}
} else {
validChecks = append(validChecks, check)
}
}
if len(validChecks) != len(n.tableDesc.Checks) {
n.tableDesc.Checks = validChecks
descriptorChanged = true
}
if err != nil {
return err
}
if err := params.p.removeColumnComment(params.ctx, n.tableDesc.ID, col.ID); err != nil {
return err
}
found := false
for i := range n.tableDesc.Columns {
if n.tableDesc.Columns[i].ID == col.ID {
n.tableDesc.AddColumnMutation(col, sqlbase.DescriptorMutation_DROP)
n.tableDesc.Columns = append(n.tableDesc.Columns[:i], n.tableDesc.Columns[i+1:]...)
found = true
break
}
}
if !found {
return fmt.Errorf("column %q in the middle of being added, try again later", t.Column)
}
case *tree.AlterTableDropConstraint:
info, err := n.tableDesc.GetConstraintInfo(params.ctx, nil)
if err != nil {
return err
}
name := string(t.Constraint)
details, ok := info[name]
if !ok {
if t.IfExists {
continue
}
return fmt.Errorf("constraint %q does not exist", t.Constraint)
}
switch details.Kind {
case sqlbase.ConstraintTypePK:
return fmt.Errorf("cannot drop primary key")
case sqlbase.ConstraintTypeUnique:
return fmt.Errorf("UNIQUE constraint depends on index %q, use DROP INDEX with CASCADE if you really want to drop it", t.Constraint)
case sqlbase.ConstraintTypeCheck:
for i, c := range n.tableDesc.Checks {
if c.Name == name {
if c.Validity == sqlbase.ConstraintValidity_Validating {
return fmt.Errorf("constraint %q in the middle of being added, try again later", t.Constraint)
}
n.tableDesc.Checks = append(n.tableDesc.Checks[:i], n.tableDesc.Checks[i+1:]...)
descriptorChanged = true
break
}
}
case sqlbase.ConstraintTypeFK:
idx, err := n.tableDesc.FindIndexByID(details.Index.ID)
if err != nil {
return err
}
if err := params.p.removeFKBackReference(params.ctx, n.tableDesc, idx); err != nil {
return err
}
idx.ForeignKey = sqlbase.ForeignKeyReference{}
descriptorChanged = true
default:
return errors.Errorf("dropping %s constraint %q unsupported", details.Kind, t.Constraint)
}
case *tree.AlterTableValidateConstraint:
info, err := n.tableDesc.GetConstraintInfo(params.ctx, nil)
if err != nil {
return err
}
name := string(t.Constraint)
constraint, ok := info[name]
if !ok {
return fmt.Errorf("constraint %q does not exist", t.Constraint)
}
if !constraint.Unvalidated {
continue
}
switch constraint.Kind {
case sqlbase.ConstraintTypeCheck:
found := false
var idx int
for idx = range n.tableDesc.Checks {
if n.tableDesc.Checks[idx].Name == name {
found = true
break
}
}
if !found {
panic("constraint returned by GetConstraintInfo not found")
}
if n.tableDesc.Checks[idx].Validity == sqlbase.ConstraintValidity_Validating {
return fmt.Errorf("constraint %q in the middle of being added, try again later", t.Constraint)
}
ck := n.tableDesc.Checks[idx]
if err := validateCheckExpr(
params.ctx, ck.Expr, n.tableDesc.TableDesc(), params.EvalContext().InternalExecutor, params.EvalContext().Txn,
); err != nil {
return err
}
n.tableDesc.Checks[idx].Validity = sqlbase.ConstraintValidity_Validated
descriptorChanged = true
case sqlbase.ConstraintTypeFK:
found := false
var id sqlbase.IndexID
for _, idx := range n.tableDesc.AllNonDropIndexes() {
if idx.ForeignKey.IsSet() && idx.ForeignKey.Name == name {
found = true
id = idx.ID
break
}
}
if !found {
panic("constraint returned by GetConstraintInfo not found")
}
idx, err := n.tableDesc.FindIndexByID(id)
if err != nil {
panic(err)
}
if err := params.p.validateForeignKey(params.ctx, n.tableDesc.TableDesc(), idx); err != nil {
return err
}
idx.ForeignKey.Validity = sqlbase.ConstraintValidity_Validated
descriptorChanged = true
default:
return errors.Errorf("validating %s constraint %q unsupported", constraint.Kind, t.Constraint)
}
case tree.ColumnMutationCmd:
// Column mutations
col, dropped, err := n.tableDesc.FindColumnByName(t.GetColumn())
if err != nil {
return err
}
if dropped {
return fmt.Errorf("column %q in the middle of being dropped", t.GetColumn())
}
if err := applyColumnMutation(n.tableDesc, &col, t, params); err != nil {
return err
}
n.tableDesc.UpdateColumnDescriptor(col)
descriptorChanged = true
case *tree.AlterTablePartitionBy:
partitioning, err := CreatePartitioning(
params.ctx, params.p.ExecCfg().Settings,
params.EvalContext(),
n.tableDesc, &n.tableDesc.PrimaryIndex, t.PartitionBy)
if err != nil {
return err
}
descriptorChanged = !proto.Equal(
&n.tableDesc.PrimaryIndex.Partitioning,
&partitioning,
)
err = deleteRemovedPartitionZoneConfigs(
params.ctx, params.p.txn,
n.tableDesc.TableDesc(), &n.tableDesc.PrimaryIndex, &n.tableDesc.PrimaryIndex.Partitioning,
&partitioning, params.extendedEvalCtx.ExecCfg,
)
if err != nil {
return err
}
n.tableDesc.PrimaryIndex.Partitioning = partitioning
case *tree.AlterTableSetAudit:
var err error
descriptorChanged, err = params.p.setAuditMode(params.ctx, n.tableDesc.TableDesc(), t.Mode)
if err != nil {
return err
}
case *tree.AlterTableInjectStats:
sd, ok := n.statsData[i]
if !ok {
return pgerror.NewAssertionErrorf("missing stats data")
}
if err := injectTableStats(params, n.tableDesc.TableDesc(), sd); err != nil {
return err
}
case *tree.AlterTableRenameColumn:
descChanged, err := params.p.renameColumn(params.ctx, n.tableDesc, &t.Column, &t.NewName)
if err != nil {
return err
}
descriptorChanged = descChanged
case *tree.AlterTableRenameConstraint:
info, err := n.tableDesc.GetConstraintInfo(params.ctx, nil)
if err != nil {
return err
}
details, ok := info[string(t.Constraint)]
if !ok {
return fmt.Errorf("constraint %q does not exist", tree.ErrString(&t.Constraint))
}
if t.Constraint == t.NewName {
// Nothing to do.
break
}
if _, ok := info[string(t.NewName)]; ok {
return errors.Errorf("duplicate constraint name: %q", tree.ErrString(&t.NewName))
}
if err := params.p.CheckPrivilege(params.ctx, n.tableDesc, privilege.CREATE); err != nil {
return err
}
depViewRenameError := func(objType string, refTableID sqlbase.ID) error {
return params.p.dependentViewRenameError(params.ctx,
objType, tree.ErrString(&t.NewName), n.tableDesc.ParentID, refTableID)
}
if err := n.tableDesc.RenameConstraint(
details, string(t.Constraint), string(t.NewName), depViewRenameError); err != nil {
return err
}
descriptorChanged = true
default:
return pgerror.NewAssertionErrorf("unsupported alter command: %T", cmd)
}
}
// Were some changes made?
//
// This is only really needed for the unittests that add dummy mutations
// before calling ALTER TABLE commands. We do not want to apply those
// dummy mutations. Most tests trigger errors above
// this line, but tests that run redundant operations like dropping
// a column when it's already dropped will hit this condition and exit.
addedMutations := len(n.tableDesc.Mutations) > origNumMutations
if !addedMutations && !descriptorChanged {
return nil
}
if err := n.tableDesc.AllocateIDs(); err != nil {
return err
}
mutationID := sqlbase.InvalidMutationID
if addedMutations {
var err error
mutationID, err = params.p.createOrUpdateSchemaChangeJob(params.ctx, n.tableDesc,
tree.AsStringWithFlags(n.n, tree.FmtAlwaysQualifyTableNames))
if err != nil {
return err
}
}
if err := params.p.writeSchemaChange(params.ctx, n.tableDesc, mutationID); err != nil {
return err
}
// Record this table alteration in the event log. This is an auditable log
// event and is recorded in the same transaction as the table descriptor
// update.
return MakeEventLogger(params.extendedEvalCtx.ExecCfg).InsertEventRecord(
params.ctx,
params.p.txn,
EventLogAlterTable,
int32(n.tableDesc.ID),
int32(params.extendedEvalCtx.NodeID),
struct {
TableName string
Statement string
User string
MutationID uint32
CascadeDroppedViews []string
}{n.n.Table.FQString(), n.n.String(),
params.SessionData().User, uint32(mutationID), droppedViews},
)
}
func (p *planner) setAuditMode(
ctx context.Context, desc *sqlbase.TableDescriptor, auditMode tree.AuditMode,
) (bool, error) {
// An auditing config change is itself auditable!
// We record the event even if the permission check below fails:
// auditing wants to know who tried to change the settings.
p.curPlan.auditEvents = append(p.curPlan.auditEvents,
auditEvent{desc: desc, writing: true})
// We require root for now. Later maybe use a different permission?
if err := p.RequireSuperUser(ctx, "change auditing settings on a table"); err != nil {
return false, err
}
return desc.SetAuditMode(auditMode)
}
func (n *alterTableNode) Next(runParams) (bool, error) { return false, nil }
func (n *alterTableNode) Values() tree.Datums { return tree.Datums{} }
func (n *alterTableNode) Close(context.Context) {}
// applyColumnMutation applies the mutation specified in `mut` to the given
// columnDescriptor, and saves the containing table descriptor. If the column's
// dependencies on sequences change, it updates them as well.
func applyColumnMutation(
tableDesc *sqlbase.MutableTableDescriptor,
col *sqlbase.ColumnDescriptor,
mut tree.ColumnMutationCmd,
params runParams,
) error {
switch t := mut.(type) {
case *tree.AlterTableAlterColumnType:
// Convert the parsed type into one of the basic datum types.
datum := coltypes.CastTargetToDatumType(t.ToType)
// Special handling for STRING COLLATE xy to verify that we recognize the language.
if t.Collation != "" {
if types.IsStringType(datum) {
if _, err := language.Parse(t.Collation); err != nil {
return pgerror.NewErrorf(pgerror.CodeSyntaxError, `invalid locale %s`, t.Collation)
}
datum = types.TCollatedString{Locale: t.Collation}
} else {
return pgerror.NewError(pgerror.CodeSyntaxError, "COLLATE can only be used with string types")
}
}
// First pass at converting the datum type to the SQL column type.
nextType, err := sqlbase.DatumTypeToColumnType(datum)
if err != nil {
return err
}
// Finish populating width, precision, etc. from parsed data.
nextType, err = sqlbase.PopulateTypeAttrs(nextType, t.ToType)
if err != nil {
return err
}
// No-op if the types are Equal. We don't use Equivalent here
// because the user may want to change the visible type of the
// column without changing the underlying semantic type.
if col.Type.Equal(nextType) {
return nil
}
kind, err := schemachange.ClassifyConversion(&col.Type, &nextType)
if err != nil {
return err
}
switch kind {
case schemachange.ColumnConversionDangerous, schemachange.ColumnConversionImpossible:
// We're not going to make it impossible for the user to perform
// this conversion, but we do want them to explicit about
// what they're going for.
return pgerror.NewErrorf(pgerror.CodeCannotCoerceError,
"the requested type conversion (%s -> %s) requires an explicit USING expression",
col.Type.SQLString(), nextType.SQLString())
case schemachange.ColumnConversionTrivial:
col.Type = nextType
default:
return pgerror.UnimplementedWithIssueDetailError(9851,
fmt.Sprintf("%s->%s", col.Type.SQLString(), nextType.SQLString()),
"type conversion not yet implemented")
}
case *tree.AlterTableSetDefault:
if len(col.UsesSequenceIds) > 0 {
if err := removeSequenceDependencies(tableDesc, col, params); err != nil {
return err
}
}
if t.Default == nil {
col.DefaultExpr = nil
} else {
colDatumType := col.Type.ToDatumType()
expr, err := sqlbase.SanitizeVarFreeExpr(
t.Default, colDatumType, "DEFAULT", ¶ms.p.semaCtx, true, /* allowImpure */
)
if err != nil {
return err
}
s := tree.Serialize(t.Default)
col.DefaultExpr = &s
// Add references to the sequence descriptors this column is now using.
changedSeqDescs, err := maybeAddSequenceDependencies(params.ctx, params.p, tableDesc, col, expr)
if err != nil {
return err
}
for _, changedSeqDesc := range changedSeqDescs {
if err := params.p.writeSchemaChange(params.ctx, changedSeqDesc, sqlbase.InvalidMutationID); err != nil {
return err
}
}
}
case *tree.AlterTableDropNotNull:
col.Nullable = true
case *tree.AlterTableDropStored:
if !col.IsComputed() {
return pgerror.NewErrorf(pgerror.CodeInvalidColumnDefinitionError,
"column %q is not a computed column", col.Name)
}
col.ComputeExpr = nil
}
return nil
}
func labeledRowValues(cols []sqlbase.ColumnDescriptor, values tree.Datums) string {
var s bytes.Buffer
for i := range cols {
if i != 0 {
s.WriteString(`, `)
}
s.WriteString(cols[i].Name)
s.WriteString(`=`)
s.WriteString(values[i].String())
}
return s.String()
}
// injectTableStats implements the INJECT STATISTICS command, which deletes any
// existing statistics on the table and replaces them with the statistics in the
// given json object (in the same format as the result of SHOW STATISTICS USING
// JSON). This is useful for reproducing planning issues without importing the
// data.
func injectTableStats(
params runParams, desc *sqlbase.TableDescriptor, statsExpr tree.TypedExpr,
) error {
val, err := statsExpr.Eval(params.EvalContext())
if err != nil {
return err
}
if val == tree.DNull {
return fmt.Errorf("statistics cannot be NULL")
}
jsonStr := val.(*tree.DJSON).JSON.String()
var jsonStats []stats.JSONStatistic
if err := gojson.Unmarshal([]byte(jsonStr), &jsonStats); err != nil {
return err
}
// First, delete all statistics for the table.
if _ /* rows */, err := params.extendedEvalCtx.ExecCfg.InternalExecutor.Exec(
params.ctx,
"delete-stats",
params.EvalContext().Txn,
`DELETE FROM system.table_statistics WHERE "tableID" = $1`, desc.ID,
); err != nil {
return errors.Wrapf(err, "failed to delete old stats")
}
// Insert each statistic.
for i := range jsonStats {
s := &jsonStats[i]
h, err := s.GetHistogram(params.EvalContext())
if err != nil {
return err
}
// histogram will be passed to the INSERT statement; we want it to be a
// nil interface{} if we don't generate a histogram.
var histogram interface{}
if h != nil {
histogram, err = protoutil.Marshal(h)
if err != nil {
return err
}
}
columnIDs := tree.NewDArray(types.Int)
for _, colName := range s.Columns {
colDesc, _, err := desc.FindColumnByName(tree.Name(colName))
if err != nil {
return err
}
if err := columnIDs.Append(tree.NewDInt(tree.DInt(colDesc.ID))); err != nil {
return err
}
}
var name interface{}
if s.Name != "" {
name = s.Name
}
if _ /* rows */, err := params.extendedEvalCtx.ExecCfg.InternalExecutor.Exec(
params.ctx,
"insert-stats",
params.EvalContext().Txn,
`INSERT INTO system.table_statistics (
"tableID",
"name",
"columnIDs",
"createdAt",
"rowCount",
"distinctCount",
"nullCount",
histogram
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
desc.ID,
name,
columnIDs,
s.CreatedAt,
s.RowCount,
s.DistinctCount,
s.NullCount,
histogram,
); err != nil {
return errors.Wrapf(err, "failed to insert stats")
}
}
// Invalidate the local cache synchronously; this guarantees that the next
// statement in the same session won't use a stale cache (whereas the gossip
// update is handled asynchronously).
params.extendedEvalCtx.ExecCfg.TableStatsCache.InvalidateTableStats(params.ctx, desc.ID)
return stats.GossipTableStatAdded(params.extendedEvalCtx.ExecCfg.Gossip, desc.ID)
}
func (p *planner) removeColumnComment(
ctx context.Context, tableID sqlbase.ID, columnID sqlbase.ColumnID,
) error {
_, err := p.ExtendedEvalContext().ExecCfg.InternalExecutor.Exec(
ctx,
"delete-column-comment",
p.txn,
"DELETE FROM system.comments WHERE type=$1 AND object_id=$2 AND sub_id=$3",
keys.ColumnCommentType,
tableID,
columnID)
return err
}