-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
opt_index_selection.go
690 lines (626 loc) · 21.8 KB
/
opt_index_selection.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
// Copyright 2016 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 (
"context"
"fmt"
"sort"
"github.com/pkg/errors"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/constraint"
"github.com/cockroachdb/cockroach/pkg/sql/opt/exec"
"github.com/cockroachdb/cockroach/pkg/sql/opt/exec/execbuilder"
"github.com/cockroachdb/cockroach/pkg/sql/opt/idxconstraint"
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
"github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder"
"github.com/cockroachdb/cockroach/pkg/sql/opt/xform"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
const nonCoveringIndexPenalty = 10
// analyzeOrderingFn is the interface through which the index selection code
// discovers how useful is the ordering provided by a certain index. The higher
// layer (select) desires a certain ordering on a number of columns; it calls
// into the index selection code with an analyzeOrderingFn that computes how
// many columns of that desired ordering are satisfied by the index ordering.
// Both the number of matching columns and the total columns in the desired
// ordering are returned.
//
// For example, consider the table t {
// a INT,
// b INT,
// c INT,
// INDEX ab (a, b)
// INDEX bac (b, a, c)
// }
//
// For `SELECT * FROM t ORDER BY a, c`, the desired ordering is (a, c);
// totalCols is 2. In this case:
// - the primary index has no ordering on a, b, c; matchingCols is 0.
// - the ab index matches the first column of the desired ordering;
// matchingCols is 1.
// - the bac index doesn't match the desired ordering at all; mathcingCols
// is 0.
//
// For `SELECT * FROM t WHERE b=1 ORDER BY a, c`, the desired ordering is (a, c);
// totalCols is 2. In this case:
// - the primary index has no ordering on a, b, c; matchingCols is 0.
// - the ab index matches the first column of the desired ordering;
// matchingCols is 1.
// - the bac index, along with the fact that b is constrained to a single
// value, matches the desired ordering; matchingCols is 2.
type analyzeOrderingFn func(indexProps physicalProps) (matchingCols, totalCols int)
// selectIndex analyzes the scanNode to determine if there is an index
// available that can fulfill the query with a more restrictive scan.
//
// Analysis currently consists of a simplification of the filter expression,
// replacing expressions which are not usable by indexes by "true". The
// simplified expression is then considered for each index and a set of range
// constraints is created for the index. The candidate indexes are ranked using
// these constraints and the best index is selected. The constraints are then
// transformed into a set of spans to scan within the index.
//
// The analyzeOrdering function is used to determine how useful the ordering of
// an index is. If no particular ordering is desired, it can be nil.
//
// If preferOrderMatching is true, we prefer an index that matches the desired
// ordering completely, even if it is not a covering index.
func (p *planner) selectIndex(
ctx context.Context, s *scanNode, analyzeOrdering analyzeOrderingFn, preferOrderMatching bool,
) (planNode, error) {
if s.desc.IsEmpty() {
// No table.
s.initOrdering(0 /* exactPrefix */, p.EvalContext())
return s, nil
}
if s.filter == nil && analyzeOrdering == nil && s.specifiedIndex == nil {
// No where-clause, no ordering, and no specified index.
s.initOrdering(0 /* exactPrefix */, p.EvalContext())
var err error
s.spans, err = unconstrainedSpans(s.desc, s.index)
if err != nil {
return nil, errors.Wrapf(err, "table ID = %d, index ID = %d", s.desc.ID, s.index.ID)
}
return s, nil
}
candidates := make([]*indexInfo, 0, len(s.desc.Indexes)+1)
if s.specifiedIndex != nil {
// An explicit secondary index was requested. Only add it to the candidate
// indexes list.
candidates = append(candidates, &indexInfo{
desc: s.desc,
index: s.specifiedIndex,
})
} else {
candidates = append(candidates, &indexInfo{
desc: s.desc,
index: &s.desc.PrimaryIndex,
})
for i := range s.desc.Indexes {
candidates = append(candidates, &indexInfo{
desc: s.desc,
index: &s.desc.Indexes[i],
})
}
}
for _, c := range candidates {
c.init(s)
}
var optimizer xform.Optimizer
if s.filter != nil {
optimizer.Init(p.EvalContext())
md := optimizer.Memo().Metadata()
for i := range s.resultColumns {
md.AddColumn(s.resultColumns[i].Name, s.resultColumns[i].Typ)
}
bld := optbuilder.NewScalar(ctx, &p.semaCtx, p.EvalContext(), optimizer.Factory())
bld.AllowUnsupportedExpr = true
err := bld.Build(s.filter)
if err != nil {
return nil, err
}
filterExpr := optimizer.Memo().Root()
for _, c := range candidates {
if err := c.makeIndexConstraints(
&optimizer, filterExpr, p.EvalContext(),
); err != nil {
return nil, err
}
if c.ic.Constraint().IsContradiction() {
// No spans (i.e. the filter is always false). Note that if a filter
// results in no constraints, ok would be false.
return newZeroNode(s.resultColumns), nil
}
}
}
// Remove any inverted indexes that don't generate any spans, a full-scan of
// an inverted index is always invalid.
for i := 0; i < len(candidates); {
c := candidates[i].ic.Constraint()
if candidates[i].index.Type == sqlbase.IndexDescriptor_INVERTED && (c == nil || c.IsUnconstrained()) {
candidates[i] = candidates[len(candidates)-1]
candidates = candidates[:len(candidates)-1]
} else {
i++
}
}
if len(candidates) == 0 {
// The primary index is never inverted. So the only way this can happen is
// if we had a specified index.
if s.specifiedIndex == nil {
panic("no non-inverted indexes")
}
return nil, fmt.Errorf("index \"%s\" is inverted and cannot be used for this query",
s.specifiedIndex.Name)
}
if s.noIndexJoin {
// Eliminate non-covering indexes. We do this after the check above for
// constant false filter.
for i := 0; i < len(candidates); {
if !candidates[i].covering {
candidates[i] = candidates[len(candidates)-1]
candidates = candidates[:len(candidates)-1]
} else {
i++
}
}
if len(candidates) == 0 {
// The primary index is always covering. So the only way this can
// happen is if we had a specified index.
if s.specifiedIndex == nil {
panic("no covering indexes")
}
return nil, fmt.Errorf("index \"%s\" is not covering and NO_INDEX_JOIN was specified",
s.specifiedIndex.Name)
}
}
for _, c := range candidates {
if analyzeOrdering != nil {
c.analyzeOrdering(ctx, s, analyzeOrdering, preferOrderMatching, p.EvalContext())
}
}
indexInfoByCost(candidates).Sort()
if log.V(2) {
for i, c := range candidates {
log.Infof(
ctx, "%d: selectIndex(%s): cost=%v constraint=%s reverse=%t",
i, c.index.Name, c.cost, c.ic.Constraint(), c.reverse,
)
}
}
// After sorting, candidates[0] contains the best index. Copy its info into
// the scanNode.
c := candidates[0]
s.index = c.index
s.specifiedIndex = nil
s.run.isSecondaryIndex = (c.index != &s.desc.PrimaryIndex)
var err error
s.spans, err = spansFromConstraint(s.desc, c.index, c.ic.Constraint(), s.valNeededForCol)
if err != nil {
return nil, errors.Wrapf(
err, "constraint = %s, table ID = %d, index ID = %d",
c.ic.Constraint(), s.desc.ID, s.index.ID,
)
}
if len(s.spans) == 0 {
// There are no spans to scan.
return newZeroNode(s.resultColumns), nil
}
s.origFilter = s.filter
if s.filter != nil {
remGroup := c.ic.RemainingFilter()
remEv := memo.MakeNormExprView(optimizer.Memo(), remGroup)
if remEv.Operator() == opt.TrueOp {
s.filter = nil
} else {
execBld := execbuilder.New(nil /* execFactory */, remEv, nil /* evalCtx */)
s.filter, err = execBld.BuildScalar(&s.filterVars)
if err != nil {
return nil, err
}
}
}
s.filterVars.Rebind(s.filter, true, false)
s.reverse = c.reverse
var plan planNode
if c.covering {
s.initOrdering(c.exactPrefix, p.EvalContext())
plan = s
} else {
// Note: makeIndexJoin destroys s and returns a new index scan
// node. The filter in that node may be different from the
// original table filter.
plan, s, err = p.makeIndexJoin(s, c.exactPrefix)
if err != nil {
return nil, err
}
}
if log.V(3) {
log.Infof(ctx, "%s: filter=%v", c.index.Name, s.filter)
for i, span := range s.spans {
log.Infof(ctx, "%s/%d: %s", c.index.Name, i, sqlbase.PrettySpan(sqlbase.IndexKeyValDirs(c.index), span, 2))
}
}
return plan, nil
}
type indexInfo struct {
desc *sqlbase.TableDescriptor
index *sqlbase.IndexDescriptor
cost float64
covering bool // Does the index cover the required IndexedVars?
reverse bool
exactPrefix int
ic idxconstraint.Instance
}
func (v *indexInfo) init(s *scanNode) {
v.covering = v.isCoveringIndex(s)
// The base cost is the number of keys per row.
if v.index == &v.desc.PrimaryIndex {
// The primary index contains 1 key per column plus the sentinel key per
// row.
v.cost = float64(1 + len(v.desc.Columns) - len(v.desc.PrimaryIndex.ColumnIDs))
} else {
v.cost = 1
if !v.covering {
v.cost += float64(1 + len(v.desc.Columns) - len(v.desc.PrimaryIndex.ColumnIDs))
// Non-covering indexes are significantly more expensive than covering
// indexes.
v.cost *= nonCoveringIndexPenalty
}
}
}
// analyzeOrdering analyzes the ordering provided by the index and determines
// if it matches the ordering requested by the query. Non-matching orderings
// increase the cost of using the index.
//
// If preferOrderMatching is true, we prefer an index that matches the desired
// ordering completely, even if it is not a covering index.
func (v *indexInfo) analyzeOrdering(
ctx context.Context,
scan *scanNode,
analyzeOrdering analyzeOrderingFn,
preferOrderMatching bool,
evalCtx *tree.EvalContext,
) {
// Analyze the ordering provided by the index (either forward or reverse).
fwdIndexProps := scan.computePhysicalProps(v.index, v.exactPrefix, false, evalCtx)
revIndexProps := scan.computePhysicalProps(v.index, v.exactPrefix, true, evalCtx)
fwdMatch, fwdOrderCols := analyzeOrdering(fwdIndexProps)
revMatch, revOrderCols := analyzeOrdering(revIndexProps)
if fwdOrderCols != revOrderCols {
panic(fmt.Sprintf("fwdOrderCols(%d) != revOrderCols(%d)", fwdOrderCols, revOrderCols))
}
orderCols := fwdOrderCols
// Weigh the cost by how much of the ordering matched.
//
// TODO(pmattis): Need to determine the relative weight for index selection
// based on sorting vs index selection based on filtering. Sorting is
// expensive due to the need to buffer up the rows and perform the sort, but
// not filtering is also expensive due to the larger number of rows scanned.
match := fwdMatch
if match < revMatch {
match = revMatch
v.reverse = true
}
weight := float64(orderCols+1) / float64(match+1)
v.cost *= weight
if match == orderCols && preferOrderMatching {
// Offset the non-covering index cost penalty.
v.cost *= (1.0 / nonCoveringIndexPenalty)
}
if log.V(2) {
log.Infof(ctx, "%s: analyzeOrdering: weight=%0.2f reverse=%v match=%d",
v.index.Name, weight, v.reverse, match)
}
}
// isCoveringIndex returns true if all of the columns needed from the scanNode are contained within
// the index. This allows a scan of only the index to be performed without requiring subsequent
// lookup of the full row.
func (v *indexInfo) isCoveringIndex(scan *scanNode) bool {
if v.index.Type == sqlbase.IndexDescriptor_INVERTED {
return false
}
if v.index == &v.desc.PrimaryIndex {
// The primary key index always covers all of the columns.
return true
}
for _, colIdx := range scan.valNeededForCol.Ordered() {
// This is possible during a schema change when we have
// additional mutation columns.
if colIdx >= len(v.desc.Columns) && len(v.desc.Mutations) > 0 {
return false
}
colID := v.desc.Columns[colIdx].ID
if !v.index.ContainsColumnID(colID) {
return false
}
}
return true
}
type indexInfoByCost []*indexInfo
func (v indexInfoByCost) Len() int {
return len(v)
}
func (v indexInfoByCost) Less(i, j int) bool {
return v[i].cost < v[j].cost
}
func (v indexInfoByCost) Swap(i, j int) {
v[i], v[j] = v[j], v[i]
}
func (v indexInfoByCost) Sort() {
sort.Sort(v)
}
// makeIndexConstraints uses the opt code to generate index
// constraints. Initializes v.ic, as well as v.exactPrefix and v.cost (with a
// baseline cost for the index).
func (v *indexInfo) makeIndexConstraints(
optimizer *xform.Optimizer, filter memo.ExprView, evalCtx *tree.EvalContext,
) error {
numIndexCols := len(v.index.ColumnIDs)
numExtraCols := 0
isInverted := (v.index.Type == sqlbase.IndexDescriptor_INVERTED)
// TODO(radu): we currently don't support index constraints on PK
// columns on an inverted index.
if !isInverted && !v.index.Unique {
// We have a non-unique index; the extra columns are added to the key and we
// can use them for index constraints.
numExtraCols = len(v.index.ExtraColumnIDs)
}
colIdxMap := make(map[sqlbase.ColumnID]int, len(v.desc.Columns))
for i := range v.desc.Columns {
colIdxMap[v.desc.Columns[i].ID] = i
}
columns := make([]opt.OrderingColumn, 0, numIndexCols+numExtraCols)
var notNullCols opt.ColSet
for i := 0; i < numIndexCols+numExtraCols; i++ {
var colID sqlbase.ColumnID
var dir encoding.Direction
if i < numIndexCols {
colID = v.index.ColumnIDs[i]
var err error
dir, err = v.index.ColumnDirections[i].ToEncodingDirection()
if err != nil {
return err
}
} else {
colID = v.index.ExtraColumnIDs[i-numIndexCols]
// Extra columns are always ascending.
dir = encoding.Ascending
}
idx, ok := colIdxMap[colID]
if !ok {
// Inactive column.
break
}
col := opt.MakeOrderingColumn(opt.ColumnID(idx+1), dir == encoding.Descending)
columns = append(columns, col)
if !v.desc.Columns[idx].Nullable {
notNullCols.Add(idx + 1)
}
}
v.ic.Init(filter, columns, notNullCols, isInverted, evalCtx, optimizer.Factory())
idxConstraint := v.ic.Constraint()
if idxConstraint.IsUnconstrained() {
// The index isn't being restricted at all, bump the cost significantly to
// make any index which does restrict the keys more desirable.
v.cost *= 1000
} else {
v.exactPrefix = idxConstraint.ExactPrefix(evalCtx)
// Find the number of columns that are restricted in all spans.
numCols := len(columns)
for i := 0; i < idxConstraint.Spans.Count(); i++ {
sp := idxConstraint.Spans.Get(i)
// Take the max between the length of the start values and the end
// values.
n := sp.StartKey().Length()
if e := sp.EndKey().Length(); n < e {
n = e
}
// Take the minimum n across all spans.
if numCols > n {
numCols = n
}
}
// Boost the cost by what fraction of columns have constraints. The higher
// the fraction, the smaller the cost.
v.cost *= float64((numIndexCols + numExtraCols)) / float64(numCols)
}
return nil
}
func unconstrainedSpans(
tableDesc *sqlbase.TableDescriptor, index *sqlbase.IndexDescriptor,
) (roachpb.Spans, error) {
return spansFromConstraint(tableDesc, index, nil, exec.ColumnOrdinalSet{})
}
// spansFromConstraint converts the spans in a Constraint to roachpb.Spans.
//
// interstices are pieces of the key that need to be inserted after each column
// (for interleavings).
func spansFromConstraint(
tableDesc *sqlbase.TableDescriptor,
index *sqlbase.IndexDescriptor,
c *constraint.Constraint,
needed exec.ColumnOrdinalSet,
) (roachpb.Spans, error) {
interstices := make([][]byte, len(index.ColumnDirections)+len(index.ExtraColumnIDs)+1)
interstices[0] = sqlbase.MakeIndexKeyPrefix(tableDesc, index.ID)
if len(index.Interleave.Ancestors) > 0 {
// TODO(eisen): too much of this code is copied from EncodePartialIndexKey.
sharedPrefixLen := 0
for i, ancestor := range index.Interleave.Ancestors {
// The first ancestor is already encoded in interstices[0].
if i != 0 {
interstices[sharedPrefixLen] =
encoding.EncodeUvarintAscending(interstices[sharedPrefixLen], uint64(ancestor.TableID))
interstices[sharedPrefixLen] =
encoding.EncodeUvarintAscending(interstices[sharedPrefixLen], uint64(ancestor.IndexID))
}
sharedPrefixLen += int(ancestor.SharedPrefixLen)
interstices[sharedPrefixLen] = encoding.EncodeInterleavedSentinel(interstices[sharedPrefixLen])
}
interstices[sharedPrefixLen] =
encoding.EncodeUvarintAscending(interstices[sharedPrefixLen], uint64(tableDesc.ID))
interstices[sharedPrefixLen] =
encoding.EncodeUvarintAscending(interstices[sharedPrefixLen], uint64(index.ID))
}
if c == nil || c.IsUnconstrained() {
// Encode a full span.
sp, err := spansFromConstraintSpan(
tableDesc, index, &constraint.UnconstrainedSpan, interstices, needed)
if err != nil {
return nil, err
}
return sp, nil
}
spans := make(roachpb.Spans, 0, c.Spans.Count())
for i := 0; i < c.Spans.Count(); i++ {
s, err := spansFromConstraintSpan(tableDesc, index, c.Spans.Get(i), interstices, needed)
if err != nil {
return nil, err
}
spans = append(spans, s...)
}
return spans, nil
}
// encodeConstraintKey encodes each logical part of a constraint.Key into a
// roachpb.Key; interstices[i] is inserted before the i-th value.
func encodeConstraintKey(
index *sqlbase.IndexDescriptor, ck constraint.Key, interstices [][]byte,
) (roachpb.Key, error) {
var key roachpb.Key
for i := 0; i < ck.Length(); i++ {
val := ck.Value(i)
key = append(key, interstices[i]...)
var err error
// For extra columns (like implicit columns), the direction
// is ascending.
dir := encoding.Ascending
if i < len(index.ColumnDirections) {
dir, err = index.ColumnDirections[i].ToEncodingDirection()
if err != nil {
return nil, err
}
}
if index.Type == sqlbase.IndexDescriptor_INVERTED {
keys, err := sqlbase.EncodeInvertedIndexTableKeys(val, key)
if err != nil {
return nil, err
}
if len(keys) > 1 {
err := pgerror.NewError(
pgerror.CodeInternalError, "trying to use multiple keys in index lookup",
)
return nil, err
}
key = keys[0]
} else {
key, err = sqlbase.EncodeTableKey(key, val, dir)
if err != nil {
return nil, err
}
}
}
return key, nil
}
// spansFromConstraintSpan converts a constraint.Span to one or more
// roachpb.Spans. It returns multiple spans in the case that multiple,
// non-adjacent column families should be scanned.
func spansFromConstraintSpan(
tableDesc *sqlbase.TableDescriptor,
index *sqlbase.IndexDescriptor,
cs *constraint.Span,
interstices [][]byte,
needed exec.ColumnOrdinalSet,
) (roachpb.Spans, error) {
var s roachpb.Span
var err error
// Encode each logical part of the start key.
s.Key, err = encodeConstraintKey(index, cs.StartKey(), interstices)
if err != nil {
return nil, err
}
if cs.StartBoundary() == constraint.IncludeBoundary {
s.Key = append(s.Key, interstices[cs.StartKey().Length()]...)
} else {
// We need to exclude the value this logical part refers to.
s.Key = s.Key.PrefixEnd()
}
// Encode each logical part of the end key.
s.EndKey, err = encodeConstraintKey(index, cs.EndKey(), interstices)
if err != nil {
return nil, err
}
s.EndKey = append(s.EndKey, interstices[cs.EndKey().Length()]...)
// Optimization: for single row lookups on a table with multiple column
// families, only scan the relevant column families.
if needed.Len() > 0 &&
index.ID == tableDesc.PrimaryIndex.ID &&
len(tableDesc.Families) > 1 &&
cs.StartKey().Length() == len(tableDesc.PrimaryIndex.ColumnIDs) &&
s.Key.Equal(s.EndKey) {
neededFamilyIDs := neededColumnFamilyIDs(tableDesc, needed)
if len(neededFamilyIDs) < len(tableDesc.Families) {
spans := make(roachpb.Spans, 0, len(neededFamilyIDs))
for i, familyID := range neededFamilyIDs {
var span roachpb.Span
span.Key = make(roachpb.Key, len(s.Key))
copy(span.Key, s.Key)
span.Key = keys.MakeFamilyKey(span.Key, uint32(familyID))
span.EndKey = span.Key.PrefixEnd()
if i > 0 && familyID == neededFamilyIDs[i-1]+1 {
// This column family is adjacent to the previous one. We can merge
// the two spans into one.
spans[len(spans)-1].EndKey = span.EndKey
} else {
spans = append(spans, span)
}
}
return spans, nil
}
}
// We tighten the end key to prevent reading interleaved children after the
// last parent key. If cs.End.Inclusive is true, we also advance the key as
// necessary.
endInclusive := cs.EndBoundary() == constraint.IncludeBoundary
s.EndKey, err = sqlbase.AdjustEndKeyForInterleave(tableDesc, index, s.EndKey, endInclusive)
if err != nil {
return nil, err
}
return roachpb.Spans{s}, nil
}
func neededColumnFamilyIDs(
tableDesc *sqlbase.TableDescriptor, neededCols exec.ColumnOrdinalSet,
) []sqlbase.FamilyID {
colIdxMap := tableDesc.ColumnIdxMap()
var needed []sqlbase.FamilyID
for _, family := range tableDesc.Families {
for _, columnID := range family.ColumnIDs {
columnOrdinal := colIdxMap[columnID]
if neededCols.Contains(columnOrdinal) {
needed = append(needed, family.ID)
break
}
}
}
// TODO(solon): There is a further optimization possible here: if there is at
// least one non-nullable column in the needed column families, we can
// potentially omit the primary family, since the primary keys are encoded
// in all families.
return needed
}