-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
alter_table_locality.go
291 lines (260 loc) · 8.91 KB
/
alter_table_locality.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
// 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/sql/catalog/catalogkv"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
"github.com/cockroachdb/errors"
)
type alterTableSetLocalityNode struct {
n tree.AlterTableLocality
tableDesc *tabledesc.Mutable
}
// AlterTableLocality transforms a tree.AlterTableLocality into a plan node.
func (p *planner) AlterTableLocality(
ctx context.Context, n *tree.AlterTableLocality,
) (planNode, error) {
if err := checkSchemaChangeEnabled(
ctx,
p.ExecCfg(),
"ALTER TABLE",
); err != nil {
return nil, err
}
tableDesc, err := p.ResolveMutableTableDescriptorEx(
ctx, n.Name, !n.IfExists, tree.ResolveRequireTableDesc,
)
if err != nil {
return nil, err
}
if tableDesc == nil {
return newZeroNode(nil /* columns */), nil
}
// This check for CREATE privilege is kept for backwards compatibility.
if err := p.CheckPrivilege(ctx, tableDesc, privilege.CREATE); err != nil {
return nil, pgerror.Newf(pgcode.InsufficientPrivilege,
"must be owner of table %s or have CREATE privilege on table %s",
tree.Name(tableDesc.GetName()), tree.Name(tableDesc.GetName()))
}
return &alterTableSetLocalityNode{
n: *n,
tableDesc: tableDesc,
}, nil
}
func (n *alterTableSetLocalityNode) Next(runParams) (bool, error) { return false, nil }
func (n *alterTableSetLocalityNode) Values() tree.Datums { return tree.Datums{} }
func (n *alterTableSetLocalityNode) Close(context.Context) {}
func (n *alterTableSetLocalityNode) alterTableLocalityGlobalToRegionalByTable(
params runParams, dbDesc *dbdesc.Immutable,
) error {
if !n.tableDesc.IsLocalityGlobal() {
f := tree.NewFmtCtx(tree.FmtSimple)
if err := tabledesc.FormatTableLocalityConfig(n.tableDesc.LocalityConfig, f); err != nil {
// While we're in an error path and generally it's bad to return a
// different error in an error path, we will only get an error here if the
// locality is corrupted, in which case, it's probably the right error
// to return.
return err
}
return errors.AssertionFailedf(
"invalid call %q on incorrect table locality %s",
"alter table locality GLOBAL to REGIONAL BY TABLE",
f.String(),
)
}
n.tableDesc.SetTableLocalityRegionalByTable(n.n.Locality.TableRegion)
// Finalize the alter by writing a new table descriptor and updating the zone
// configuration.
if err := n.validateAndWriteNewTableLocalityAndZoneConfig(
params,
dbDesc,
); err != nil {
return err
}
return nil
}
func (n *alterTableSetLocalityNode) alterTableLocalityRegionalByTableToGlobal(
params runParams, dbDesc *dbdesc.Immutable,
) error {
const operation string = "alter table locality REGIONAL BY TABLE to GLOBAL"
if !n.tableDesc.IsLocalityRegionalByTable() {
return errors.AssertionFailedf(
"invalid call %q on incorrect table locality. %v",
operation,
n.tableDesc.LocalityConfig,
)
}
n.tableDesc.SetTableLocalityGlobal()
// Finalize the alter by writing a new table descriptor and updating the zone
// configuration.
if err := n.validateAndWriteNewTableLocalityAndZoneConfig(
params,
dbDesc,
); err != nil {
return err
}
return nil
}
func (n *alterTableSetLocalityNode) alterTableLocalityRegionalByTableToRegionalByTable(
params runParams, dbDesc *dbdesc.Immutable,
) error {
const operation string = "alter table locality REGIONAL BY TABLE to REGIONAL BY TABLE"
if !n.tableDesc.IsLocalityRegionalByTable() {
return errors.AssertionFailedf(
"invalid call %q on incorrect table locality. %v",
operation,
n.tableDesc.LocalityConfig,
)
}
n.tableDesc.SetTableLocalityRegionalByTable(n.n.Locality.TableRegion)
// Finalize the alter by writing a new table descriptor and updating the zone configuration.
if err := n.validateAndWriteNewTableLocalityAndZoneConfig(
params,
dbDesc,
); err != nil {
return err
}
return nil
}
func (n *alterTableSetLocalityNode) startExec(params runParams) error {
// Ensure that the database is multi-region enabled.
dbDesc, err := params.p.Descriptors().GetImmutableDatabaseByID(
params.ctx,
params.p.txn,
n.tableDesc.GetParentID(),
tree.DatabaseLookupFlags{},
)
if err != nil {
return err
}
if !dbDesc.IsMultiRegion() {
return pgerror.Newf(
pgcode.InvalidTableDefinition,
"cannot alter a table's LOCALITY if its database is not multi-region enabled",
)
}
newLocality := n.n.Locality
existingLocality := n.tableDesc.LocalityConfig
// Look at the existing locality, and implement any changes required to move to
// the new locality.
switch existingLocality.Locality.(type) {
case *descpb.TableDescriptor_LocalityConfig_Global_:
switch newLocality.LocalityLevel {
case tree.LocalityLevelGlobal:
// GLOBAL to GLOBAL - no op.
return nil
case tree.LocalityLevelRow:
// GLOBAL to REGIONAL BY ROW
return unimplemented.New("alter table locality to REGIONAL BY ROW", "implementation pending")
case tree.LocalityLevelTable:
if err = n.alterTableLocalityGlobalToRegionalByTable(params, dbDesc); err != nil {
return err
}
default:
return errors.AssertionFailedf("unknown table locality: %v", newLocality)
}
case *descpb.TableDescriptor_LocalityConfig_RegionalByTable_:
switch newLocality.LocalityLevel {
case tree.LocalityLevelGlobal:
err = n.alterTableLocalityRegionalByTableToGlobal(params, dbDesc)
if err != nil {
return err
}
case tree.LocalityLevelRow:
return unimplemented.New("alter table locality to REGIONAL BY ROW", "implementation pending")
case tree.LocalityLevelTable:
err = n.alterTableLocalityRegionalByTableToRegionalByTable(params, dbDesc)
if err != nil {
return err
}
default:
return errors.AssertionFailedf("unknown table locality: %v", newLocality)
}
case *descpb.TableDescriptor_LocalityConfig_RegionalByRow_:
switch newLocality.LocalityLevel {
case tree.LocalityLevelGlobal:
return unimplemented.New("alter table locality from REGIONAL BY ROW", "implementation pending")
case tree.LocalityLevelRow:
// Altering to same table locality pattern. We're done.
return unimplemented.New("alter table locality from REGIONAL BY ROW", "implementation pending")
case tree.LocalityLevelTable:
return unimplemented.New("alter table locality from REGIONAL BY ROW", "implementation pending")
default:
return errors.AssertionFailedf("unknown table locality: %v", newLocality)
}
default:
return errors.AssertionFailedf("unknown table locality: %v", existingLocality)
}
// 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 params.p.logEvent(params.ctx,
n.tableDesc.ID,
&eventpb.AlterTable{
TableName: n.n.Name.String(),
})
}
// validateAndWriteNewTableLocalityAndZoneConfig validates the newly updated
// LocalityConfig in a table descriptor, writes that table descriptor, and
// writes a new zone configuration for the given table.
func (n *alterTableSetLocalityNode) validateAndWriteNewTableLocalityAndZoneConfig(
params runParams, dbDesc *dbdesc.Immutable,
) error {
// Get the fully resolved table name for use in the calls below.
resolvedSchema, err := params.p.Descriptors().GetImmutableSchemaByID(
params.ctx,
params.p.txn,
n.tableDesc.GetParentSchemaID(),
tree.SchemaLookupFlags{})
if err != nil {
return err
}
tableName := tree.MakeTableNameWithSchema(
tree.Name(dbDesc.Name),
tree.Name(resolvedSchema.Name),
tree.Name(n.tableDesc.GetName()),
)
// Validate the new locality before updating the table descriptor.
dg := catalogkv.NewOneLevelUncachedDescGetter(params.p.txn, params.EvalContext().Codec)
if err := n.tableDesc.ValidateTableLocalityConfig(
params.ctx,
dg,
); err != nil {
return err
}
// Write out the table descriptor update.
if err := params.p.writeSchemaChange(
params.ctx,
n.tableDesc,
descpb.InvalidMutationID,
tree.AsStringWithFQNames(&n.n, params.Ann()),
); err != nil {
return err
}
// Update the zone configuration.
if err := params.p.applyZoneConfigFromTableLocalityConfig(
params.ctx,
tableName,
n.tableDesc.TableDesc(),
*dbDesc.RegionConfig,
); err != nil {
return err
}
return nil
}