-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathlogical_props_factory.go
382 lines (306 loc) · 12.8 KB
/
logical_props_factory.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
// 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/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sem/types"
)
// logicalPropsFactory is a helper class that consolidates the code that
// derives a parent expression's logical properties from those of its
// children.
// NOTE: The factory is defined as an empty struct with methods rather than as
// functions in order to keep the methods grouped and self-contained.
type logicalPropsFactory struct {
evalCtx *tree.EvalContext
}
// constructProps is called by the memo group construction code in order to
// initialize the new group's logical properties.
// NOTE: When deriving properties from children, be sure to keep the child
// properties immutable by copying them if necessary.
// NOTE: The parent expression is passed as an ExprView for convenient access
// to children, but certain properties on it are not yet defined (like
// its logical properties!).
func (f logicalPropsFactory) constructProps(ev ExprView) LogicalProps {
if ev.IsRelational() {
return f.constructRelationalProps(ev)
}
return f.constructScalarProps(ev)
}
func (f logicalPropsFactory) constructRelationalProps(ev ExprView) LogicalProps {
switch ev.Operator() {
case opt.ScanOp:
return f.constructScanProps(ev)
case opt.SelectOp:
return f.constructSelectProps(ev)
case opt.ProjectOp:
return f.constructProjectProps(ev)
case opt.ValuesOp:
return f.constructValuesProps(ev)
case opt.InnerJoinOp, opt.LeftJoinOp, opt.RightJoinOp, opt.FullJoinOp,
opt.SemiJoinOp, opt.AntiJoinOp, opt.InnerJoinApplyOp, opt.LeftJoinApplyOp,
opt.RightJoinApplyOp, opt.FullJoinApplyOp, opt.SemiJoinApplyOp, opt.AntiJoinApplyOp:
return f.constructJoinProps(ev)
case opt.UnionOp, opt.IntersectOp, opt.ExceptOp,
opt.UnionAllOp, opt.IntersectAllOp, opt.ExceptAllOp:
return f.constructSetProps(ev)
case opt.GroupByOp:
return f.constructGroupByProps(ev)
case opt.LimitOp:
return f.constructLimitProps(ev)
case opt.OffsetOp, opt.Max1RowOp:
return f.passThroughRelationalProps(ev, 0 /* childIdx */)
}
panic(fmt.Sprintf("unrecognized relational expression type: %v", ev.op))
}
func (f logicalPropsFactory) constructScanProps(ev ExprView) LogicalProps {
props := LogicalProps{Relational: &RelationalProps{}}
md := ev.Metadata()
def := ev.Private().(*ScanOpDef)
tab := md.Table(def.Table)
// Scan output columns are stored in the definition.
props.Relational.OutputCols = def.Cols
// Initialize not-NULL columns from the table schema.
for i := 0; i < tab.ColumnCount(); i++ {
if !tab.Column(i).IsNullable() {
colID := md.TableColumn(def.Table, i)
if def.Cols.Contains(int(colID)) {
props.Relational.NotNullCols.Add(int(colID))
}
}
}
// Initialize weak keys from the table schema.
props.Relational.WeakKeys = md.TableWeakKeys(def.Table)
filterWeakKeys(props.Relational)
// TODO: Need actual number of rows.
if def.Constraint != nil {
props.Relational.Stats.RowCount = 100
} else {
props.Relational.Stats.RowCount = 1000
}
// Cap number of rows at limit, if it exists.
if def.HardLimit > 0 && uint64(def.HardLimit) < props.Relational.Stats.RowCount {
props.Relational.Stats.RowCount = uint64(def.HardLimit)
}
return props
}
func (f logicalPropsFactory) constructSelectProps(ev ExprView) LogicalProps {
props := LogicalProps{Relational: &RelationalProps{}}
inputProps := ev.lookupChildGroup(0).logical.Relational
// Inherit input properties as starting point.
*props.Relational = *inputProps
// TODO: Need better estimate based on actual filter conditions.
props.Relational.Stats.RowCount = inputProps.Stats.RowCount / 10
return props
}
func (f logicalPropsFactory) constructProjectProps(ev ExprView) LogicalProps {
props := LogicalProps{Relational: &RelationalProps{}}
inputProps := ev.lookupChildGroup(0).logical.Relational
// Use output columns from projection list.
props.Relational.OutputCols = opt.ColListToSet(ev.Child(1).Private().(opt.ColList))
// Inherit not null columns from input, but only use those that are also
// output columns.
props.Relational.NotNullCols = inputProps.NotNullCols
filterNullCols(props.Relational)
// Inherit outer columns from input.
props.Relational.OuterCols = inputProps.OuterCols
// Inherit weak keys that are composed entirely of output columns.
props.Relational.WeakKeys = inputProps.WeakKeys
filterWeakKeys(props.Relational)
props.Relational.Stats.RowCount = inputProps.Stats.RowCount
return props
}
func (f logicalPropsFactory) constructJoinProps(ev ExprView) LogicalProps {
props := LogicalProps{Relational: &RelationalProps{}}
leftProps := ev.lookupChildGroup(0).logical.Relational
rightProps := ev.lookupChildGroup(1).logical.Relational
// Output columns are union of columns from left and right inputs, except
// in case of semi and anti joins, which only project the left columns.
props.Relational.OutputCols = leftProps.OutputCols.Copy()
switch ev.Operator() {
case opt.SemiJoinOp, opt.AntiJoinOp, opt.SemiJoinApplyOp, opt.AntiJoinApplyOp:
default:
props.Relational.OutputCols.UnionWith(rightProps.OutputCols)
}
// Left/full outer joins can result in right columns becoming null.
// Otherwise, propagate not null setting from right child.
switch ev.Operator() {
case opt.LeftJoinOp, opt.FullJoinOp, opt.LeftJoinApplyOp, opt.FullJoinApplyOp,
opt.SemiJoinOp, opt.SemiJoinApplyOp, opt.AntiJoinOp, opt.AntiJoinApplyOp:
default:
props.Relational.NotNullCols = rightProps.NotNullCols.Copy()
}
// Right/full outer joins can result in left columns becoming null.
// Otherwise, propagate not null setting from left child.
switch ev.Operator() {
case opt.RightJoinOp, opt.FullJoinOp, opt.RightJoinApplyOp, opt.FullJoinApplyOp:
default:
props.Relational.NotNullCols.UnionWith(leftProps.NotNullCols)
}
// TODO: Need better estimate based on actual on conditions.
props.Relational.Stats.RowCount = leftProps.Stats.RowCount * rightProps.Stats.RowCount
if ev.Child(2).Operator() != opt.TrueOp {
props.Relational.Stats.RowCount /= 10
}
return props
}
func (f logicalPropsFactory) constructGroupByProps(ev ExprView) LogicalProps {
props := LogicalProps{Relational: &RelationalProps{}}
inputProps := ev.lookupChildGroup(0).logical.Relational
// Output columns are the union of grouping columns with columns from the
// aggregate projection list.
groupingColSet := ev.Private().(opt.ColSet)
props.Relational.OutputCols = groupingColSet
aggColList := ev.Child(1).Private().(opt.ColList)
props.Relational.OutputCols.UnionWith(opt.ColListToSet(aggColList))
// Propagate not null setting from input columns that are being grouped.
props.Relational.NotNullCols = inputProps.NotNullCols.Copy()
props.Relational.NotNullCols.IntersectionWith(groupingColSet)
// Scalar group by has no grouping columns and always a single row.
if groupingColSet.Empty() {
// Any combination of columns is a weak key when there is one row.
props.Relational.WeakKeys = opt.WeakKeys{groupingColSet}
props.Relational.Stats.RowCount = 1
} else {
// The grouping columns always form a key because the GroupBy operation
// eliminates all duplicates. The result WeakKeys property either contains
// only the grouping column set, or else it contains one or more weak keys
// that are strict subsets of the grouping column set. This is because
// the grouping column set contains every output column (except aggregate
// columns, which aren't relevant since they're newly synthesized).
if inputProps.WeakKeys.ContainsSubsetOf(groupingColSet) {
props.Relational.WeakKeys = inputProps.WeakKeys
filterWeakKeys(props.Relational)
} else {
props.Relational.WeakKeys = opt.WeakKeys{groupingColSet}
}
// TODO: Need better estimate.
props.Relational.Stats.RowCount = inputProps.Stats.RowCount / 10
}
return props
}
func (f logicalPropsFactory) constructSetProps(ev ExprView) LogicalProps {
props := LogicalProps{Relational: &RelationalProps{}}
leftProps := ev.lookupChildGroup(0).logical.Relational
rightProps := ev.lookupChildGroup(1).logical.Relational
colMap := *ev.Private().(*SetOpColMap)
if len(colMap.Out) != len(colMap.Left) || len(colMap.Out) != len(colMap.Right) {
panic(fmt.Errorf("lists in SetOpColMap are not all the same length. new:%d, left:%d, right:%d",
len(colMap.Out), len(colMap.Left), len(colMap.Right)))
}
// Set the new output columns.
props.Relational.OutputCols = opt.ColListToSet(colMap.Out)
// Columns have to be not-null on both sides to be not-null in result.
// colMap matches columns on the left and right sides of the operator
// with the output columns, since OutputCols are not ordered and may
// not correspond to each other.
for i := range colMap.Out {
if leftProps.NotNullCols.Contains(int((colMap.Left)[i])) &&
rightProps.NotNullCols.Contains(int((colMap.Right)[i])) {
props.Relational.NotNullCols.Add(int((colMap.Out)[i]))
}
}
// TODO: Need better estimate.
switch ev.Operator() {
case opt.UnionOp, opt.UnionAllOp:
props.Relational.Stats.RowCount = leftProps.Stats.RowCount + rightProps.Stats.RowCount
default:
props.Relational.Stats.RowCount = (leftProps.Stats.RowCount + rightProps.Stats.RowCount) / 2
}
return props
}
func (f logicalPropsFactory) constructValuesProps(ev ExprView) LogicalProps {
props := LogicalProps{Relational: &RelationalProps{}}
// Use output columns that are attached to the values op.
props.Relational.OutputCols = opt.ColListToSet(ev.Private().(opt.ColList))
props.Relational.Stats.RowCount = uint64(ev.ChildCount())
return props
}
func (f logicalPropsFactory) constructLimitProps(ev ExprView) LogicalProps {
props := LogicalProps{Relational: &RelationalProps{}}
inputProps := ev.Child(0).Logical().Relational
limit := ev.Child(1)
// Start with pass-through props from input.
*props.Relational = *inputProps
// Update row count if limit is a constant.
if limit.Operator() == opt.ConstOp {
hardLimit := *limit.Private().(*tree.DInt)
if hardLimit > 0 {
props.Relational.Stats.RowCount = uint64(hardLimit)
}
}
return props
}
// passThroughRelationalProps returns the relational properties of the given
// child group.
func (f logicalPropsFactory) passThroughRelationalProps(ev ExprView, childIdx int) LogicalProps {
// Properties are immutable after construction, so just inherit relational
// props pointer from child.
return LogicalProps{Relational: ev.lookupChildGroup(childIdx).logical.Relational}
}
func (f logicalPropsFactory) constructScalarProps(ev ExprView) LogicalProps {
props := LogicalProps{Scalar: &ScalarProps{Type: InferType(ev)}}
switch ev.Operator() {
case opt.VariableOp:
// Variable introduces outer column.
props.Scalar.OuterCols.Add(int(ev.Private().(opt.ColumnID)))
return props
}
// By default, union outer cols from all children, both relational and scalar.
for i := 0; i < ev.ChildCount(); i++ {
logical := &ev.lookupChildGroup(i).logical
if logical.Scalar != nil {
props.Scalar.OuterCols.UnionWith(logical.Scalar.OuterCols)
} else {
props.Scalar.OuterCols.UnionWith(logical.Relational.OuterCols)
}
}
if props.Scalar.Type == types.Bool {
cb := constraintsBuilder{md: ev.Metadata(), evalCtx: f.evalCtx}
props.Scalar.Constraints, props.Scalar.TightConstraints = cb.buildConstraints(ev)
}
return props
}
// filterNullCols ensures that the set of null columns is a subset of the output
// columns. It respects immutability by making a copy of the null columns if
// they need to be updated.
func filterNullCols(props *RelationalProps) {
if !props.NotNullCols.SubsetOf(props.OutputCols) {
props.NotNullCols = props.NotNullCols.Intersection(props.OutputCols)
}
}
// filterWeakKeys ensures that each weak key is a subset of the output columns.
// It respects immutability by making a copy of the weak keys if they need to be
// updated.
func filterWeakKeys(props *RelationalProps) {
var filtered opt.WeakKeys
for i, weakKey := range props.WeakKeys {
// Discard weak keys that have columns that are not part of the output
// column set.
if !weakKey.SubsetOf(props.OutputCols) {
if filtered == nil {
filtered = make(opt.WeakKeys, i, len(props.WeakKeys)-1)
copy(filtered, props.WeakKeys[:i])
}
} else {
if filtered != nil {
filtered = append(filtered, weakKey)
}
}
}
if filtered != nil {
props.WeakKeys = filtered
}
}