Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle empty vector in decode_vector #29

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions tests/peewee/test_peewee.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def teardown_class(self):
def setup_method(self):
Item1Model.truncate_table()

def test_empty_vector(self):
Item1Model.create(embedding=[])
assert Item1Model.select().count() == 1
item1 = Item1Model.get()
assert np.array_equal(item1.embedding, np.array([]))
assert item1.embedding.dtype == np.float32

def test_insert_get_record(self):
Item1Model.create(embedding=[1, 2, 3])
assert Item1Model.select().count() == 1
Expand Down
10 changes: 10 additions & 0 deletions tests/sqlalchemy/test_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ def test_insert_get_record(self):
assert np.array_equal(item1.embedding, np.array([1, 2, 3]))
assert item1.embedding.dtype == np.float32

def test_empty_vector(self):
with Session() as session:
item1 = Item1Model(embedding=[])
session.add(item1)
session.commit()
assert session.query(Item1Model).count() == 1
item1 = session.query(Item1Model).first()
assert np.array_equal(item1.embedding, np.array([]))
assert item1.embedding.dtype == np.float32

def test_get_with_different_dimensions(self):
with Session() as session:
item1 = Item1Model(embedding=[1, 2, 3])
Expand Down
3 changes: 3 additions & 0 deletions tidb_vector/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ def decode_vector(value):
if isinstance(value, bytes):
value = value.decode("utf-8")

if value == "[]":
return np.array([], dtype=np.float32)

return np.array(value[1:-1].split(","), dtype=np.float32)
Loading