-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
operator.go
473 lines (401 loc) · 13.5 KB
/
operator.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package colexec
import (
"context"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/sql/colexecbase"
"github.com/cockroachdb/cockroach/pkg/sql/colexecbase/colexecerror"
"github.com/cockroachdb/cockroach/pkg/sql/colmem"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)
// OperatorInitStatus indicates whether Init method has already been called on
// an Operator.
type OperatorInitStatus int
const (
// OperatorNotInitialized indicates that Init has not been called yet.
OperatorNotInitialized OperatorInitStatus = iota
// OperatorInitialized indicates that Init has already been called.
OperatorInitialized
)
// NonExplainable is a marker interface which identifies an Operator that
// should be omitted from the output of EXPLAIN (VEC). Note that VERBOSE
// explain option will override the omitting behavior.
type NonExplainable interface {
// nonExplainableMarker is just a marker method. It should never be called.
nonExplainableMarker()
}
// NewOneInputNode returns an execinfra.OpNode with a single Operator input.
func NewOneInputNode(input colexecbase.Operator) OneInputNode {
return OneInputNode{input: input}
}
// OneInputNode is an execinfra.OpNode with a single Operator input.
type OneInputNode struct {
input colexecbase.Operator
}
// ChildCount implements the execinfra.OpNode interface.
func (OneInputNode) ChildCount(verbose bool) int {
return 1
}
// Child implements the execinfra.OpNode interface.
func (n OneInputNode) Child(nth int, verbose bool) execinfra.OpNode {
if nth == 0 {
return n.input
}
colexecerror.InternalError(errors.AssertionFailedf("invalid index %d", nth))
// This code is unreachable, but the compiler cannot infer that.
return nil
}
// Input returns the single input of this OneInputNode as an Operator.
func (n OneInputNode) Input() colexecbase.Operator {
return n.input
}
// newTwoInputNode returns an execinfra.OpNode with two Operator inputs.
func newTwoInputNode(inputOne, inputTwo colexecbase.Operator) twoInputNode {
return twoInputNode{inputOne: inputOne, inputTwo: inputTwo}
}
type twoInputNode struct {
inputOne colexecbase.Operator
inputTwo colexecbase.Operator
}
func (twoInputNode) ChildCount(verbose bool) int {
return 2
}
func (n *twoInputNode) Child(nth int, verbose bool) execinfra.OpNode {
switch nth {
case 0:
return n.inputOne
case 1:
return n.inputTwo
}
colexecerror.InternalError(errors.AssertionFailedf("invalid idx %d", nth))
// This code is unreachable, but the compiler cannot infer that.
return nil
}
// TODO(yuzefovich): audit all Operators to make sure that all internal memory
// is accounted for.
// InternalMemoryOperator is an interface that operators which use internal
// memory need to implement. "Internal memory" is defined as memory that is
// "private" to the operator and is not exposed to the outside; notably, it
// does *not* include any coldata.Batch'es and coldata.Vec's.
type InternalMemoryOperator interface {
colexecbase.Operator
// InternalMemoryUsage reports the internal memory usage (in bytes) of an
// operator.
InternalMemoryUsage() int
}
// resetter is an interface that operators can implement if they can be reset
// either for reusing (to keep the already allocated memory) or during tests.
type resetter interface {
reset(ctx context.Context)
}
// ResettableOperator is an Operator that can be reset.
type ResettableOperator interface {
colexecbase.Operator
resetter
}
// Closer is an object that releases resources when Close is called. Note that
// this interface must be implemented by all operators that could be planned on
// top of other operators that do actually need to release the resources (e.g.
// if we have a simple project on top of a disk-backed operator, that simple
// project needs to implement this interface so that Close() call could be
// propagated correctly).
type Closer interface {
Close(ctx context.Context) error
}
// Closers is a slice of Closers.
type Closers []Closer
// CloseAndLogOnErr closes all Closers and logs the error if the log verbosity
// is 1 or higher. The given prefix is prepended to the log message.
// Note: this method should *only* be used when returning an error doesn't make
// sense.
func (c Closers) CloseAndLogOnErr(ctx context.Context, prefix string) {
prefix += ":"
for _, closer := range c {
if err := closer.Close(ctx); err != nil && log.V(1) {
log.Infof(ctx, "%s error closing Closer: %v", prefix, err)
}
}
}
// Close closes all Closers and returns the last error (if any occurs).
func (c Closers) Close(ctx context.Context) error {
var lastErr error
for _, closer := range c {
if err := closer.Close(ctx); err != nil {
lastErr = err
}
}
return lastErr
}
// CallbackCloser is a utility struct that implements the Closer interface by
// calling a provided callback.
type CallbackCloser struct {
CloseCb func(context.Context) error
}
// Close implements the Closer interface.
func (c *CallbackCloser) Close(ctx context.Context) error {
return c.CloseCb(ctx)
}
// closerHelper is a simple helper that helps Operators implement
// Closer. If close returns true, resources may be released, if it
// returns false, close has already been called.
// use.
type closerHelper struct {
closed bool
}
// close marks the closerHelper as closed. If true is returned, this is the
// first call to close.
func (c *closerHelper) close() bool {
if c.closed {
return false
}
c.closed = true
return true
}
type closableOperator interface {
colexecbase.Operator
Closer
}
func makeOneInputCloserHelper(input colexecbase.Operator) oneInputCloserHelper {
return oneInputCloserHelper{
OneInputNode: NewOneInputNode(input),
}
}
type oneInputCloserHelper struct {
OneInputNode
closerHelper
}
var _ Closer = &oneInputCloserHelper{}
func (c *oneInputCloserHelper) Close(ctx context.Context) error {
if !c.close() {
return nil
}
if closer, ok := c.input.(Closer); ok {
return closer.Close(ctx)
}
return nil
}
type noopOperator struct {
OneInputNode
NonExplainable
}
var _ colexecbase.Operator = &noopOperator{}
// NewNoop returns a new noop Operator.
func NewNoop(input colexecbase.Operator) colexecbase.Operator {
return &noopOperator{OneInputNode: NewOneInputNode(input)}
}
func (n *noopOperator) Init() {
n.input.Init()
}
func (n *noopOperator) Next(ctx context.Context) coldata.Batch {
return n.input.Next(ctx)
}
func (n *noopOperator) reset(ctx context.Context) {
if r, ok := n.input.(resetter); ok {
r.reset(ctx)
}
}
type zeroOperator struct {
OneInputNode
NonExplainable
}
var _ colexecbase.Operator = &zeroOperator{}
// NewZeroOp creates a new operator which just returns an empty batch.
func NewZeroOp(input colexecbase.Operator) colexecbase.Operator {
return &zeroOperator{OneInputNode: NewOneInputNode(input)}
}
func (s *zeroOperator) Init() {
s.input.Init()
}
func (s *zeroOperator) Next(ctx context.Context) coldata.Batch {
return coldata.ZeroBatch
}
type zeroOperatorNoInput struct {
colexecbase.ZeroInputNode
NonExplainable
}
var _ colexecbase.Operator = &zeroOperatorNoInput{}
// NewZeroOpNoInput creates a new operator which just returns an empty batch
// and doesn't an input.
func NewZeroOpNoInput() colexecbase.Operator {
return &zeroOperatorNoInput{}
}
func (s *zeroOperatorNoInput) Init() {}
func (s *zeroOperatorNoInput) Next(ctx context.Context) coldata.Batch {
return coldata.ZeroBatch
}
type singleTupleNoInputOperator struct {
colexecbase.ZeroInputNode
NonExplainable
batch coldata.Batch
nexted bool
}
var _ colexecbase.Operator = &singleTupleNoInputOperator{}
// NewSingleTupleNoInputOp creates a new Operator which returns a batch of
// length 1 with no actual columns on the first call to Next() and zero-length
// batches on all consecutive calls.
func NewSingleTupleNoInputOp(allocator *colmem.Allocator) colexecbase.Operator {
return &singleTupleNoInputOperator{
batch: allocator.NewMemBatchWithFixedCapacity(nil /* types */, 1 /* size */),
}
}
func (s *singleTupleNoInputOperator) Init() {
}
func (s *singleTupleNoInputOperator) Next(ctx context.Context) coldata.Batch {
s.batch.ResetInternalBatch()
if s.nexted {
return coldata.ZeroBatch
}
s.nexted = true
s.batch.SetLength(1)
return s.batch
}
// FeedOperator is used to feed an Operator chain with input by manually
// setting the next batch.
type FeedOperator struct {
colexecbase.ZeroInputNode
NonExplainable
batch coldata.Batch
}
// NewFeedOperator returns a new feed operator.
func NewFeedOperator() *FeedOperator {
return &FeedOperator{}
}
// Init implements the colexecbase.Operator interface.
func (FeedOperator) Init() {}
// Next implements the colexecbase.Operator interface.
func (o *FeedOperator) Next(context.Context) coldata.Batch {
return o.batch
}
var _ colexecbase.Operator = &FeedOperator{}
// vectorTypeEnforcer is a utility Operator that on every call to Next
// enforces that non-zero length batch from the input has a vector of the
// desired type in the desired position. If the width of the batch is less than
// the desired position, a new vector will be appended; if the batch has a
// well-typed vector of an undesired type in the desired position, an error
// will occur.
//
// This Operator is designed to be planned as a wrapper on the input to a
// "projecting" Operator (such Operator that has a single column as its output
// and does not touch other columns by simply passing them along).
//
// The intended diagram is as follows:
//
// original input (with schema [t1, ..., tN])
// --------------
// |
// ↓
// vectorTypeEnforcer (will enforce that tN+1 = outputType)
// ------------------
// |
// ↓
// "projecting" operator (projects its output of type outputType
// --------------------- in column at position of N+1)
//
type vectorTypeEnforcer struct {
oneInputCloserHelper
NonExplainable
allocator *colmem.Allocator
typ *types.T
idx int
}
var _ ResettableOperator = &vectorTypeEnforcer{}
func newVectorTypeEnforcer(
allocator *colmem.Allocator, input colexecbase.Operator, typ *types.T, idx int,
) colexecbase.Operator {
return &vectorTypeEnforcer{
oneInputCloserHelper: makeOneInputCloserHelper(input),
allocator: allocator,
typ: typ,
idx: idx,
}
}
func (e *vectorTypeEnforcer) Init() {
e.input.Init()
}
func (e *vectorTypeEnforcer) Next(ctx context.Context) coldata.Batch {
b := e.input.Next(ctx)
if b.Length() == 0 {
return b
}
e.allocator.MaybeAppendColumn(b, e.typ, e.idx)
return b
}
func (e *vectorTypeEnforcer) reset(ctx context.Context) {
if r, ok := e.input.(resetter); ok {
r.reset(ctx)
}
}
// BatchSchemaSubsetEnforcer is similar to vectorTypeEnforcer in its purpose,
// but it enforces that the subset of the columns of the non-zero length batch
// satisfies the desired schema. It needs to wrap the input to a "projecting"
// operator that internally uses other "projecting" operators (for example,
// caseOp and logical projection operators). This operator supports type
// schemas with unsupported types in which case in the corresponding
// position an "unknown" vector can be appended.
//
// The word "subset" is actually more like a "range", but we chose the former
// since the latter is overloaded.
//
// NOTE: the type schema passed into BatchSchemaSubsetEnforcer *must* include
// the output type of the Operator that the enforcer will be the input to.
type BatchSchemaSubsetEnforcer struct {
oneInputCloserHelper
NonExplainable
allocator *colmem.Allocator
typs []*types.T
subsetStartIdx, subsetEndIdx int
}
var _ colexecbase.Operator = &BatchSchemaSubsetEnforcer{}
// NewBatchSchemaSubsetEnforcer creates a new BatchSchemaSubsetEnforcer.
// - subsetStartIdx and subsetEndIdx define the boundaries of the range of
// columns that the projecting operator and its internal projecting operators
// own.
func NewBatchSchemaSubsetEnforcer(
allocator *colmem.Allocator,
input colexecbase.Operator,
typs []*types.T,
subsetStartIdx, subsetEndIdx int,
) *BatchSchemaSubsetEnforcer {
return &BatchSchemaSubsetEnforcer{
oneInputCloserHelper: makeOneInputCloserHelper(input),
allocator: allocator,
typs: typs,
subsetStartIdx: subsetStartIdx,
subsetEndIdx: subsetEndIdx,
}
}
// Init implements the colexecbase.Operator interface.
func (e *BatchSchemaSubsetEnforcer) Init() {
e.input.Init()
if e.subsetStartIdx >= e.subsetEndIdx {
colexecerror.InternalError(errors.AssertionFailedf("unexpectedly subsetStartIdx is not less than subsetEndIdx"))
}
}
// Next implements the colexecbase.Operator interface.
func (e *BatchSchemaSubsetEnforcer) Next(ctx context.Context) coldata.Batch {
b := e.input.Next(ctx)
if b.Length() == 0 {
return b
}
for i := e.subsetStartIdx; i < e.subsetEndIdx; i++ {
e.allocator.MaybeAppendColumn(b, e.typs[i], i)
}
return b
}
// SetTypes sets the types of this schema subset enforcer, and sets the end
// of the range of enforced columns to the length of the input types.
func (e *BatchSchemaSubsetEnforcer) SetTypes(typs []*types.T) {
e.typs = typs
e.subsetEndIdx = len(typs)
}