Skip to content

Commit

Permalink
Merge pull request #91 from supabase/or/bugfix-lookup-indexes-by-table
Browse files Browse the repository at this point in the history
Scope index lookups to a table to remove singleton ix issue
  • Loading branch information
olirice authored Jul 31, 2024
2 parents 0dd987a + 7d7d431 commit 668249d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
32 changes: 32 additions & 0 deletions src/tests/test_issue_90.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import vecs


def test_issue_90_multiple_index_support(client: vecs.Client) -> None:
# Create a collection
col1 = client.get_or_create_collection(name="col1", dimension=3)

# Upsert some records
col1.upsert(
records=[
(
"vec0", # the vector's identifier
[0.1, 0.2, 0.3], # the vector. list or np.array
{"year": 1973}, # associated metadata
),
("vec1", [0.7, 0.8, 0.9], {"year": 2012}),
]
)

# Creat an index on the first collection
col1.create_index()

# Create a second collection
col2 = client.get_or_create_collection(name="col2", dimension=3)

# Create an index on the second collection
col2.create_index()

assert col1.index is not None
assert col2.index is not None

assert col1.index != col2.index
2 changes: 1 addition & 1 deletion src/vecs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
)

__project__ = "vecs"
__version__ = "0.4.3"
__version__ = "0.4.4"


__all__ = [
Expand Down
18 changes: 12 additions & 6 deletions src/vecs/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Importing from the `vecs.collection` directly is not supported.
All public classes, enums, and functions are re-exported by the top level `vecs` module.
"""

from __future__ import annotations

import math
Expand Down Expand Up @@ -638,17 +639,22 @@ def index(self) -> Optional[str]:
query = text(
"""
select
relname as table_name
pi.relname as index_name
from
pg_class pc
pg_class pi -- index info
join pg_index i -- extend index info
on pi.oid = i.indexrelid
join pg_class pt -- owning table info
on pt.oid = i.indrelid
where
pc.relnamespace = 'vecs'::regnamespace
and relname ilike 'ix_vector%'
and pc.relkind = 'i'
pi.relnamespace = 'vecs'::regnamespace
and pi.relname ilike 'ix_vector%'
and pi.relkind = 'i'
and pt.relname = :table_name
"""
)
with self.client.Session() as sess:
ix_name = sess.execute(query).scalar()
ix_name = sess.execute(query, {"table_name": self.name}).scalar()
self._index = ix_name
return self._index

Expand Down

0 comments on commit 668249d

Please sign in to comment.