-
Notifications
You must be signed in to change notification settings - Fork 0
/
processors.go
413 lines (391 loc) · 11.9 KB
/
processors.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
// 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.
//
// Author: Radu Berinde (radu@cockroachlabs.com)
package distsqlrun
import (
"math"
"sync"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/pkg/errors"
)
// processor is a common interface implemented by all processors, used by the
// higher-level flow orchestration code.
type processor interface {
// Run is the main loop of the processor.
// If wg is non-nil, wg.Done is called before exiting.
Run(ctx context.Context, wg *sync.WaitGroup)
}
// procOutputHelper is a helper type that performs filtering and projection on
// the output of a processor.
type procOutputHelper struct {
numInternalCols int
output RowReceiver
rowAlloc sqlbase.EncDatumRowAlloc
filter *exprHelper
renderExprs []exprHelper
renderTypes []sqlbase.ColumnType
outputCols []uint32
// offset is the number of rows that are suppressed.
offset uint64
// maxRowIdx is the number of rows after which we can stop (offset + limit),
// or MaxUint64 if there is no limit.
maxRowIdx uint64
rowIdx uint64
}
// init sets up a procOutputHelper. The types describe the internal schema of
// the processor (as described for each processor core spec); they can be
// omitted if there is no filtering expression.
func (h *procOutputHelper) init(
post *PostProcessSpec,
types []sqlbase.ColumnType,
evalCtx *parser.EvalContext,
output RowReceiver,
) error {
if !post.Projection && len(post.OutputColumns) > 0 {
return errors.Errorf("post-processing has projection unset but output columns set: %s", post)
}
if post.Projection && len(post.RenderExprs) > 0 {
return errors.Errorf("post-processing has both projection and rendering: %s", post)
}
h.output = output
h.numInternalCols = len(types)
if post.Filter.Expr != "" {
h.filter = &exprHelper{}
if err := h.filter.init(post.Filter, types, evalCtx); err != nil {
return err
}
}
if post.Projection {
for _, col := range post.OutputColumns {
if int(col) >= h.numInternalCols {
return errors.Errorf("invalid output column %d (only %d available)", col, h.numInternalCols)
}
}
h.outputCols = post.OutputColumns
if h.outputCols == nil {
// nil indicates no projection; use an empty slice.
h.outputCols = make([]uint32, 0)
}
} else if len(post.RenderExprs) > 0 {
h.renderExprs = make([]exprHelper, len(post.RenderExprs))
h.renderTypes = make([]sqlbase.ColumnType, len(post.RenderExprs))
for i, expr := range post.RenderExprs {
if err := h.renderExprs[i].init(expr, types, evalCtx); err != nil {
return err
}
h.renderTypes[i] = sqlbase.DatumTypeToColumnType(h.renderExprs[i].expr.ResolvedType())
}
}
h.offset = post.Offset
if post.Limit == 0 || post.Limit >= math.MaxUint64-h.offset {
h.maxRowIdx = math.MaxUint64
} else {
h.maxRowIdx = h.offset + post.Limit
}
return nil
}
// neededColumns calculates the set of internal processor columns that are
// actually used by the post-processing stage.
func (h *procOutputHelper) neededColumns() []bool {
needed := make([]bool, h.numInternalCols)
if h.outputCols == nil && h.renderExprs == nil {
// No projection or rendering; all columns are needed.
for i := range needed {
needed[i] = true
}
return needed
}
for _, c := range h.outputCols {
needed[c] = true
}
if h.filter != nil {
for i := range needed {
if !needed[i] {
needed[i] = h.filter.vars.IndexedVarUsed(i)
}
}
}
if h.renderExprs != nil {
for i := range needed {
if !needed[i] {
for j := range h.renderExprs {
if h.renderExprs[j].vars.IndexedVarUsed(i) {
needed[i] = true
break
}
}
}
}
}
return needed
}
// emitHelper is a utility wrapper on top of procOutputHelper.emitRow().
// It takes a row to emit and, if anything happens other than the normal
// situation where the emitting succeeds and the consumer still needs rows, both
// the (potentially many) inputs and the output are properly closed after
// potentially draining the inputs. It's allowed to not pass any inputs, in
// which case nothing will be drained (this can happen when the caller has
// already fully consumed the inputs).
//
// As opposed to emitRow(), this also supports metadata rows which bypass the
// procOutputHelper and are routed directly to its output.
//
// If the consumer signals the producer to drain, the message is relayed and all
// the draining metadata is consumed and forwarded.
//
// inputs can be nil.
//
// Returns true if more rows are needed, false otherwise. If false is returned,
// both the inputs and the output have been properly closed.
func emitHelper(
ctx context.Context,
output *procOutputHelper,
row sqlbase.EncDatumRow,
meta ProducerMetadata,
inputs ...RowSource,
) bool {
var consumerStatus ConsumerStatus
if !meta.Empty() {
if row != nil {
log.Fatalf(ctx, "both row data and metadata in the same emitHelper call. "+
"row: %s. meta: %+v", row, meta)
}
// Bypass emitRow() and send directly to output.output.
consumerStatus = output.output.Push(nil /* row */, meta)
} else {
var err error
consumerStatus, err = output.emitRow(ctx, row)
if err != nil {
for _, input := range inputs {
input.ConsumerClosed()
}
output.output.Push(nil /* row */, ProducerMetadata{Err: err})
output.close()
return false
}
}
switch consumerStatus {
case NeedMoreRows:
return true
case DrainRequested:
log.VEventf(ctx, 1, "no more rows required. drain requested.")
DrainAndClose(ctx, output.output, nil /* cause */, inputs...)
return false
case ConsumerClosed:
log.VEventf(ctx, 1, "no more rows required. Consumer shut down.")
for _, input := range inputs {
input.ConsumerClosed()
}
output.close()
return false
default:
log.Fatalf(ctx, "unexpected consumerStatus: %d", consumerStatus)
return false
}
}
// emitRow sends a row through the post-processing stage. The same row can be
// reused.
//
// It returns the consumer's status that was observed when pushing this row. If
// an error is returned, it's coming from the procOutputHelper's filtering or
// rendering processing; the output has not been closed.
//
// Note: check out emitHelper() for a useful wrapper.
func (h *procOutputHelper) emitRow(
ctx context.Context, row sqlbase.EncDatumRow,
) (ConsumerStatus, error) {
if h.rowIdx >= h.maxRowIdx {
return DrainRequested, nil
}
if h.filter != nil {
// Filtering.
passes, err := h.filter.evalFilter(row)
if err != nil {
return ConsumerClosed, err
}
if !passes {
if log.V(3) {
log.Infof(ctx, "filtered out row %s", row)
}
return NeedMoreRows, nil
}
}
h.rowIdx++
if h.rowIdx <= h.offset {
// Suppress row.
return NeedMoreRows, nil
}
var outRow sqlbase.EncDatumRow
if h.renderExprs != nil {
// Rendering.
outRow = h.rowAlloc.AllocRow(len(h.renderExprs))
for i := range h.renderExprs {
datum, err := h.renderExprs[i].eval(row)
if err != nil {
return ConsumerClosed, err
}
outRow[i] = sqlbase.DatumToEncDatum(h.renderTypes[i], datum)
}
} else if h.outputCols != nil {
// Projection.
outRow = h.rowAlloc.AllocRow(len(h.outputCols))
for i, col := range h.outputCols {
outRow[i] = row[col]
}
} else {
// No rendering or projection.
outRow = h.rowAlloc.AllocRow(len(row))
copy(outRow, row)
}
if log.V(3) {
log.Infof(ctx, "pushing row %s", outRow)
}
if r := h.output.Push(outRow, ProducerMetadata{}); r != NeedMoreRows {
log.VEventf(ctx, 1, "no more rows required. drain requested: %t",
r == DrainRequested)
return r, nil
}
if h.rowIdx == h.maxRowIdx {
log.VEventf(ctx, 1, "hit row limit; asking producer to drain")
return DrainRequested, nil
}
return NeedMoreRows, nil
}
func (h *procOutputHelper) close() {
h.output.ProducerDone()
}
// noopProcessor is a processor that simply passes rows through from the
// synchronizer to the post-processing stage. It can be useful for its
// post-processing or in the last stage of a computation, where we may only
// need the synchronizer to join streams.
type noopProcessor struct {
flowCtx *FlowCtx
input RowSource
out procOutputHelper
}
var _ processor = &noopProcessor{}
func newNoopProcessor(
flowCtx *FlowCtx, input RowSource, post *PostProcessSpec, output RowReceiver,
) (*noopProcessor, error) {
n := &noopProcessor{flowCtx: flowCtx, input: input}
if err := n.out.init(post, input.Types(), &flowCtx.evalCtx, output); err != nil {
return nil, err
}
return n, nil
}
// Run is part of the processor interface.
func (n *noopProcessor) Run(ctx context.Context, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
ctx, span := tracing.ChildSpan(ctx, "noop")
defer tracing.FinishSpan(span)
for {
row, meta := n.input.Next()
if row == nil && meta.Empty() {
n.out.close()
return
}
if !emitHelper(ctx, &n.out, row, meta, n.input) {
return
}
}
}
func newProcessor(
flowCtx *FlowCtx,
core *ProcessorCoreUnion,
post *PostProcessSpec,
inputs []RowSource,
outputs []RowReceiver,
) (processor, error) {
if core.Noop != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return newNoopProcessor(flowCtx, inputs[0], post, outputs[0])
}
if core.Values != nil {
if err := checkNumInOut(inputs, outputs, 0, 1); err != nil {
return nil, err
}
return newValuesProcessor(flowCtx, core.Values, post, outputs[0])
}
if core.TableReader != nil {
if err := checkNumInOut(inputs, outputs, 0, 1); err != nil {
return nil, err
}
return newTableReader(flowCtx, core.TableReader, post, outputs[0])
}
if core.JoinReader != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return newJoinReader(flowCtx, core.JoinReader, inputs[0], post, outputs[0])
}
if core.Sorter != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return newSorter(flowCtx, core.Sorter, inputs[0], post, outputs[0])
}
if core.Distinct != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return newDistinct(flowCtx, core.Distinct, inputs[0], post, outputs[0])
}
if core.Aggregator != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return newAggregator(flowCtx, core.Aggregator, inputs[0], post, outputs[0])
}
if core.MergeJoiner != nil {
if err := checkNumInOut(inputs, outputs, 2, 1); err != nil {
return nil, err
}
return newMergeJoiner(
flowCtx, core.MergeJoiner, inputs[0], inputs[1], post, outputs[0],
)
}
if core.HashJoiner != nil {
if err := checkNumInOut(inputs, outputs, 2, 1); err != nil {
return nil, err
}
return newHashJoiner(flowCtx, core.HashJoiner, inputs[0], inputs[1], post, outputs[0])
}
if core.Backfiller != nil {
if err := checkNumInOut(inputs, outputs, 0, 1); err != nil {
return nil, err
}
switch core.Backfiller.Type {
case BackfillerSpec_Index:
return newIndexBackfiller(flowCtx, *core.Backfiller, post, outputs[0])
case BackfillerSpec_Column:
return newColumnBackfiller(flowCtx, *core.Backfiller, post, outputs[0])
}
}
if core.SetOp != nil {
if err := checkNumInOut(inputs, outputs, 2, 1); err != nil {
return nil, err
}
return newAlgebraicSetOp(flowCtx, core.SetOp, inputs[0], inputs[1], post, outputs[0])
}
return nil, errors.Errorf("unsupported processor core %s", core)
}