forked from cockroachdb/cockroach
/
var_name.go
278 lines (240 loc) · 8.86 KB
/
var_name.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 2016 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: Raphael 'kena' Poss (knz@cockroachlabs.com)
package parser
import (
"bytes"
"fmt"
)
// Variable names are used in multiples places in SQL:
//
// - if the context is the LHS of an UPDATE, then the name is for an
// unqualified column or part of a column.
//
// Syntax: <column-name> [ . <subfield-name> | '[' <index> ']' ]*
// (the name always *starts* with a column name)
//
// Represented by: ColumnItem
// Found by: NormalizeUnqualifiedColumnItem()
//
// - if the context is a direct select target, then the name may end
// with '*' for a column group, with optional database and table prefix.
//
// Syntax: [ [ <database-name> '.' ] <table-name> '.' ] '*'
//
// Represented by: UnqualifiedStar, *AllColumnsSelector (VarName)
// Found by: NormalizeVarName()
//
// - elsewhere, the name is for a optionally-qualified column name
// with optional array subscript followed by additional optional
// subfield or array subscripts.
//
// Syntax: [ [ <database-name> '.' ] <table-name> '.' ]
// <column-name>
// [ '[' <index> ']' [ '[' <index> ']' | '.' <subfield> ] * ]
// (either there is no array subscript and the qualified name *ends*
// with a column name; or there is an array subscript and the
// supporting column's name is the last unqualified name before the first
// array subscript).
//
// Represented by: ColumnItem (VarName)
// Found by: NormalizeVarName()
//
// The common type in expression contexts is VarName. This extends
// TypedExpr and VariableExpr so it can be used in expression trees
// directly. During parsing a name in expressions always begins as an
// UnresolvedName instance. During either IndexedVar substitution, type
// checking or render target expansion (render node) this is
// normalized and replaced by either *ColumnItem, UnqualifiedStar or
// AllColumnsSelector using the NormalizeVarName() method.
//
// In the context of UpdateExprs, UnresolvedNames are translated to
// ColumnItem directly by NormalizeUnqualifiedColumnItem() without
// going through the VarName interface at all.
// VarName is the common interface to UnresolvedName,
// ColumnItem and AllColumnsSelector for use in expression contexts.
type VarName interface {
TypedExpr
NormalizeVarName() (VarName, error)
}
var _ VarName = UnresolvedName{}
var _ VarName = UnqualifiedStar{}
var _ VarName = &AllColumnsSelector{}
var _ VarName = &ColumnItem{}
// NormalizeVarName is a no-op for UnqualifiedStar (already normalized)
func (u UnqualifiedStar) NormalizeVarName() (VarName, error) { return u, nil }
var singletonStarName VarName = UnqualifiedStar{}
// StarExpr is a convenience function that represents an unqualified "*".
func StarExpr() VarName { return singletonStarName }
// ResolvedType implements the TypedExpr interface.
func (UnqualifiedStar) ResolvedType() Type {
panic("unqualified stars ought to be replaced before this point")
}
// Variable implements the VariableExpr interface.
func (UnqualifiedStar) Variable() {}
// ResolvedType implements the TypedExpr interface.
func (UnresolvedName) ResolvedType() Type {
panic("unresolved names ought to be replaced before this point")
}
// Variable implements the VariableExpr interface. Although, the
// UnresolvedName ought to be replaced to an IndexedVar before the points the
// VariableExpr interface is used.
func (UnresolvedName) Variable() {}
// AllColumnsSelector corresponds to a selection of all
// columns in a table when used in a SELECT clause.
// (e.g. `table.*`)
type AllColumnsSelector struct {
TableName
}
// Format implements the NodeFormatter interface.
func (a *AllColumnsSelector) Format(buf *bytes.Buffer, f FmtFlags) {
if a.TableName.DatabaseName != "" {
FormatNode(buf, f, a.TableName.DatabaseName)
buf.WriteByte('.')
}
FormatNode(buf, f, a.TableName.TableName)
buf.WriteString(".*")
}
func (a *AllColumnsSelector) String() string { return AsString(a) }
// NormalizeVarName is a no-op for AllColumnsSelector (already normalized)
func (a *AllColumnsSelector) NormalizeVarName() (VarName, error) { return a, nil }
// Variable implements the VariableExpr interface. Although, the
// AllColumnsSelector ought to be replaced to an IndexedVar before the points the
// VariableExpr interface is used.
func (a *AllColumnsSelector) Variable() {}
// ResolvedType implements the TypedExpr interface.
func (*AllColumnsSelector) ResolvedType() Type {
panic("all-columns selectors ought to be replaced before this point")
}
// ColumnItem corresponds to the name of a column or sub-item
// of a column in an expression.
type ColumnItem struct {
// TableName holds the table prefix, if the name refers to a column.
TableName TableName
// ColumnName names the designated column.
ColumnName Name
// Selector defines which sub-part of the variable is being
// accessed.
Selector NameParts
}
// Format implements the NodeFormatter interface.
func (c *ColumnItem) Format(buf *bytes.Buffer, f FmtFlags) {
if c.TableName.TableName != "" {
if c.TableName.DatabaseName != "" {
FormatNode(buf, f, c.TableName.DatabaseName)
buf.WriteByte('.')
}
FormatNode(buf, f, c.TableName.TableName)
buf.WriteByte('.')
}
FormatNode(buf, f, c.ColumnName)
if len(c.Selector) > 0 {
if _, ok := c.Selector[0].(*ArraySubscript); !ok {
buf.WriteByte('.')
}
FormatNode(buf, f, c.Selector)
}
}
func (c *ColumnItem) String() string { return AsString(c) }
// NormalizeVarName is a no-op for ColumnItem (already normalized)
func (c *ColumnItem) NormalizeVarName() (VarName, error) { return c, nil }
// Column retrieves the unqualified column name.
func (c *ColumnItem) Column() string {
return string(c.ColumnName)
}
// Variable implements the VariableExpr interface. Although, the
// ColumnItem ought to be replaced to an IndexedVar before the points the
// VariableExpr interface is used.
func (c *ColumnItem) Variable() {}
// ResolvedType implements the TypedExpr interface.
func (c *ColumnItem) ResolvedType() Type {
if presetTypesForTesting == nil {
return nil
}
return presetTypesForTesting[c.String()]
}
// NormalizeVarName normalizes a UnresolvedName for all the forms it can have
// inside an expression context.
func (n UnresolvedName) NormalizeVarName() (VarName, error) {
if len(n) == 0 {
return nil, fmt.Errorf("invalid name: %q", n)
}
if s, isStar := n[len(n)-1].(UnqualifiedStar); isStar {
// Either a single '*' or a name of the form [db.]table.*
if len(n) == 1 {
return s, nil
}
// The prefix before the star must be a valid table name. Use the
// existing normalize code to enforce that, since we can reuse the
// resulting TableName.
t, err := n[:len(n)-1].normalizeTableNameAsValue()
if err != nil {
return nil, err
}
return &AllColumnsSelector{t}, nil
}
// In the remaining case, we have an optional table name prefix,
// followed by a column name, followed by some additional selector.
// Find the first array subscript, if any.
i := len(n)
for j, p := range n {
if _, ok := p.(*ArraySubscript); ok {
i = j
break
}
}
// The element at position i - 1 must be the column name.
// (We don't support record types yet.)
if i == 0 {
return nil, fmt.Errorf("invalid column name: %q", n)
}
colName, ok := n[i-1].(Name)
if !ok {
return nil, fmt.Errorf("invalid column name: %q", n[:i])
}
if len(colName) == 0 {
return nil, fmt.Errorf("empty column name: %q", n)
}
// Everything afterwards is the selector.
res := &ColumnItem{ColumnName: colName, Selector: NameParts(n[i:])}
if i-1 > 0 {
// What's before must be a valid table name. Use the existing
// normalize code to enforce that, since we can reuse the
// resulting TableName.
t, err := n[:i-1].normalizeTableNameAsValue()
if err != nil {
return nil, err
}
res.TableName = t
}
return res, nil
}
// NormalizeUnqualifiedColumnItem normalizes a UnresolvedName for all
// the forms it can have inside a context that requires an unqualified
// column item (e.g. UPDATE LHS, INSERT, etc.).
func (n UnresolvedName) NormalizeUnqualifiedColumnItem() (*ColumnItem, error) {
if len(n) == 0 {
return nil, fmt.Errorf("invalid column name: %q", n)
}
colName, ok := n[0].(Name)
if !ok {
return nil, fmt.Errorf("invalid column name: %q", n)
}
if colName == "" {
return nil, fmt.Errorf("empty column name: %q", n)
}
// Remainder is a selector.
return &ColumnItem{ColumnName: colName, Selector: NameParts(n[1:])}, nil
}