-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
build.go
165 lines (148 loc) · 4.52 KB
/
build.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
// Copyright 2017 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.
package opt
import (
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
)
// Map from tree.ComparisonOperator to operator.
var comparisonOpMap = [...]operator{
tree.EQ: eqOp,
tree.LT: ltOp,
tree.GT: gtOp,
tree.LE: leOp,
tree.GE: geOp,
tree.NE: neOp,
tree.In: inOp,
tree.NotIn: notInOp,
tree.Like: likeOp,
tree.NotLike: notLikeOp,
tree.ILike: iLikeOp,
tree.NotILike: notILikeOp,
tree.SimilarTo: similarToOp,
tree.NotSimilarTo: notSimilarToOp,
tree.RegMatch: regMatchOp,
tree.NotRegMatch: notRegMatchOp,
tree.RegIMatch: regIMatchOp,
tree.NotRegIMatch: notRegIMatchOp,
tree.IsDistinctFrom: isDistinctFromOp,
tree.IsNotDistinctFrom: isNotDistinctFromOp,
tree.Is: isOp,
tree.IsNot: isNotOp,
tree.Any: anyOp,
tree.Some: someOp,
tree.All: allOp,
}
// Map from tree.BinaryOperator to operator.
var binaryOpMap = [...]operator{
tree.Bitand: bitandOp,
tree.Bitor: bitorOp,
tree.Bitxor: bitxorOp,
tree.Plus: plusOp,
tree.Minus: minusOp,
tree.Mult: multOp,
tree.Div: divOp,
tree.FloorDiv: floorDivOp,
tree.Mod: modOp,
tree.Pow: powOp,
tree.Concat: concatOp,
tree.LShift: lShiftOp,
tree.RShift: rShiftOp,
}
// Map from tree.UnaryOperator to operator.
var unaryOpMap = [...]operator{
tree.UnaryPlus: unaryPlusOp,
tree.UnaryMinus: unaryMinusOp,
tree.UnaryComplement: unaryComplementOp,
}
// We allocate *scalarProps and *expr in chunks of these sizes.
const exprAllocChunk = 16
const scalarPropsAllocChunk = 16
type buildContext struct {
preallocScalarProps []scalarProps
preallocExprs []expr
}
func (bc *buildContext) newScalarProps() *scalarProps {
if len(bc.preallocScalarProps) == 0 {
bc.preallocScalarProps = make([]scalarProps, scalarPropsAllocChunk)
}
p := &bc.preallocScalarProps[0]
bc.preallocScalarProps = bc.preallocScalarProps[1:]
return p
}
// newExpr returns a new *expr with a new, blank scalarProps.
func (bc *buildContext) newExpr() *expr {
if len(bc.preallocExprs) == 0 {
bc.preallocExprs = make([]expr, exprAllocChunk)
}
e := &bc.preallocExprs[0]
bc.preallocExprs = bc.preallocExprs[1:]
e.scalarProps = bc.newScalarProps()
return e
}
// buildScalar converts a tree.TypedExpr to an expr tree.
func buildScalar(buildCtx *buildContext, pexpr tree.TypedExpr) *expr {
switch t := pexpr.(type) {
case *tree.ParenExpr:
return buildScalar(buildCtx, t.TypedInnerExpr())
}
e := buildCtx.newExpr()
e.scalarProps.typ = pexpr.ResolvedType()
switch t := pexpr.(type) {
case *tree.AndExpr:
initBinaryExpr(
e, andOp,
buildScalar(buildCtx, t.TypedLeft()),
buildScalar(buildCtx, t.TypedRight()),
)
case *tree.OrExpr:
initBinaryExpr(
e, orOp,
buildScalar(buildCtx, t.TypedLeft()),
buildScalar(buildCtx, t.TypedRight()),
)
case *tree.NotExpr:
initUnaryExpr(e, notOp, buildScalar(buildCtx, t.TypedInnerExpr()))
case *tree.BinaryExpr:
initBinaryExpr(
e, binaryOpMap[t.Operator],
buildScalar(buildCtx, t.TypedLeft()),
buildScalar(buildCtx, t.TypedRight()),
)
case *tree.ComparisonExpr:
initBinaryExpr(
e, comparisonOpMap[t.Operator],
buildScalar(buildCtx, t.TypedLeft()),
buildScalar(buildCtx, t.TypedRight()),
)
case *tree.UnaryExpr:
initUnaryExpr(e, unaryOpMap[t.Operator], buildScalar(buildCtx, t.TypedInnerExpr()))
case *tree.FuncExpr:
def, err := t.Func.Resolve(tree.SearchPath{})
if err != nil {
panic(err.Error())
}
children := make([]*expr, len(t.Exprs))
for i, pexpr := range t.Exprs {
children[i] = buildScalar(buildCtx, pexpr.(tree.TypedExpr))
}
initFunctionCallExpr(e, def, children)
case *tree.IndexedVar:
initVariableExpr(e, t.Idx)
case tree.Datum:
initConstExpr(e, t)
default:
panicf("node %T not supported", t)
}
return e
}