forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliases.go
304 lines (268 loc) · 9.99 KB
/
aliases.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
// Copyright 2020-2021 Dolthub, Inc.
//
// 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.
package analyzer
import (
"fmt"
"strings"
"github.com/gabereiser/go-mysql-server/sql"
"github.com/gabereiser/go-mysql-server/sql/expression"
"github.com/gabereiser/go-mysql-server/sql/plan"
"github.com/gabereiser/go-mysql-server/sql/transform"
)
type TableAliases map[string]sql.Nameable
// add adds the given table alias referring to the node given. Adding a case insensitive alias that already exists
// returns an error.
func (ta TableAliases) add(alias sql.Nameable, target sql.Nameable) error {
lowerName := strings.ToLower(alias.Name())
if _, ok := ta[lowerName]; ok && lowerName != plan.DualTableName {
return sql.ErrDuplicateAliasOrTable.New(alias.Name())
}
ta[lowerName] = target
return nil
}
// putAll adds all aliases in the given aliases to the receiver. Silently overwrites existing entries.
func (ta TableAliases) putAll(other TableAliases) {
for alias, target := range other {
ta[alias] = target
}
}
// findConflicts returns a list of aliases that are in both sets of aliases, and a list of aliases that are just in
// the current set of aliases.
func (ta TableAliases) findConflicts(other TableAliases) (conflicts []string, nonConflicted []string) {
conflicts = []string{}
nonConflicted = []string{}
for alias := range other {
if _, ok := ta[alias]; ok {
conflicts = append(conflicts, alias)
} else {
nonConflicted = append(nonConflicted, alias)
}
}
return
}
// getTableAliases returns a map of all aliases of resolved tables / subqueries in the node, keyed by their alias name.
// Unaliased tables are returned keyed by their original lower-cased name.
func getTableAliases(n sql.Node, scope *Scope) (TableAliases, error) {
var passAliases TableAliases
var aliasFn func(node sql.Node) bool
var analysisErr error
var recScope *Scope
if !scope.IsEmpty() {
recScope = recScope.withMemos(scope.memos)
}
aliasFn = func(node sql.Node) bool {
if node == nil {
return false
}
if at, ok := node.(*plan.TableAlias); ok {
switch t := at.Child.(type) {
case *plan.ResolvedTable, *plan.SubqueryAlias, *plan.ValueDerivedTable, *plan.TransformedNamedNode, *plan.RecursiveTable, *plan.DeferredAsOfTable:
analysisErr = passAliases.add(at, t.(sql.NameableNode))
case *plan.TableAlias:
analysisErr = passAliases.add(at, t)
case *plan.IndexedTableAccess:
analysisErr = passAliases.add(at, t)
case *plan.RecursiveCte:
case *plan.UnresolvedTable:
panic("Table not resolved")
default:
panic(fmt.Sprintf("Unexpected child type of TableAlias: %T", at.Child))
}
return false
}
switch node := node.(type) {
case *plan.CreateTrigger:
// trigger bodies are evaluated separately
rt := getResolvedTable(node.Table)
analysisErr = passAliases.add(rt, rt)
return false
case *plan.Procedure:
return false
case *plan.Block:
// blocks should not be parsed as a whole, just their statements individually
for _, child := range node.Children() {
_, analysisErr = getTableAliases(child, recScope)
if analysisErr != nil {
break
}
}
return false
case *plan.InsertInto:
rt := getResolvedTable(node.Destination)
analysisErr = passAliases.add(rt, rt)
return false
case *plan.ResolvedTable, *plan.SubqueryAlias, *plan.ValueDerivedTable, *plan.TransformedNamedNode, *plan.RecursiveTable:
analysisErr = passAliases.add(node.(sql.Nameable), node.(sql.Nameable))
return false
case *plan.IndexedTableAccess:
rt := getResolvedTable(node.ResolvedTable)
analysisErr = passAliases.add(rt, node)
return false
case *plan.UnresolvedTable:
panic("Table not resolved")
}
if opaque, ok := node.(sql.OpaqueNode); ok && opaque.Opaque() {
return false
}
return true
}
if analysisErr != nil {
return nil, analysisErr
}
// Inspect all of the scopes, outer to inner. Within a single scope, a name conflict is an error. But an inner scope
// can overwrite a name in an outer scope, and it's not an error.
aliases := make(TableAliases)
for _, scopeNode := range scope.OuterToInner() {
passAliases = make(TableAliases)
transform.Inspect(scopeNode, aliasFn)
if analysisErr != nil {
return nil, analysisErr
}
recScope = recScope.newScope(scopeNode)
aliases.putAll(passAliases)
}
passAliases = make(TableAliases)
transform.Inspect(n, aliasFn)
if analysisErr != nil {
return nil, analysisErr
}
aliases.putAll(passAliases)
return aliases, analysisErr
}
// aliasedExpressionsInNode returns a map of the aliased expressions defined in the first Projector node found (starting
// the search from the specified node), mapped from the expression string to the alias name. Returned
// map keys are normalized to lower case.
func aliasedExpressionsInNode(n sql.Node) map[string]string {
projector := findFirstProjectorNode(n)
if projector == nil {
return nil
}
aliasesFromExpressionToName := make(map[string]string)
for _, e := range projector.ProjectedExprs() {
alias, ok := e.(*expression.Alias)
if ok {
aliasesFromExpressionToName[strings.ToLower(alias.Child.String())] = alias.Name()
}
}
return aliasesFromExpressionToName
}
// aliasesDefinedInNode returns the expression aliases that are defined in the first Projector node found, starting
// the search from the specified node. All returned alias names are normalized to lower case.
func aliasesDefinedInNode(n sql.Node) []string {
projector := findFirstProjectorNode(n)
if projector == nil {
return nil
}
var aliases []string
for _, e := range projector.ProjectedExprs() {
alias, ok := e.(*expression.Alias)
if ok {
aliases = append(aliases, strings.ToLower(alias.Name()))
}
}
return aliases
}
// normalizeExpressions returns the expressions given after normalizing them to replace table and expression aliases
// with their underlying names. This is necessary to match such expressions against those declared by implementors of
// various interfaces that declare expressions to handle, such as Index.Expressions(), FilteredTable, etc.
func normalizeExpressions(tableAliases TableAliases, expr ...sql.Expression) []sql.Expression {
expressions := make([]sql.Expression, len(expr))
for i, e := range expr {
expressions[i] = normalizeExpression(tableAliases, e)
}
return expressions
}
// normalizeExpression returns the expression given after normalizing it to replace table aliases with their underlying
// names. This is necessary to match such expressions against those declared by implementors of various interfaces that
// declare expressions to handle, such as Index.Expressions(), FilteredTable, etc.
func normalizeExpression(tableAliases TableAliases, e sql.Expression) sql.Expression {
// If the query has table aliases, use them to replace any table aliases in column expressions
normalized, _, _ := transform.Expr(e, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
if field, ok := e.(*expression.GetField); ok {
table := strings.ToLower(field.Table())
if rt, ok := tableAliases[table]; ok {
return field.WithTable(rt.Name()), transform.NewTree, nil
}
}
return e, transform.SameTree, nil
})
return normalized
}
// renameAliasesInExpressions returns expressions where any table references are renamed to the new table name.
func renameAliasesInExpressions(expressions []sql.Expression, oldNameLower string, newName string) ([]sql.Expression, error) {
for i, e := range expressions {
newExpression, same, err := renameAliasesInExp(e, oldNameLower, newName)
if err != nil {
return nil, err
}
if !same {
expressions[i] = newExpression
}
}
return expressions, nil
}
// renameAliasesInExp returns an expression where any table references are renamed to the new table name.
func renameAliasesInExp(exp sql.Expression, oldNameLower string, newName string) (sql.Expression, transform.TreeIdentity, error) {
return transform.Expr(exp, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
switch e := e.(type) {
case *expression.GetField:
if strings.EqualFold(e.Table(), oldNameLower) {
gf := e.WithTable(newName)
return gf, transform.NewTree, nil
}
case *expression.UnresolvedColumn:
if strings.EqualFold(e.Table(), oldNameLower) {
return expression.NewUnresolvedQualifiedColumn(newName, e.Name()), transform.NewTree, nil
}
case *plan.Subquery:
newSubquery, tree, err := renameAliases(e.Query, oldNameLower, newName)
if err != nil {
return nil, tree, err
}
if tree == transform.NewTree {
e.WithQuery(newSubquery)
}
return e, tree, nil
}
return e, transform.SameTree, nil
})
}
// renameAliasesInExp returns a node where any table references are renamed to the new table name.
func renameAliases(node sql.Node, oldNameLower string, newName string) (sql.Node, transform.TreeIdentity, error) {
return transform.Node(node, func(node sql.Node) (sql.Node, transform.TreeIdentity, error) {
newNode := node
allSame := transform.SameTree
// update TableAlias directly
tableAlias, ok := newNode.(*plan.TableAlias)
if ok {
if strings.EqualFold(tableAlias.Name(), oldNameLower) {
newNode = tableAlias.WithName(newName)
allSame = transform.NewTree
}
}
// update expressions
newNode, same, err := transform.NodeExprs(newNode, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
return renameAliasesInExp(e, oldNameLower, newName)
})
if err != nil {
return nil, transform.SameTree, err
}
allSame = allSame && same
if allSame {
return node, transform.SameTree, nil
} else {
return newNode, transform.NewTree, nil
}
})
}