-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathexpr.go
280 lines (237 loc) · 9.13 KB
/
expr.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
// 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 memo
import (
"fmt"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/props"
"github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical"
"github.com/cockroachdb/cockroach/pkg/sql/sem/types"
)
// RelExpr is implemented by all operators tagged as Relational. Relational
// expressions have a set of logical properties that describe the content and
// characteristics of their behavior and results. They are stored as part of a
// memo group that contains other logically equivalent expressions. Expressions
// in the same memo group are linked together in a list that can be traversed
// via calls to FirstExpr and NextExpr:
//
// +--------------------------------------+
// | +---------------+ |
// | | |FirstExpr |FirstExpr
// v v | |
// member #1 -------> member #2 --------> member #3 -------> nil
// NextExpr NextExpr NextExpr
//
// A relational expression's physical properties and cost are defined once it
// has been optimized.
type RelExpr interface {
opt.Expr
// Memo is the memo which contains this relational expression.
Memo() *Memo
// Relational is the set of logical properties that describe the content and
// characteristics of this expression's behavior and results.
Relational() *props.Relational
// RequiredPhysical is the set of required physical properties with respect to
// which this expression was optimized. Enforcers may be added to the
// expression tree to ensure the physical properties are provided.
//
// Set when optimization is complete, only for the expressions in the final
// tree.
RequiredPhysical() *physical.Required
// ProvidedPhysical is the set of provided physical properties (which must be
// compatible with the set of required physical properties).
//
// Set when optimization is complete, only for the expressions in the final
// tree.
ProvidedPhysical() *physical.Provided
// Cost is an estimate of the cost of executing this expression tree. Set
// when optimization is complete, only for the expressions in the final tree.
Cost() Cost
// FirstExpr returns the first member expression in the memo group (could be
// this expression if it happens to be first in the group). Subsequent members
// can be enumerated by then calling NextExpr. Note that enforcer operators
// are not part of this list (but do maintain a link to it).
FirstExpr() RelExpr
// NextExpr returns the next member expression in the memo group, or nil if
// there are no further members in the group.
NextExpr() RelExpr
// group returns the memo group that contains this expression and any other
// logically equivalent expressions. There is one group struct for each memo
// group that stores the properties for the group, as well as the pointer to
// the first member of the group.
group() exprGroup
// bestProps returns the instance of bestProps associated with this
// expression.
bestProps() *bestProps
// setNext sets this expression's next pointer to point to the given
// expression. setNext will panic if the next pointer has already been set.
setNext(e RelExpr)
}
// ScalarPropsExpr is implemented by scalar expressions which cache scalar
// properties, like FiltersExpr and ProjectionsExpr. The scalar properties are
// lazily populated only when requested by walking the expression subtree.
type ScalarPropsExpr interface {
opt.ScalarExpr
// ScalarProps returns the scalar properties associated with the expression.
// These can be lazily calculated using the given memo as context.
ScalarProps(mem *Memo) *props.Scalar
}
// TrueSingleton is a global instance of TrueExpr, to avoid allocations.
var TrueSingleton = &TrueExpr{}
// FalseSingleton is a global instance of FalseExpr, to avoid allocations.
var FalseSingleton = &FalseExpr{}
// NullSingleton is a global instance of NullExpr having the Unknown type (most
// common case), to avoid allocations.
var NullSingleton = &NullExpr{Typ: types.Unknown}
// CountRowsSingleton maintains a global instance of CountRowsExpr, to avoid
// allocations.
var CountRowsSingleton = &CountRowsExpr{}
// TrueFilter is a global instance of the empty FiltersExpr, used in situations
// where the filter should always evaluate to true:
//
// SELECT * FROM a INNER JOIN b ON True
//
var TrueFilter = FiltersExpr{}
// FalseFilter is a global instance of a FiltersExpr that contains a single
// False expression, used in contradiction situations:
//
// SELECT * FROM a WHERE 1=0
//
var FalseFilter = FiltersExpr{{Condition: FalseSingleton}}
// EmptyTuple is a global instance of a TupleExpr that contains no elements.
// While this cannot be created in SQL, it can be the created by normalizations.
var EmptyTuple = &TupleExpr{Typ: types.TTuple{}}
// ScalarListWithEmptyTuple is a global instance of a ScalarListExpr containing
// a TupleExpr that contains no elements. It's used when constructing an empty
// ValuesExpr:
//
// SELECT 1
//
var ScalarListWithEmptyTuple = ScalarListExpr{EmptyTuple}
// EmptyGroupingPrivate is a global instance of a GroupingPrivate that has no
// grouping columns and no ordering.
var EmptyGroupingPrivate = GroupingPrivate{}
// MaxAggChildren is the maximum number of children that any aggregate operator
// can have.
const MaxAggChildren = 3
// LastGroupMember returns the last member in the same memo group of the given
// relational expression.
func LastGroupMember(e RelExpr) RelExpr {
for {
next := e.NextExpr()
if next == nil {
return e
}
e = next
}
}
// IsTrue is true if the FiltersExpr always evaluates to true. This is the case
// when it has zero conditions.
func (n FiltersExpr) IsTrue() bool {
return len(n) == 0
}
// IsFalse is true if the FiltersExpr always evaluates to false. The only case
// that's checked is the fully normalized case, when the list contains a single
// False condition.
func (n FiltersExpr) IsFalse() bool {
return len(n) == 1 && n[0].Condition.Op() == opt.FalseOp
}
// OuterCols returns the set of outer columns needed by any of the filter
// condition expressions.
func (n FiltersExpr) OuterCols(mem *Memo) opt.ColSet {
var colSet opt.ColSet
for i := range n {
colSet.UnionWith(n[i].ScalarProps(mem).OuterCols)
}
return colSet
}
// OutputCols returns the set of columns constructed by the Aggregations
// expression.
func (n AggregationsExpr) OutputCols() opt.ColSet {
var colSet opt.ColSet
for i := range n {
colSet.Add(int(n[i].Col))
}
return colSet
}
// OuterCols returns the set of outer columns needed by any of the zip
// expressions.
func (n ZipExpr) OuterCols(mem *Memo) opt.ColSet {
var colSet opt.ColSet
for i := range n {
colSet.UnionWith(n[i].ScalarProps(mem).OuterCols)
}
return colSet
}
// OutputCols returns the set of columns constructed by the Zip expression.
func (n ZipExpr) OutputCols() opt.ColSet {
var colSet opt.ColSet
for i := range n {
for _, col := range n[i].Cols {
colSet.Add(int(col))
}
}
return colSet
}
// TupleOrdinal is an ordinal index into an expression of type Tuple. It is
// used by the ColumnAccess scalar expression.
type TupleOrdinal uint32
// ScanLimit is used for a limited table or index scan and stores the limit as
// well as the desired scan direction. A value of 0 means that there is no
// limit.
type ScanLimit int64
// MakeScanLimit initializes a ScanLimit with a number of rows and a direction.
func MakeScanLimit(rowCount int64, reverse bool) ScanLimit {
if reverse {
return ScanLimit(-rowCount)
}
return ScanLimit(rowCount)
}
// IsSet returns true if there is a limit.
func (sl ScanLimit) IsSet() bool {
return sl != 0
}
// RowCount returns the number of rows in the limit.
func (sl ScanLimit) RowCount() int64 {
if sl.Reverse() {
return int64(-sl)
}
return int64(sl)
}
// Reverse returns true if the limit requires a reverse scan.
func (sl ScanLimit) Reverse() bool {
return sl < 0
}
func (sl ScanLimit) String() string {
if sl.Reverse() {
return fmt.Sprintf("%d(rev)", -sl)
}
return fmt.Sprintf("%d", sl)
}
// ScanFlags stores any flags for the scan specified in the query (see
// tree.IndexFlags). These flags may be consulted by transformation rules or the
// coster.
type ScanFlags struct {
// NoIndexJoin disallows use of non-covering indexes (index-join) for scanning
// this table.
NoIndexJoin bool
// ForceIndex forces the use of a specific index (specified in Index).
// ForceIndex and NoIndexJoin cannot both be set at the same time.
ForceIndex bool
Index int
}
// Empty returns true if there are no flags set.
func (sf *ScanFlags) Empty() bool {
return !sf.NoIndexJoin && !sf.ForceIndex
}