-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
column.go
400 lines (362 loc) · 12 KB
/
column.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// 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 schemaexpr
import (
"context"
"fmt"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"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/catid"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/types"
)
// DequalifyColumnRefs returns a serialized expression with database and table
// names stripped from qualified column names.
//
// For example:
//
// tab.a > 0 AND db.tab.b = 'foo'
// =>
// a > 0 AND b = 'foo'
//
// This dequalification is necessary when CHECK constraints, computed columns,
// or partial index predicates are created. If the table name was not stripped,
// these expressions would become invalid if the table is renamed.
func DequalifyColumnRefs(
ctx context.Context, source *colinfo.DataSourceInfo, expr tree.Expr,
) (string, error) {
e, err := dequalifyColumnRefs(ctx, source, expr)
if err != nil {
return "", err
}
return tree.Serialize(e), nil
}
// dequalifyColumnRefs returns an expression with database and table names
// stripped from qualified column names.
func dequalifyColumnRefs(
ctx context.Context, source *colinfo.DataSourceInfo, expr tree.Expr,
) (tree.Expr, error) {
resolver := colinfo.ColumnResolver{Source: source}
return tree.SimpleVisit(
expr,
func(expr tree.Expr) (recurse bool, newExpr tree.Expr, err error) {
if vBase, ok := expr.(tree.VarName); ok {
v, err := vBase.NormalizeVarName()
if err != nil {
return false, nil, err
}
if c, ok := v.(*tree.ColumnItem); ok {
_, err := colinfo.ResolveColumnItem(ctx, &resolver, c)
if err != nil {
return false, nil, err
}
colIdx := resolver.ResolverState.ColIdx
col := source.SourceColumns[colIdx]
return false, &tree.ColumnItem{ColumnName: tree.Name(col.Name)}, nil
}
}
return true, expr, err
},
)
}
// FormatColumnForDisplay formats a column descriptor as a SQL string. It
// converts user defined types in default and computed expressions to a
// human-readable form.
func FormatColumnForDisplay(
ctx context.Context,
tbl catalog.TableDescriptor,
col catalog.Column,
semaCtx *tree.SemaContext,
sessionData *sessiondata.SessionData,
redactableValues bool,
) (string, error) {
f := tree.NewFmtCtx(tree.FmtSimple)
name := col.GetName()
f.FormatNameP(&name)
f.WriteByte(' ')
f.WriteString(col.GetType().SQLString())
if col.IsHidden() {
f.WriteString(" NOT VISIBLE")
}
if col.IsNullable() {
f.WriteString(" NULL")
} else {
f.WriteString(" NOT NULL")
}
fmtFlags := tree.FmtParsable
if redactableValues {
fmtFlags |= tree.FmtMarkRedactionNode | tree.FmtOmitNameRedaction
}
if col.HasDefault() {
if col.IsGeneratedAsIdentity() {
if col.IsGeneratedAlwaysAsIdentity() {
f.WriteString(" GENERATED ALWAYS AS IDENTITY")
} else if col.IsGeneratedByDefaultAsIdentity() {
f.WriteString(" GENERATED BY DEFAULT AS IDENTITY")
}
if col.HasGeneratedAsIdentitySequenceOption() {
seqOpt := col.GetGeneratedAsIdentitySequenceOptionStr()
s := formatGeneratedAsIdentitySequenceOption(seqOpt)
f.WriteString(s)
}
} else {
f.WriteString(" DEFAULT ")
defExpr, err := FormatExprForDisplay(ctx, tbl, col.GetDefaultExpr(), semaCtx, sessionData, fmtFlags)
if err != nil {
return "", err
}
f.WriteString(defExpr)
}
}
if col.HasOnUpdate() {
f.WriteString(" ON UPDATE ")
onUpdateExpr, err := FormatExprForDisplay(ctx, tbl, col.GetOnUpdateExpr(), semaCtx, sessionData, fmtFlags)
if err != nil {
return "", err
}
f.WriteString(onUpdateExpr)
}
if col.IsComputed() {
f.WriteString(" AS (")
compExpr, err := FormatExprForDisplay(ctx, tbl, col.GetComputeExpr(), semaCtx, sessionData, fmtFlags)
if err != nil {
return "", err
}
f.WriteString(compExpr)
if col.IsVirtual() {
f.WriteString(") VIRTUAL")
} else {
f.WriteString(") STORED")
}
}
return f.CloseAndGetString(), nil
}
// RenameColumn replaces any occurrence of the column from in expr with to, and
// returns a string representation of the new expression.
func RenameColumn(expr string, from tree.Name, to tree.Name) (string, error) {
parsed, err := parser.ParseExpr(expr)
if err != nil {
return "", err
}
replaceFn := func(expr tree.Expr) (recurse bool, newExpr tree.Expr, err error) {
if vBase, ok := expr.(tree.VarName); ok {
v, err := vBase.NormalizeVarName()
if err != nil {
return false, nil, err
}
if c, ok := v.(*tree.ColumnItem); ok {
if string(c.ColumnName) == string(from) {
c.ColumnName = to
}
}
return false, v, nil
}
return true, expr, nil
}
renamed, err := tree.SimpleVisit(parsed, replaceFn)
if err != nil {
return "", err
}
return renamed.String(), nil
}
// iterColDescriptors iterates over the expression's variable columns and
// calls f on each.
//
// If the expression references a column that does not exist in the table
// descriptor, iterColDescriptors errs with pgcode.UndefinedColumn.
func iterColDescriptors(
desc catalog.TableDescriptor, rootExpr tree.Expr, f func(column catalog.Column) error,
) error {
_, err := tree.SimpleVisit(rootExpr, func(expr tree.Expr) (recurse bool, newExpr tree.Expr, err error) {
vBase, ok := expr.(tree.VarName)
if !ok {
// Not a VarName, don't do anything to this node.
return true, expr, nil
}
v, err := vBase.NormalizeVarName()
if err != nil {
return false, nil, err
}
c, ok := v.(*tree.ColumnItem)
if !ok {
return true, expr, nil
}
col, err := catalog.MustFindColumnByTreeName(desc, c.ColumnName)
if err != nil || col.Dropped() {
return false, nil, pgerror.Newf(pgcode.UndefinedColumn,
"column %q does not exist, referenced in %q", c.ColumnName, rootExpr.String())
}
if err := f(col); err != nil {
return false, nil, err
}
return false, expr, err
})
return err
}
// dummyColumn represents a variable column that can type-checked. It is used
// in validating check constraint and partial index predicate expressions. This
// validation requires that the expression can be both both typed-checked and
// examined for variable expressions.
type dummyColumn struct {
typ *types.T
name tree.Name
}
// String implements the Stringer interface.
func (d *dummyColumn) String() string {
return tree.AsString(d)
}
// Format implements the NodeFormatter interface.
func (d *dummyColumn) Format(ctx *tree.FmtCtx) {
d.name.Format(ctx)
}
// Walk implements the Expr interface.
func (d *dummyColumn) Walk(_ tree.Visitor) tree.Expr {
return d
}
// TypeCheck implements the Expr interface.
func (d *dummyColumn) TypeCheck(
_ context.Context, _ *tree.SemaContext, desired *types.T,
) (tree.TypedExpr, error) {
return d, nil
}
func (*dummyColumn) Eval(ctx context.Context, v tree.ExprEvaluator) (tree.Datum, error) {
panic("dummyColumnItem.EvalVisit() is undefined")
}
// ResolvedType implements the TypedExpr interface.
func (d *dummyColumn) ResolvedType() *types.T {
return d.typ
}
// ReplaceColumnVars replaces the occurrences of column names in an expression with
// dummyColumns containing their type, so that they may be type-checked. It
// returns this new expression tree alongside a set containing the ColumnID of
// each column seen in the expression.
//
// If the expression references a column that does not exist in the table
// descriptor, replaceColumnVars errs with pgcode.UndefinedColumn.
//
// The column lookup function allows looking up columns both in the descriptor
// or in declarative schema changer elements.
func ReplaceColumnVars(
rootExpr tree.Expr,
columnLookupFn func(columnName tree.Name) (exists bool, accessible bool, id catid.ColumnID, typ *types.T),
) (tree.Expr, catalog.TableColSet, error) {
var colIDs catalog.TableColSet
newExpr, err := tree.SimpleVisit(rootExpr, func(expr tree.Expr) (recurse bool, newExpr tree.Expr, err error) {
vBase, ok := expr.(tree.VarName)
if !ok {
// Not a VarName, don't do anything to this node.
return true, expr, nil
}
v, err := vBase.NormalizeVarName()
if err != nil {
return false, nil, err
}
c, ok := v.(*tree.ColumnItem)
if !ok {
return true, expr, nil
}
colExists, colIsAccessible, colID, colType := columnLookupFn(c.ColumnName)
if !colExists {
return false, nil, pgerror.Newf(pgcode.UndefinedColumn,
"column %q does not exist, referenced in %q", c.ColumnName, rootExpr.String())
}
if !colIsAccessible {
return false, nil, pgerror.Newf(pgcode.UndefinedColumn,
"column %q is inaccessible and cannot be referenced", c.ColumnName)
}
colIDs.Add(colID)
// Convert to a dummyColumn of the correct type.
return false, &dummyColumn{typ: colType, name: c.ColumnName}, nil
})
return newExpr, colIDs, err
}
// replaceColumnVars is a convenience function for ReplaceColumnVars.
func replaceColumnVars(
tbl catalog.TableDescriptor, rootExpr tree.Expr,
) (tree.Expr, catalog.TableColSet, error) {
lookupFn := func(columnName tree.Name) (exists bool, accessible bool, id catid.ColumnID, typ *types.T) {
col, err := catalog.MustFindColumnByTreeName(tbl, columnName)
if err != nil || col.Dropped() {
return false, false, 0, nil
}
return true, !col.IsInaccessible(), col.GetID(), col.GetType()
}
return ReplaceColumnVars(rootExpr, lookupFn)
}
// ReplaceSequenceIDsWithFQNames walks the given expr and replaces occurrences
// of regclass IDs in the expr with the descriptor's fully qualified name.
// For example, nextval(12345::REGCLASS) => nextval('foo.public.seq').
func ReplaceSequenceIDsWithFQNames(
ctx context.Context, rootExpr tree.Expr, semaCtx *tree.SemaContext,
) (tree.Expr, error) {
replaceFn := func(expr tree.Expr) (recurse bool, newExpr tree.Expr, err error) {
id, ok := GetSeqIDFromExpr(expr)
if !ok {
return true, expr, nil
}
// If it's not a sequence or the resolution fails, skip this node.
seqName, err := semaCtx.NameResolver.GetQualifiedTableNameByID(ctx, id, tree.ResolveRequireSequenceDesc)
if err != nil {
return true, expr, nil //nolint:returnerrcheck
}
// Omit the database qualification if the sequence lives in the current database.
currDb := semaCtx.NameResolver.CurrentDatabase()
if seqName.Catalog() == currDb {
seqName.CatalogName = ""
seqName.ExplicitCatalog = false
}
// Swap out this node to use the qualified table name for the sequence.
return false, &tree.CastExpr{
Type: types.RegClass,
SyntaxMode: tree.CastShort,
Expr: tree.NewStrVal(seqName.String()),
}, nil
}
newExpr, err := tree.SimpleVisit(rootExpr, replaceFn)
return newExpr, err
}
// GetSeqIDFromExpr takes an expr and looks for a sequence ID in
// this expr. If it finds one, it will return that ID.
func GetSeqIDFromExpr(expr tree.Expr) (int64, bool) {
// Depending on if the given expr is typed checked, we're looking
// for either a *tree.AnnotateTypeExpr (if not typed checked), or
// a *tree.DOid (if type checked).
switch n := expr.(type) {
case *tree.AnnotateTypeExpr:
if typ, safe := tree.GetStaticallyKnownType(n.Type); !safe || typ.Family() != types.OidFamily {
return 0, false
}
numVal, ok := n.Expr.(*tree.NumVal)
if !ok {
return 0, false
}
id, err := numVal.AsInt64()
if err != nil {
return 0, false
}
return id, true
case *tree.DOid:
return int64(n.Oid), true
default:
return 0, false
}
}
// formatGeneratedAsIdentitySequenceOption returns the formatted sequence option
// expression for GENERATED AS IDENTITY column.
func formatGeneratedAsIdentitySequenceOption(seqOpt string) string {
if seqOpt == "" {
return ""
}
return fmt.Sprintf(" (%s)", seqOpt)
}