Repository navigation
fix(pgvector): cast numeric meta filters to numeric instead of integer/real - #4149
Conversation
Coverage report (pgvector)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
|
Hi @astroknot-sheep, thanks a lot for your contribution! 🙏 We noticed that the Contributor License Agreement (CLA) check ( To get your PR reviewed, please sign the CLA via the link in the |
|
Thanks for signing the CLA, @astroknot-sheep! 🎉 This PR is now ready for review again and the reviewer has been re-assigned. |
|
Thanks for this fix. I applied it locally and ran about 1,000 random filters against One related case is still failing, so it might fit in this PR: store.filter_documents({"field": "meta.score", "operator": "in", "value": [-2.5, -2]})
# DocumentStoreError: ... DataError('cannot dump lists of mixed types; got: float, int')Since the column side is now from decimal import Decimal
def _list_param(value: list) -> list:
# psycopg can't adapt a list that mixes int and float, so send all-numeric lists as numeric[]
if value and all(isinstance(v, (int, float)) and not isinstance(v, bool) for v in value):
return [Decimal(str(v)) for v in value]
return valueThen (Found with Claude Code while fuzzing document store filters.) |
…ent, adapt mixed numeric in-lists
|
Thanks @sjrl! Updated the @Harshil26202 thanks for catching the mixed int/float |
|
|
||
|
|
||
| @pytest.mark.parametrize("operator", ["in", "not in"]) | ||
| def test_in_mixed_int_float_list_is_sent_as_numeric(operator): |
There was a problem hiding this comment.
Could we also add an integration test in TestFilters for in and not in with a mixed list like [1.5, 3], asserting the returned docs?
There was a problem hiding this comment.
Added test_in_and_not_in_with_mixed_int_float_list to TestFilters. It writes docs with 1.5, 3, 2 and one without number, then filters with [1.5, 3]: in returns the 1.5 and 3 docs, not in returns the 2 doc and the one without the field. It fails on main and passes with this change.
Related Issues
Proposed Changes:
Meta values are read from the JSONB column as text and cast according to the Python type of the filter value. Before this change,
intmapped tointegerandfloattoreal. Both now map tonumeric.numericis exact and accepts any JSON number, so:meta.n == 0.1now matches),InvalidTextRepresentation),NumericValueOutOfRange.Bool and string handling is unchanged.
How did you test it?
TestFiltersintests/test_filters.py, one per symptom in PgvectorDocumentStore: numeric meta filters are cast to integer/real, giving wrong results or errors #4148. All three fail onmainand pass with this change.::integer/::realbecome::numeric).hatch run test:allagainst PostgreSQL 17 + pgvector: 300 passed, 5 skipped.hatch run fmt-checkandhatch run test:typesare clean.in/not in, nested AND/OR, missing keys,None) againstInMemoryDocumentStore. The numeric cases now match.Notes for the reviewer
The change is confined to the
PYTHON_TYPES_TO_PG_TYPESmap, andAlloyDBDocumentStoreinherits it. I used AI assistance while investigating and drafting this PR. I reviewed every line and ran the tests myself.Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:.