forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alter_table.go
196 lines (175 loc) · 5.95 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
// 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 (
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/cockroachdb/cockroach/sql/privilege"
)
// 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, *roachpb.Error) {
if err := n.Table.NormalizeTableName(p.session.Database); err != nil {
return nil, roachpb.NewError(err)
}
dbDesc, err := p.getDatabaseDesc(n.Table.Database())
if err != nil {
return nil, err
}
// Check if table exists.
tbKey := tableKey{dbDesc.ID, n.Table.Table()}.Key()
gr, pErr := p.txn.Get(tbKey)
if pErr != nil {
return nil, pErr
}
if !gr.Exists() {
if n.IfExists {
// Noop.
return &valuesNode{}, nil
}
// Key does not exist, but we want it to: error out.
return nil, roachpb.NewUErrorf("table %q does not exist", n.Table.Table())
}
tableDesc, err := p.getTableDesc(n.Table)
if err != nil {
return nil, err
}
if err := p.checkPrivilege(tableDesc, privilege.CREATE); err != nil {
return nil, err
}
numMutations := len(tableDesc.Mutations)
for _, cmd := range n.Cmds {
switch t := cmd.(type) {
case *parser.AlterTableAddColumn:
d := t.ColumnDef
col, idx, err := makeColumnDefDescs(d)
if err != nil {
return nil, err
}
status, i, err := tableDesc.FindColumnByName(col.Name)
if err == nil {
if status == DescriptorIncomplete && tableDesc.Mutations[i].Direction == DescriptorMutation_DROP {
return nil, roachpb.NewUErrorf("column %q being dropped, try again later", col.Name)
}
}
tableDesc.addColumnMutation(*col, DescriptorMutation_ADD)
if idx != nil {
tableDesc.addIndexMutation(*idx, DescriptorMutation_ADD)
}
case *parser.AlterTableAddConstraint:
switch d := t.ConstraintDef.(type) {
case *parser.UniqueConstraintTableDef:
if d.PrimaryKey {
return nil, roachpb.NewUErrorf("multiple primary keys for table %q are not allowed", tableDesc.Name)
}
name := string(d.Name)
idx := IndexDescriptor{
Name: name,
Unique: true,
StoreColumnNames: d.Storing,
}
if err := idx.fillColumns(d.Columns); err != nil {
return nil, err
}
status, i, err := tableDesc.FindIndexByName(name)
if err == nil {
if status == DescriptorIncomplete && tableDesc.Mutations[i].Direction == DescriptorMutation_DROP {
return nil, roachpb.NewUErrorf("index %q being dropped, try again later", name)
}
}
tableDesc.addIndexMutation(idx, DescriptorMutation_ADD)
default:
return nil, roachpb.NewErrorf("unsupported constraint: %T", t.ConstraintDef)
}
case *parser.AlterTableDropColumn:
status, i, err := tableDesc.FindColumnByName(t.Column)
if err != nil {
if t.IfExists {
// Noop.
continue
}
return nil, err
}
switch status {
case DescriptorActive:
col := tableDesc.Columns[i]
if tableDesc.PrimaryIndex.containsColumnID(col.ID) {
return nil, roachpb.NewUErrorf("column %q is referenced by the primary key", col.Name)
}
for _, idx := range tableDesc.allNonDropIndexes() {
if idx.containsColumnID(col.ID) {
return nil, roachpb.NewUErrorf("column %q is referenced by existing index %q", col.Name, idx.Name)
}
}
tableDesc.addColumnMutation(col, DescriptorMutation_DROP)
tableDesc.Columns = append(tableDesc.Columns[:i], tableDesc.Columns[i+1:]...)
case DescriptorIncomplete:
switch tableDesc.Mutations[i].Direction {
case DescriptorMutation_ADD:
return nil, roachpb.NewUErrorf("column %q in the middle of being added, try again later", t.Column)
case DescriptorMutation_DROP:
// Noop.
}
}
case *parser.AlterTableDropConstraint:
status, i, err := tableDesc.FindIndexByName(t.Constraint)
if err != nil {
if t.IfExists {
// Noop.
continue
}
return nil, err
}
switch status {
case DescriptorActive:
tableDesc.addIndexMutation(tableDesc.Indexes[i], DescriptorMutation_DROP)
tableDesc.Indexes = append(tableDesc.Indexes[:i], tableDesc.Indexes[i+1:]...)
case DescriptorIncomplete:
switch tableDesc.Mutations[i].Direction {
case DescriptorMutation_ADD:
return nil, roachpb.NewUErrorf("constraint %q in the middle of being added, try again later", t.Constraint)
case DescriptorMutation_DROP:
// Noop.
}
}
default:
return nil, roachpb.NewErrorf("unsupported alter cmd: %T", cmd)
}
}
// Were some mutations added?
//
// 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.
if numMutations == len(tableDesc.Mutations) {
return &valuesNode{}, nil
}
tableDesc.UpVersion = true
mutationID := tableDesc.NextMutationID
tableDesc.NextMutationID++
if err := tableDesc.AllocateIDs(); err != nil {
return nil, err
}
if pErr := p.txn.Put(MakeDescMetadataKey(tableDesc.GetID()), wrapDescriptor(tableDesc)); err != nil {
return nil, pErr
}
p.notifySchemaChange(tableDesc.ID, mutationID)
return &valuesNode{}, nil
}