Repository navigation
Fix halfvec table storage insert: wrap arrays in pgvector's HalfVector - #911
Conversation
pgvector-python's HalfVectorDumper has no special case for a raw numpy.ndarray (unlike VectorDumper, which does), so a bare np.float16(...) array passed into a column typed halfvec during a binary COPY fails with "'numpy.ndarray' object has no attribute 'to_binary'". Wrap it in pgvector.HalfVector(...) first, same fix already applied to pgsvs.py's identical code (PR zilliztech#910). Add a regression test (test_insert_and_search_halfvec_table) mirroring the existing test_insert_and_search, with table_quantization_type set to halfvec. Verified against pgvector/pgvector:pg17 in Docker: fails with the exact error above on the pre-fix code, passes after. Signed-off-by: Olasoji <olasoji.denloye@intel.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: asonje The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
|
||
| import numpy as np | ||
| import psycopg | ||
| from pgvector import HalfVector |
There was a problem hiding this comment.
Thanks for tracking down the halfvec insert failure. Could you change pgvector to pgvector>=0.4.0 in install/requirements_py3.11.txt and use the same minimum in the pgvector and test extras in pyproject.toml? I reproduced an import failure with 0.3.6 even for ordinary vector tables; 0.4.0 exports HalfVector at the package root.
We propose making VDBBench’s dependency requirements explicit one backend at a time, as changes establish which SDK versions are needed. This PR is a good first step: just declare the pgvector minimum here; no need to update unrelated dependencies.
HalfVector moved to pgvector/__init__.py in 0.4.0; versions before that only expose it under submodules like pgvector.utils.halfvec, so `from pgvector import HalfVector` fails on anything older. Pin it in install/requirements_py3.11.txt and the pgvector/test extras, the only places that import HalfVector. Signed-off-by: Olasoji <olasoji.denloye@intel.com>
Summary
Fix an insert failure when table_quantization_type=halfvec is used with the pgvector client. This has been broken since
table_quantization_type was introduced (#427) and has no test coverage, so it has gone unnoticed.
Root Cause
pgvector-python's HalfVectorDumper has no special case for a raw numpy.ndarray (unlike VectorDumper, which does accept one
directly). insert_embeddings's halfvec branch passes a bare np.float16(embeddings_arr[i]) into a column typed halfvec during
a binary COPY, which psycopg can't serialize:
AttributeError: 'numpy.ndarray' object has no attribute 'to_binary'
Fix
Wrap the array in pgvector.HalfVector(...) before handing it to copy.write_row, the type HalfVectorDumper actually knows how
to serialize. Same fix already applied to the pgsvs client's identical code path in #910.
Testing
Added test_insert_and_search_halfvec_table to tests/test_pgvector.py, mirroring the existing test_insert_and_search with
table_quantization_type="halfvec". Verified against pgvector/pgvector:pg17 in Docker: