-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathrow_source_to_plan_node.go
124 lines (106 loc) · 3.2 KB
/
row_source_to_plan_node.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
// 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"
"fmt"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"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"
)
// rowSourceToPlanNode wraps a RowSource and presents it as a PlanNode. It must
// be constructed with Create(), after which it is a PlanNode and can be treated
// as such.
type rowSourceToPlanNode struct {
source execinfra.RowSource
forwarder metadataForwarder
// originalPlanNode is the original planNode that the wrapped RowSource got
// planned for.
originalPlanNode planNode
planCols colinfo.ResultColumns
// Temporary variables
row rowenc.EncDatumRow
da rowenc.DatumAlloc
datumRow tree.Datums
}
var _ planNode = &rowSourceToPlanNode{}
// makeRowSourceToPlanNode creates a new planNode that wraps a RowSource. It
// takes an optional metadataForwarder, which if non-nil is invoked for every
// piece of metadata this wrapper receives from the wrapped RowSource.
// It also takes an optional planNode, which is the planNode that the RowSource
// that this rowSourceToPlanNode is wrapping originally replaced. That planNode
// will be closed when this one is closed.
func makeRowSourceToPlanNode(
s execinfra.RowSource,
forwarder metadataForwarder,
planCols colinfo.ResultColumns,
originalPlanNode planNode,
) *rowSourceToPlanNode {
row := make(tree.Datums, len(planCols))
return &rowSourceToPlanNode{
source: s,
datumRow: row,
forwarder: forwarder,
planCols: planCols,
originalPlanNode: originalPlanNode,
}
}
func (r *rowSourceToPlanNode) startExec(params runParams) error {
r.source.Start(params.ctx)
return nil
}
func (r *rowSourceToPlanNode) Next(params runParams) (bool, error) {
for {
var p *execinfrapb.ProducerMetadata
r.row, p = r.source.Next()
if p != nil {
if p.Err != nil {
return false, p.Err
}
if r.forwarder != nil {
r.forwarder.forwardMetadata(p)
continue
}
if p.TraceData != nil {
// We drop trace metadata since we have no reasonable way to propagate
// it in local SQL execution.
continue
}
return false, fmt.Errorf("unexpected producer metadata: %+v", p)
}
if r.row == nil {
return false, nil
}
types := r.source.OutputTypes()
for i := range r.planCols {
encDatum := r.row[i]
err := encDatum.EnsureDecoded(types[i], &r.da)
if err != nil {
return false, err
}
r.datumRow[i] = encDatum.Datum
}
return true, nil
}
}
func (r *rowSourceToPlanNode) Values() tree.Datums {
return r.datumRow
}
func (r *rowSourceToPlanNode) Close(ctx context.Context) {
if r.source != nil {
r.source.ConsumerClosed()
r.source = nil
}
if r.originalPlanNode != nil {
r.originalPlanNode.Close(ctx)
}
}