forked from cockroachdb/cockroach
/
scan.go
368 lines (322 loc) · 10.4 KB
/
scan.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// 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.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
package sql
import (
"bytes"
"fmt"
"math"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/pkg/errors"
)
// A scanNode handles scanning over the key/value pairs for a table and
// reconstructing them into rows.
type scanNode struct {
p *planner
desc sqlbase.TableDescriptor
index *sqlbase.IndexDescriptor
// Set if an index was explicitly specified.
specifiedIndex *sqlbase.IndexDescriptor
// Set if the NO_INDEX_JOIN hint was given.
noIndexJoin bool
// The table columns, possibly including ones currently in schema changes.
cols []sqlbase.ColumnDescriptor
// There is a 1-1 correspondence between cols and resultColumns.
resultColumns ResultColumns
// Contains values for the current row. There is a 1-1 correspondence
// between resultColumns and values in row.
row parser.DTuple
// For each column in resultColumns, indicates if the value is
// needed (used as an optimization when the upper layer doesn't need
// all values).
// TODO(radu/knz): currently the optimization always loads the
// entire row from KV and only skips unnecessary decodes to
// Datum. Investigate whether performance is to be gained (e.g. for
// tables with wide rows) by reading only certain columns from KV
// using point lookups instead of a single range lookup for the
// entire row.
valNeededForCol []bool
// Map used to get the index for columns in cols.
colIdxMap map[sqlbase.ColumnID]int
spans []roachpb.Span
isSecondaryIndex bool
reverse bool
ordering orderingInfo
explain explainMode
rowIndex int // the index of the current row
debugVals debugValues
// filter that can be evaluated using only this table/index; it contains
// parser.IndexedVar leaves generated using filterVars.
filter parser.TypedExpr
filterVars parser.IndexedVarHelper
scanInitialized bool
fetcher sqlbase.RowFetcher
limitHint int64
limitSoft bool
disableBatchLimits bool
// This struct must be allocated on the heap and its location stay
// stable after construction because it implements
// IndexedVarContainer and the IndexedVar objects in sub-expressions
// will link to it by reference after checkRenderStar / analyzeExpr.
// Enforce this using NoCopy.
noCopy util.NoCopy
}
func (p *planner) Scan() *scanNode {
return &scanNode{p: p}
}
func (n *scanNode) Columns() ResultColumns {
return n.resultColumns
}
func (n *scanNode) Ordering() orderingInfo {
return n.ordering
}
func (n *scanNode) Values() parser.DTuple {
return n.row
}
func (n *scanNode) MarkDebug(mode explainMode) {
if mode != explainDebug {
panic(fmt.Sprintf("unknown debug mode %d", mode))
}
n.explain = mode
}
func (n *scanNode) DebugValues() debugValues {
if n.explain != explainDebug {
panic(fmt.Sprintf("node not in debug mode (mode %d)", n.explain))
}
return n.debugVals
}
func (n *scanNode) SetLimitHint(numRows int64, soft bool) {
// Either a limitNode or EXPLAIN is pushing a limit down onto this
// node. The special value math.MaxInt64 means "no limit".
if !n.disableBatchLimits && numRows != math.MaxInt64 {
n.limitHint = numRows
// If we have a filter, some of the rows we retrieve may not pass the
// filter so the limit becomes "soft".
n.limitSoft = soft || n.filter != nil
}
}
// disableBatchLimit disables the kvfetcher batch limits. Used for index-join,
// where we scan batches of unordered spans.
func (n *scanNode) disableBatchLimit() {
n.disableBatchLimits = true
n.limitHint = 0
n.limitSoft = false
}
func (n *scanNode) Start() error {
err := n.fetcher.Init(&n.desc, n.colIdxMap, n.index, n.reverse, n.isSecondaryIndex, n.cols,
n.valNeededForCol)
if err != nil {
return err
}
return n.p.startSubqueryPlans(n.filter)
}
func (n *scanNode) Close() {}
// initScan sets up the rowFetcher and starts a scan.
func (n *scanNode) initScan() error {
if len(n.spans) == 0 {
// If no spans were specified retrieve all of the keys that start with our
// index key prefix. This isn't needed for the fetcher, but it is for
// other external users of n.spans.
start := roachpb.Key(sqlbase.MakeIndexKeyPrefix(&n.desc, n.index.ID))
n.spans = append(n.spans, roachpb.Span{Key: start, EndKey: start.PrefixEnd()})
}
limitHint := n.limitHint
if limitHint != 0 && n.limitSoft {
// Read a multiple of the limit if the limit is "soft".
limitHint *= 2
}
if err := n.fetcher.StartScan(n.p.txn, n.spans, !n.disableBatchLimits, limitHint); err != nil {
return err
}
n.scanInitialized = true
return nil
}
// debugNext is a helper function used by Next() when in explainDebug mode.
func (n *scanNode) debugNext() (bool, error) {
// In debug mode, we output a set of debug values for each key.
n.debugVals.rowIdx = n.rowIndex
var err error
var encRow sqlbase.EncDatumRow
n.debugVals.key, n.debugVals.value, encRow, err = n.fetcher.NextKeyDebug()
if err != nil || n.debugVals.key == "" {
return false, err
}
if encRow == nil {
n.row = nil
n.debugVals.output = debugValuePartial
return true, nil
}
tuple := make(parser.DTuple, len(encRow))
var da sqlbase.DatumAlloc
if err := sqlbase.EncDatumRowToDTuple(tuple, encRow, &da); err != nil {
return false, errors.Errorf("Could not decode row: %v", err)
}
n.row = tuple
passesFilter, err := sqlbase.RunFilter(n.filter, &n.p.evalCtx)
if err != nil {
return false, err
}
if passesFilter {
n.debugVals.output = debugValueRow
} else {
n.debugVals.output = debugValueFiltered
}
n.rowIndex++
return true, nil
}
func (n *scanNode) Next() (bool, error) {
tracing.AnnotateTrace()
if !n.scanInitialized {
if err := n.initScan(); err != nil {
return false, err
}
}
if n.explain == explainDebug {
return n.debugNext()
}
// We fetch one row at a time until we find one that passes the filter.
for {
var err error
n.row, err = n.fetcher.NextRowDecoded()
if err != nil || n.row == nil {
return false, err
}
passesFilter, err := sqlbase.RunFilter(n.filter, &n.p.evalCtx)
if err != nil {
return false, err
}
if passesFilter {
return true, nil
}
}
}
// Initializes a scanNode with a table descriptor.
func (n *scanNode) initTable(
p *planner,
desc *sqlbase.TableDescriptor,
indexHints *parser.IndexHints,
scanVisibility scanVisibility,
) error {
n.desc = *desc
if !p.skipSelectPrivilegeChecks {
if err := p.checkPrivilege(&n.desc, privilege.SELECT); err != nil {
return err
}
}
if indexHints != nil && indexHints.Index != "" {
indexName := indexHints.Index.Normalize()
if indexName == parser.ReNormalizeName(n.desc.PrimaryIndex.Name) {
n.specifiedIndex = &n.desc.PrimaryIndex
} else {
for i := range n.desc.Indexes {
if indexName == parser.ReNormalizeName(n.desc.Indexes[i].Name) {
n.specifiedIndex = &n.desc.Indexes[i]
break
}
}
if n.specifiedIndex == nil {
return fmt.Errorf("index \"%s\" not found", indexName)
}
}
}
n.noIndexJoin = (indexHints != nil && indexHints.NoIndexJoin)
n.initDescDefaults(scanVisibility)
return nil
}
// Initializes the column structures.
func (n *scanNode) initDescDefaults(scanVisibility scanVisibility) {
n.index = &n.desc.PrimaryIndex
n.cols = make([]sqlbase.ColumnDescriptor, 0, len(n.desc.Columns)+len(n.desc.Mutations))
switch scanVisibility {
case publicColumns:
n.cols = append(n.cols, n.desc.Columns...)
case publicAndNonPublicColumns:
n.cols = append(n.cols, n.desc.Columns...)
for _, mutation := range n.desc.Mutations {
if c := mutation.GetColumn(); c != nil {
n.cols = append(n.cols, *c)
}
}
}
n.resultColumns = makeResultColumns(n.cols)
n.colIdxMap = make(map[sqlbase.ColumnID]int, len(n.cols))
for i, c := range n.cols {
n.colIdxMap[c.ID] = i
}
n.valNeededForCol = make([]bool, len(n.cols))
for i := range n.cols {
n.valNeededForCol[i] = true
}
n.row = make([]parser.Datum, len(n.cols))
n.filterVars = parser.MakeIndexedVarHelper(n, len(n.cols))
}
// initOrdering initializes the ordering info using the selected index. This
// must be called after index selection is performed.
func (n *scanNode) initOrdering(exactPrefix int) {
if n.index == nil {
return
}
n.ordering = n.computeOrdering(n.index, exactPrefix, n.reverse)
}
// computeOrdering calculates ordering information for table columns assuming that:
// - we scan a given index (potentially in reverse order), and
// - the first `exactPrefix` columns of the index each have an exact (single value) match
// (see orderingInfo).
func (n *scanNode) computeOrdering(
index *sqlbase.IndexDescriptor, exactPrefix int, reverse bool,
) orderingInfo {
var ordering orderingInfo
columnIDs, dirs := index.FullColumnIDs()
for i, colID := range columnIDs {
idx, ok := n.colIdxMap[colID]
if !ok {
panic(fmt.Sprintf("index refers to unknown column id %d", colID))
}
if i < exactPrefix {
ordering.addExactMatchColumn(idx)
} else {
dir := dirs[i]
if reverse {
dir = dir.Reverse()
}
ordering.addColumn(idx, dir)
}
}
// We included any implicit columns, so the results are unique.
ordering.unique = true
return ordering
}
// scanNode implements parser.IndexedVarContainer.
var _ parser.IndexedVarContainer = &scanNode{}
func (n *scanNode) IndexedVarEval(idx int, ctx *parser.EvalContext) (parser.Datum, error) {
return n.row[idx].Eval(ctx)
}
func (n *scanNode) IndexedVarResolvedType(idx int) parser.Type {
return n.resultColumns[idx].Typ
}
func (n *scanNode) IndexedVarFormat(buf *bytes.Buffer, _ parser.FmtFlags, idx int) {
buf.WriteString(n.resultColumns[idx].Name)
}
// scanVisibility represents which table columns should be included in a scan.
type scanVisibility int
const (
publicColumns scanVisibility = 0
publicAndNonPublicColumns scanVisibility = 1
)