forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix_field_indexes.go
237 lines (216 loc) · 7.27 KB
/
fix_field_indexes.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
// 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 (
"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"
)
// FixFieldIndexesOnExpressions executes FixFieldIndexes on a list of exprs.
func FixFieldIndexesOnExpressions(scope *Scope, a *Analyzer, schema sql.Schema, expressions ...sql.Expression) ([]sql.Expression, transform.TreeIdentity, error) {
var result []sql.Expression
var res sql.Expression
var same transform.TreeIdentity
var err error
for i := range expressions {
e := expressions[i]
res, same, err = FixFieldIndexes(scope, a, schema, e)
if err != nil {
return nil, transform.SameTree, err
}
if !same {
if result == nil {
result = make([]sql.Expression, len(expressions))
copy(result, expressions)
}
result[i] = res
}
}
if len(result) > 0 {
return result, transform.NewTree, nil
}
return expressions, transform.SameTree, nil
}
// FixFieldIndexes transforms the given expression by correcting the indexes of columns in GetField expressions,
// according to the schema given. Used when combining multiple tables together into a single join result, or when
// otherwise changing / combining schemas in the node tree.
func FixFieldIndexes(scope *Scope, a *Analyzer, schema sql.Schema, exp sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
scopeLen := len(scope.Schema())
return transform.Expr(exp, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
switch e := e.(type) {
// For each GetField expression, re-index it with the appropriate index from the schema.
case *expression.GetField:
for i, col := range schema {
newIndex := scopeLen + i
if e.Name() == col.Name && e.Table() == col.Source {
if newIndex != e.Index() {
a.Log("Rewriting field %s.%s from index %d to %d", e.Table(), e.Name(), e.Index(), newIndex)
return expression.NewGetFieldWithTable(
newIndex,
e.Type(),
e.Table(),
e.Name(),
e.IsNullable(),
), transform.NewTree, nil
}
return e, transform.SameTree, nil
}
}
// If we didn't find the column in the schema of the node itself, look outward in surrounding scopes. Work
// inner-to-outer, in accordance with MySQL scope naming precedence rules.
offset := 0
for _, n := range scope.InnerToOuter() {
schema := schemas(n.Children())
offset += len(schema)
for i, col := range schema {
if e.Name() == col.Name && e.Table() == col.Source {
newIndex := scopeLen - offset + i
if e.Index() != newIndex {
a.Log("Rewriting field %s.%s from index %d to %d", e.Table(), e.Name(), e.Index(), newIndex)
return expression.NewGetFieldWithTable(
newIndex,
e.Type(),
e.Table(),
e.Name(),
e.IsNullable(),
), transform.NewTree, nil
}
return e, transform.SameTree, nil
}
}
}
return nil, transform.SameTree, ErrFieldMissing.New(e.Name())
}
return e, transform.SameTree, nil
})
}
// schemas returns the schemas for the nodes given appended in to a single one
func schemas(nodes []sql.Node) sql.Schema {
var schema sql.Schema
for _, n := range nodes {
schema = append(schema, n.Schema()...)
}
return schema
}
// FixFieldIndexesForExpressions transforms the expressions in the Node given, fixing the field indexes.
func FixFieldIndexesForExpressions(a *Analyzer, node sql.Node, scope *Scope) (sql.Node, transform.TreeIdentity, error) {
if _, ok := node.(sql.Expressioner); !ok {
return node, transform.SameTree, nil
}
var schemas []sql.Schema
for _, child := range node.Children() {
schemas = append(schemas, child.Schema())
}
if len(schemas) < 1 {
return node, transform.SameTree, nil
}
n, sameC, err := transform.OneNodeExprsWithNode(node, func(_ sql.Node, e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
for _, schema := range schemas {
fixed, same, err := FixFieldIndexes(scope, a, schema, e)
if err == nil {
return fixed, same, nil
}
if ErrFieldMissing.Is(err) {
continue
}
return nil, transform.SameTree, err
}
return e, transform.SameTree, nil
})
if err != nil {
return nil, transform.SameTree, err
}
j, ok := n.(*plan.JoinNode)
if !ok {
return n, sameC, nil
}
sameJ := transform.SameTree
var cond sql.Expression
switch {
case j.Op.IsCross():
case j.Op.IsPartial():
if !ok {
return n, sameC && sameJ, nil
}
cond, sameJ, err = FixFieldIndexes(scope, a, append(j.Left().Schema(), j.Right().Schema()...), j.JoinCond())
if err != nil {
return nil, transform.SameTree, err
}
if !sameJ {
n, err = j.WithExpressions(cond)
if err != nil {
return nil, transform.SameTree, err
}
}
default:
cond, sameJ, err = FixFieldIndexes(scope, a, j.Schema(), j.JoinCond())
if err != nil {
return nil, transform.SameTree, err
}
if !sameJ {
n, err = j.WithExpressions(cond)
if err != nil {
return nil, transform.SameTree, err
}
}
}
return n, sameC && sameJ, nil
}
// FixFieldIndexesForTableNode transforms the expressions in the Node given,
// fixing the field indexes. This is useful for Table nodes that have
// expressions but no children.
func FixFieldIndexesForTableNode(ctx *sql.Context, a *Analyzer, node sql.Node, scope *Scope) (sql.Node, transform.TreeIdentity, error) {
if _, ok := node.(sql.Expressioner); !ok {
return node, transform.SameTree, nil
}
return transform.OneNodeExprsWithNode(node, func(_ sql.Node, e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
schema := node.Schema()
fixed, same, err := FixFieldIndexes(scope, a, schema, e)
if err != nil {
if ErrFieldMissing.Is(err) {
return e, transform.SameTree, nil
}
return nil, transform.SameTree, err
}
return fixed, same, nil
})
}
func FixFieldIndexesForNode(a *Analyzer, scope *Scope, n sql.Node) (sql.Node, transform.TreeIdentity, error) {
return transform.Node(n, func(n sql.Node) (sql.Node, transform.TreeIdentity, error) {
ret := n
var err error
sameN := transform.SameTree
switch n := n.(type) {
case *plan.SubqueryAlias:
scope := scope.newScopeFromSubqueryAlias(n)
ret, sameN, err = FixFieldIndexesForExpressions(a, n, scope)
default:
ret, sameN, err = FixFieldIndexesForExpressions(a, n, scope)
}
if err != nil {
return n, transform.SameTree, err
}
ret, sameE, err := transform.NodeExprs(ret, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
if sq, ok := e.(*plan.Subquery); ok {
return FixFieldIndexes(scope, a, ret.Schema(), sq)
}
return e, transform.SameTree, nil
})
if err != nil {
return n, transform.SameTree, err
}
return ret, sameN && sameE, nil
})
}