forked from cockroachdb/cockroach
/
values.go
108 lines (92 loc) · 2.64 KB
/
values.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
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package distsqlrun
import (
"sync"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
)
// valuesProcessor is a processor that has no inputs and generates "pre-canned"
// rows.
type valuesProcessor struct {
processorBase
flowCtx *FlowCtx
columns []DatumInfo
data [][]byte
}
var _ Processor = &valuesProcessor{}
func newValuesProcessor(
flowCtx *FlowCtx, spec *ValuesCoreSpec, post *PostProcessSpec, output RowReceiver,
) (*valuesProcessor, error) {
v := &valuesProcessor{
flowCtx: flowCtx,
columns: spec.Columns,
data: spec.RawBytes,
}
types := make([]sqlbase.ColumnType, len(v.columns))
for i := range v.columns {
types[i] = v.columns[i].Type
}
if err := v.out.Init(post, types, &flowCtx.EvalCtx, output); err != nil {
return nil, err
}
return v, nil
}
// Run is part of the processor interface.
func (v *valuesProcessor) Run(ctx context.Context, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
ctx, span := processorSpan(ctx, "values")
defer tracing.FinishSpan(span)
// We reuse the code in StreamDecoder for decoding the raw data. We just need
// to manufacture ProducerMessages.
var sd StreamDecoder
m := ProducerMessage{
Typing: v.columns,
// Add a bogus header to apease the StreamDecoder, which wants to receive a
// header before any data.
Header: &ProducerHeader{}}
if err := sd.AddMessage(&m); err != nil {
DrainAndClose(ctx, v.out.output, err)
return
}
m = ProducerMessage{}
rowBuf := make(sqlbase.EncDatumRow, len(v.columns))
for len(v.data) > 0 {
// Push a chunk of data.
m.Data.RawBytes = v.data[0]
if err := sd.AddMessage(&m); err != nil {
DrainAndClose(ctx, v.out.output, err)
return
}
v.data = v.data[1:]
for {
row, meta, err := sd.GetRow(rowBuf)
if err != nil {
// If we got a decoding error, pass it along.
meta.Err = err
}
if row == nil && meta.Empty() {
break
}
if !emitHelper(ctx, &v.out, row, meta) {
return
}
}
}
sendTraceData(ctx, v.out.output)
v.out.Close()
}