-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
inline.go
283 lines (258 loc) · 9.33 KB
/
inline.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
// Copyright 2018 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 norm
import (
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
)
// FindInlinableConstants returns the set of input columns that are synthesized
// constant value expressions: ConstOp, TrueOp, FalseOp, or NullOp. Constant
// value expressions can often be inlined into referencing expressions. Only
// Project and Values operators synthesize constant value expressions.
func (c *CustomFuncs) FindInlinableConstants(input memo.RelExpr) opt.ColSet {
var cols opt.ColSet
if project, ok := input.(*memo.ProjectExpr); ok {
for i := range project.Projections {
item := &project.Projections[i]
if opt.IsConstValueOp(item.Element) {
cols.Add(int(item.Col))
}
}
} else if values, ok := input.(*memo.ValuesExpr); ok && len(values.Rows) == 1 {
tup := values.Rows[0].(*memo.TupleExpr)
for i, scalar := range tup.Elems {
if opt.IsConstValueOp(scalar) {
cols.Add(int(values.Cols[i]))
}
}
}
return cols
}
// InlineProjectionConstants recursively searches each projection expression and
// replaces any references to input columns that are constant. It returns a new
// Projections list containing the replaced expressions.
func (c *CustomFuncs) InlineProjectionConstants(
projections memo.ProjectionsExpr, input memo.RelExpr, constCols opt.ColSet,
) memo.ProjectionsExpr {
newProjections := make(memo.ProjectionsExpr, len(projections))
for i := range projections {
item := &projections[i]
newProjections[i].Col = item.Col
newProjections[i].Element = c.inlineConstants(item.Element, input, constCols).(opt.ScalarExpr)
}
return newProjections
}
// InlineFilterConstants recursively searches each filter expression and
// replaces any references to input columns that are constant. It returns a new
// Filters list containing the replaced expressions.
func (c *CustomFuncs) InlineFilterConstants(
filters memo.FiltersExpr, input memo.RelExpr, constCols opt.ColSet,
) memo.FiltersExpr {
newFilters := make(memo.FiltersExpr, len(filters))
for i := range filters {
item := &filters[i]
newFilters[i].Condition = c.inlineConstants(item.Condition, input, constCols).(opt.ScalarExpr)
}
return newFilters
}
// inlineConstants recursively searches the given expression and replaces any
// references to input columns that are constant. It returns the replaced
// expression.
func (c *CustomFuncs) inlineConstants(
e opt.Expr, input memo.RelExpr, constCols opt.ColSet,
) opt.Expr {
var replace ReconstructFunc
replace = func(e opt.Expr) opt.Expr {
switch t := e.(type) {
case *memo.VariableExpr:
if constCols.Contains(int(t.Col)) {
return c.extractColumn(input, t.Col)
}
return t
}
return c.f.Reconstruct(e, replace)
}
return replace(e)
}
// extractColumn searches a Project or Values input expression for the column
// having the given id. It returns the expression for that column.
func (c *CustomFuncs) extractColumn(input memo.RelExpr, col opt.ColumnID) opt.ScalarExpr {
if project, ok := input.(*memo.ProjectExpr); ok {
for i := range project.Projections {
item := &project.Projections[i]
if item.Col == col {
return item.Element
}
}
} else if values, ok := input.(*memo.ValuesExpr); ok && len(values.Rows) == 1 {
tup := values.Rows[0].(*memo.TupleExpr)
for i, scalar := range tup.Elems {
if values.Cols[i] == col {
return scalar
}
}
}
panic("could not find column to extract")
}
// HasDuplicateRefs returns true if the target projection expressions or
// passthrough columns reference any outer column more than one time, or if the
// projection expressions contain a correlated subquery. For example:
//
// SELECT x+1, x+2, y FROM a
//
// HasDuplicateRefs would be true, since the x column is referenced twice.
//
// Correlated subqueries are disallowed since it introduces additional
// complexity for a case that's not too important for inlining.
func (c *CustomFuncs) HasDuplicateRefs(
projections memo.ProjectionsExpr, passthrough opt.ColSet,
) bool {
// Start with copy of passthrough columns, as they each count as a ref.
refs := passthrough.Copy()
for i := range projections {
item := &projections[i]
if item.ScalarProps(c.mem).HasCorrelatedSubquery {
// Don't traverse the expression tree if there is a correlated subquery.
return true
}
// When a column reference is found, add it to the refs set. If the set
// already contains a reference to that column, then there is a duplicate.
// findDupRefs returns true if the subtree contains at least one duplicate.
var findDupRefs func(e opt.Expr) bool
findDupRefs = func(e opt.Expr) bool {
switch t := e.(type) {
case *memo.VariableExpr:
// Count Variable references.
if refs.Contains(int(t.Col)) {
return true
}
refs.Add(int(t.Col))
return false
case memo.RelExpr:
// We know that this is not a correlated subquery since
// HasCorrelatedSubquery was already checked above. Uncorrelated
// subqueries never have references.
return false
}
for i, n := 0, e.ChildCount(); i < n; i++ {
if findDupRefs(e.Child(i)) {
return true
}
}
return false
}
if findDupRefs(item.Element) {
return true
}
}
return false
}
// CanInlineProjections returns true if all projection expressions can be
// inlined. See CanInline for details.
func (c *CustomFuncs) CanInlineProjections(projections memo.ProjectionsExpr) bool {
for i := range projections {
if !c.CanInline(projections[i].Element) {
return false
}
}
return true
}
// CanInline returns true if the given expression consists only of "simple"
// operators like Variable, Const, Eq, and Plus. These operators are assumed to
// be relatively inexpensive to evaluate, and therefore potentially evaluating
// them multiple times is not a big concern.
func (c *CustomFuncs) CanInline(scalar opt.ScalarExpr) bool {
switch scalar.Op() {
case opt.AndOp, opt.OrOp, opt.NotOp, opt.TrueOp, opt.FalseOp,
opt.EqOp, opt.NeOp, opt.LeOp, opt.LtOp, opt.GeOp, opt.GtOp,
opt.IsOp, opt.IsNotOp, opt.InOp, opt.NotInOp,
opt.VariableOp, opt.ConstOp, opt.NullOp,
opt.PlusOp, opt.MinusOp, opt.MultOp:
// Recursively verify that children are also inlinable.
for i, n := 0, scalar.ChildCount(); i < n; i++ {
if !c.CanInline(scalar.Child(i).(opt.ScalarExpr)) {
return false
}
}
return true
}
return false
}
// InlineSelectProject searches the filter conditions for any variable
// references to columns from the given projections expression. Each variable is
// replaced by the corresponding inlined projection expression.
func (c *CustomFuncs) InlineSelectProject(
filters memo.FiltersExpr, projections memo.ProjectionsExpr,
) memo.FiltersExpr {
newFilters := make(memo.FiltersExpr, len(filters))
for i := range filters {
item := &filters[i]
newFilters[i].Condition = c.inlineProjections(item.Condition, projections).(opt.ScalarExpr)
}
return newFilters
}
// InlineProjectProject searches the projection expressions for any variable
// references to columns from the given input (which must be a Project
// operator). Each variable is replaced by the corresponding inlined projection
// expression.
func (c *CustomFuncs) InlineProjectProject(
input memo.RelExpr, projections memo.ProjectionsExpr, passthrough opt.ColSet,
) memo.RelExpr {
innerProject := input.(*memo.ProjectExpr)
innerProjections := innerProject.Projections
newProjections := make(memo.ProjectionsExpr, len(projections))
for i := range projections {
item := &projections[i]
newItem := &newProjections[i]
newItem.Element = c.inlineProjections(item.Element, innerProjections).(opt.ScalarExpr)
newItem.Col = item.Col
}
// Add any outer passthrough columns that refer to inner synthesized columns.
newPassthrough := passthrough
if !newPassthrough.Empty() {
for i := range innerProjections {
item := &innerProjections[i]
if newPassthrough.Contains(int(item.Col)) {
newProjections = append(newProjections, *item)
newPassthrough.Remove(int(item.Col))
}
}
}
return c.f.ConstructProject(innerProject.Input, newProjections, newPassthrough)
}
// Recursively walk the tree looking for references to projection expressions
// that need to be replaced.
func (c *CustomFuncs) inlineProjections(e opt.Expr, projections memo.ProjectionsExpr) opt.Expr {
var replace ReconstructFunc
replace = func(e opt.Expr) opt.Expr {
switch t := e.(type) {
case *memo.VariableExpr:
for i := range projections {
if projections[i].Col == t.Col {
return projections[i].Element
}
}
return t
case memo.RelExpr:
if !c.OuterCols(t).Empty() {
// Should have prevented this in HasDuplicateRefs/HasCorrelatedSubquery.
panic("cannot inline references within correlated subqueries")
}
// No projections references possible, since there are no outer cols.
return t
}
return c.f.Reconstruct(e, replace)
}
return replace(e)
}