Skip to content

fix(pgvector): cast numeric meta filters to numeric instead of integer/real - #4149

Merged
sjrl merged 3 commits into
deepset-ai:mainfrom
astroknot-sheep:fix/pgvector-numeric-meta-cast
Nov 13, 2026
Merged

sjrl merged 3 commits into
deepset-ai:mainfrom
astroknot-sheep:fix/pgvector-numeric-meta-cast

Conversation

@astroknot-sheep

Copy link
Copy Markdown
Contributor

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, int mapped to integer and float to real. Both now map to numeric. numeric is exact and accepts any JSON number, so:

  • float comparisons are no longer rounded to single precision (meta.n == 0.1 now matches),
  • an int filter value works on float metadata (no more InvalidTextRepresentation),
  • ints beyond 32 bits no longer raise NumericValueOutOfRange.

Bool and string handling is unchanged.

How did you test it?

  • Added three integration tests to TestFilters in tests/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 on main and pass with this change.
  • Updated the expected SQL in the existing unit tests (::integer / ::real become ::numeric).
  • hatch run test:all against PostgreSQL 17 + pgvector: 300 passed, 5 skipped. hatch run fmt-check and hatch run test:types are clean.
  • Manually compared about 30 filters (ranges, in/not in, nested AND/OR, missing keys, None) against InMemoryDocumentStore. The numeric cases now match.

Notes for the reviewer

The change is confined to the PYTHON_TYPES_TO_PG_TYPES map, and AlloyDBDocumentStore inherits it. I used AI assistance while investigating and drafting this PR. I reviewed every line and ran the tests myself.

Checklist

@astroknot-sheep
astroknot-sheep requested a review from a team as a code owner October 21, 2026 21:50
@astroknot-sheep
astroknot-sheep requested review from sjrl and removed request for a team October 21, 2026 21:50
@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report (pgvector)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/pgvector/src/haystack_integrations/document_stores/pgvector
  filters.py
Project Total  

This report was generated by python-coverage-comment-action

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report (supabase)

This PR does not seem to contain any modification to coverable code.

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report (alloydb)

This PR does not seem to contain any modification to coverable code.

@HaystackBot

Copy link
Copy Markdown
Contributor

Hi @astroknot-sheep, thanks a lot for your contribution! 🙏

We noticed that the Contributor License Agreement (CLA) check (license/cla) hasn't passed yet, so we've temporarily moved this PR to draft and paused the review assignment.

To get your PR reviewed, please sign the CLA via the link in the license/cla check below (or in the CLA bot comment). As soon as the check turns green, this PR will automatically be marked ready for review again and a reviewer will be re-assigned.

@HaystackBot
HaystackBot removed the request for review from sjrl October 21, 2026 22:51
@HaystackBot HaystackBot added the cla-pending PR is in draft until the contributor signs the CLA label Oct 21, 2026
@HaystackBot
HaystackBot marked this pull request as draft October 21, 2026 22:51
@HaystackBot
HaystackBot marked this pull request as ready for review October 21, 2026 23:21
@HaystackBot
HaystackBot requested a review from sjrl October 21, 2026 23:21
@HaystackBot HaystackBot removed the cla-pending PR is in draft until the contributor signs the CLA label Oct 21, 2026
@HaystackBot

Copy link
Copy Markdown
Contributor

Thanks for signing the CLA, @astroknot-sheep! 🎉 This PR is now ready for review again and the reviewer has been re-assigned.

@Harshil26202

Copy link
Copy Markdown
Contributor

Thanks for this fix. I applied it locally and ran about 1,000 random filters against InMemoryDocumentStore on a real pgvector container. The cast problems from #4148 are all gone, and I didn't see any wrong results.

One related case is still failing, so it might fit in this PR: in and not in with a list that mixes int and float. psycopg refuses to adapt the list before the query even reaches Postgres:

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 ::numeric, sending all-numeric lists as Decimal makes psycopg adapt them as numeric[], and it also stays exact for large ints:

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 value

Then _in and _not_in pass [_list_param(value)] instead of [value]. With that on top of your branch, [-2.5, -2] and [1.0, 2] match the in-memory results for both operators. Happy to open it as a follow-up instead if you'd rather keep this PR focused.

(Found with Claude Code while fuzzing document store filters.)

@astroknot-sheep

Copy link
Copy Markdown
Contributor Author

Thanks @sjrl! Updated the _treat_meta_field docstring example to ::numeric, and shortened the comment as you suggested.

@Harshil26202 thanks for catching the mixed int/float in/not in case. I reproduced the psycopg error and included the fix here: all-numeric lists are sent as Decimal so they adapt as numeric[]. Added a unit test for it.

@github-actions github-actions Bot added the type:documentation Improvements or additions to documentation label Nov 11, 2026


@pytest.mark.parametrize("operator", ["in", "not in"])
def test_in_mixed_int_float_list_is_sent_as_numeric(operator):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sjrl sjrl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sjrl
sjrl merged commit 0de9916 into deepset-ai:main Nov 13, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration:pgvector type:documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PgvectorDocumentStore: numeric meta filters are cast to integer/real, giving wrong results or errors

4 participants