Skip to content

Commit 9a05848

Browse files
committed
sql/colfetcher: remove catalog.TableColMap from cFetcherTableArgs
The `catalog.TableColMap` in `cFetcherTableArgs` has been removed. Information in `IndexFetchSpec` and `cTableInfo` is used instead. Release note: None
1 parent c554b13 commit 9a05848

File tree

3 files changed

+52
-56
lines changed

3 files changed

+52
-56
lines changed

pkg/sql/colfetcher/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ go_library(
2626
"//pkg/roachpb",
2727
"//pkg/settings",
2828
"//pkg/settings/cluster",
29-
"//pkg/sql/catalog",
3029
"//pkg/sql/catalog/catenumpb",
3130
"//pkg/sql/catalog/catpb",
3231
"//pkg/sql/catalog/colinfo",

pkg/sql/colfetcher/cfetcher.go

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ func (m colIdxMap) Swap(i, j int) {
178178
m.ords[i], m.ords[j] = m.ords[j], m.ords[i]
179179
}
180180

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+
181194
type cFetcherArgs struct {
182195
// memoryLimit determines the maximum memory footprint of the output batch.
183196
memoryLimit int64
@@ -387,15 +400,15 @@ func (cf *cFetcher) Init(
387400
return errors.AssertionFailedf("unsupported IndexFetchSpec version %d", tableArgs.spec.Version)
388401
}
389402
table := newCTableInfo()
390-
nCols := tableArgs.ColIdxMap.Len()
403+
nCols := len(tableArgs.spec.FetchedColumns)
391404
if cap(table.orderedColIdxMap.vals) < nCols {
392405
table.orderedColIdxMap.vals = make(descpb.ColumnIDs, 0, nCols)
393406
table.orderedColIdxMap.ords = make([]int, 0, nCols)
394407
}
395408
for i := range tableArgs.spec.FetchedColumns {
396409
id := tableArgs.spec.FetchedColumns[i].ColumnID
397410
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)
399412
}
400413
sort.Sort(table.orderedColIdxMap)
401414
*table = cTableInfo{
@@ -464,10 +477,9 @@ func (cf *cFetcher) Init(
464477
needToDecodeDecimalKey := false
465478
for i := range fullColumns {
466479
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 {
471483
cf.mustDecodeIndexKey = true
472484
needToDecodeDecimalKey = needToDecodeDecimalKey || tableArgs.spec.FetchedColumns[colIdx].Type.Family() == types.DecimalFamily
473485
// A composite column might also have a value encoding which must be
@@ -477,9 +489,6 @@ func (cf *cFetcher) Init(
477489
} else {
478490
table.neededValueColsByIdx.Remove(colIdx)
479491
}
480-
} else {
481-
//gcassert:bce
482-
indexColOrdinals[i] = -1
483492
}
484493
}
485494
if needToDecodeDecimalKey && cap(cf.scratch.decoding) < 64 {
@@ -496,8 +505,7 @@ func (cf *cFetcher) Init(
496505
suffixCols := table.spec.KeySuffixColumns()
497506
for i := range suffixCols {
498507
id := suffixCols[i].ColumnID
499-
colIdx, ok := tableArgs.ColIdxMap.Get(id)
500-
if ok {
508+
if colIdx := table.orderedColIdxMap.Get(id); colIdx >= 0 {
501509
if suffixCols[i].IsComposite {
502510
table.compositeIndexColOrdinals.Add(colIdx)
503511
// Note: we account for these composite columns separately: we add
@@ -521,14 +529,8 @@ func (cf *cFetcher) Init(
521529
extraValColOrdinals := table.extraValColOrdinals
522530
_ = extraValColOrdinals[len(suffixCols)-1]
523531
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)
532534
}
533535
}
534536

@@ -1150,21 +1152,23 @@ func (cf *cFetcher) processValue(ctx context.Context, familyID descpb.FamilyID)
11501152
if familyID == 0 {
11511153
break
11521154
}
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
11551158
for _, f := range table.spec.FamilyDefaultColumns {
11561159
if f.FamilyID == familyID {
1157-
defaultColumnID = f.DefaultColumnID
1160+
defaultColumnIdx = table.orderedColIdxMap.Get(f.DefaultColumnID)
1161+
found = true
11581162
break
11591163
}
11601164
}
1161-
if defaultColumnID == 0 {
1165+
if !found {
11621166
return scrub.WrapError(
11631167
scrub.IndexKeyDecodingError,
1164-
errors.AssertionFailedf("single entry value with no default column id"),
1168+
errors.AssertionFailedf("single entry value with no default column"),
11651169
)
11661170
}
1167-
prettyKey, prettyValue, err = cf.processValueSingle(table, defaultColumnID, prettyKey)
1171+
prettyKey, prettyValue, err = cf.processValueSingle(table, defaultColumnIdx, prettyKey)
11681172
}
11691173
if err != nil {
11701174
return scrub.WrapError(scrub.IndexValueDecodingError, err)
@@ -1232,35 +1236,35 @@ func (cf *cFetcher) processValue(ctx context.Context, familyID descpb.FamilyID)
12321236
// value in cf.machine.colvecs accordingly.
12331237
// The key is only used for logging.
12341238
func (cf *cFetcher) processValueSingle(
1235-
table *cTableInfo, colID descpb.ColumnID, prettyKeyPrefix string,
1239+
table *cTableInfo, idx int, prettyKeyPrefix string,
12361240
) (prettyKey string, prettyValue string, err error) {
12371241
prettyKey = prettyKeyPrefix
12381242

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.
12591246
return prettyKey, prettyValue, nil
12601247
}
12611248

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+
}
12641268
return prettyKey, prettyValue, nil
12651269
}
12661270

pkg/sql/colfetcher/cfetcher_setup.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"context"
1010
"sync"
1111

12-
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
1312
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
1413
"github.com/cockroachdb/cockroach/pkg/sql/catalog/fetchpb"
1514
"github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc"
@@ -22,9 +21,6 @@ import (
2221
// caller) are included in the internal state.
2322
type cFetcherTableArgs struct {
2423
spec fetchpb.IndexFetchSpec
25-
// ColIdxMap is a mapping from ColumnID to the ordinal of the corresponding
26-
// column within spec.FetchedColumns.
27-
ColIdxMap catalog.TableColMap
2824
// typs are the types from spec.FetchedColumns.
2925
typs []*types.T
3026
}
@@ -98,9 +94,6 @@ func populateTableArgs(
9894
}
9995
}
10096
args.populateTypes(args.spec.FetchedColumns)
101-
for i := range args.spec.FetchedColumns {
102-
args.ColIdxMap.Set(args.spec.FetchedColumns[i].ColumnID, i)
103-
}
10497

10598
return args, nil
10699
}

0 commit comments

Comments
 (0)