-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
cfetcher.go
1318 lines (1205 loc) · 46.9 KB
/
cfetcher.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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 colfetcher
import (
"bytes"
"context"
"fmt"
"sort"
"strings"
"sync"
"github.com/cockroachdb/apd/v3"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/colconv"
"github.com/cockroachdb/cockroach/pkg/sql/colencoding"
"github.com/cockroachdb/cockroach/pkg/sql/colexecerror"
"github.com/cockroachdb/cockroach/pkg/sql/colmem"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra/execreleasable"
"github.com/cockroachdb/cockroach/pkg/sql/row"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc/keyside"
"github.com/cockroachdb/cockroach/pkg/sql/rowinfra"
"github.com/cockroachdb/cockroach/pkg/sql/scrub"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
"github.com/lib/pq/oid"
)
type cTableInfo struct {
// -- Fields initialized once --
*cFetcherTableArgs
// The set of required value-component column ordinals among only needed
// columns.
neededValueColsByIdx util.FastIntSet
// Map used to get the column index based on the descpb.ColumnID.
// It's kept as a pointer so we don't have to re-allocate to sort it each
// time.
orderedColIdxMap *colIdxMap
// One value per column that is part of the key; each value is a column
// ordinal among only needed columns; -1 if we don't need the value for
// that column.
indexColOrdinals []int
// The set of column ordinals which are both composite and part of the index
// key.
compositeIndexColOrdinals util.FastIntSet
// One number per column coming from the "key suffix" that is part of the
// value; each number is a column ordinal among only needed columns; -1 if
// we don't need the value for that column.
//
// The "key suffix" columns are only used for secondary indexes:
// - for non-unique indexes, these columns are appended to the key (and will
// be included in indexColOrdinals instead);
// - for unique indexes, these columns are stored in the value (unless the
// key contains a NULL value: then the extra columns are appended to the key
// to unique-ify it).
extraValColOrdinals []int
// The following fields contain MVCC metadata for each row and may be
// returned to users of cFetcher immediately after NextBatch returns.
//
// rowLastModified is the timestamp of the last time any family in the row
// was modified in any way.
rowLastModified hlc.Timestamp
// timestampOutputIdx controls at what column ordinal in the output batch to
// write the timestamp for the MVCC timestamp system column.
timestampOutputIdx int
// oidOutputIdx controls at what column ordinal in the output batch to write
// the value for the tableoid system column.
oidOutputIdx int
da tree.DatumAlloc
}
var _ execreleasable.Releasable = &cTableInfo{}
var cTableInfoPool = sync.Pool{
New: func() interface{} {
return &cTableInfo{
orderedColIdxMap: &colIdxMap{},
}
},
}
func newCTableInfo() *cTableInfo {
return cTableInfoPool.Get().(*cTableInfo)
}
// Release implements the execinfra.Releasable interface.
func (c *cTableInfo) Release() {
c.cFetcherTableArgs.Release()
// Note that all slices are being reused, but there is no need to deeply
// reset them since all of the slices are of Go native types.
c.orderedColIdxMap.ords = c.orderedColIdxMap.ords[:0]
c.orderedColIdxMap.vals = c.orderedColIdxMap.vals[:0]
*c = cTableInfo{
orderedColIdxMap: c.orderedColIdxMap,
indexColOrdinals: c.indexColOrdinals[:0],
extraValColOrdinals: c.extraValColOrdinals[:0],
}
cTableInfoPool.Put(c)
}
// colIdxMap is a "map" that contains the ordinals for each ColumnID among the
// columns that need to be fetched. This map is used to figure out what index
// within a row a particular value-component column goes into. Value-component
// columns are encoded with a column id prefix, with the guarantee that within
// any given row, the column ids are always increasing. Because of this
// guarantee, we can store this map as two sorted lists that the fetcher keeps
// an index into, giving fast access during decoding.
//
// It implements sort.Interface to be sortable on vals, while keeping ords
// matched up to the order of vals.
type colIdxMap struct {
// vals is the sorted list of descpb.ColumnIDs in the table to fetch.
vals descpb.ColumnIDs
// ords is the list of ordinals into all columns of the table for each
// column in vals. The ith entry in ords is the ordinal among all columns of
// the table for the ith column in vals.
ords []int
}
// Len implements sort.Interface.
func (m colIdxMap) Len() int {
return len(m.vals)
}
// Less implements sort.Interface.
func (m colIdxMap) Less(i, j int) bool {
return m.vals[i] < m.vals[j]
}
// Swap implements sort.Interface.
func (m colIdxMap) Swap(i, j int) {
m.vals[i], m.vals[j] = m.vals[j], m.vals[i]
m.ords[i], m.ords[j] = m.ords[j], m.ords[i]
}
type cFetcherArgs struct {
// memoryLimit determines the maximum memory footprint of the output batch.
memoryLimit int64
// estimatedRowCount is the optimizer-derived number of expected rows that
// this fetch will produce, if non-zero.
estimatedRowCount uint64
// traceKV indicates whether or not session tracing is enabled. It is set
// when initializing the fetcher.
traceKV bool
// singleUse, if true, indicates that the cFetcher will only need to scan a
// single set of spans. This allows the cFetcher to close itself eagerly,
// once it finishes the first fetch.
singleUse bool
}
// noOutputColumn is a sentinel value to denote that a system column is not
// part of the output.
const noOutputColumn = -1
// cFetcher handles fetching kvs and forming table rows for an
// arbitrary number of tables.
// Usage:
// var cf cFetcher
// err := cf.Init(..)
// // Handle err
// err := cf.StartScan(..)
// // Handle err
// for {
// res, err := cf.NextBatch()
// // Handle err
// if res.colBatch.Length() == 0 {
// // Done
// break
// }
// // Process res.colBatch
// }
// cf.Close(ctx)
type cFetcher struct {
cFetcherArgs
// table is the table that's configured for fetching.
table *cTableInfo
// True if the index key must be decoded. This is only false if there are no
// needed columns.
mustDecodeIndexKey bool
// mvccDecodeStrategy controls whether or not MVCC timestamps should
// be decoded from KV's fetched. It is set if any of the requested tables
// are required to produce an MVCC timestamp system column.
mvccDecodeStrategy row.MVCCDecodingStrategy
// fetcher is the underlying fetcher that provides KVs.
fetcher *row.KVFetcher
// bytesRead and batchRequestsIssued store the total number of bytes read
// and of BatchRequests issued, respectively, by this cFetcher throughout
// its lifetime in case when the underlying row.KVFetcher has already been
// closed and nil-ed out.
//
// The fields should not be accessed directly by the users of the cFetcher -
// getBytesRead() and getBatchRequestsIssued() should be used instead.
bytesRead int64
batchRequestsIssued int64
// machine contains fields that get updated during the run of the fetcher.
machine struct {
// state is the queue of next states of the state machine. The 0th entry
// is the next state.
state [3]fetcherState
// rowIdx is always set to the ordinal of the row we're currently writing to
// within the current batch. It's incremented as soon as we detect that a row
// is finished.
rowIdx int
// nextKV is the kv to process next.
nextKV roachpb.KeyValue
// limitHint is a hint as to the number of rows that the caller expects
// to be returned from this fetch. It will be decremented whenever a
// batch is returned by the length of the batch so that it tracks the
// hint for the rows remaining to be returned. It might become negative
// indicating that the hint is no longer applicable.
limitHint int
// remainingValueColsByIdx is the set of value columns that are yet to be
// seen during the decoding of the current row.
remainingValueColsByIdx util.FastIntSet
// lastRowPrefix is the row prefix for the last row we saw a key for. New
// keys are compared against this prefix to determine whether they're part
// of a new row or not.
lastRowPrefix roachpb.Key
// prettyValueBuf is a temp buffer used to create strings for tracing.
prettyValueBuf *bytes.Buffer
// batch is the output batch the fetcher writes to.
batch coldata.Batch
// colvecs are the vectors of batch that have been converted to the well
// typed columns to avoid expensive type casts on each row.
colvecs coldata.TypedVecs
// timestampCol is the underlying ColVec for the timestamp output column,
// or nil if the timestamp column was not requested. It is pulled out from
// colvecs to avoid having to cast the vec to decimal on every write.
timestampCol []apd.Decimal
// tableoidCol is the same as timestampCol but for the tableoid system column.
tableoidCol coldata.DatumVec
}
// scratch is a scratch space used when decoding bytes-like and decimal
// keys.
scratch []byte
accountingHelper colmem.SetAccountingHelper
}
func (cf *cFetcher) resetBatch() {
var reallocated bool
var minDesiredCapacity int
if cf.machine.limitHint > 0 && (cf.estimatedRowCount == 0 || uint64(cf.machine.limitHint) < cf.estimatedRowCount) {
// If we have a limit hint, and either
// 1) we don't have an estimate, or
// 2) we have a soft limit,
// use the hint to size the batch. Note that if it exceeds
// coldata.BatchSize, ResetMaybeReallocate will chop it down.
minDesiredCapacity = cf.machine.limitHint
} else {
// Otherwise, use the estimate. Note that if the estimate is not
// present, it'll be 0 and ResetMaybeReallocate will allocate the
// initial batch of capacity 1 which is the desired behavior.
//
// We need to transform our cf.estimatedRowCount, which is a uint64,
// into an int. We have to be careful: if we just cast it directly, a
// giant estimate will wrap around and become negative.
if cf.estimatedRowCount > uint64(coldata.BatchSize()) {
minDesiredCapacity = coldata.BatchSize()
} else {
minDesiredCapacity = int(cf.estimatedRowCount)
}
}
cf.machine.batch, reallocated = cf.accountingHelper.ResetMaybeReallocate(
cf.table.typs, cf.machine.batch, minDesiredCapacity, false, /* desiredCapacitySufficient */
)
if reallocated {
cf.machine.colvecs.SetBatch(cf.machine.batch)
// Pull out any requested system column output vecs.
if cf.table.timestampOutputIdx != noOutputColumn {
cf.machine.timestampCol = cf.machine.colvecs.DecimalCols[cf.machine.colvecs.ColsMap[cf.table.timestampOutputIdx]]
}
if cf.table.oidOutputIdx != noOutputColumn {
cf.machine.tableoidCol = cf.machine.colvecs.DatumCols[cf.machine.colvecs.ColsMap[cf.table.oidOutputIdx]]
}
// Change the allocation size to be the same as the capacity of the
// batch we allocated above.
cf.table.da.AllocSize = cf.machine.batch.Capacity()
}
}
// Init sets up the cFetcher based on the table args. Only columns present in
// tableArgs.cols will be fetched.
func (cf *cFetcher) Init(
allocator *colmem.Allocator, kvFetcher *row.KVFetcher, tableArgs *cFetcherTableArgs,
) error {
if tableArgs.spec.Version != descpb.IndexFetchSpecVersionInitial {
return errors.Newf("unsupported IndexFetchSpec version %d", tableArgs.spec.Version)
}
table := newCTableInfo()
nCols := tableArgs.ColIdxMap.Len()
if cap(table.orderedColIdxMap.vals) < nCols {
table.orderedColIdxMap.vals = make(descpb.ColumnIDs, 0, nCols)
table.orderedColIdxMap.ords = make([]int, 0, nCols)
}
for i := range tableArgs.spec.FetchedColumns {
id := tableArgs.spec.FetchedColumns[i].ColumnID
table.orderedColIdxMap.vals = append(table.orderedColIdxMap.vals, id)
table.orderedColIdxMap.ords = append(table.orderedColIdxMap.ords, tableArgs.ColIdxMap.GetDefault(id))
}
sort.Sort(table.orderedColIdxMap)
*table = cTableInfo{
cFetcherTableArgs: tableArgs,
orderedColIdxMap: table.orderedColIdxMap,
indexColOrdinals: table.indexColOrdinals[:0],
extraValColOrdinals: table.extraValColOrdinals[:0],
timestampOutputIdx: noOutputColumn,
oidOutputIdx: noOutputColumn,
}
if nCols > 0 {
table.neededValueColsByIdx.AddRange(0 /* start */, nCols-1)
}
// Check for system columns.
for idx := range tableArgs.spec.FetchedColumns {
colID := tableArgs.spec.FetchedColumns[idx].ColumnID
if colinfo.IsColIDSystemColumn(colID) {
// Set up extra metadata for system columns.
//
// Currently the system columns are present in neededValueColsByIdx,
// but we don't want to include them in that set because the
// handling of system columns is separate from the standard value
// decoding process.
switch colinfo.GetSystemColumnKindFromColumnID(colID) {
case catpb.SystemColumnKind_MVCCTIMESTAMP:
table.timestampOutputIdx = idx
cf.mvccDecodeStrategy = row.MVCCDecodingRequired
table.neededValueColsByIdx.Remove(idx)
case catpb.SystemColumnKind_TABLEOID:
table.oidOutputIdx = idx
table.neededValueColsByIdx.Remove(idx)
}
}
}
fullColumns := table.spec.KeyFullColumns()
nIndexCols := len(fullColumns)
if cap(table.indexColOrdinals) >= nIndexCols {
table.indexColOrdinals = table.indexColOrdinals[:nIndexCols]
} else {
table.indexColOrdinals = make([]int, nIndexCols)
}
indexColOrdinals := table.indexColOrdinals
_ = indexColOrdinals[len(fullColumns)-1]
needToDecodeDecimalKey := false
for i := range fullColumns {
col := &fullColumns[i]
colIdx, ok := tableArgs.ColIdxMap.Get(col.ColumnID)
if ok {
//gcassert:bce
indexColOrdinals[i] = colIdx
cf.mustDecodeIndexKey = true
needToDecodeDecimalKey = needToDecodeDecimalKey || tableArgs.spec.FetchedColumns[colIdx].Type.Family() == types.DecimalFamily
// A composite column might also have a value encoding which must be
// decoded. Others can be removed from neededValueColsByIdx.
if col.IsComposite {
table.compositeIndexColOrdinals.Add(colIdx)
} else {
table.neededValueColsByIdx.Remove(colIdx)
}
} else {
//gcassert:bce
indexColOrdinals[i] = -1
}
}
if needToDecodeDecimalKey && cap(cf.scratch) < 64 {
// If we need to decode the decimal key encoding, it might use a scratch
// byte slice internally, so we'll allocate such a space to be reused
// for every decimal.
// TODO(yuzefovich): 64 was chosen arbitrarily, tune it.
cf.scratch = make([]byte, 64)
}
// Unique secondary indexes contain the extra column IDs as part of
// the value component. We process these separately, so we need to know
// what extra columns are composite or not.
if table.spec.NumKeySuffixColumns > 0 && table.spec.IsSecondaryIndex && table.spec.IsUniqueIndex {
suffixCols := table.spec.KeySuffixColumns()
for i := range suffixCols {
id := suffixCols[i].ColumnID
colIdx, ok := tableArgs.ColIdxMap.Get(id)
if ok {
if suffixCols[i].IsComposite {
table.compositeIndexColOrdinals.Add(colIdx)
// Note: we account for these composite columns separately: we add
// them back into the remaining values set in processValueBytes.
table.neededValueColsByIdx.Remove(colIdx)
}
}
}
// Unique secondary indexes have a value that is the
// primary index key.
// Primary indexes only contain ascendingly-encoded
// values. If this ever changes, we'll probably have to
// figure out the directions here too.
if cap(table.extraValColOrdinals) >= len(suffixCols) {
table.extraValColOrdinals = table.extraValColOrdinals[:len(suffixCols)]
} else {
table.extraValColOrdinals = make([]int, len(suffixCols))
}
extraValColOrdinals := table.extraValColOrdinals
_ = extraValColOrdinals[len(suffixCols)-1]
for i := range suffixCols {
idx, ok := tableArgs.ColIdxMap.Get(suffixCols[i].ColumnID)
if ok {
//gcassert:bce
extraValColOrdinals[i] = idx
} else {
//gcassert:bce
extraValColOrdinals[i] = -1
}
}
}
cf.table = table
cf.fetcher = kvFetcher
cf.accountingHelper.Init(allocator, cf.memoryLimit, cf.table.typs)
return nil
}
// StartScan initializes and starts the key-value scan. Can only be used
// multiple times if cFetcherArgs.singleUse was set to false in Init().
//
// The fetcher takes ownership of the spans slice - it can modify the slice and
// will perform the memory accounting accordingly. The caller can only reuse the
// spans slice after the fetcher emits a zero-length batch, and if the caller
// does, it becomes responsible for the memory accounting.
func (cf *cFetcher) StartScan(
ctx context.Context,
spans roachpb.Spans,
limitBatches bool,
batchBytesLimit rowinfra.BytesLimit,
limitHint rowinfra.RowLimit,
) error {
if len(spans) == 0 {
return errors.AssertionFailedf("no spans")
}
if !limitBatches && batchBytesLimit != rowinfra.NoBytesLimit {
return errors.AssertionFailedf("batchBytesLimit set without limitBatches")
}
// If we have a limit hint, we limit the first batch size. Subsequent
// batches get larger to avoid making things too slow (e.g. in case we have
// a very restrictive filter and actually have to retrieve a lot of rows).
firstBatchLimit := rowinfra.KeyLimit(limitHint)
if firstBatchLimit != 0 {
// The limitHint is a row limit, but each row could be made up of more
// than one key. We take the maximum possible keys per row out of all
// the table rows we could potentially scan over.
//
// Note that unlike for the row.Fetcher, we don't need an extra key to
// form the last row in the cFetcher because we are eagerly finalizing
// each row once we know that all KVs comprising that row have been
// fetched. Consider several cases:
// - the table has only one column family - then we can finalize each
// row right after the first KV is decoded;
// - the table has multiple column families:
// - KVs for all column families are present for all rows - then for
// each row, when its last KV is fetched, the row can be finalized
// (and firstBatchLimit asks exactly for the correct number of KVs);
// - KVs for some column families are omitted for some rows - then we
// will actually fetch more KVs than necessary, but we'll decode
// limitHint number of rows.
firstBatchLimit = rowinfra.KeyLimit(int(limitHint) * int(cf.table.spec.MaxKeysPerRow))
}
cf.machine.lastRowPrefix = nil
cf.machine.limitHint = int(limitHint)
cf.machine.state[0] = stateResetBatch
cf.machine.state[1] = stateInitFetch
return cf.fetcher.SetupNextFetch(
ctx, spans, nil /* spanIDs */, batchBytesLimit, firstBatchLimit,
)
}
// fetcherState is the state enum for NextBatch.
type fetcherState int
//go:generate stringer -type=fetcherState
const (
stateInvalid fetcherState = iota
// stateInitFetch is the empty state of a fetcher: there is no current KV to
// look at, and there's no current row, either because the fetcher has just
// started, or because the last row was already finalized.
//
// 1. fetch next kv into nextKV buffer
// -> decodeFirstKVOfRow
stateInitFetch
// stateResetBatch resets the batch of a fetcher, removing nulls and the
// selection vector.
stateResetBatch
// stateDecodeFirstKVOfRow is the state of looking at a key that is part of
// a row that the fetcher hasn't processed before. s.machine.nextKV must be
// set.
// 1. skip common prefix
// 2. parse key (past common prefix) into row buffer, setting last row prefix buffer
// 3. parse value into row buffer.
// 4. 1-cf or secondary index?
// -> doneRow(initFetch)
// else:
// -> fetchNextKVWithUnfinishedRow
stateDecodeFirstKVOfRow
// stateFetchNextKVWithUnfinishedRow is the state of getting a new key for
// the current row. The machine will read a new key from the underlying
// fetcher, process it, and either add the results to the current row, or
// shift to a new row.
// 1. fetch next kv into nextKV buffer
// 2. skip common prefix
// 3. check equality to last row prefix buffer
// 4. no?
// -> finalizeRow(decodeFirstKVOfRow)
// 5. skip to end of last row prefix buffer
// 6. parse value into row buffer
// 7. -> fetchNextKVWithUnfinishedRow
stateFetchNextKVWithUnfinishedRow
// stateFinalizeRow is the state of finalizing a row. It assumes that no more
// keys for the current row are present.
// state[1] must be set, and stateFinalizeRow will transition to that state
// once it finishes finalizing the row.
// 1. fill missing nulls
// 2. bump rowIdx
// -> nextState and optionally return if row-by-row or batch full
stateFinalizeRow
// stateEmitLastBatch emits the current batch and then transitions to
// stateFinished.
stateEmitLastBatch
// stateFinished is the end state of the state machine - it causes NextBatch
// to return empty batches forever.
stateFinished
)
// Turn this on to enable super verbose logging of the fetcher state machine.
const debugState = false
func (cf *cFetcher) setEstimatedRowCount(estimatedRowCount uint64) {
cf.estimatedRowCount = estimatedRowCount
}
// setNextKV sets the next KV to process to the input KV. needsCopy, if true,
// causes the input kv to be deep copied. needsCopy should be set to true if
// the input KV is pointing to the last KV of a batch, so that the batch can
// be garbage collected before fetching the next one.
// gcassert:inline
func (cf *cFetcher) setNextKV(kv roachpb.KeyValue, needsCopy bool) {
if !needsCopy {
cf.machine.nextKV = kv
return
}
// If we've made it to the very last key in the batch, copy out the key
// so that the GC can reclaim the large backing slice before we call
// NextKV() again.
kvCopy := roachpb.KeyValue{}
kvCopy.Key = make(roachpb.Key, len(kv.Key))
copy(kvCopy.Key, kv.Key)
kvCopy.Value.RawBytes = make([]byte, len(kv.Value.RawBytes))
copy(kvCopy.Value.RawBytes, kv.Value.RawBytes)
kvCopy.Value.Timestamp = kv.Value.Timestamp
cf.machine.nextKV = kvCopy
}
// NextBatch processes keys until we complete one batch of rows (subject to the
// limit hint and the memory limit while being max coldata.BatchSize() in
// length), which are returned in columnar format as a coldata.Batch. The batch
// contains one Vec per table column, regardless of the index used; columns that
// are not needed (as per neededCols) are filled with nulls. The Batch should
// not be modified and is only valid until the next call. When there are no more
// rows, the Batch.Length is 0.
func (cf *cFetcher) NextBatch(ctx context.Context) (coldata.Batch, error) {
for {
if debugState {
log.Infof(ctx, "State %s", cf.machine.state[0])
}
switch cf.machine.state[0] {
case stateInvalid:
return nil, errors.New("invalid fetcher state")
case stateInitFetch:
moreKVs, kv, _, finalReferenceToBatch, err := cf.fetcher.NextKV(ctx, cf.mvccDecodeStrategy)
if err != nil {
return nil, cf.convertFetchError(ctx, err)
}
if !moreKVs {
cf.machine.state[0] = stateEmitLastBatch
continue
}
// TODO(jordan): parse the logical longest common prefix of the span
// into a buffer. The logical longest common prefix is the longest
// common prefix that contains only full key components. For example,
// the keys /Table/53/1/foo/bar/10 and /Table/53/1/foo/bop/10 would
// have LLCS of /Table/53/1/foo, even though they share a b prefix of
// the next key, since that prefix isn't a complete key component.
/*
if newSpan {
lcs := cf.fetcher.span.LongestCommonPrefix()
// parse lcs into stuff
key, matches, err := rowenc.DecodeIndexKeyWithoutTableIDIndexIDPrefix(
cf.table.desc, cf.table.info.index, cf.table.info.keyValTypes,
cf.table.keyVals, cf.table.info.indexColumnDirs, kv.Key[cf.table.info.knownPrefixLength:],
)
if err != nil {
// This is expected - the longest common prefix of the keyspan might
// end half way through a key. Suppress the error and set the actual
// LCS we'll use later to the decodable components of the key.
}
}
*/
cf.setNextKV(kv, finalReferenceToBatch)
cf.machine.state[0] = stateDecodeFirstKVOfRow
case stateResetBatch:
cf.resetBatch()
cf.shiftState()
case stateDecodeFirstKVOfRow:
// Reset MVCC metadata for the table, since this is the first KV of a row.
cf.table.rowLastModified = hlc.Timestamp{}
// foundNull is set when decoding a new index key for a row finds a NULL value
// in the index key. This is used when decoding unique secondary indexes in order
// to tell whether they have extra columns appended to the key.
var foundNull bool
if cf.mustDecodeIndexKey {
if debugState {
log.Infof(ctx, "decoding first key %s", cf.machine.nextKV.Key)
}
var (
key []byte
err error
)
// For unique secondary indexes on tables with multiple column
// families, we must check all columns for NULL values in order
// to determine whether a KV belongs to the same row as the
// previous KV or a different row.
checkAllColsForNull := cf.table.spec.IsSecondaryIndex && cf.table.spec.IsUniqueIndex && cf.table.spec.MaxKeysPerRow != 1
key, foundNull, cf.scratch, err = colencoding.DecodeKeyValsToCols(
&cf.table.da,
&cf.machine.colvecs,
cf.machine.rowIdx,
cf.table.indexColOrdinals,
checkAllColsForNull,
cf.table.spec.KeyFullColumns(),
nil, /* unseen */
cf.machine.nextKV.Key[cf.table.spec.KeyPrefixLength:],
cf.scratch,
)
if err != nil {
return nil, err
}
prefix := cf.machine.nextKV.Key[:len(cf.machine.nextKV.Key)-len(key)]
cf.machine.lastRowPrefix = prefix
} else {
prefixLen, err := keys.GetRowPrefixLength(cf.machine.nextKV.Key)
if err != nil {
return nil, err
}
cf.machine.lastRowPrefix = cf.machine.nextKV.Key[:prefixLen]
}
// For unique secondary indexes on tables with multiple column
// families, the index-key does not distinguish one row from the
// next if both rows contain identical values along with a NULL.
// Consider the keys:
//
// /test/unique_idx/NULL/0
// /test/unique_idx/NULL/1
//
// The index-key extracted from the above keys is
// /test/unique_idx/NULL. The trailing /0 and /1 are the primary key
// used to unique-ify the keys when a NULL is present. When a null
// is present in the index key, we include the primary key columns
// in lastRowPrefix.
//
// Note that we do not need to do this for non-unique secondary
// indexes because the extra columns in the primary key will
// _always_ be there, so we can decode them when processing the
// index. The difference with unique secondary indexes is that the
// extra columns are not always there, and are used to unique-ify
// the index key, rather than provide the primary key column values.
//
// We also do not need to do this when a table has only one column
// family because it is guaranteed that there is only one KV per
// row. We entirely skip the check that determines if the row is
// unfinished.
if foundNull && cf.table.spec.IsSecondaryIndex && cf.table.spec.IsUniqueIndex && cf.table.spec.MaxKeysPerRow != 1 {
// We get the remaining bytes after the computed prefix, and then
// slice off the extra encoded columns from those bytes. We calculate
// how many bytes were sliced away, and then extend lastRowPrefix
// by that amount.
prefixLen := len(cf.machine.lastRowPrefix)
remainingBytes := cf.machine.nextKV.Key[prefixLen:]
origRemainingBytesLen := len(remainingBytes)
for i := 0; i < int(cf.table.spec.NumKeySuffixColumns); i++ {
var err error
// Slice off an extra encoded column from remainingBytes.
remainingBytes, err = keyside.Skip(remainingBytes)
if err != nil {
return nil, err
}
}
cf.machine.lastRowPrefix = cf.machine.nextKV.Key[:prefixLen+(origRemainingBytesLen-len(remainingBytes))]
}
familyID, err := cf.getCurrentColumnFamilyID()
if err != nil {
return nil, err
}
cf.machine.remainingValueColsByIdx.CopyFrom(cf.table.neededValueColsByIdx)
// Process the current KV's value component.
if err := cf.processValue(ctx, familyID); err != nil {
return nil, err
}
// Update the MVCC values for this row.
if cf.table.rowLastModified.Less(cf.machine.nextKV.Value.Timestamp) {
cf.table.rowLastModified = cf.machine.nextKV.Value.Timestamp
}
// If the index has only one column family, then the next KV will
// always belong to a different row than the current KV.
if cf.table.spec.MaxKeysPerRow == 1 {
cf.machine.state[0] = stateFinalizeRow
cf.machine.state[1] = stateInitFetch
continue
}
// If the table has more than one column family, then the next KV
// may belong to the same row as the current KV.
cf.machine.state[0] = stateFetchNextKVWithUnfinishedRow
case stateFetchNextKVWithUnfinishedRow:
moreKVs, kv, _, finalReferenceToBatch, err := cf.fetcher.NextKV(ctx, cf.mvccDecodeStrategy)
if err != nil {
return nil, cf.convertFetchError(ctx, err)
}
if !moreKVs {
// No more data. Finalize the row and exit.
cf.machine.state[0] = stateFinalizeRow
cf.machine.state[1] = stateEmitLastBatch
continue
}
// TODO(jordan): if nextKV returns newSpan = true, set the new span
// prefix and indicate that it needs decoding.
cf.setNextKV(kv, finalReferenceToBatch)
if debugState {
log.Infof(ctx, "decoding next key %s", cf.machine.nextKV.Key)
}
// TODO(yuzefovich): optimize this prefix check by skipping logical
// longest common span prefix.
if !bytes.HasPrefix(kv.Key[cf.table.spec.KeyPrefixLength:], cf.machine.lastRowPrefix[cf.table.spec.KeyPrefixLength:]) {
// The kv we just found is from a different row.
cf.machine.state[0] = stateFinalizeRow
cf.machine.state[1] = stateDecodeFirstKVOfRow
continue
}
familyID, err := cf.getCurrentColumnFamilyID()
if err != nil {
return nil, err
}
// Process the current KV's value component.
if err := cf.processValue(ctx, familyID); err != nil {
return nil, err
}
// Update the MVCC values for this row.
if cf.table.rowLastModified.Less(cf.machine.nextKV.Value.Timestamp) {
cf.table.rowLastModified = cf.machine.nextKV.Value.Timestamp
}
if familyID == cf.table.spec.MaxFamilyID {
// We know the row can't have any more keys, so finalize the row.
cf.machine.state[0] = stateFinalizeRow
cf.machine.state[1] = stateInitFetch
} else {
// Continue with current state.
cf.machine.state[0] = stateFetchNextKVWithUnfinishedRow
}
case stateFinalizeRow:
// Populate the timestamp system column if needed. We have to do it
// on a per row basis since each row can be modified at a different
// time.
if cf.table.timestampOutputIdx != noOutputColumn {
cf.machine.timestampCol[cf.machine.rowIdx] = eval.TimestampToDecimal(cf.table.rowLastModified)
}
// We're finished with a row. Fill the row in with nulls if
// necessary, perform the memory accounting for the row, bump the
// row index, emit the batch if necessary, and move to the next
// state.
if err := cf.fillNulls(); err != nil {
return nil, err
}
// Note that we haven't set the tableoid value (if that system
// column is requested) yet, but it is ok for the purposes of the
// memory accounting - oids are fixed length values and, thus, have
// already been accounted for when the batch was allocated.
emitBatch := cf.accountingHelper.AccountForSet(cf.machine.rowIdx)
cf.machine.rowIdx++
cf.shiftState()
if cf.machine.limitHint > 0 && cf.machine.rowIdx >= cf.machine.limitHint {
// If we made it to our limit hint, so output our batch early to
// make sure that we don't bother filling in extra data if we
// don't need to.
emitBatch = true
// Update the limit hint to track the expected remaining rows to
// be fetched.
//
// Note that limitHint might become negative at which point we
// will start ignoring it.
cf.machine.limitHint -= cf.machine.rowIdx
}
if emitBatch {
cf.pushState(stateResetBatch)
cf.finalizeBatch()
return cf.machine.batch, nil
}
case stateEmitLastBatch:
cf.machine.state[0] = stateFinished
cf.finalizeBatch()
if cf.singleUse {
// Close the fetcher eagerly so that its memory could be GCed.
cf.Close(ctx)
}
return cf.machine.batch, nil
case stateFinished:
if cf.singleUse {
// Close the fetcher eagerly so that its memory could be GCed.
cf.Close(ctx)
}
return coldata.ZeroBatch, nil
}
}
}
// shiftState shifts the state queue to the left, removing the first element and
// clearing the last element.
func (cf *cFetcher) shiftState() {
copy(cf.machine.state[:2], cf.machine.state[1:])
cf.machine.state[2] = stateInvalid
}
func (cf *cFetcher) pushState(state fetcherState) {
copy(cf.machine.state[1:], cf.machine.state[:2])
cf.machine.state[0] = state
}
// getDatumAt returns the converted datum object at the given (colIdx, rowIdx).
// This function is meant for tracing and should not be used in hot paths.
func (cf *cFetcher) getDatumAt(colIdx int, rowIdx int) tree.Datum {
res := []tree.Datum{nil}
colconv.ColVecToDatumAndDeselect(res, cf.machine.colvecs.Vecs[colIdx], 1 /* length */, []int{rowIdx}, &cf.table.da)
return res[0]
}
// writeDecodedCols writes the stringified representation of the decoded columns
// specified by colOrdinals. -1 in colOrdinals indicates that a column wasn't
// actually decoded (this is represented as "?" in the result). separator is
// inserted between each two subsequent decoded column values (but not before
// the first one).
func (cf *cFetcher) writeDecodedCols(buf *strings.Builder, colOrdinals []int, separator byte) {
for i, idx := range colOrdinals {
if i > 0 {
buf.WriteByte(separator)
}
if idx != -1 {
buf.WriteString(cf.getDatumAt(idx, cf.machine.rowIdx).String())
} else {
buf.WriteByte('?')
}
}
}
// processValue processes the state machine's current value component, setting
// columns in the rowIdx'th tuple in the current batch depending on what data
// is found in the current value component.
func (cf *cFetcher) processValue(ctx context.Context, familyID descpb.FamilyID) (err error) {
table := cf.table
var prettyKey, prettyValue string
if cf.traceKV {
defer func() {
if err == nil {
log.VEventf(ctx, 2, "fetched: %s -> %s", prettyKey, prettyValue)
}
}()
var buf strings.Builder
buf.WriteByte('/')
buf.WriteString(cf.table.spec.TableName)
buf.WriteByte('/')
buf.WriteString(cf.table.spec.IndexName)
buf.WriteByte('/')
cf.writeDecodedCols(&buf, cf.table.indexColOrdinals, '/')
prettyKey = buf.String()
}
if len(table.spec.FetchedColumns) == 0 {
// We don't need to decode any values.
return nil
}
val := cf.machine.nextKV.Value
if !table.spec.IsSecondaryIndex || table.spec.EncodingType == descpb.PrimaryIndexEncoding {
// If familyID is 0, kv.Value contains values for composite key columns.
// These columns already have a table.row value assigned above, but that value
// (obtained from the key encoding) might not be correct (e.g. for decimals,
// it might not contain the right number of trailing 0s; for collated
// strings, it is one of potentially many strings with the same collation
// key).
//
// In these cases, the correct value will be present in family 0 and the
// table.row value gets overwritten.
switch val.GetTag() {
case roachpb.ValueType_TUPLE:
// In this case, we don't need to decode the column family ID, because
// the ValueType_TUPLE encoding includes the column id with every encoded
// column value.
var tupleBytes []byte
tupleBytes, err = val.GetTuple()
if err != nil {
break
}
prettyKey, prettyValue, err = cf.processValueBytes(ctx, table, tupleBytes, prettyKey)
default:
// If familyID is 0, this is the row sentinel (in the legacy pre-family format),
// and a value is not expected, so we're done.
if familyID == 0 {
break
}
// Find the default column ID for the family.
var defaultColumnID descpb.ColumnID
for _, f := range table.spec.FamilyDefaultColumns {
if f.FamilyID == familyID {
defaultColumnID = f.DefaultColumnID
break
}
}
if defaultColumnID == 0 {
return scrub.WrapError(
scrub.IndexKeyDecodingError,
errors.Errorf("single entry value with no default column id"),
)
}
prettyKey, prettyValue, err = cf.processValueSingle(ctx, table, defaultColumnID, prettyKey)