-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathsequence.go
331 lines (304 loc) · 10.2 KB
/
sequence.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// 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 seqexpr provides functionality to find usages of sequences in
// expressions.
//
// The logic here would fit nicely into schemaexpr if it weren't for the
// dependency on builtins, which itself depends on schemaexpr.
package seqexpr
import (
"go/constant"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/builtins"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/errors"
)
// SeqIdentifier wraps together different ways of identifying a sequence.
// The sequence can either be identified via either its name, or its ID.
type SeqIdentifier struct {
SeqName string
SeqID int64
}
// IsByID indicates whether the SeqIdentifier is identifying
// the sequence by its ID or by its name.
func (si *SeqIdentifier) IsByID() bool {
return len(si.SeqName) == 0
}
// GetSequenceFromFunc extracts a sequence identifier from a FuncExpr if the function
// takes a sequence identifier as an arg (a sequence identifier can either be
// a sequence name or an ID), wrapped in the SeqIdentifier type.
// Returns the identifier of the sequence or nil if no sequence was found.
func GetSequenceFromFunc(funcExpr *tree.FuncExpr) (*SeqIdentifier, error) {
// Resolve doesn't use the searchPath for resolving FunctionDefinitions
// so we can pass in an empty SearchPath.
def, err := funcExpr.Func.Resolve(tree.EmptySearchPath)
if err != nil {
return nil, err
}
fnProps, overloads := builtins.GetBuiltinProperties(def.Name)
if fnProps != nil && fnProps.HasSequenceArguments {
found := false
for _, overload := range overloads {
// Find the overload that matches funcExpr.
if len(funcExpr.Exprs) == overload.Types.Length() {
found = true
argTypes, ok := overload.Types.(tree.ArgTypes)
if !ok {
panic(pgerror.Newf(
pgcode.InvalidFunctionDefinition,
"%s has invalid argument types", funcExpr.Func.String(),
))
}
for i := 0; i < overload.Types.Length(); i++ {
// Find the sequence name arg.
argName := argTypes[i].Name
if argName == builtins.SequenceNameArg {
arg := funcExpr.Exprs[i]
if seqIdentifier := getSequenceIdentifier(arg); seqIdentifier != nil {
return seqIdentifier, nil
}
}
}
}
}
if !found {
panic(pgerror.New(
pgcode.DatatypeMismatch,
"could not find matching function overload for given arguments",
))
}
}
return nil, nil
}
// getSequenceIdentifier takes a tree.Expr and extracts the
// sequence identifier (either its name or its ID) if it exists.
func getSequenceIdentifier(expr tree.Expr) *SeqIdentifier {
switch a := expr.(type) {
case *tree.DString:
seqName := string(*a)
return &SeqIdentifier{
SeqName: seqName,
}
case *tree.DOid:
id := int64(a.DInt)
return &SeqIdentifier{
SeqID: id,
}
case *tree.StrVal:
seqName := a.RawString()
return &SeqIdentifier{
SeqName: seqName,
}
case *tree.NumVal:
id, err := a.AsInt64()
if err == nil {
return &SeqIdentifier{
SeqID: id,
}
}
case *tree.CastExpr:
return getSequenceIdentifier(a.Expr)
case *tree.AnnotateTypeExpr:
return getSequenceIdentifier(a.Expr)
}
return nil
}
// GetUsedSequences returns the identifier of the sequence passed to
// a call to sequence function in the given expression or nil if no sequence
// identifiers are found. The identifier is wrapped in a SeqIdentifier.
// e.g. nextval('foo') => "foo"; nextval(123::regclass) => 123; <some other expression> => nil
func GetUsedSequences(defaultExpr tree.Expr) ([]SeqIdentifier, error) {
var seqIdentifiers []SeqIdentifier
_, err := tree.SimpleVisit(
defaultExpr,
func(expr tree.Expr) (recurse bool, newExpr tree.Expr, err error) {
switch t := expr.(type) {
case *tree.FuncExpr:
identifier, err := GetSequenceFromFunc(t)
if err != nil {
return false, nil, err
}
if identifier != nil {
seqIdentifiers = append(seqIdentifiers, *identifier)
}
}
return true, expr, nil
},
)
if err != nil {
return nil, err
}
return seqIdentifiers, nil
}
// ReplaceSequenceNamesWithIDs walks the given expression, and replaces
// any sequence names in the expression by their IDs instead.
// e.g. nextval('foo') => nextval(123::regclass)
func ReplaceSequenceNamesWithIDs(
defaultExpr tree.Expr, nameToID map[string]int64,
) (tree.Expr, error) {
replaceFn := func(expr tree.Expr) (recurse bool, newExpr tree.Expr, err error) {
switch t := expr.(type) {
case *tree.FuncExpr:
identifier, err := GetSequenceFromFunc(t)
if err != nil {
return false, nil, err
}
if identifier == nil || identifier.IsByID() {
return true, expr, nil
}
id, ok := nameToID[identifier.SeqName]
if !ok {
return true, expr, nil
}
return false, &tree.FuncExpr{
Func: t.Func,
Exprs: tree.Exprs{
&tree.AnnotateTypeExpr{
Type: types.RegClass,
SyntaxMode: tree.AnnotateShort,
Expr: tree.NewNumVal(constant.MakeInt64(id), "", false),
},
},
}, nil
}
return true, expr, nil
}
newExpr, err := tree.SimpleVisit(defaultExpr, replaceFn)
return newExpr, err
}
// UpgradeSequenceReferenceInExpr upgrades all by-name reference in `expr` to by-ID.
// The name to ID resolution logic is aided by injecting a set of sequence names
// (`allUsedSeqNames`). This set is expected to contain names of all sequences used
// in `expr`, so all we need to do is to match each by-name seq reference in `expr`
// to one entry in `allUsedSeqNames`.
func UpgradeSequenceReferenceInExpr(
expr *string, allUsedSeqNames map[descpb.ID]*tree.TableName,
) (hasUpgraded bool, err error) {
// Find all sequence references in `expr`.
parsedExpr, err := parser.ParseExpr(*expr)
if err != nil {
return hasUpgraded, err
}
seqRefs, err := GetUsedSequences(parsedExpr)
if err != nil {
return hasUpgraded, err
}
// Construct the key mapping from seq-by-name-reference to their IDs.
seqByNameRefToID := make(map[string]int64)
for _, seqIdentifier := range seqRefs {
if seqIdentifier.IsByID() {
continue
}
parsedSeqName, err := parser.ParseTableName(seqIdentifier.SeqName)
if err != nil {
return hasUpgraded, err
}
seqByNameRefInTableName := parsedSeqName.ToTableName()
// Pairing: find out which sequence name in `allUsedSeqNames` matches
// `seqByNameRefInTableName` so we know the ID of this seq identifier.
idOfSeqIdentifier, err := findUniqueBestMatchingForTableName(allUsedSeqNames, seqByNameRefInTableName)
if err != nil {
return hasUpgraded, err
}
seqByNameRefToID[seqIdentifier.SeqName] = int64(idOfSeqIdentifier)
}
// With this name-to-ID mapping, we can upgrade `expr`.
newExpr, err := ReplaceSequenceNamesWithIDs(parsedExpr, seqByNameRefToID)
if err != nil {
return hasUpgraded, err
}
// Modify `expr` in place, if any upgrade.
if *expr != tree.Serialize(newExpr) {
hasUpgraded = true
*expr = tree.Serialize(newExpr)
}
return hasUpgraded, nil
}
// findUniqueBestMatchingForTableName picks the "best-matching" name from `allTableNamesByID` for `tableName`.
// The best-matching name is the one that matches all parts of `tableName`.
//
// Example 1:
// allTableNamesByID = {23 : 'db.sc1.t', 25 : 'db.sc2.t'}
// tableName = 'sc2.t'
// return = 25 (because `db.sc2.t` best-matches `sc2.t`)
// Example 2:
// allTableNamesByID = {23 : 'db.sc1.t', 25 : 'sc2.t'}
// tableName = 'sc2.t'
// return = 25 (because `sc2.t` best-matches `sc2.t`)
//
// It returns a non-nill error if `tableName` does not uniquely match a name in `allTableNamesByID`.
//
// Example 3:
// allTableNamesByID = {23 : 'sc1.t', 25 : 'sc2.t'}
// tableName = 't'
// return = non-nil error (because both 'sc1.t' and 'sc2.t' are equally good matches
// for 't' and we cannot decide, i.e., >1 valid candidates left.)
// Example 4:
// allTableNamesByID = {23 : 'sc1.t', 25 : 'sc2.t'}
// tableName = 't2'
// return = non-nil error (because neither 'sc1.t' nor 'sc2.t' matches 't2', that is, 0 valid candidate left)
func findUniqueBestMatchingForTableName(
allTableNamesByID map[descpb.ID]*tree.TableName, targetTableName tree.TableName,
) (descpb.ID, error) {
candidates := make(map[descpb.ID]*tree.TableName)
// Get all candidates whose table name is equal to `t`.
t := targetTableName.Table()
if t == "" {
return descpb.InvalidID, errors.AssertionFailedf("input tableName does not have a Table field.")
}
for id, tableName := range allTableNamesByID {
if tableName.Table() == t {
candidates[id] = tableName
}
}
if len(candidates) == 0 {
return descpb.InvalidID, errors.AssertionFailedf("no table name found to match input %v", t)
}
// Eliminate candidates whose schema is not equal to `sc`.
sc := targetTableName.Schema()
if sc != "" {
for id, candidateTableName := range candidates {
if candidateTableName.Schema() != sc {
delete(candidates, id)
}
}
}
// Eliminate candidates whose catalog is not equal to `db`.
db := targetTableName.Catalog()
if db != "" {
for id, candidateTableName := range candidates {
if candidateTableName.Catalog() != db {
delete(candidates, id)
}
}
}
// There should be only one candidate left; Return errors accordingly if not.
if len(candidates) == 0 {
return descpb.InvalidID, errors.AssertionFailedf("no table name found to match input %v", t)
}
if len(candidates) > 1 {
candidateTableNames := make([]string, 0)
for _, candidateTableName := range candidates {
candidateTableNames = append(candidateTableNames, candidateTableName.String())
}
return descpb.InvalidID, errors.AssertionFailedf("more than 1 matches found for %v: %v",
targetTableName.String(), candidateTableNames)
}
// Get that only one candidate and return.
var result descpb.ID
for id := range candidates {
result = id
}
return result, nil
}