-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathalter_type.go
486 lines (429 loc) · 13.5 KB
/
alter_type.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
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package sql
import (
"context"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkv"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgnotice"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
"github.com/cockroachdb/errors"
)
type alterTypeNode struct {
n *tree.AlterType
prefix catalog.ResolvedObjectPrefix
desc *typedesc.Mutable
}
// alterTypeNode implements planNode. We set n here to satisfy the linter.
var _ planNode = &alterTypeNode{n: nil}
func (p *planner) AlterType(ctx context.Context, n *tree.AlterType) (planNode, error) {
if err := checkSchemaChangeEnabled(
ctx,
p.ExecCfg(),
"ALTER TYPE",
); err != nil {
return nil, err
}
// Resolve the type.
prefix, desc, err := p.ResolveMutableTypeDescriptor(ctx, n.Type, true /* required */)
if err != nil {
return nil, err
}
// The user needs ownership privilege to alter the type.
if err := p.canModifyType(ctx, desc); err != nil {
return nil, err
}
switch desc.Kind {
case descpb.TypeDescriptor_ALIAS:
// The implicit array types are not modifiable.
return nil, pgerror.Newf(
pgcode.WrongObjectType,
"%q is an implicit array type and cannot be modified",
tree.AsStringWithFQNames(n.Type, &p.semaCtx.Annotations),
)
case descpb.TypeDescriptor_MULTIREGION_ENUM:
// Multi-region enums can't be directly modified except for OWNER TO.
if _, isAlterTypeOwner := n.Cmd.(*tree.AlterTypeOwner); !isAlterTypeOwner {
return nil, errors.WithHint(
pgerror.Newf(
pgcode.WrongObjectType,
"%q is a multi-region enum and can't be modified using the alter type command",
tree.AsStringWithFQNames(n.Type, &p.semaCtx.Annotations)),
"try adding/removing the region using ALTER DATABASE")
}
case descpb.TypeDescriptor_ENUM:
sqltelemetry.IncrementEnumCounter(sqltelemetry.EnumAlter)
}
return &alterTypeNode{
n: n,
prefix: prefix,
desc: desc,
}, nil
}
func (n *alterTypeNode) startExec(params runParams) error {
telemetry.Inc(n.n.Cmd.TelemetryCounter())
typeName := tree.AsStringWithFQNames(n.n.Type, params.p.Ann())
eventLogDone := false
var err error
switch t := n.n.Cmd.(type) {
case *tree.AlterTypeAddValue:
err = params.p.addEnumValue(params.ctx, n.desc, t, tree.AsStringWithFQNames(n.n, params.p.Ann()))
case *tree.AlterTypeRenameValue:
err = params.p.renameTypeValue(params.ctx, n, string(t.OldVal), string(t.NewVal))
case *tree.AlterTypeRename:
if err = params.p.renameType(params.ctx, n, string(t.NewName)); err != nil {
return err
}
err = params.p.logEvent(params.ctx, n.desc.ID, &eventpb.RenameType{
TypeName: typeName,
NewTypeName: string(t.NewName),
})
eventLogDone = true
case *tree.AlterTypeSetSchema:
// TODO(knz): this is missing dedicated logging,
// See https://github.com/cockroachdb/cockroach/issues/57741
err = params.p.setTypeSchema(params.ctx, n, string(t.Schema))
case *tree.AlterTypeOwner:
if err = params.p.alterTypeOwner(params.ctx, n, t.Owner); err != nil {
return err
}
eventLogDone = true // done inside alterTypeOwner().
case *tree.AlterTypeDropValue:
err = params.p.dropEnumValue(params.ctx, n.desc, t.Val)
default:
err = errors.AssertionFailedf("unknown alter type cmd %s", t)
}
if err != nil {
return err
}
if !eventLogDone {
// Write a log event.
if err := params.p.logEvent(params.ctx,
n.desc.ID,
&eventpb.AlterType{
TypeName: typeName,
}); err != nil {
return err
}
}
return nil
}
func findEnumMemberByName(
desc *typedesc.Mutable, val tree.EnumValue,
) (bool, *descpb.TypeDescriptor_EnumMember) {
for _, member := range desc.EnumMembers {
if member.LogicalRepresentation == string(val) {
return true, &member
}
}
return false, nil
}
func (p *planner) addEnumValue(
ctx context.Context, desc *typedesc.Mutable, node *tree.AlterTypeAddValue, jobDesc string,
) error {
if desc.Kind != descpb.TypeDescriptor_ENUM &&
desc.Kind != descpb.TypeDescriptor_MULTIREGION_ENUM {
return pgerror.Newf(pgcode.WrongObjectType, "%q is not an enum", desc.Name)
}
// See if the value already exists in the enum or not.
found, member := findEnumMemberByName(desc, node.NewVal)
if found {
if enumMemberIsRemoving(member) {
return pgerror.Newf(pgcode.ObjectNotInPrerequisiteState,
"enum value %q is being dropped, try again later", node.NewVal)
}
if node.IfNotExists {
p.BufferClientNotice(
ctx,
pgnotice.Newf("enum value %q already exists, skipping", node.NewVal),
)
return nil
}
return pgerror.Newf(pgcode.DuplicateObject, "enum value %q already exists", node.NewVal)
}
if err := desc.AddEnumValue(node); err != nil {
return err
}
return p.writeTypeSchemaChange(ctx, desc, jobDesc)
}
func (p *planner) dropEnumValue(
ctx context.Context, desc *typedesc.Mutable, val tree.EnumValue,
) error {
if desc.Kind != descpb.TypeDescriptor_ENUM &&
desc.Kind != descpb.TypeDescriptor_MULTIREGION_ENUM {
return pgerror.Newf(pgcode.WrongObjectType, "%q is not an enum", desc.Name)
}
found, member := findEnumMemberByName(desc, val)
if !found {
return pgerror.Newf(pgcode.UndefinedObject, "enum value %q does not exist", val)
}
// Do not allow drops if the enum value isn't public yet.
if enumMemberIsRemoving(member) {
return pgerror.Newf(pgcode.ObjectNotInPrerequisiteState,
"enum value %q is already being dropped", val)
}
if enumMemberIsAdding(member) {
return pgerror.Newf(pgcode.ObjectNotInPrerequisiteState,
"enum value %q is being added, try again later", val)
}
desc.DropEnumValue(val)
return p.writeTypeSchemaChange(ctx, desc, desc.Name)
}
func (p *planner) renameType(ctx context.Context, n *alterTypeNode, newName string) error {
err := catalogkv.CheckObjectCollision(
ctx,
p.txn,
p.ExecCfg().Codec,
n.desc.ParentID,
n.desc.ParentSchemaID,
tree.NewUnqualifiedTypeName(newName),
)
if err != nil {
return err
}
// Rename the base descriptor.
if err := p.performRenameTypeDesc(
ctx,
n.desc,
newName,
n.desc.ParentSchemaID,
tree.AsStringWithFQNames(n.n, p.Ann()),
); err != nil {
return err
}
// Now rename the array type.
newArrayName, err := findFreeArrayTypeName(
ctx,
p.txn,
p.ExecCfg().Codec,
n.desc.ParentID,
n.desc.ParentSchemaID,
newName,
)
if err != nil {
return err
}
arrayDesc, err := p.Descriptors().GetMutableTypeVersionByID(ctx, p.txn, n.desc.ArrayTypeID)
if err != nil {
return err
}
if err := p.performRenameTypeDesc(
ctx,
arrayDesc,
newArrayName,
arrayDesc.ParentSchemaID,
tree.AsStringWithFQNames(n.n, p.Ann()),
); err != nil {
return err
}
return nil
}
// performRenameTypeDesc renames and/or sets the schema of a type descriptor.
// newName and newSchemaID may be the same as the current name and schemaid.
func (p *planner) performRenameTypeDesc(
ctx context.Context,
desc *typedesc.Mutable,
newName string,
newSchemaID descpb.ID,
jobDesc string,
) error {
// Record the rename details in the descriptor for draining.
name := descpb.NameInfo{
ParentID: desc.ParentID,
ParentSchemaID: desc.ParentSchemaID,
Name: desc.Name,
}
desc.AddDrainingName(name)
// Set the descriptor up with the new name.
desc.Name = newName
// Set the descriptor to the new schema ID.
desc.SetParentSchemaID(newSchemaID)
if err := p.writeTypeSchemaChange(ctx, desc, jobDesc); err != nil {
return err
}
// Write the new namespace key.
return p.writeNameKey(ctx, desc, desc.ID)
}
func (p *planner) renameTypeValue(
ctx context.Context, n *alterTypeNode, oldVal string, newVal string,
) error {
enumMemberIndex := -1
// Do one pass to verify that the oldVal exists and there isn't already
// a member that is named newVal.
for i := range n.desc.EnumMembers {
member := n.desc.EnumMembers[i]
if member.LogicalRepresentation == oldVal {
enumMemberIndex = i
} else if member.LogicalRepresentation == newVal {
return pgerror.Newf(pgcode.DuplicateObject,
"enum value %s already exists", newVal)
}
}
// An enum member with the name oldVal was not found.
if enumMemberIndex == -1 {
return pgerror.Newf(pgcode.InvalidParameterValue,
"%s is not an existing enum value", oldVal)
}
if enumMemberIsRemoving(&n.desc.EnumMembers[enumMemberIndex]) {
return pgerror.Newf(pgcode.ObjectNotInPrerequisiteState,
"enum value %q is being dropped", oldVal)
}
if enumMemberIsAdding(&n.desc.EnumMembers[enumMemberIndex]) {
return pgerror.Newf(pgcode.ObjectNotInPrerequisiteState,
"enum value %q is being added, try again later", oldVal)
}
n.desc.EnumMembers[enumMemberIndex].LogicalRepresentation = newVal
return p.writeTypeSchemaChange(
ctx,
n.desc,
tree.AsStringWithFQNames(n.n, p.Ann()),
)
}
func (p *planner) setTypeSchema(ctx context.Context, n *alterTypeNode, schema string) error {
typeDesc := n.desc
schemaID := typeDesc.GetParentSchemaID()
oldName, err := p.getQualifiedTypeName(ctx, typeDesc)
if err != nil {
return err
}
desiredSchemaID, err := p.prepareSetSchema(ctx, n.prefix.Database, typeDesc, schema)
if err != nil {
return err
}
// If the schema being changed to is the same as the current schema for the
// type, do a no-op.
if desiredSchemaID == schemaID {
return nil
}
err = p.performRenameTypeDesc(
ctx, typeDesc, typeDesc.Name, desiredSchemaID, tree.AsStringWithFQNames(n.n, p.Ann()),
)
if err != nil {
return err
}
arrayDesc, err := p.Descriptors().GetMutableTypeVersionByID(ctx, p.txn, n.desc.ArrayTypeID)
if err != nil {
return err
}
if err := p.performRenameTypeDesc(
ctx, arrayDesc, arrayDesc.Name, desiredSchemaID, tree.AsStringWithFQNames(n.n, p.Ann()),
); err != nil {
return err
}
newName, err := p.getQualifiedTypeName(ctx, typeDesc)
if err != nil {
return err
}
return p.logEvent(ctx,
desiredSchemaID,
&eventpb.SetSchema{
CommonEventDetails: eventpb.CommonEventDetails{},
CommonSQLEventDetails: eventpb.CommonSQLEventDetails{},
DescriptorName: oldName.FQString(),
NewDescriptorName: newName.FQString(),
DescriptorType: "type",
},
)
}
func (p *planner) alterTypeOwner(
ctx context.Context, n *alterTypeNode, newOwner security.SQLUsername,
) error {
typeDesc := n.desc
oldOwner := typeDesc.GetPrivileges().Owner()
arrayDesc, err := p.Descriptors().GetMutableTypeVersionByID(ctx, p.txn, typeDesc.ArrayTypeID)
if err != nil {
return err
}
if err := p.checkCanAlterToNewOwner(ctx, typeDesc, newOwner); err != nil {
return err
}
// Ensure the new owner has CREATE privilege on the type's schema.
if err := p.canCreateOnSchema(
ctx, typeDesc.GetParentSchemaID(), typeDesc.ParentID, newOwner, checkPublicSchema); err != nil {
return err
}
typeNameWithPrefix := tree.MakeTypeNameWithPrefix(n.prefix.NamePrefix(), typeDesc.GetName())
arrayTypeNameWithPrefix := tree.MakeTypeNameWithPrefix(n.prefix.NamePrefix(), arrayDesc.GetName())
if err := p.setNewTypeOwner(ctx, typeDesc, arrayDesc, typeNameWithPrefix,
arrayTypeNameWithPrefix, newOwner); err != nil {
return err
}
// If the owner we want to set to is the current owner, do a no-op.
if newOwner == oldOwner {
return nil
}
if err := p.writeTypeSchemaChange(
ctx, typeDesc, tree.AsStringWithFQNames(n.n, p.Ann()),
); err != nil {
return err
}
return p.writeTypeSchemaChange(
ctx, arrayDesc, tree.AsStringWithFQNames(n.n, p.Ann()),
)
}
// setNewTypeOwner handles setting a new type owner.
// Called in ALTER TYPE and REASSIGN OWNED BY.
func (p *planner) setNewTypeOwner(
ctx context.Context,
typeDesc *typedesc.Mutable,
arrayTypeDesc *typedesc.Mutable,
typeName tree.TypeName,
arrayTypeName tree.TypeName,
newOwner security.SQLUsername,
) error {
privs := typeDesc.GetPrivileges()
privs.SetOwner(newOwner)
// Also have to change the owner of the implicit array type.
arrayTypeDesc.Privileges.SetOwner(newOwner)
if err := p.logEvent(ctx,
typeDesc.GetID(),
&eventpb.AlterTypeOwner{
TypeName: typeName.FQString(),
Owner: newOwner.Normalized(),
}); err != nil {
return err
}
return p.logEvent(ctx,
arrayTypeDesc.GetID(),
&eventpb.AlterTypeOwner{
TypeName: arrayTypeName.FQString(),
Owner: newOwner.Normalized(),
})
}
func (n *alterTypeNode) Next(params runParams) (bool, error) { return false, nil }
func (n *alterTypeNode) Values() tree.Datums { return tree.Datums{} }
func (n *alterTypeNode) Close(ctx context.Context) {}
func (n *alterTypeNode) ReadingOwnWrites() {}
func (p *planner) canModifyType(ctx context.Context, desc *typedesc.Mutable) error {
hasAdmin, err := p.HasAdminRole(ctx)
if err != nil {
return err
}
if hasAdmin {
return nil
}
hasOwnership, err := p.HasOwnership(ctx, desc)
if err != nil {
return err
}
if !hasOwnership {
return pgerror.Newf(pgcode.InsufficientPrivilege,
"must be owner of type %s", tree.Name(desc.GetName()))
}
return nil
}