-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathprocessors.go
339 lines (329 loc) · 11.8 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
// Copyright 2019 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 rowexec
import (
"context"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)
// 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 are optional.
//
// pushTrailingMeta is called after draining the sources and before calling
// dst.ProducerDone(). It gives the caller the opportunity to push some trailing
// metadata (e.g. tracing information and txn updates, if applicable).
//
// 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 *execinfra.ProcOutputHelper,
row sqlbase.EncDatumRow,
meta *execinfrapb.ProducerMetadata,
pushTrailingMeta func(context.Context),
inputs ...execinfra.RowSource,
) bool {
if output.Output() == nil {
panic("output RowReceiver not initialized for emitting")
}
var consumerStatus execinfra.ConsumerStatus
if meta != nil {
if row != nil {
panic("both row data and metadata in the same emitHelper call")
}
// Bypass EmitRow() and send directly to output.output.
foundErr := meta.Err != nil
consumerStatus = output.Output().Push(nil /* row */, meta)
if foundErr {
consumerStatus = execinfra.ConsumerClosed
}
} else {
var err error
consumerStatus, err = output.EmitRow(ctx, row)
if err != nil {
output.Output().Push(nil /* row */, &execinfrapb.ProducerMetadata{Err: err})
consumerStatus = execinfra.ConsumerClosed
}
}
switch consumerStatus {
case execinfra.NeedMoreRows:
return true
case execinfra.DrainRequested:
log.VEventf(ctx, 1, "no more rows required. drain requested.")
execinfra.DrainAndClose(ctx, output.Output(), nil /* cause */, pushTrailingMeta, inputs...)
return false
case execinfra.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
}
}
func checkNumInOut(
inputs []execinfra.RowSource, outputs []execinfra.RowReceiver, numIn, numOut int,
) error {
if len(inputs) != numIn {
return errors.Errorf("expected %d input(s), got %d", numIn, len(inputs))
}
if len(outputs) != numOut {
return errors.Errorf("expected %d output(s), got %d", numOut, len(outputs))
}
return nil
}
// NewProcessor creates a new execinfra.Processor according to the provided
// core.
func NewProcessor(
ctx context.Context,
flowCtx *execinfra.FlowCtx,
processorID int32,
core *execinfrapb.ProcessorCoreUnion,
post *execinfrapb.PostProcessSpec,
inputs []execinfra.RowSource,
outputs []execinfra.RowReceiver,
localProcessors []execinfra.LocalProcessor,
) (execinfra.Processor, error) {
if core.Noop != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return newNoopProcessor(flowCtx, processorID, inputs[0], post, outputs[0])
}
if core.Values != nil {
if err := checkNumInOut(inputs, outputs, 0, 1); err != nil {
return nil, err
}
return newValuesProcessor(flowCtx, processorID, core.Values, post, outputs[0])
}
if core.TableReader != nil {
if err := checkNumInOut(inputs, outputs, 0, 1); err != nil {
return nil, err
}
if core.TableReader.IsCheck {
return newScrubTableReader(flowCtx, processorID, core.TableReader, post, outputs[0])
}
return newTableReader(flowCtx, processorID, core.TableReader, post, outputs[0])
}
if core.JoinReader != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
if len(core.JoinReader.LookupColumns) == 0 {
return newIndexJoiner(
flowCtx, processorID, core.JoinReader, inputs[0], post, outputs[0])
}
return newJoinReader(flowCtx, processorID, 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(ctx, flowCtx, processorID, 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, processorID, core.Distinct, inputs[0], post, outputs[0])
}
if core.Ordinality != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return newOrdinalityProcessor(flowCtx, processorID, core.Ordinality, inputs[0], post, outputs[0])
}
if core.Aggregator != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return newAggregator(flowCtx, processorID, 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, processorID, core.MergeJoiner, inputs[0], inputs[1], post, outputs[0],
)
}
if core.InterleavedReaderJoiner != nil {
if err := checkNumInOut(inputs, outputs, 0, 1); err != nil {
return nil, err
}
return newInterleavedReaderJoiner(
flowCtx, processorID, core.InterleavedReaderJoiner, post, outputs[0],
)
}
if core.ZigzagJoiner != nil {
if err := checkNumInOut(inputs, outputs, 0, 1); err != nil {
return nil, err
}
return newZigzagJoiner(
flowCtx, processorID, core.ZigzagJoiner, nil, post, outputs[0],
)
}
if core.HashJoiner != nil {
if err := checkNumInOut(inputs, outputs, 2, 1); err != nil {
return nil, err
}
return newHashJoiner(
flowCtx, processorID, core.HashJoiner, inputs[0], inputs[1], post,
outputs[0], false, /* disableTempStorage */
)
}
if core.Backfiller != nil {
if err := checkNumInOut(inputs, outputs, 0, 1); err != nil {
return nil, err
}
switch core.Backfiller.Type {
case execinfrapb.BackfillerSpec_Index:
return newIndexBackfiller(flowCtx, processorID, *core.Backfiller, post, outputs[0])
case execinfrapb.BackfillerSpec_Column:
return newColumnBackfiller(ctx, flowCtx, processorID, *core.Backfiller, post, outputs[0])
}
}
if core.Sampler != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return newSamplerProcessor(flowCtx, processorID, core.Sampler, inputs[0], post, outputs[0])
}
if core.SampleAggregator != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return newSampleAggregator(flowCtx, processorID, core.SampleAggregator, inputs[0], post, outputs[0])
}
if core.ReadImport != nil {
if err := checkNumInOut(inputs, outputs, 0, 1); err != nil {
return nil, err
}
if NewReadImportDataProcessor == nil {
return nil, errors.New("ReadImportData processor unimplemented")
}
return NewReadImportDataProcessor(flowCtx, processorID, *core.ReadImport, outputs[0])
}
if core.CSVWriter != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
if NewCSVWriterProcessor == nil {
return nil, errors.New("CSVWriter processor unimplemented")
}
return NewCSVWriterProcessor(flowCtx, processorID, *core.CSVWriter, inputs[0], outputs[0])
}
if core.BulkRowWriter != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return newBulkRowWriterProcessor(flowCtx, processorID, *core.BulkRowWriter, inputs[0], outputs[0])
}
if core.MetadataTestSender != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return execinfra.NewMetadataTestSender(flowCtx, processorID, inputs[0], post, outputs[0], core.MetadataTestSender.ID)
}
if core.MetadataTestReceiver != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return execinfra.NewMetadataTestReceiver(
flowCtx, processorID, inputs[0], post, outputs[0], core.MetadataTestReceiver.SenderIDs,
)
}
if core.ProjectSet != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return newProjectSetProcessor(flowCtx, processorID, core.ProjectSet, inputs[0], post, outputs[0])
}
if core.Windower != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return newWindower(flowCtx, processorID, core.Windower, inputs[0], post, outputs[0])
}
if core.LocalPlanNode != nil {
numInputs := 0
if core.LocalPlanNode.NumInputs != nil {
numInputs = int(*core.LocalPlanNode.NumInputs)
}
if err := checkNumInOut(inputs, outputs, numInputs, 1); err != nil {
return nil, err
}
processor := localProcessors[*core.LocalPlanNode.RowSourceIdx]
if err := processor.InitWithOutput(post, outputs[0]); err != nil {
return nil, err
}
if numInputs == 1 {
if err := processor.SetInput(ctx, inputs[0]); err != nil {
return nil, err
}
} else if numInputs > 1 {
return nil, errors.Errorf("invalid localPlanNode core with multiple inputs %+v", core.LocalPlanNode)
}
return processor, nil
}
if core.ChangeAggregator != nil {
if err := checkNumInOut(inputs, outputs, 0, 1); err != nil {
return nil, err
}
if NewChangeAggregatorProcessor == nil {
return nil, errors.New("ChangeAggregator processor unimplemented")
}
return NewChangeAggregatorProcessor(flowCtx, processorID, *core.ChangeAggregator, outputs[0])
}
if core.ChangeFrontier != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
if NewChangeFrontierProcessor == nil {
return nil, errors.New("ChangeFrontier processor unimplemented")
}
return NewChangeFrontierProcessor(flowCtx, processorID, *core.ChangeFrontier, inputs[0], outputs[0])
}
if core.InvertedFilterer != nil {
if err := checkNumInOut(inputs, outputs, 1, 1); err != nil {
return nil, err
}
return newInvertedFilterer(flowCtx, processorID, core.InvertedFilterer, inputs[0], post, outputs[0])
}
return nil, errors.Errorf("unsupported processor core %q", core)
}
// NewReadImportDataProcessor is externally implemented and registered by
// ccl/sqlccl/csv.go.
var NewReadImportDataProcessor func(*execinfra.FlowCtx, int32, execinfrapb.ReadImportDataSpec, execinfra.RowReceiver) (execinfra.Processor, error)
// NewCSVWriterProcessor is externally implemented.
var NewCSVWriterProcessor func(*execinfra.FlowCtx, int32, execinfrapb.CSVWriterSpec, execinfra.RowSource, execinfra.RowReceiver) (execinfra.Processor, error)
// NewChangeAggregatorProcessor is externally implemented.
var NewChangeAggregatorProcessor func(*execinfra.FlowCtx, int32, execinfrapb.ChangeAggregatorSpec, execinfra.RowReceiver) (execinfra.Processor, error)
// NewChangeFrontierProcessor is externally implemented.
var NewChangeFrontierProcessor func(*execinfra.FlowCtx, int32, execinfrapb.ChangeFrontierSpec, execinfra.RowSource, execinfra.RowReceiver) (execinfra.Processor, error)