forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
437 lines (388 loc) · 11.7 KB
/
node.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// 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 transform
import (
"github.com/gabereiser/go-mysql-server/sql"
)
// NodeFunc is a function that given a node will return that node
// as is or transformed, a TreeIdentity to indicate whether the
// node was modified, and an error or nil.
type NodeFunc func(n sql.Node) (sql.Node, TreeIdentity, error)
// ExprFunc is a function that given an expression will return that
// expression as is or transformed, a TreeIdentity to indicate
// whether the expression was modified, and an error or nil.
type ExprFunc func(e sql.Expression) (sql.Expression, TreeIdentity, error)
// Context provides additional metadata to a SelectorFunc about the
// active node in a traversal, including the parent node, and a
// partial prefix schema of sibling nodes in a level order traversal.
type Context struct {
// Node is the currently visited node which will be transformed.
Node sql.Node
// Parent is the current parent of the transforming node.
Parent sql.Node
// ChildNum is the index of Node in Parent.Children().
ChildNum int
// SchemaPrefix is the concatenation of the Parent's SchemaPrefix with
// child.Schema() for all child with an index < ChildNum in
// Parent.Children(). For many Node, this represents the schema of the
// |row| parameter that is going to be passed to this node by its
// parent in a RowIter() call. This field is only non-nil if the entire
// in-order traversal of the tree up to this point is Resolved().
SchemaPrefix sql.Schema
}
// CtxFunc is a function which will return new sql.Node values for a given
// Context.
type CtxFunc func(Context) (sql.Node, TreeIdentity, error)
// SelectorFunc is a function which will allow NodeWithCtx to not
// traverse past a certain Context. If this function returns |false|
// for a given Context, the subtree is not transformed and the child
// is kept in its existing place in the parent as-is.
type SelectorFunc func(Context) bool
// ExprWithNodeFunc is a function that given an expression and the node
// that contains it, will return that expression as is or transformed
// along with an error, if any.
type ExprWithNodeFunc func(sql.Node, sql.Expression) (sql.Expression, TreeIdentity, error)
// TreeIdentity tracks modifications to node and expression trees.
// Only return SameTree when it is acceptable to return the original
// input and discard the returned result as a performance improvement.
type TreeIdentity bool
const (
SameTree TreeIdentity = true
NewTree TreeIdentity = false
)
// NodeExprsWithNode applies a transformation function to all expressions
// on the given tree from the bottom up.
func NodeExprsWithNode(node sql.Node, f ExprWithNodeFunc) (sql.Node, TreeIdentity, error) {
return Node(node, func(n sql.Node) (sql.Node, TreeIdentity, error) {
return OneNodeExprsWithNode(n, f)
})
}
// NodeExprs applies a transformation function to all expressions
// on the given plan tree from the bottom up.
func NodeExprs(node sql.Node, f ExprFunc) (sql.Node, TreeIdentity, error) {
return NodeExprsWithNode(node, func(n sql.Node, e sql.Expression) (sql.Expression, TreeIdentity, error) {
return f(e)
})
}
// NodeExprsWithNodeWithOpaque applies a transformation function to all expressions
// on the given tree from the bottom up, including through opaque nodes.
func NodeExprsWithNodeWithOpaque(node sql.Node, f ExprWithNodeFunc) (sql.Node, TreeIdentity, error) {
return NodeWithOpaque(node, func(n sql.Node) (sql.Node, TreeIdentity, error) {
return OneNodeExprsWithNode(n, f)
})
}
// NodeExprsWithOpaque applies a transformation function to all expressions
// on the given plan tree from the bottom up, including through opaque nodes.
func NodeExprsWithOpaque(node sql.Node, f ExprFunc) (sql.Node, TreeIdentity, error) {
return NodeExprsWithNodeWithOpaque(node, func(n sql.Node, e sql.Expression) (sql.Expression, TreeIdentity, error) {
return f(e)
})
}
// OneNodeExprsWithNode applies a transformation function to all expressions
// on the specified node. It does not traverse the children of the specified node.
func OneNodeExprsWithNode(n sql.Node, f ExprWithNodeFunc) (sql.Node, TreeIdentity, error) {
ne, ok := n.(sql.Expressioner)
if !ok {
return n, SameTree, nil
}
exprs := ne.Expressions()
if len(exprs) == 0 {
return n, SameTree, nil
}
var (
newExprs []sql.Expression
err error
)
for i := range exprs {
e := exprs[i]
e, same, err := ExprWithNode(n, e, f)
if err != nil {
return nil, SameTree, err
}
if !same {
if newExprs == nil {
newExprs = make([]sql.Expression, len(exprs))
copy(newExprs, exprs)
}
newExprs[i] = e
}
}
if len(newExprs) > 0 {
n, err = ne.WithExpressions(newExprs...)
if err != nil {
return nil, SameTree, err
}
return n, NewTree, nil
}
return n, SameTree, nil
}
// OneNodeExpressions applies a transformation function to all expressions
// on the specified node. It does not traverse the children of the specified node.
func OneNodeExpressions(n sql.Node, f ExprFunc) (sql.Node, TreeIdentity, error) {
e, ok := n.(sql.Expressioner)
if !ok {
return n, SameTree, nil
}
exprs := e.Expressions()
if len(exprs) == 0 {
return n, SameTree, nil
}
var newExprs []sql.Expression
for i := range exprs {
expr := exprs[i]
expr, same, err := Expr(expr, f)
if err != nil {
return nil, SameTree, err
}
if !same {
if newExprs == nil {
newExprs = make([]sql.Expression, len(exprs))
copy(newExprs, exprs)
}
newExprs[i] = expr
}
}
if len(newExprs) > 0 {
n, err := e.WithExpressions(newExprs...)
if err != nil {
return nil, SameTree, err
}
return n, NewTree, nil
}
return n, SameTree, nil
}
// NodeWithCtx transforms |n| from the bottom up, left to right, by passing
// each node to |f|. If |s| is non-nil, does not descend into children where
// |s| returns false.
func NodeWithCtx(n sql.Node, s SelectorFunc, f CtxFunc) (sql.Node, TreeIdentity, error) {
return nodeWithCtxHelper(Context{n, nil, -1, sql.Schema{}}, s, f)
}
func nodeWithCtxHelper(c Context, s SelectorFunc, f CtxFunc) (sql.Node, TreeIdentity, error) {
node := c.Node
_, ok := node.(sql.OpaqueNode)
if ok {
return f(c)
}
children := node.Children()
if len(children) == 0 {
return f(c)
}
var (
newChildren []sql.Node
err error
)
for i := range children {
child := children[i]
cc := Context{child, node, i, nil}
if s == nil || s(cc) {
child, same, err := nodeWithCtxHelper(cc, s, f)
if err != nil {
return nil, SameTree, err
}
if !same {
if newChildren == nil {
newChildren = make([]sql.Node, len(children))
copy(newChildren, children)
}
newChildren[i] = child
}
}
}
sameC := SameTree
if len(newChildren) > 0 {
sameC = NewTree
node, err = node.WithChildren(newChildren...)
if err != nil {
return nil, SameTree, err
}
}
node, sameN, err := f(Context{node, c.Parent, c.ChildNum, c.SchemaPrefix})
if err != nil {
return nil, SameTree, err
}
return node, sameC && sameN, nil
}
// NodeWithPrefixSchema transforms |n| from the bottom up, left to right, by passing
// each node to |f|. If |s| is non-nil, does not descend into children where
// |s| returns false.
func NodeWithPrefixSchema(n sql.Node, s SelectorFunc, f CtxFunc) (sql.Node, TreeIdentity, error) {
return transformUpWithPrefixSchemaHelper(Context{n, nil, -1, sql.Schema{}}, s, f)
}
func transformUpWithPrefixSchemaHelper(c Context, s SelectorFunc, f CtxFunc) (sql.Node, TreeIdentity, error) {
node := c.Node
_, ok := node.(sql.OpaqueNode)
if ok {
return f(c)
}
children := node.Children()
if len(children) == 0 {
return f(c)
}
var (
newChildren []sql.Node
err error
)
childPrefix := append(sql.Schema{}, c.SchemaPrefix...)
for i := range children {
child := children[i]
cc := Context{child, node, i, childPrefix}
if s == nil || s(cc) {
child, same, err := transformUpWithPrefixSchemaHelper(cc, s, f)
if err != nil {
return nil, SameTree, err
}
if !same {
if newChildren == nil {
newChildren = make([]sql.Node, len(children))
copy(newChildren, children)
}
newChildren[i] = child
}
if child.Resolved() && childPrefix != nil {
cs := child.Schema()
childPrefix = append(childPrefix, cs...)
} else {
childPrefix = nil
}
}
}
sameC := SameTree
if len(newChildren) > 0 {
sameC = NewTree
node, err = node.WithChildren(newChildren...)
if err != nil {
return nil, SameTree, err
}
}
node, sameN, err := f(Context{node, c.Parent, c.ChildNum, c.SchemaPrefix})
if err != nil {
return nil, SameTree, err
}
return node, sameC && sameN, nil
}
// Node applies a transformation function to the given tree from the
// bottom up.
func Node(node sql.Node, f NodeFunc) (sql.Node, TreeIdentity, error) {
_, ok := node.(sql.OpaqueNode)
if ok {
return f(node)
}
children := node.Children()
if len(children) == 0 {
return f(node)
}
var (
newChildren []sql.Node
child sql.Node
)
for i := range children {
child = children[i]
child, same, err := Node(child, f)
if err != nil {
return nil, SameTree, err
}
if !same {
if newChildren == nil {
newChildren = make([]sql.Node, len(children))
copy(newChildren, children)
}
newChildren[i] = child
}
}
var err error
sameC := SameTree
if len(newChildren) > 0 {
sameC = NewTree
node, err = node.WithChildren(newChildren...)
if err != nil {
return nil, SameTree, err
}
}
node, sameN, err := f(node)
if err != nil {
return nil, SameTree, err
}
return node, sameC && sameN, nil
}
// NodeWithOpaque applies a transformation function to the given tree from the bottom up, including through
// opaque nodes. This method is generally not safe to use for a transformation. Opaque nodes need to be considered in
// isolation except for very specific exceptions.
func NodeWithOpaque(node sql.Node, f NodeFunc) (sql.Node, TreeIdentity, error) {
children := node.Children()
if len(children) == 0 {
return f(node)
}
var (
newChildren []sql.Node
err error
)
for i := range children {
c := children[i]
c, same, err := NodeWithOpaque(c, f)
if err != nil {
return nil, SameTree, err
}
if !same {
if newChildren == nil {
newChildren = make([]sql.Node, len(children))
copy(newChildren, children)
}
newChildren[i] = c
}
}
sameC := SameTree
if len(newChildren) > 0 {
sameC = NewTree
node, err = node.WithChildren(newChildren...)
if err != nil {
return nil, SameTree, err
}
}
node, sameN, err := f(node)
if err != nil {
return nil, SameTree, err
}
return node, sameC && sameN, nil
}
// NodeChildren applies a transformation function to the given node's children.
func NodeChildren(node sql.Node, f NodeFunc) (sql.Node, TreeIdentity, error) {
children := node.Children()
if len(children) == 0 {
return node, SameTree, nil
}
var (
newChildren []sql.Node
child sql.Node
)
for i := range children {
child = children[i]
child, same, err := f(child)
if err != nil {
return nil, SameTree, err
}
if !same {
if newChildren == nil {
newChildren = make([]sql.Node, len(children))
copy(newChildren, children)
}
newChildren[i] = child
}
}
var err error
if len(newChildren) > 0 {
node, err = node.WithChildren(newChildren...)
return node, NewTree, err
}
return node, SameTree, nil
}