-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
plan_node_to_row_source.go
238 lines (212 loc) · 7.11 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
// 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"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"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/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.OpNode = &planNodeToRowSource{}
func makePlanNodeToRowSource(
source planNode, params runParams, fastPath bool,
) (*planNodeToRowSource, error) {
var typs []*types.T
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.
typs = []*types.T{types.Int}
} else {
typs = getTypesFromResultColumns(planColumns(source))
}
row := make(rowenc.EncDatumRow, len(typs))
return &planNodeToRowSource{
node: source,
params: params,
outputTypes: typs,
row: row,
fastPath: fastPath,
}, nil
}
var _ execinfra.LocalProcessor = &planNodeToRowSource{}
// 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, post *execinfrapb.PostProcessSpec, output execinfra.RowReceiver,
) error {
return 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
// makePlanNodeToRowSource, so we can just use the eval context from the
// params.
p.params.EvalContext(),
0, /* processorID */
output,
nil, /* memMonitor */
execinfra.ProcStateOpts{
TrailingMetaCallback: func() []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
},
},
)
}
// 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
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 makeRowSourceToPlanNode(input, p, planColumns(p.firstNotWrapped), p.firstNotWrapped), nil
}
return nil, nil
},
})
}
func (p *planNodeToRowSource) Start(ctx context.Context) {
ctx = p.StartInternalNoSpan(ctx)
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)
}
// ChildCount is part of the execinfra.OpNode interface.
func (p *planNodeToRowSource) ChildCount(verbose bool) int {
if _, ok := p.input.(execinfra.OpNode); ok {
return 1
}
return 0
}
// Child is part of the execinfra.OpNode interface.
func (p *planNodeToRowSource) Child(nth int, verbose bool) execinfra.OpNode {
switch nth {
case 0:
if n, ok := p.input.(execinfra.OpNode); ok {
return n
}
panic("input to planNodeToRowSource is not an execinfra.OpNode")
default:
panic(errors.AssertionFailedf("invalid index %d", nth))
}
}