-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathplan_node_to_row_source.go
304 lines (276 loc) · 9.36 KB
/
plan_node_to_row_source.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
// 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 sql
import (
"context"
"sync"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra/execopnode"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra/execreleasable"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/execstats"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/optional"
"github.com/cockroachdb/errors"
)
type metadataForwarder interface {
forwardMetadata(metadata *execinfrapb.ProducerMetadata)
}
type planNodeToRowSource struct {
execinfra.ProcessorBase
input execinfra.RowSource
fastPath bool
node planNode
params runParams
outputTypes []*types.T
firstNotWrapped planNode
// run time state machine values
row rowenc.EncDatumRow
}
var _ execinfra.LocalProcessor = &planNodeToRowSource{}
var _ execreleasable.Releasable = &planNodeToRowSource{}
var _ execopnode.OpNode = &planNodeToRowSource{}
var planNodeToRowSourcePool = sync.Pool{
New: func() interface{} {
return &planNodeToRowSource{}
},
}
func newPlanNodeToRowSource(
source planNode, params runParams, fastPath bool, firstNotWrapped planNode,
) *planNodeToRowSource {
p := planNodeToRowSourcePool.Get().(*planNodeToRowSource)
*p = planNodeToRowSource{
ProcessorBase: p.ProcessorBase,
fastPath: fastPath,
node: source,
params: params,
firstNotWrapped: firstNotWrapped,
row: p.row,
}
if fastPath {
// If our node is a "fast path node", it means that we're set up to
// just return a row count meaning we'll output a single row with a
// single INT column.
p.outputTypes = []*types.T{types.Int}
} else {
p.outputTypes = getTypesFromResultColumns(planColumns(source))
}
if p.row != nil && cap(p.row) >= len(p.outputTypes) {
// In some cases we might have no output columns, so nil row would have
// sufficient width, yet nil row is a special value, so we can only
// reuse the old row if it's non-nil.
p.row = p.row[:len(p.outputTypes)]
} else {
p.row = make(rowenc.EncDatumRow, len(p.outputTypes))
}
return p
}
// MustBeStreaming implements the execinfra.Processor interface.
func (p *planNodeToRowSource) MustBeStreaming() bool {
// hookFnNode is special because it might be blocked forever if we decide to
// buffer its output.
_, isHookFnNode := p.node.(*hookFnNode)
return isHookFnNode
}
// InitWithOutput implements the LocalProcessor interface.
func (p *planNodeToRowSource) InitWithOutput(
flowCtx *execinfra.FlowCtx,
processorID int32,
post *execinfrapb.PostProcessSpec,
output execinfra.RowReceiver,
) error {
if err := p.InitWithEvalCtx(
p,
post,
p.outputTypes,
flowCtx,
// Note that we have already created a copy of the extendedEvalContext
// (which made a copy of the EvalContext) right before calling
// newPlanNodeToRowSource, so we can just use the eval context from the
// params.
p.params.EvalContext(),
processorID,
output,
nil, /* memMonitor */
execinfra.ProcStateOpts{
// Input to drain is added in SetInput.
TrailingMetaCallback: p.trailingMetaCallback,
},
); err != nil {
return err
}
if execstats.ShouldCollectStats(flowCtx.EvalCtx.Ctx(), flowCtx.CollectStats) {
p.ExecStatsForTrace = p.execStatsForTrace
}
return nil
}
// SetInput implements the LocalProcessor interface.
// input is the first upstream RowSource. When we're done executing, we need to
// drain this row source of its metadata in case the planNode tree we're
// wrapping returned an error, since planNodes don't know how to drain trailing
// metadata.
func (p *planNodeToRowSource) SetInput(ctx context.Context, input execinfra.RowSource) error {
if p.firstNotWrapped == nil {
// Short-circuit if we never set firstNotWrapped - indicating this planNode
// tree had no DistSQL-plannable subtrees.
return nil
}
p.input = input
// Adding the input to drain ensures that the input will be properly closed
// by this planNodeToRowSource. This is important since the
// rowSourceToPlanNode created below is not responsible for that.
p.AddInputToDrain(input)
// Search the plan we're wrapping for firstNotWrapped, which is the planNode
// that DistSQL planning resumed in. Replace that planNode with input,
// wrapped as a planNode.
return walkPlan(ctx, p.node, planObserver{
replaceNode: func(ctx context.Context, nodeName string, plan planNode) (planNode, error) {
if plan == p.firstNotWrapped {
return newRowSourceToPlanNode(input, p, planColumns(p.firstNotWrapped), p.firstNotWrapped), nil
}
return nil, nil
},
})
}
func (p *planNodeToRowSource) Start(ctx context.Context) {
ctx = p.StartInternal(ctx, nodeName(p.node))
p.params.ctx = ctx
// This starts all of the nodes below this node.
if err := startExec(p.params, p.node); err != nil {
p.MoveToDraining(err)
}
}
func (p *planNodeToRowSource) Next() (rowenc.EncDatumRow, *execinfrapb.ProducerMetadata) {
if p.State == execinfra.StateRunning && p.fastPath {
var count int
// If our node is a "fast path node", it means that we're set up to just
// return a row count. So trigger the fast path and return the row count as
// a row with a single column.
fastPath, ok := p.node.(planNodeFastPath)
if ok {
var res bool
if count, res = fastPath.FastPathResults(); res {
if p.params.extendedEvalCtx.Tracing.Enabled() {
log.VEvent(p.params.ctx, 2, "fast path completed")
}
} else {
// Fall back to counting the rows.
count = 0
ok = false
}
}
if !ok {
// If we have no fast path to trigger, fall back to counting the rows
// by Nexting our source until exhaustion.
next, err := p.node.Next(p.params)
for ; next; next, err = p.node.Next(p.params) {
count++
}
if err != nil {
p.MoveToDraining(err)
return nil, p.DrainHelper()
}
}
p.MoveToDraining(nil /* err */)
// Return the row count the only way we can: as a single-column row with
// the count inside.
return rowenc.EncDatumRow{rowenc.EncDatum{Datum: tree.NewDInt(tree.DInt(count))}}, nil
}
for p.State == execinfra.StateRunning {
valid, err := p.node.Next(p.params)
if err != nil || !valid {
p.MoveToDraining(err)
return nil, p.DrainHelper()
}
for i, datum := range p.node.Values() {
if datum != nil {
p.row[i] = rowenc.DatumToEncDatum(p.outputTypes[i], datum)
}
}
// ProcessRow here is required to deal with projections, which won't be
// pushed into the wrapped plan.
if outRow := p.ProcessRowHelper(p.row); outRow != nil {
return outRow, nil
}
}
return nil, p.DrainHelper()
}
// forwardMetadata will be called by any upstream rowSourceToPlanNode processors
// that need to forward metadata to the end of the flow. They can't pass
// metadata through local processors, so they instead add the metadata to our
// trailing metadata and expect us to forward it further.
func (p *planNodeToRowSource) forwardMetadata(metadata *execinfrapb.ProducerMetadata) {
p.ProcessorBase.AppendTrailingMeta(*metadata)
}
func (p *planNodeToRowSource) trailingMetaCallback() []execinfrapb.ProducerMetadata {
var meta []execinfrapb.ProducerMetadata
if p.InternalClose() {
// Check if we're wrapping a mutation and emit the rows written metric
// if so.
if m, ok := p.node.(mutationPlanNode); ok {
metrics := execinfrapb.GetMetricsMeta()
metrics.RowsWritten = m.rowsWritten()
meta = []execinfrapb.ProducerMetadata{{Metrics: metrics}}
}
}
return meta
}
// execStatsForTrace implements ProcessorBase.ExecStatsForTrace.
func (p *planNodeToRowSource) execStatsForTrace() *execinfrapb.ComponentStats {
// Propagate RUs from IO requests.
// TODO(drewk): we should consider propagating other stats for planNode
// operators.
scanStats := execstats.GetScanStats(p.Ctx(), p.ExecStatsTrace)
if scanStats.ConsumedRU == 0 {
return nil
}
return &execinfrapb.ComponentStats{
Exec: execinfrapb.ExecStats{
ConsumedRU: optional.MakeUint(scanStats.ConsumedRU),
},
}
}
// Release releases this planNodeToRowSource back to the pool.
func (p *planNodeToRowSource) Release() {
p.ProcessorBase.Reset()
// Deeply reset the row.
for i := range p.row {
p.row[i] = rowenc.EncDatum{}
}
// Note that we don't reuse the outputTypes slice because it is exposed to
// the outer physical planning code.
*p = planNodeToRowSource{
ProcessorBase: p.ProcessorBase,
row: p.row[:0],
}
planNodeToRowSourcePool.Put(p)
}
// ChildCount is part of the execopnode.OpNode interface.
func (p *planNodeToRowSource) ChildCount(verbose bool) int {
if _, ok := p.input.(execopnode.OpNode); ok {
return 1
}
return 0
}
// Child is part of the execopnode.OpNode interface.
func (p *planNodeToRowSource) Child(nth int, verbose bool) execopnode.OpNode {
switch nth {
case 0:
if n, ok := p.input.(execopnode.OpNode); ok {
return n
}
panic("input to planNodeToRowSource is not an execopnode.OpNode")
default:
panic(errors.AssertionFailedf("invalid index %d", nth))
}
}