-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
helpers.go
278 lines (252 loc) · 7.65 KB
/
helpers.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 2023 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 current
import (
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/rel"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scplan/internal/rules"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/screl"
"github.com/cockroachdb/errors"
)
const (
// rulesVersion version of elements that can be appended to rel rule names.
rulesVersion = "-23.1"
)
// rulesVersionKey version of elements used by this rule set.
var rulesVersionKey = clusterversion.V23_1
// descriptorIsNotBeingDropped creates a clause which leads to the outer clause
// failing to unify if the passed element is part of a descriptor and
// that descriptor is being dropped.
var descriptorIsNotBeingDropped = screl.Schema.DefNotJoin1(
"descriptorIsNotBeingDropped"+rulesVersion, "element", func(
element rel.Var,
) rel.Clauses {
descriptor := rules.MkNodeVars("descriptor")
return rel.Clauses{
descriptor.TypeFilter(rulesVersionKey, isDescriptor),
descriptor.JoinTarget(),
rules.JoinOnDescIDUntyped(descriptor.El, element, "id"),
descriptor.TargetStatus(scpb.ToAbsent),
}
},
)
// isDescriptor returns true for a descriptor-element, i.e. an element which
// owns its corresponding descriptor.
func isDescriptor(e scpb.Element) bool {
switch e.(type) {
case *scpb.Database, *scpb.Schema, *scpb.Table, *scpb.View, *scpb.Sequence,
*scpb.AliasType, *scpb.EnumType, *scpb.CompositeType, *scpb.Function:
return true
}
return false
}
// IsDescriptor returns true for a descriptor-element, i.e. an element which
// owns its corresponding descriptor. This is only used for exports
func IsDescriptor(e scpb.Element) bool {
return isDescriptor(e)
}
func isSubjectTo2VersionInvariant(e scpb.Element) bool {
// TODO(ajwerner): This should include constraints and enum values but it
// currently does not because we do not support dropping them unless we're
// dropping the descriptor and we do not support adding them.
return isIndex(e) || isColumn(e) || isNonIndexBackedComplexConstraint(e)
}
func isIndex(e scpb.Element) bool {
switch e.(type) {
case *scpb.PrimaryIndex, *scpb.SecondaryIndex, *scpb.TemporaryIndex:
return true
}
return false
}
func isColumn(e scpb.Element) bool {
_, ok := e.(*scpb.Column)
return ok
}
func isSimpleDependent(e scpb.Element) bool {
return !isDescriptor(e) && !isSubjectTo2VersionInvariant(e) && !isData(e)
}
func getTypeT(element scpb.Element) (*scpb.TypeT, error) {
switch e := element.(type) {
case *scpb.ColumnType:
if e == nil {
return nil, nil
}
return &e.TypeT, nil
case *scpb.AliasType:
if e == nil {
return nil, nil
}
return &e.TypeT, nil
}
return nil, errors.AssertionFailedf("element %T does not have an embedded scpb.TypeT", element)
}
func isWithTypeT(element scpb.Element) bool {
_, err := getTypeT(element)
return err == nil
}
func getExpression(element scpb.Element) (*scpb.Expression, error) {
switch e := element.(type) {
case *scpb.ColumnType:
if e == nil {
return nil, nil
}
return e.ComputeExpr, nil
case *scpb.ColumnDefaultExpression:
if e == nil {
return nil, nil
}
return &e.Expression, nil
case *scpb.ColumnOnUpdateExpression:
if e == nil {
return nil, nil
}
return &e.Expression, nil
case *scpb.SecondaryIndexPartial:
if e == nil {
return nil, nil
}
return &e.Expression, nil
case *scpb.CheckConstraint:
if e == nil {
return nil, nil
}
return &e.Expression, nil
case *scpb.CheckConstraintNotValid:
if e == nil {
return nil, nil
}
return &e.Expression, nil
case *scpb.FunctionParamDefaultExpression:
if e == nil {
return nil, nil
}
return &e.Expression, nil
}
return nil, errors.AssertionFailedf("element %T does not have an embedded scpb.Expression", element)
}
func isWithExpression(element scpb.Element) bool {
_, err := getExpression(element)
return err == nil
}
func isTypeDescriptor(element scpb.Element) bool {
switch element.(type) {
case *scpb.EnumType, *scpb.AliasType, *scpb.CompositeType:
return true
default:
return false
}
}
func isColumnDependent(e scpb.Element) bool {
switch e.(type) {
case *scpb.ColumnType, *scpb.ColumnNotNull:
return true
case *scpb.ColumnName, *scpb.ColumnComment, *scpb.IndexColumn:
return true
}
return isColumnTypeDependent(e)
}
func isColumnTypeDependent(e scpb.Element) bool {
switch e.(type) {
case *scpb.SequenceOwner, *scpb.ColumnDefaultExpression, *scpb.ColumnOnUpdateExpression:
return true
}
return false
}
func isIndexDependent(e scpb.Element) bool {
switch e.(type) {
case *scpb.IndexName, *scpb.IndexComment, *scpb.IndexColumn:
return true
case *scpb.IndexPartitioning, *scpb.SecondaryIndexPartial:
return true
}
return false
}
// CRDB supports five constraints of two categories:
// - PK, Unique (index-backed)
// - Check, UniqueWithoutIndex, FK (non-index-backed)
func isConstraint(e scpb.Element) bool {
return isIndex(e) || isNonIndexBackedConstraint(e)
}
// isNonIndexBackedConstraint returns true if `e` is a non-index-backed constraint.
func isNonIndexBackedConstraint(e scpb.Element) bool {
switch e.(type) {
case *scpb.CheckConstraint, *scpb.UniqueWithoutIndexConstraint, *scpb.ForeignKeyConstraint,
*scpb.ColumnNotNull:
return true
case *scpb.CheckConstraintNotValid, *scpb.UniqueWithoutIndexConstraintNotValid,
*scpb.ForeignKeyConstraintNotValid:
return true
}
return false
}
// isNonIndexBackedComplexConstraint returns true if `e` is a non-index-backed
// constraint that needs to transition through an intermediate state on its
// adding/dropping path.
//
// We call them "complex" constraint in contrast to simple constraint that
// does not have an intermediate state.
func isNonIndexBackedComplexConstraint(e scpb.Element) bool {
switch e.(type) {
case *scpb.CheckConstraint, *scpb.UniqueWithoutIndexConstraint, *scpb.ForeignKeyConstraint,
*scpb.ColumnNotNull:
return true
}
return false
}
// isNonIndexBackedCrossDescriptorConstraint returns true if `e` is a
// non-index-backed constraint and it can potentially reference another
// descriptor.
//
// This filter exists because in general we need to drop the constraint first
// before dropping referencing/referenced descriptor. Read rules that use
// this filter for more details.
//
// TODO (xiang): UniqueWithoutIndex and UniqueWithoutIndexNotValid should
// also be treated as cross-descriptor constraint because its partial predicate
// can references other descriptors.
func isNonIndexBackedCrossDescriptorConstraint(e scpb.Element) bool {
switch e.(type) {
case *scpb.CheckConstraint, *scpb.UniqueWithoutIndexConstraint,
*scpb.ForeignKeyConstraint:
return true
case *scpb.CheckConstraintNotValid, *scpb.UniqueWithoutIndexConstraintNotValid,
*scpb.ForeignKeyConstraintNotValid:
return true
}
return false
}
func isConstraintDependent(e scpb.Element) bool {
switch e.(type) {
case *scpb.ConstraintWithoutIndexName:
return true
case *scpb.ConstraintComment:
return true
}
return false
}
func isData(e scpb.Element) bool {
switch e.(type) {
case *scpb.DatabaseData:
return true
case *scpb.TableData:
return true
case *scpb.IndexData:
return true
}
return false
}
func isDescriptorParentReference(e scpb.Element) bool {
switch e.(type) {
case *scpb.ObjectParent, *scpb.SchemaParent:
return true
}
return false
}