@@ -1124,14 +1124,8 @@ func encodeTrigramInvertedIndexTableKeys(
1124
1124
return outKeys , nil
1125
1125
}
1126
1126
1127
- // EncodePrimaryIndex constructs a list of k/v pairs for a
1128
- // row encoded as a primary index. This function mirrors the encoding
1129
- // logic in prepareInsertOrUpdateBatch in pkg/sql/row/writer.go.
1130
- // It is somewhat duplicated here due to the different arguments
1131
- // that prepareOrInsertUpdateBatch needs and uses to generate
1132
- // the k/v's for the row it inserts. includeEmpty controls
1133
- // whether or not k/v's with empty values should be returned.
1134
- // It returns indexEntries in family sorted order.
1127
+ // EncodePrimaryIndex constructs the key prefix for the primary index and
1128
+ // delegates the rest of the encoding to EncodePrimaryIndexWithKeyPrefix.
1135
1129
func EncodePrimaryIndex (
1136
1130
codec keys.SQLCodec ,
1137
1131
tableDesc catalog.TableDescriptor ,
@@ -1141,6 +1135,25 @@ func EncodePrimaryIndex(
1141
1135
includeEmpty bool ,
1142
1136
) ([]IndexEntry , error ) {
1143
1137
keyPrefix := MakeIndexKeyPrefix (codec , tableDesc .GetID (), index .GetID ())
1138
+ return EncodePrimaryIndexWithKeyPrefix (tableDesc , index , keyPrefix , colMap , values , includeEmpty )
1139
+ }
1140
+
1141
+ // EncodePrimaryIndexWithKeyPrefix constructs a list of k/v pairs for a
1142
+ // row encoded as a primary index, using the provided key prefix specific to
1143
+ // that index. This function mirrors the encoding logic in
1144
+ // prepareInsertOrUpdateBatch in pkg/sql/row/writer.go. It is somewhat
1145
+ // duplicated here due to the different arguments that
1146
+ // prepareOrInsertUpdateBatch needs and uses to generate the k/v's for the row
1147
+ // it inserts. includeEmpty controls whether or not k/v's with empty values
1148
+ // should be returned. It returns indexEntries in family sorted order.
1149
+ func EncodePrimaryIndexWithKeyPrefix (
1150
+ tableDesc catalog.TableDescriptor ,
1151
+ index catalog.Index ,
1152
+ keyPrefix []byte ,
1153
+ colMap catalog.TableColMap ,
1154
+ values []tree.Datum ,
1155
+ includeEmpty bool ,
1156
+ ) ([]IndexEntry , error ) {
1144
1157
indexKey , containsNull , err := EncodeIndexKey (tableDesc , index , colMap , values , keyPrefix )
1145
1158
if err != nil {
1146
1159
return nil , err
@@ -1283,9 +1296,8 @@ func MakeNullPKError(
1283
1296
return errors .AssertionFailedf ("NULL value in unknown key column" )
1284
1297
}
1285
1298
1286
- // EncodeSecondaryIndexKey encodes the key for a secondary index. The 'colMap'
1287
- // maps descpb.ColumnIDs to positions in 'values'. This function returns a slice
1288
- // of byte arrays representing the key values.
1299
+ // EncodeSecondaryIndexKey constructs the key prefix for the secondary index and
1300
+ // delegates the rest of the encoding to EncodeSecondaryIndexWithKeyPrefix.
1289
1301
func EncodeSecondaryIndexKey (
1290
1302
ctx context.Context ,
1291
1303
codec keys.SQLCodec ,
@@ -1294,29 +1306,40 @@ func EncodeSecondaryIndexKey(
1294
1306
colMap catalog.TableColMap ,
1295
1307
values []tree.Datum ,
1296
1308
) ([][]byte , bool , error ) {
1297
- secondaryIndexKeyPrefix := MakeIndexKeyPrefix (codec , tableDesc .GetID (), secondaryIndex .GetID ())
1309
+ keyPrefix := MakeIndexKeyPrefix (codec , tableDesc .GetID (), secondaryIndex .GetID ())
1310
+ return EncodeSecondaryIndexKeyWithKeyPrefix (ctx , tableDesc , secondaryIndex , keyPrefix , colMap ,
1311
+ values )
1312
+ }
1298
1313
1314
+ // EncodeSecondaryIndexKeyWithKeyPrefix generates a slice of byte arrays
1315
+ // representing encoded key values for the given secondary index, using the
1316
+ // provided key prefix specific to that index. The colMap maps descpb.ColumnIDs
1317
+ // to positions in the values slice.
1318
+ func EncodeSecondaryIndexKeyWithKeyPrefix (
1319
+ ctx context.Context ,
1320
+ tableDesc catalog.TableDescriptor ,
1321
+ secondaryIndex catalog.Index ,
1322
+ keyPrefix []byte ,
1323
+ colMap catalog.TableColMap ,
1324
+ values []tree.Datum ,
1325
+ ) ([][]byte , bool , error ) {
1299
1326
var containsNull = false
1300
1327
var secondaryKeys [][]byte
1301
1328
var err error
1302
1329
if secondaryIndex .GetType () == descpb .IndexDescriptor_INVERTED {
1303
- secondaryKeys , err = EncodeInvertedIndexKeys (ctx , secondaryIndex , colMap , values , secondaryIndexKeyPrefix )
1330
+ secondaryKeys , err = EncodeInvertedIndexKeys (ctx , secondaryIndex , colMap , values , keyPrefix )
1304
1331
} else {
1305
1332
var secondaryIndexKey []byte
1306
1333
secondaryIndexKey , containsNull , err = EncodeIndexKey (
1307
- tableDesc , secondaryIndex , colMap , values , secondaryIndexKeyPrefix )
1334
+ tableDesc , secondaryIndex , colMap , values , keyPrefix )
1308
1335
1309
1336
secondaryKeys = [][]byte {secondaryIndexKey }
1310
1337
}
1311
1338
return secondaryKeys , containsNull , err
1312
1339
}
1313
1340
1314
- // EncodeSecondaryIndex encodes key/values for a secondary
1315
- // index. colMap maps descpb.ColumnIDs to indices in `values`. This returns a
1316
- // slice of IndexEntry. includeEmpty controls whether or not
1317
- // EncodeSecondaryIndex should return k/v's that contain
1318
- // empty values. For forward indexes the returned list of
1319
- // index entries is in family sorted order.
1341
+ // EncodeSecondaryIndex constructs the key prefix for the secondary index and
1342
+ // delegates the rest of the encoding to EncodeSecondaryIndexWithKeyPrefix.
1320
1343
func EncodeSecondaryIndex (
1321
1344
ctx context.Context ,
1322
1345
codec keys.SQLCodec ,
@@ -1325,13 +1348,36 @@ func EncodeSecondaryIndex(
1325
1348
colMap catalog.TableColMap ,
1326
1349
values []tree.Datum ,
1327
1350
includeEmpty bool ,
1351
+ ) ([]IndexEntry , error ) {
1352
+ keyPrefix := MakeIndexKeyPrefix (codec , tableDesc .GetID (), secondaryIndex .GetID ())
1353
+ return EncodeSecondaryIndexWithKeyPrefix (ctx , tableDesc , secondaryIndex , keyPrefix , colMap ,
1354
+ values , includeEmpty )
1355
+ }
1356
+
1357
+ // EncodeSecondaryIndexWithKeyPrefix generates a slice of IndexEntry objects
1358
+ // representing encoded key/value pairs for the given secondary index, using the
1359
+ // provided key prefix specific to that index. This encoding is performed in
1360
+ // EncodeSecondaryIndexKeyWithKeyPrefix for secondary indexes. The colMap maps
1361
+ // descpb.ColumnIDs to positions in the values slice. The 'includeEmpty'
1362
+ // parameter determines whether entries with empty values should be included.
1363
+ // For forward indexes, the resulting entries are sorted by column family order.
1364
+ func EncodeSecondaryIndexWithKeyPrefix (
1365
+ ctx context.Context ,
1366
+ tableDesc catalog.TableDescriptor ,
1367
+ secondaryIndex catalog.Index ,
1368
+ keyPrefix []byte ,
1369
+ colMap catalog.TableColMap ,
1370
+ values []tree.Datum ,
1371
+ includeEmpty bool ,
1328
1372
) ([]IndexEntry , error ) {
1329
1373
// Use the primary key encoding for covering indexes.
1330
1374
if secondaryIndex .GetEncodingType () == catenumpb .PrimaryIndexEncoding {
1331
- return EncodePrimaryIndex (codec , tableDesc , secondaryIndex , colMap , values , includeEmpty )
1375
+ return EncodePrimaryIndexWithKeyPrefix (tableDesc , secondaryIndex , keyPrefix , colMap , values ,
1376
+ includeEmpty )
1332
1377
}
1333
1378
1334
- secondaryKeys , containsNull , err := EncodeSecondaryIndexKey (ctx , codec , tableDesc , secondaryIndex , colMap , values )
1379
+ secondaryKeys , containsNull , err := EncodeSecondaryIndexKeyWithKeyPrefix (ctx , tableDesc ,
1380
+ secondaryIndex , keyPrefix , colMap , values )
1335
1381
if err != nil {
1336
1382
return []IndexEntry {}, err
1337
1383
}
@@ -1592,14 +1638,18 @@ func writeColumnValues(
1592
1638
}
1593
1639
1594
1640
// EncodeSecondaryIndexes encodes key/values for the secondary indexes. colMap
1595
- // maps descpb.ColumnIDs to indices in `values`. secondaryIndexEntries is the return
1596
- // value (passed as a parameter so the caller can reuse between rows) and is
1597
- // expected to be the same length as indexes.
1641
+ // maps descpb.ColumnIDs to indices in `values`. keyPrefixes is a slice that
1642
+ // associates indexes to their key prefix; the caller can reuse this between
1643
+ // rows to save work from creating key prefixes. the indexes and keyPrefixes
1644
+ // slice should have the same ordering. secondaryIndexEntries is the return
1645
+ // value (passed as a parameter so the caller can reuse between rows) and
1646
+ // is expected to be the same length as indexes.
1598
1647
func EncodeSecondaryIndexes (
1599
1648
ctx context.Context ,
1600
1649
codec keys.SQLCodec ,
1601
1650
tableDesc catalog.TableDescriptor ,
1602
1651
indexes []catalog.Index ,
1652
+ keyPrefixes [][]byte ,
1603
1653
colMap catalog.TableColMap ,
1604
1654
values []tree.Datum ,
1605
1655
secondaryIndexEntries []IndexEntry ,
@@ -1612,8 +1662,16 @@ func EncodeSecondaryIndexes(
1612
1662
}
1613
1663
const sizeOfIndexEntry = int64 (unsafe .Sizeof (IndexEntry {}))
1614
1664
1615
- for i := range indexes {
1616
- entries , err := EncodeSecondaryIndex (ctx , codec , tableDesc , indexes [i ], colMap , values , includeEmpty )
1665
+ for i , idx := range indexes {
1666
+ keyPrefix := keyPrefixes [i ]
1667
+ // TODO(annie): For now, we recompute the key prefix of inverted indexes. This is because index
1668
+ // keys with multiple associated values somehow get encoded into the same kv pair when using
1669
+ // our precomputed key prefix. `inverted_index/arrays` (logictest) illustrates this issue.
1670
+ if idx .GetType () == descpb .IndexDescriptor_INVERTED {
1671
+ keyPrefix = MakeIndexKeyPrefix (codec , tableDesc .GetID (), idx .GetID ())
1672
+ }
1673
+ entries , err := EncodeSecondaryIndexWithKeyPrefix (ctx , tableDesc , idx , keyPrefix , colMap , values ,
1674
+ includeEmpty )
1617
1675
if err != nil {
1618
1676
return secondaryIndexEntries , 0 , err
1619
1677
}
0 commit comments