-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.go
executable file
·133 lines (116 loc) · 3.69 KB
/
transform.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
// 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 expression
import (
"errors"
"github.com/Rock-liyi/p2pdb-store/sql"
)
// TransformExprWithNodeFunc 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 TransformExprWithNodeFunc func(sql.Node, sql.Expression) (sql.Expression, error)
// TransformUp applies a transformation function to the given expression from the
// bottom up.
func TransformUp(e sql.Expression, f sql.TransformExprFunc) (sql.Expression, error) {
children := e.Children()
if len(children) == 0 {
e, err := e.WithChildren()
if err != nil {
return nil, err
}
return f(e)
}
newChildren := make([]sql.Expression, len(children))
for i, c := range children {
c, err := TransformUp(c, f)
if err != nil {
return nil, err
}
newChildren[i] = c
}
e, err := e.WithChildren(newChildren...)
if err != nil {
return nil, err
}
return f(e)
}
// InspectUp traverses the given tree from the bottom up, breaking if
// stop = true. Returns a bool indicating whether traversal was interrupted.
func InspectUp(node sql.Expression, f func(sql.Expression) bool) bool {
stop := errors.New("stop")
wrap := func(n sql.Expression) (sql.Expression, error) {
ok := f(n)
if ok {
return nil, stop
}
return n, nil
}
_, err := TransformUp(node, func(node sql.Expression) (sql.Expression, error) {
return wrap(node)
})
return errors.Is(err, stop)
}
// Clone duplicates an existing sql.Expression, returning new nodes with the
// same structure and internal values. It can be useful when dealing with
// stateful expression nodes where an evaluation needs to create multiple
// independent histories of the internal state of the expression nodes.
func Clone(expr sql.Expression) (sql.Expression, error) {
return TransformUp(expr, func(e sql.Expression) (sql.Expression, error) {
return e, nil
})
}
// TransformUpWithNode applies a transformation function to the given expression from the bottom up.
func TransformUpWithNode(n sql.Node, e sql.Expression, f TransformExprWithNodeFunc) (sql.Expression, error) {
children := e.Children()
if len(children) == 0 {
e, err := e.WithChildren()
if err != nil {
return nil, err
}
return f(n, e)
}
newChildren := make([]sql.Expression, len(children))
for i, c := range children {
c, err := TransformUpWithNode(n, c, f)
if err != nil {
return nil, err
}
newChildren[i] = c
}
e, err := e.WithChildren(newChildren...)
if err != nil {
return nil, err
}
return f(n, e)
}
// ExpressionToColumn converts the expression to the form that should be used in a Schema. Expressions that have Name()
// and Table() methods will use these; otherwise, String() and "" are used, respectively. The type and nullability are
// taken from the expression directly.
func ExpressionToColumn(e sql.Expression) *sql.Column {
var name string
if n, ok := e.(sql.Nameable); ok {
name = n.Name()
} else {
name = e.String()
}
var table string
if t, ok := e.(sql.Tableable); ok {
table = t.Table()
}
return &sql.Column{
Name: name,
Type: e.Type(),
Nullable: e.IsNullable(),
Source: table,
}
}