-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
plan_node_to_row_source.go
223 lines (198 loc) · 6.51 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
// 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"
)
type metadataForwarder interface {
forwardMetadata(metadata *execinfrapb.ProducerMetadata)
}
type planNodeToRowSource struct {
execinfra.ProcessorBase
started bool
fastPath bool
node planNode
params runParams
outputTypes []*types.T
firstNotWrapped planNode
// run time state machine values
row rowenc.EncDatumRow
}
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{}
// 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,
p.params.EvalContext(),
0, /* processorID */
output,
nil, /* memMonitor */
execinfra.ProcStateOpts{
TrailingMetaCallback: func(context.Context) []execinfrapb.ProducerMetadata {
p.InternalClose()
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.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) context.Context {
// We do not call p.StartInternal to avoid creating a span. Only the context
// needs to be set.
p.Ctx = ctx
p.params.ctx = ctx
if !p.started {
p.started = true
// This starts all of the nodes below this node.
if err := startExec(p.params, p.node); err != nil {
p.MoveToDraining(err)
return ctx
}
}
return ctx
}
func (p *planNodeToRowSource) InternalClose() bool {
p.started = true
return p.ProcessorBase.InternalClose()
}
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()
}
func (p *planNodeToRowSource) ConsumerClosed() {
// The consumer is done, Next() will not be called again.
p.InternalClose()
}
// IsException implements the VectorizeAlwaysException interface.
func (p *planNodeToRowSource) IsException() bool {
if _, ok := p.node.(*setVarNode); ok {
// We need to make an exception for changing a session variable.
return true
}
if d, ok := p.node.(*delayedNode); ok {
// We want to make an exception for retrieving the current database
// name (which is done via a scan of 'session_variables' virtual table.
return d.name == "session_variables@primary"
}
return false
}
// 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)
}