-
Notifications
You must be signed in to change notification settings - Fork 1
/
values.go
286 lines (243 loc) · 8.21 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
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
// Copyright 2015 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 sql
import (
"container/heap"
"fmt"
"strconv"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
func newValuesListLenErr(exp, got int) error {
return errors.Errorf("VALUES lists must all be the same length, expected %d columns, found %d",
exp, got)
}
type valuesNode struct {
n *parser.ValuesClause
p *planner
columns sqlbase.ResultColumns
ordering sqlbase.ColumnOrdering
tuples [][]parser.TypedExpr
rows *sqlbase.RowContainer
// isConst is set if the valuesNode only contains constant expressions (no
// subqueries). In this case, rows will be evaluated during the first call
// to planNode.Start and memoized for future consumption. A valuesNode with
// isConst = true can serve its values multiple times. See valuesNode.Reset.
isConst bool
// rowsPopped is used for heaps, it indicates the number of rows that were
// "popped". These rows are still part of the underlying sqlbase.RowContainer, in the
// range [rows.Len()-n.rowsPopped, rows.Len).
rowsPopped int
nextRow int // The index of the next row.
invertSorting bool // Inverts the sorting predicate.
}
func (p *planner) newContainerValuesNode(columns sqlbase.ResultColumns, capacity int) *valuesNode {
return &valuesNode{
p: p,
columns: columns,
rows: sqlbase.NewRowContainer(
p.session.TxnState.makeBoundAccount(), sqlbase.ColTypeInfoFromResCols(columns), capacity,
),
isConst: true,
}
}
func (p *planner) ValuesClause(
ctx context.Context, n *parser.ValuesClause, desiredTypes []parser.Type,
) (planNode, error) {
v := &valuesNode{
p: p,
n: n,
isConst: true,
}
if len(n.Tuples) == 0 {
return v, nil
}
numCols := len(n.Tuples[0].Exprs)
v.tuples = make([][]parser.TypedExpr, 0, len(n.Tuples))
tupleBuf := make([]parser.TypedExpr, len(n.Tuples)*numCols)
v.columns = make(sqlbase.ResultColumns, 0, numCols)
defer func(prev bool) { p.hasSubqueries = prev }(p.hasSubqueries)
p.hasSubqueries = false
for num, tuple := range n.Tuples {
if a, e := len(tuple.Exprs), numCols; a != e {
return nil, newValuesListLenErr(e, a)
}
// Chop off prefix of tupleBuf and limit its capacity.
tupleRow := tupleBuf[:numCols:numCols]
tupleBuf = tupleBuf[numCols:]
for i, expr := range tuple.Exprs {
if err := p.parser.AssertNoAggregationOrWindowing(
expr, "VALUES", p.session.SearchPath,
); err != nil {
return nil, err
}
desired := parser.TypeAny
if len(desiredTypes) > i {
desired = desiredTypes[i]
}
typedExpr, err := p.analyzeExpr(ctx, expr, nil, parser.IndexedVarHelper{}, desired, false, "")
if err != nil {
return nil, err
}
typ := typedExpr.ResolvedType()
if num == 0 {
v.columns = append(v.columns, sqlbase.ResultColumn{Name: "column" + strconv.Itoa(i+1), Typ: typ})
} else if v.columns[i].Typ == parser.TypeNull {
v.columns[i].Typ = typ
} else if typ != parser.TypeNull && !typ.Equivalent(v.columns[i].Typ) {
return nil, fmt.Errorf("VALUES list type mismatch, %s for %s", typ, v.columns[i].Typ)
}
tupleRow[i] = typedExpr
}
v.tuples = append(v.tuples, tupleRow)
}
// TODO(nvanbenschoten): if v.isConst, we should be able to evaluate n.rows
// ahead of time. This requires changing the contract for planNode.Close such
// that it must always be called unless an error is returned from a planNode
// constructor. This would simplify the Close contract, but would make some
// code (like in planner.SelectClause) more messy.
v.isConst = !p.hasSubqueries
return v, nil
}
// Start implements the planNode interface.
func (n *valuesNode) Start(params runParams) error {
if n.rows != nil {
if !n.isConst {
log.Fatalf(params.ctx, "valuesNode evaluted twice")
}
return nil
}
// This node is coming from a SQL query (as opposed to sortNode and
// others that create a valuesNode internally for storing results
// from other planNodes), so its expressions need evaluting.
// This may run subqueries.
n.rows = sqlbase.NewRowContainer(
n.p.session.TxnState.makeBoundAccount(),
sqlbase.ColTypeInfoFromResCols(n.columns),
len(n.n.Tuples),
)
row := make([]parser.Datum, len(n.columns))
for _, tupleRow := range n.tuples {
for i, typedExpr := range tupleRow {
if n.columns[i].Omitted {
row[i] = parser.DNull
} else {
var err error
row[i], err = typedExpr.Eval(&n.p.evalCtx)
if err != nil {
return err
}
}
}
if _, err := n.rows.AddRow(params.ctx, row); err != nil {
return err
}
}
return nil
}
func (n *valuesNode) Values() parser.Datums {
return n.rows.At(n.nextRow - 1)
}
func (n *valuesNode) Next(runParams) (bool, error) {
if n.nextRow >= n.rows.Len() {
return false, nil
}
n.nextRow++
return true, nil
}
// Reset resets the valuesNode processing state without requiring recomputation
// of the values tuples if the valuesNode is processed again. Reset can only
// be called if valuesNode.isConst.
func (n *valuesNode) Reset(ctx context.Context) {
if !n.isConst {
log.Fatalf(ctx, "valuesNode.Reset can only be called on constant valuesNodes")
}
n.nextRow = 0
}
func (n *valuesNode) Close(ctx context.Context) {
if n.rows != nil {
n.rows.Close(ctx)
n.rows = nil
}
}
func (n *valuesNode) Len() int {
return n.rows.Len() - n.rowsPopped
}
func (n *valuesNode) Less(i, j int) bool {
// TODO(pmattis): An alternative to this type of field-based comparison would
// be to construct a sort-key per row using encodeTableKey(). Using a
// sort-key approach would likely fit better with a disk-based sort.
ra, rb := n.rows.At(i), n.rows.At(j)
return n.invertSorting != n.ValuesLess(ra, rb)
}
// ValuesLess returns the comparison result between the two provided Datums slices
// in the context of the valuesNode ordering.
func (n *valuesNode) ValuesLess(ra, rb parser.Datums) bool {
return sqlbase.CompareDatums(n.ordering, &n.p.evalCtx, ra, rb) < 0
}
func (n *valuesNode) Swap(i, j int) {
n.rows.Swap(i, j)
}
var _ heap.Interface = (*valuesNode)(nil)
// Push implements the heap.Interface interface.
func (n *valuesNode) Push(x interface{}) {
}
// PushValues pushes the given Datums value into the heap representation
// of the valuesNode.
func (n *valuesNode) PushValues(ctx context.Context, values parser.Datums) error {
_, err := n.rows.AddRow(ctx, values)
heap.Push(n, nil)
return err
}
// Pop implements the heap.Interface interface.
func (n *valuesNode) Pop() interface{} {
if n.rowsPopped >= n.rows.Len() {
panic("no more rows to pop")
}
n.rowsPopped++
// Returning a Datums as an interface{} involves an allocation. Luckily, the
// value of Pop is only used for the return value of heap.Pop, which we can
// avoid using.
return nil
}
// PopValues pops the top Datums value off the heap representation
// of the valuesNode.
func (n *valuesNode) PopValues() parser.Datums {
heap.Pop(n)
// Return the last popped row.
return n.rows.At(n.rows.Len() - n.rowsPopped)
}
// ResetLen resets the length to that of the underlying row container. This
// resets the effect that popping values had on the valuesNode's visible length.
func (n *valuesNode) ResetLen() {
n.rowsPopped = 0
}
// SortAll sorts all values in the valuesNode.rows slice.
func (n *valuesNode) SortAll(cancelChecker *sqlbase.CancelChecker) {
n.invertSorting = false
sqlbase.Sort(n, cancelChecker)
}
// InitMaxHeap initializes the valuesNode.rows slice as a max-heap.
func (n *valuesNode) InitMaxHeap() {
n.invertSorting = true
heap.Init(n)
}
// InitMinHeap initializes the valuesNode.rows slice as a min-heap.
func (n *valuesNode) InitMinHeap() {
n.invertSorting = false
heap.Init(n)
}