forked from cockroachdb/cockroach
/
alter_table.go
293 lines (262 loc) · 8.91 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
// 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.
//
// Author: Tamir Duberstein (tamird@gmail.com)
package sql
import (
"fmt"
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/cockroachdb/cockroach/sql/privilege"
"github.com/cockroachdb/cockroach/sql/sqlbase"
)
type alterTableNode struct {
p *planner
n *parser.AlterTable
tableDesc *sqlbase.TableDescriptor
}
// 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(n *parser.AlterTable) (planNode, error) {
tableDesc, err := p.getTableDesc(n.Table)
if err != nil {
return nil, err
}
if tableDesc == nil {
if n.IfExists {
return &emptyNode{}, nil
}
return nil, sqlbase.NewUndefinedTableError(n.Table.String())
}
if err := p.checkPrivilege(tableDesc, privilege.CREATE); err != nil {
return nil, err
}
return &alterTableNode{n: n, p: p, tableDesc: tableDesc}, nil
}
func (n *alterTableNode) expandPlan() error {
return nil
}
func (n *alterTableNode) Start() 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)
for _, cmd := range n.n.Cmds {
switch t := cmd.(type) {
case *parser.AlterTableAddColumn:
d := t.ColumnDef
col, idx, err := sqlbase.MakeColumnDefDescs(d)
if err != nil {
return err
}
status, i, err := n.tableDesc.FindColumnByName(col.Name)
if err == nil {
if status == sqlbase.DescriptorIncomplete &&
n.tableDesc.Mutations[i].Direction == sqlbase.DescriptorMutation_DROP {
return fmt.Errorf("column %q being dropped, try again later", col.Name)
}
if status == sqlbase.DescriptorActive && t.IfNotExists {
continue
}
}
n.tableDesc.AddColumnMutation(*col, sqlbase.DescriptorMutation_ADD)
if idx != nil {
n.tableDesc.AddIndexMutation(*idx, sqlbase.DescriptorMutation_ADD)
}
if t.ColumnDef.Family.Create || len(t.ColumnDef.Family.Name) > 0 {
err := n.tableDesc.AddColumnToFamilyMaybeCreate(
col.Name, string(t.ColumnDef.Family.Name), t.ColumnDef.Family.Create,
t.ColumnDef.Family.IfNotExists)
if err != nil {
return err
}
}
case *parser.AlterTableAddConstraint:
switch d := t.ConstraintDef.(type) {
case *parser.UniqueConstraintTableDef:
if d.PrimaryKey {
return fmt.Errorf("multiple primary keys for table %q are not allowed", n.tableDesc.Name)
}
name := string(d.Name)
idx := sqlbase.IndexDescriptor{
Name: name,
Unique: true,
StoreColumnNames: d.Storing,
}
if err := idx.FillColumns(d.Columns); err != nil {
return err
}
status, i, err := n.tableDesc.FindIndexByName(name)
if err == nil {
if status == sqlbase.DescriptorIncomplete &&
n.tableDesc.Mutations[i].Direction == sqlbase.DescriptorMutation_DROP {
return fmt.Errorf("index %q being dropped, try again later", name)
}
}
n.tableDesc.AddIndexMutation(idx, sqlbase.DescriptorMutation_ADD)
default:
return fmt.Errorf("unsupported constraint: %T", t.ConstraintDef)
}
case *parser.AlterTableDropColumn:
status, i, err := n.tableDesc.FindColumnByName(t.Column)
if err != nil {
if t.IfExists {
// Noop.
continue
}
return err
}
switch status {
case sqlbase.DescriptorActive:
col := n.tableDesc.Columns[i]
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() {
if idx.ContainsColumnID(col.ID) {
return fmt.Errorf("column %q is referenced by existing index %q", col.Name, idx.Name)
}
}
n.tableDesc.AddColumnMutation(col, sqlbase.DescriptorMutation_DROP)
n.tableDesc.Columns = append(n.tableDesc.Columns[:i], n.tableDesc.Columns[i+1:]...)
case sqlbase.DescriptorIncomplete:
switch n.tableDesc.Mutations[i].Direction {
case sqlbase.DescriptorMutation_ADD:
return fmt.Errorf("column %q in the middle of being added, try again later", t.Column)
case sqlbase.DescriptorMutation_DROP:
// Noop.
}
}
case *parser.AlterTableDropConstraint:
status, i, err := n.tableDesc.FindIndexByName(t.Constraint)
if err != nil {
if t.IfExists {
// Noop.
continue
}
return err
}
switch status {
case sqlbase.DescriptorActive:
n.tableDesc.AddIndexMutation(n.tableDesc.Indexes[i], sqlbase.DescriptorMutation_DROP)
n.tableDesc.Indexes = append(n.tableDesc.Indexes[:i], n.tableDesc.Indexes[i+1:]...)
case sqlbase.DescriptorIncomplete:
switch n.tableDesc.Mutations[i].Direction {
case sqlbase.DescriptorMutation_ADD:
return fmt.Errorf("constraint %q in the middle of being added, try again later", t.Constraint)
case sqlbase.DescriptorMutation_DROP:
// Noop.
}
}
case parser.ColumnMutationCmd:
// Column mutations
status, i, err := n.tableDesc.FindColumnByName(t.GetColumn())
if err != nil {
return err
}
switch status {
case sqlbase.DescriptorActive:
if err := applyColumnMutation(&n.tableDesc.Columns[i], t); err != nil {
return err
}
descriptorChanged = true
case sqlbase.DescriptorIncomplete:
switch n.tableDesc.Mutations[i].Direction {
case sqlbase.DescriptorMutation_ADD:
return fmt.Errorf("column %q in the middle of being added, try again later", t.GetColumn())
case sqlbase.DescriptorMutation_DROP:
return fmt.Errorf("column %q in the middle of being dropped", t.GetColumn())
}
}
default:
return fmt.Errorf("unsupported alter cmd: %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
}
mutationID := sqlbase.InvalidMutationID
var err error
if addedMutations {
mutationID, err = n.tableDesc.FinalizeMutation()
} else {
err = n.tableDesc.SetUpVersion()
}
if err != nil {
return err
}
if err := n.tableDesc.AllocateIDs(); err != nil {
return err
}
if err := n.p.writeTableDesc(n.tableDesc); 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.
if err := MakeEventLogger(n.p.leaseMgr).InsertEventRecord(n.p.txn,
EventLogAlterTable,
int32(n.tableDesc.ID),
int32(n.p.evalCtx.NodeID),
struct {
TableName string
Statement string
User string
MutationID uint32
}{n.tableDesc.Name, n.n.String(), n.p.session.User, uint32(mutationID)},
); err != nil {
return err
}
n.p.notifySchemaChange(n.tableDesc.ID, mutationID)
return nil
}
func (n *alterTableNode) Next() (bool, error) { return false, nil }
func (n *alterTableNode) Columns() []ResultColumn { return make([]ResultColumn, 0) }
func (n *alterTableNode) Ordering() orderingInfo { return orderingInfo{} }
func (n *alterTableNode) Values() parser.DTuple { return parser.DTuple{} }
func (n *alterTableNode) DebugValues() debugValues { return debugValues{} }
func (n *alterTableNode) ExplainTypes(_ func(string, string)) {}
func (n *alterTableNode) SetLimitHint(_ int64, _ bool) {}
func (n *alterTableNode) MarkDebug(mode explainMode) {}
func (n *alterTableNode) ExplainPlan(v bool) (string, string, []planNode) {
return "alter table", "", nil
}
func applyColumnMutation(col *sqlbase.ColumnDescriptor, mut parser.ColumnMutationCmd) error {
switch t := mut.(type) {
case *parser.AlterTableSetDefault:
if t.Default == nil {
col.DefaultExpr = nil
} else {
colDatumType := col.Type.ToDatumType()
if err := sqlbase.SanitizeVarFreeExpr(t.Default, colDatumType, "DEFAULT"); err != nil {
return err
}
s := t.Default.String()
col.DefaultExpr = &s
}
case *parser.AlterTableDropNotNull:
col.Nullable = true
}
return nil
}