Skip to content

Commit 3feae8d

Browse files
craig[bot]mw5h
craig[bot]
andcommitted
Merge #141399
141399: tabledesc: implement vector index retrieval in the catalog r=mw5h a=mw5h Add a VectorIndexes() method to the catalog's index cache. This patch also addes ForEachVectorIndex() and FindVectorIndex(), primarily so that we can hook up the index_test to ensure that VectorIndexes() is working properly. Epic: CRDB-42943 Co-authored-by: Matt White <[email protected]>
2 parents 34b4327 + 2f7a684 commit 3feae8d

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

pkg/sql/catalog/descriptor.go

+5
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,11 @@ type TableDescriptor interface {
461461
// produced by AllIndexes and removing indexes with empty expressions.
462462
PartialIndexes() []Index
463463

464+
// VectorIndexes returns a slice of all vector indexes in the underlying
465+
// proto, in their canonical order. This is equivalent to taking the slice
466+
// produced by AllIndexes and removing indexes which are not vector indexes.
467+
VectorIndexes() []Index
468+
464469
// PublicNonPrimaryIndexes returns a slice of all active secondary indexes,
465470
// in their canonical order. This is equivalent to the Indexes array in the
466471
// proto.

pkg/sql/catalog/table_elements.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,11 @@ func ForEachPartialIndex(desc TableDescriptor, f func(idx Index) error) error {
780780
return forEachIndex(desc.PartialIndexes(), f)
781781
}
782782

783+
// ForEachVectorIndex is like ForEachIndex over VectorIndexes().
784+
func ForEachVectorIndex(desc TableDescriptor, f func(idx Index) error) error {
785+
return forEachIndex(desc.VectorIndexes(), f)
786+
}
787+
783788
// ForEachNonPrimaryIndex is like ForEachIndex over
784789
// NonPrimaryIndexes().
785790
func ForEachNonPrimaryIndex(desc TableDescriptor, f func(idx Index) error) error {
@@ -846,12 +851,18 @@ func FindNonDropIndex(desc TableDescriptor, test func(idx Index) bool) Index {
846851
return findIndex(desc.NonDropIndexes(), test)
847852
}
848853

849-
// FindPartialIndex returns the first index in PartialIndex() for which test
854+
// FindPartialIndex returns the first index in PartialIndexes() for which test
850855
// returns true.
851856
func FindPartialIndex(desc TableDescriptor, test func(idx Index) bool) Index {
852857
return findIndex(desc.PartialIndexes(), test)
853858
}
854859

860+
// FindVectorIndex returns the first index in VectorIndexes() for which test
861+
// returns true.
862+
func FindVectorIndex(desc TableDescriptor, test func(idx Index) bool) Index {
863+
return findIndex(desc.VectorIndexes(), test)
864+
}
865+
855866
// FindPublicNonPrimaryIndex returns the first index in PublicNonPrimaryIndex()
856867
// for which test returns true.
857868
func FindPublicNonPrimaryIndex(desc TableDescriptor, test func(idx Index) bool) Index {

pkg/sql/catalog/tabledesc/index.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ type indexCache struct {
664664
deletableNonPrimary []catalog.Index
665665
deleteOnlyNonPrimary []catalog.Index
666666
partial []catalog.Index
667+
vector []catalog.Index
667668
}
668669

669670
// newIndexCache returns a fresh fully-populated indexCache struct for the
@@ -713,13 +714,13 @@ func newIndexCache(desc *descpb.TableDescriptor, mutations *mutationCache) *inde
713714
if !idx.Dropped() && (!idx.Primary() || desc.IsPhysicalTable()) {
714715
lazyAllocAppendIndex(&c.nonDrop, idx, len(c.all))
715716
}
716-
// TODO(ssd): We exclude backfilling indexes from
717-
// IsPartial() for the unprincipled reason of not
718-
// wanting to modify all of the code that assumes
719-
// these are always at least delete-only.
717+
// Include only deletable indexes.
720718
if idx.IsPartial() && !idx.Backfilling() {
721719
lazyAllocAppendIndex(&c.partial, idx, len(c.all))
722720
}
721+
if idx.GetType() == idxtype.VECTOR && !idx.Backfilling() {
722+
lazyAllocAppendIndex(&c.vector, idx, len(c.all))
723+
}
723724
}
724725
return &c
725726
}

pkg/sql/catalog/tabledesc/index_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ func TestIndexInterface(t *testing.T) {
180180
catalog.ForEachPartialIndex,
181181
catalog.FindPartialIndex,
182182
},
183+
{"VectorIndex",
184+
[]string{"s7"},
185+
catalog.TableDescriptor.VectorIndexes,
186+
catalog.ForEachVectorIndex,
187+
catalog.FindVectorIndex,
188+
},
183189
{
184190
"PublicNonPrimaryIndex",
185191
indexNames[1:],

pkg/sql/catalog/tabledesc/table_desc.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,24 @@ func (desc *wrapper) NonDropIndexes() []catalog.Index {
352352

353353
// PartialIndexes returns a slice of all partial indexes in the underlying
354354
// proto, in their canonical order. This is equivalent to taking the slice
355-
// produced by AllIndexes and filtering indexes with non-empty expressions.
355+
// produced by DeletableNonPrimaryIndexes and filtering indexes with non-empty
356+
// expressions.
357+
//
358+
// Backfilling indexes are excluded.
356359
func (desc *wrapper) PartialIndexes() []catalog.Index {
357360
return desc.getExistingOrNewIndexCache().partial
358361
}
359362

363+
// VectorIndexes returns a slice of all vector indexes in the underlying
364+
// proto, in their canonical order. This is equivalent to taking the slice
365+
// produced by DeletableNonPrimaryIndexes and filtering indexes that are not
366+
// vector indexes.
367+
//
368+
// Backfilling indexes are excluded.
369+
func (desc *wrapper) VectorIndexes() []catalog.Index {
370+
return desc.getExistingOrNewIndexCache().vector
371+
}
372+
360373
// NonPrimaryIndexes returns a slice of all non-primary indexes, in
361374
// their canonical order. This is equivalent to taking the slice
362375
// produced by AllIndexes and removing the primary index.

0 commit comments

Comments
 (0)