@@ -1124,14 +1124,8 @@ func encodeTrigramInvertedIndexTableKeys(
11241124 return outKeys , nil
11251125}
11261126
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.
11351129func EncodePrimaryIndex (
11361130 codec keys.SQLCodec ,
11371131 tableDesc catalog.TableDescriptor ,
@@ -1141,6 +1135,25 @@ func EncodePrimaryIndex(
11411135 includeEmpty bool ,
11421136) ([]IndexEntry , error ) {
11431137 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 ) {
11441157 indexKey , containsNull , err := EncodeIndexKey (tableDesc , index , colMap , values , keyPrefix )
11451158 if err != nil {
11461159 return nil , err
@@ -1283,9 +1296,8 @@ func MakeNullPKError(
12831296 return errors .AssertionFailedf ("NULL value in unknown key column" )
12841297}
12851298
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.
12891301func EncodeSecondaryIndexKey (
12901302 ctx context.Context ,
12911303 codec keys.SQLCodec ,
@@ -1294,29 +1306,40 @@ func EncodeSecondaryIndexKey(
12941306 colMap catalog.TableColMap ,
12951307 values []tree.Datum ,
12961308) ([][]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+ }
12981313
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 ) {
12991326 var containsNull = false
13001327 var secondaryKeys [][]byte
13011328 var err error
13021329 if secondaryIndex .GetType () == descpb .IndexDescriptor_INVERTED {
1303- secondaryKeys , err = EncodeInvertedIndexKeys (ctx , secondaryIndex , colMap , values , secondaryIndexKeyPrefix )
1330+ secondaryKeys , err = EncodeInvertedIndexKeys (ctx , secondaryIndex , colMap , values , keyPrefix )
13041331 } else {
13051332 var secondaryIndexKey []byte
13061333 secondaryIndexKey , containsNull , err = EncodeIndexKey (
1307- tableDesc , secondaryIndex , colMap , values , secondaryIndexKeyPrefix )
1334+ tableDesc , secondaryIndex , colMap , values , keyPrefix )
13081335
13091336 secondaryKeys = [][]byte {secondaryIndexKey }
13101337 }
13111338 return secondaryKeys , containsNull , err
13121339}
13131340
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.
13201343func EncodeSecondaryIndex (
13211344 ctx context.Context ,
13221345 codec keys.SQLCodec ,
@@ -1325,13 +1348,36 @@ func EncodeSecondaryIndex(
13251348 colMap catalog.TableColMap ,
13261349 values []tree.Datum ,
13271350 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 ,
13281372) ([]IndexEntry , error ) {
13291373 // Use the primary key encoding for covering indexes.
13301374 if secondaryIndex .GetEncodingType () == catenumpb .PrimaryIndexEncoding {
1331- return EncodePrimaryIndex (codec , tableDesc , secondaryIndex , colMap , values , includeEmpty )
1375+ return EncodePrimaryIndexWithKeyPrefix (tableDesc , secondaryIndex , keyPrefix , colMap , values ,
1376+ includeEmpty )
13321377 }
13331378
1334- secondaryKeys , containsNull , err := EncodeSecondaryIndexKey (ctx , codec , tableDesc , secondaryIndex , colMap , values )
1379+ secondaryKeys , containsNull , err := EncodeSecondaryIndexKeyWithKeyPrefix (ctx , tableDesc ,
1380+ secondaryIndex , keyPrefix , colMap , values )
13351381 if err != nil {
13361382 return []IndexEntry {}, err
13371383 }
@@ -1592,14 +1638,18 @@ func writeColumnValues(
15921638}
15931639
15941640// 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.
15981647func EncodeSecondaryIndexes (
15991648 ctx context.Context ,
16001649 codec keys.SQLCodec ,
16011650 tableDesc catalog.TableDescriptor ,
16021651 indexes []catalog.Index ,
1652+ keyPrefixes [][]byte ,
16031653 colMap catalog.TableColMap ,
16041654 values []tree.Datum ,
16051655 secondaryIndexEntries []IndexEntry ,
@@ -1612,8 +1662,16 @@ func EncodeSecondaryIndexes(
16121662 }
16131663 const sizeOfIndexEntry = int64 (unsafe .Sizeof (IndexEntry {}))
16141664
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 )
16171675 if err != nil {
16181676 return secondaryIndexEntries , 0 , err
16191677 }
0 commit comments