@@ -178,6 +178,19 @@ func (m colIdxMap) Swap(i, j int) {
178
178
m .ords [i ], m .ords [j ] = m .ords [j ], m .ords [i ]
179
179
}
180
180
181
+ // Get returns the ordinal for the given column ID, returning -1 if the column
182
+ // ID is not in the map.
183
+ func (m colIdxMap ) Get (id descpb.ColumnID ) int {
184
+ // Binary search for the column id.
185
+ idx := sort .Search (len (m .vals ), func (i int ) bool {
186
+ return m .vals [i ] >= id
187
+ })
188
+ if idx < len (m .vals ) && m .vals [idx ] == id {
189
+ return m .ords [idx ]
190
+ }
191
+ return - 1
192
+ }
193
+
181
194
type cFetcherArgs struct {
182
195
// memoryLimit determines the maximum memory footprint of the output batch.
183
196
memoryLimit int64
@@ -387,15 +400,15 @@ func (cf *cFetcher) Init(
387
400
return errors .AssertionFailedf ("unsupported IndexFetchSpec version %d" , tableArgs .spec .Version )
388
401
}
389
402
table := newCTableInfo ()
390
- nCols := tableArgs .ColIdxMap . Len ( )
403
+ nCols := len ( tableArgs .spec . FetchedColumns )
391
404
if cap (table .orderedColIdxMap .vals ) < nCols {
392
405
table .orderedColIdxMap .vals = make (descpb.ColumnIDs , 0 , nCols )
393
406
table .orderedColIdxMap .ords = make ([]int , 0 , nCols )
394
407
}
395
408
for i := range tableArgs .spec .FetchedColumns {
396
409
id := tableArgs .spec .FetchedColumns [i ].ColumnID
397
410
table .orderedColIdxMap .vals = append (table .orderedColIdxMap .vals , id )
398
- table .orderedColIdxMap .ords = append (table .orderedColIdxMap .ords , tableArgs . ColIdxMap . GetDefault ( id ) )
411
+ table .orderedColIdxMap .ords = append (table .orderedColIdxMap .ords , i )
399
412
}
400
413
sort .Sort (table .orderedColIdxMap )
401
414
* table = cTableInfo {
@@ -464,10 +477,9 @@ func (cf *cFetcher) Init(
464
477
needToDecodeDecimalKey := false
465
478
for i := range fullColumns {
466
479
col := & fullColumns [i ]
467
- colIdx , ok := tableArgs .ColIdxMap .Get (col .ColumnID )
468
- if ok {
469
- //gcassert:bce
470
- indexColOrdinals [i ] = colIdx
480
+ //gcassert:bce
481
+ indexColOrdinals [i ] = table .orderedColIdxMap .Get (col .ColumnID )
482
+ if colIdx := indexColOrdinals [i ]; colIdx >= 0 {
471
483
cf .mustDecodeIndexKey = true
472
484
needToDecodeDecimalKey = needToDecodeDecimalKey || tableArgs .spec .FetchedColumns [colIdx ].Type .Family () == types .DecimalFamily
473
485
// A composite column might also have a value encoding which must be
@@ -477,9 +489,6 @@ func (cf *cFetcher) Init(
477
489
} else {
478
490
table .neededValueColsByIdx .Remove (colIdx )
479
491
}
480
- } else {
481
- //gcassert:bce
482
- indexColOrdinals [i ] = - 1
483
492
}
484
493
}
485
494
if needToDecodeDecimalKey && cap (cf .scratch .decoding ) < 64 {
@@ -496,8 +505,7 @@ func (cf *cFetcher) Init(
496
505
suffixCols := table .spec .KeySuffixColumns ()
497
506
for i := range suffixCols {
498
507
id := suffixCols [i ].ColumnID
499
- colIdx , ok := tableArgs .ColIdxMap .Get (id )
500
- if ok {
508
+ if colIdx := table .orderedColIdxMap .Get (id ); colIdx >= 0 {
501
509
if suffixCols [i ].IsComposite {
502
510
table .compositeIndexColOrdinals .Add (colIdx )
503
511
// Note: we account for these composite columns separately: we add
@@ -521,14 +529,8 @@ func (cf *cFetcher) Init(
521
529
extraValColOrdinals := table .extraValColOrdinals
522
530
_ = extraValColOrdinals [len (suffixCols )- 1 ]
523
531
for i := range suffixCols {
524
- idx , ok := tableArgs .ColIdxMap .Get (suffixCols [i ].ColumnID )
525
- if ok {
526
- //gcassert:bce
527
- extraValColOrdinals [i ] = idx
528
- } else {
529
- //gcassert:bce
530
- extraValColOrdinals [i ] = - 1
531
- }
532
+ //gcassert:bce
533
+ extraValColOrdinals [i ] = table .orderedColIdxMap .Get (suffixCols [i ].ColumnID )
532
534
}
533
535
}
534
536
@@ -1150,21 +1152,23 @@ func (cf *cFetcher) processValue(ctx context.Context, familyID descpb.FamilyID)
1150
1152
if familyID == 0 {
1151
1153
break
1152
1154
}
1153
- // Find the default column ID for the family.
1154
- var defaultColumnID descpb.ColumnID
1155
+ // Find the default column for the family.
1156
+ var defaultColumnIdx int
1157
+ found := false
1155
1158
for _ , f := range table .spec .FamilyDefaultColumns {
1156
1159
if f .FamilyID == familyID {
1157
- defaultColumnID = f .DefaultColumnID
1160
+ defaultColumnIdx = table .orderedColIdxMap .Get (f .DefaultColumnID )
1161
+ found = true
1158
1162
break
1159
1163
}
1160
1164
}
1161
- if defaultColumnID == 0 {
1165
+ if ! found {
1162
1166
return scrub .WrapError (
1163
1167
scrub .IndexKeyDecodingError ,
1164
- errors .AssertionFailedf ("single entry value with no default column id " ),
1168
+ errors .AssertionFailedf ("single entry value with no default column" ),
1165
1169
)
1166
1170
}
1167
- prettyKey , prettyValue , err = cf .processValueSingle (table , defaultColumnID , prettyKey )
1171
+ prettyKey , prettyValue , err = cf .processValueSingle (table , defaultColumnIdx , prettyKey )
1168
1172
}
1169
1173
if err != nil {
1170
1174
return scrub .WrapError (scrub .IndexValueDecodingError , err )
@@ -1232,35 +1236,35 @@ func (cf *cFetcher) processValue(ctx context.Context, familyID descpb.FamilyID)
1232
1236
// value in cf.machine.colvecs accordingly.
1233
1237
// The key is only used for logging.
1234
1238
func (cf * cFetcher ) processValueSingle (
1235
- table * cTableInfo , colID descpb. ColumnID , prettyKeyPrefix string ,
1239
+ table * cTableInfo , idx int , prettyKeyPrefix string ,
1236
1240
) (prettyKey string , prettyValue string , err error ) {
1237
1241
prettyKey = prettyKeyPrefix
1238
1242
1239
- if idx , ok := table .ColIdxMap .Get (colID ); ok {
1240
- if cf .traceKV {
1241
- prettyKey = fmt .Sprintf ("%s/%s" , prettyKey , table .spec .FetchedColumns [idx ].Name )
1242
- }
1243
- val := cf .machine .nextKV .Value
1244
- if ! val .IsPresent () {
1245
- return prettyKey , "" , nil
1246
- }
1247
- typ := cf .table .spec .FetchedColumns [idx ].Type
1248
- err := colencoding .UnmarshalColumnValueToCol (
1249
- & table .da , & cf .machine .colvecs , idx , cf .machine .rowIdx , typ , val ,
1250
- )
1251
- if err != nil {
1252
- return "" , "" , err
1253
- }
1254
- cf .machine .remainingValueColsByIdx .Remove (idx )
1255
-
1256
- if cf .traceKV {
1257
- prettyValue = cf .getDatumAt (idx , cf .machine .rowIdx ).String ()
1258
- }
1243
+ if idx < 0 {
1244
+ // No need to unmarshal the column value. Either the column was part of
1245
+ // the index key or it isn't needed.
1259
1246
return prettyKey , prettyValue , nil
1260
1247
}
1261
1248
1262
- // No need to unmarshal the column value. Either the column was part of
1263
- // the index key or it isn't needed.
1249
+ if cf .traceKV {
1250
+ prettyKey = fmt .Sprintf ("%s/%s" , prettyKey , table .spec .FetchedColumns [idx ].Name )
1251
+ }
1252
+ val := cf .machine .nextKV .Value
1253
+ if ! val .IsPresent () {
1254
+ return prettyKey , "" , nil
1255
+ }
1256
+ typ := cf .table .spec .FetchedColumns [idx ].Type
1257
+ err = colencoding .UnmarshalColumnValueToCol (
1258
+ & table .da , & cf .machine .colvecs , idx , cf .machine .rowIdx , typ , val ,
1259
+ )
1260
+ if err != nil {
1261
+ return "" , "" , err
1262
+ }
1263
+ cf .machine .remainingValueColsByIdx .Remove (idx )
1264
+
1265
+ if cf .traceKV {
1266
+ prettyValue = cf .getDatumAt (idx , cf .machine .rowIdx ).String ()
1267
+ }
1264
1268
return prettyKey , prettyValue , nil
1265
1269
}
1266
1270
0 commit comments