Skip to content

Commit c40c636

Browse files
committed
vecstore: search all PKs for get full vector index
When we're changing primary keys, it's possible that the PrimaryKey() isn't actually keyed by the keys embedded as suffixes in the vector index. Since we need these indexes to be able to execute vecstore.GetFullVectors(), build the GetFullVectorsFetchSpec using the first primary key encoded index that has the keys that we have embedded in our key. This change addresses a bug discovered during construction of the schema changer DML injection tests. Release note: None
1 parent d3cb135 commit c40c636

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

pkg/sql/vecindex/vecstore/store_txn.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -353,20 +353,38 @@ func InitGetFullVectorsFetchSpec(
353353
spec.FamilyIDs = splitter.FamilyIDs()
354354

355355
// We need to decode the columns for the PK from the vector index. If the
356-
// vector index is being mutated, there's a chance the primary key is changing.
357-
// The vector index needs to point into the NEW primary key, so find that here.
356+
// vector index is being mutated or the primary key is being changed, this
357+
// can get a bit complicated. We need to find the PK for which the vector
358+
// index was encoded, so that the vector index has the PK's key columns In most
359+
// cases, this will just be the primary key, which is the first index returned
360+
// by AllIndexes().
358361
var primaryKey catalog.Index
359-
primaryKey = tableDesc.GetPrimaryIndex()
360-
if indexDesc.IsMutation() {
361-
for _, idx := range tableDesc.NonPrimaryIndexes() {
362-
if idx.GetEncodingType() == catenumpb.PrimaryIndexEncoding && !idx.IsTemporaryIndexForBackfill() {
363-
primaryKey = idx
364-
break
365-
}
362+
haveCols := indexDesc.CollectKeyColumnIDs()
363+
haveCols.UnionWith(indexDesc.CollectKeySuffixColumnIDs())
364+
for _, idx := range tableDesc.AllIndexes() {
365+
if idx.GetEncodingType() != catenumpb.PrimaryIndexEncoding {
366+
continue
367+
}
368+
369+
if idx.IsTemporaryIndexForBackfill() {
370+
continue
371+
}
372+
373+
if !idx.CollectKeyColumnIDs().SubsetOf(haveCols) {
374+
continue
366375
}
376+
377+
primaryKey = idx
378+
break
367379
}
380+
if primaryKey == nil {
381+
return errors.AssertionFailedf("No primary key of '%s' has column '%s' and is keyed by %v",
382+
tableDesc.GetName(), vectorCol.GetName(), haveCols)
383+
}
384+
368385
keyCols := primaryKey.CollectKeyColumnIDs().Ordered()
369-
return rowenc.InitIndexFetchSpec(&spec.ExtractPKFetchSpec, evalCtx.Codec, tableDesc, indexDesc, keyCols)
386+
return rowenc.InitIndexFetchSpec(&spec.ExtractPKFetchSpec, evalCtx.Codec, tableDesc,
387+
indexDesc, keyCols)
370388
}
371389

372390
// PKDecoder is used to extract the primary key from a vector index.

0 commit comments

Comments
 (0)