-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
op_drop.go
278 lines (255 loc) · 7.8 KB
/
op_drop.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
// Copyright 2022 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 rules
import (
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/rel"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/screl"
)
// When dropping a table or a view, skip all removal ops for column elements
// and column-dependent elements (like column names).
// Columns get implicitly removed once the descriptor is marked as dropped.
//
// The column transition to ABSENT needs to remain to clean up back-references
// in types referred to by the column in its type or its computed expression.
//
// We can't skip ops for column-dependent elements which reference other
// descriptors, like for example default expressions, again because of the need
// to clean up back-references.
//
// We also can't skip ops for column-dependent elements which don't like in the
// descriptor, like column comments (which live in a dedicated system table).
func init() {
relation := mkNodeVars("relation")
column := mkNodeVars("column")
dep := mkNodeVars("column-dep")
relationID, columnID := rel.Var("relation-id"), rel.Var("column-id")
registerOpRule(
"skip column removal ops on relation drop",
column.node,
screl.MustQuery(
relation.el.Type(
(*scpb.Table)(nil),
(*scpb.View)(nil),
),
column.el.Type(
(*scpb.Column)(nil),
),
joinOnDescID(relation.el, column.el, relationID),
relation.joinTarget(),
toAbsent(relation.target, column.target),
column.joinTargetNode(),
column.node.AttrIn(screl.CurrentStatus,
// All but DELETE_ONLY which is the status leading to ABSENT.
scpb.Status_PUBLIC,
scpb.Status_WRITE_ONLY,
),
),
)
registerOpRule(
"skip column dependents removal ops on relation drop",
dep.node,
screl.MustQuery(
relation.el.Type(
(*scpb.Table)(nil),
(*scpb.View)(nil),
),
column.el.Type(
(*scpb.Column)(nil),
),
dep.el.Type(
(*scpb.ColumnName)(nil),
),
joinOnDescID(relation.el, column.el, relationID),
joinOnColumnID(column.el, dep.el, relationID, columnID),
relation.joinTarget(),
toAbsent(relation.target, column.target),
column.joinTarget(),
dep.joinTargetNode(),
dep.target.AttrEq(screl.TargetStatus, scpb.Status_ABSENT),
),
)
}
// When dropping a table or a view, skip all removal ops for index elements
// as well as elements which depend on them, provided there are no
// back-references that need to be cleaned up. This is similar as for columns.
func init() {
relation := mkNodeVars("relation")
index := mkNodeVars("index")
dep := mkNodeVars("index-dep")
relationID, indexID := rel.Var("relation-id"), rel.Var("index-id")
registerOpRule(
"skip index removal ops on relation drop",
index.node,
screl.MustQuery(
relation.el.Type(
(*scpb.Table)(nil),
(*scpb.View)(nil),
),
index.el.Type(
(*scpb.PrimaryIndex)(nil),
(*scpb.SecondaryIndex)(nil),
),
joinOnDescID(relation.el, index.el, relationID),
relation.joinTarget(),
index.joinTargetNode(),
toAbsent(relation.target, index.target),
),
)
registerOpRule(
"skip index dependents removal ops on relation drop",
dep.node,
screl.MustQuery(
relation.el.Type(
(*scpb.Table)(nil),
(*scpb.View)(nil),
),
index.el.Type(
(*scpb.PrimaryIndex)(nil),
(*scpb.SecondaryIndex)(nil),
),
dep.el.Type(
(*scpb.IndexName)(nil),
(*scpb.IndexPartitioning)(nil),
),
joinOnDescID(relation.el, index.el, relationID),
joinOnIndexID(index.el, dep.el, relationID, indexID),
relation.joinTarget(),
toAbsent(relation.target, index.target),
index.joinTarget(),
dep.joinTargetNode(),
dep.target.AttrEq(screl.TargetStatus, scpb.Status_ABSENT),
),
)
}
// When dropping a table or a view, skip all removal ops for constraint elements
// as well as elements which depend on them, provided there are no
// back-references that need to be cleaned up. This is similar as for columns
// and indexes.
func init() {
relation := mkNodeVars("relation")
constraint := mkNodeVars("constraint")
dep := mkNodeVars("constraint-dep")
relationID, constraintID := rel.Var("relation-id"), rel.Var("constraint-id")
registerOpRule(
"skip constraint removal ops on relation drop",
constraint.node,
screl.MustQuery(
relation.el.Type(
(*scpb.Table)(nil),
(*scpb.View)(nil),
),
constraint.el.Type(
(*scpb.UniqueWithoutIndexConstraint)(nil),
),
joinOnDescID(relation.el, constraint.el, relationID),
relation.joinTarget(),
toAbsent(relation.target, constraint.target),
constraint.joinTargetNode(),
),
)
registerOpRule(
"skip constraint dependents removal ops on relation drop",
dep.node,
screl.MustQuery(
relation.el.Type(
(*scpb.Table)(nil),
(*scpb.View)(nil),
),
constraint.el.Type(
(*scpb.UniqueWithoutIndexConstraint)(nil),
(*scpb.CheckConstraint)(nil),
(*scpb.ForeignKeyConstraint)(nil),
),
dep.el.Type(
(*scpb.ConstraintName)(nil),
),
joinOnDescID(relation.el, constraint.el, relationID),
joinOnConstraintID(constraint.el, dep.el, relationID, constraintID),
relation.joinTarget(),
constraint.joinTarget(),
toAbsent(relation.target, constraint.target),
dep.joinTargetNode(),
dep.target.AttrEq(screl.TargetStatus, scpb.Status_ABSENT),
),
)
}
// Skip all removal ops on elements which depend on a descriptor element that
// is being removed as well. These elements get implicitly removed once the
// descriptor is marked as dropped.
// This rule excludes:
// - index and column elements, which were handled above,
// - elements which don't live inside the descriptor, like comments,
// - elements which have forward references to other descriptors: back-
// references need to be cleaned up.
func init() {
desc := mkNodeVars("desc")
dep := mkNodeVars("dep")
descID := rel.Var("desc-id")
registerOpRule(
"skip element removal ops on descriptor drop",
dep.node,
screl.MustQuery(
desc.el.Type(
(*scpb.Database)(nil),
(*scpb.Schema)(nil),
(*scpb.Table)(nil),
(*scpb.View)(nil),
(*scpb.Sequence)(nil),
(*scpb.AliasType)(nil),
(*scpb.EnumType)(nil),
),
dep.el.Type(
(*scpb.ColumnFamily)(nil),
(*scpb.Owner)(nil),
(*scpb.UserPrivileges)(nil),
),
joinOnDescID(desc.el, dep.el, descID),
desc.joinTarget(),
toAbsent(desc.target, dep.target),
dep.joinTargetNode(),
),
)
}
// Skip all removal ops for dropping table comments corresponding to elements
// when dropping the table itself.
func init() {
desc := mkNodeVars("desc")
dep := mkNodeVars("dep")
descID := rel.Var("desc-id")
registerOpRule(
"skip table comment removal ops on descriptor drop",
dep.node,
screl.MustQuery(
desc.el.Type(
(*scpb.Table)(nil),
(*scpb.View)(nil),
(*scpb.Sequence)(nil),
),
dep.el.Type(
(*scpb.ColumnComment)(nil),
(*scpb.IndexComment)(nil),
(*scpb.ConstraintComment)(nil),
(*scpb.TableComment)(nil),
),
joinOnDescID(desc.el, dep.el, descID),
desc.joinTarget(),
toAbsent(desc.target, dep.target),
dep.joinTargetNode(),
),
)
}
// TODO(fqazi): For create operations we will need to have the ability
// to have transformations that will combine transitions into a single
// stage for execution. For example, a newly CREATE TABLE will be represented
// by a TABLE, COLUMN, and INDEX elements (among others), all of the operations
// for these elements are executable in a single stage. Having them execute
// across multiple stages would be problematic both from a validation and
// correctness viewpoint.