Skip to content

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

Closed

Description

@astroknot-sheep

Describe the bug
PgvectorDocumentStore reads meta values as text (meta->>'key') and casts them according to the Python type of the filter value: int becomes ::integer and float becomes ::real. This leads to three divergences from InMemoryDocumentStore:

  1. Silently wrong float results. real is single precision, so (meta->>'n')::real = 0.1 is compared against a double-precision 0.1 and never matches. Range filters near a value, and large floats (16777217.0 > 16777216.0), are also wrong.
  2. Error when an int filter value meets float metadata. {"field": "meta.n", "operator": ">=", "value": 1} raises DocumentStoreError ... InvalidTextRepresentation as soon as any document stores e.g. 1.5 under that key.
  3. Ints beyond 32 bits raise NumericValueOutOfRange.

To Reproduce

from haystack import Document
from haystack_integrations.document_stores.pgvector import PgvectorDocumentStore

store = PgvectorDocumentStore(embedding_dimension=4, recreate_table=True)
store.write_documents([Document(content="a", meta={"n": 0.1}), Document(content="b", meta={"n": 1.5})])

store.filter_documents({"field": "meta.n", "operator": "==", "value": 0.1})  # [] - expected [a]
store.filter_documents({"field": "meta.n", "operator": ">=", "value": 1})    # DocumentStoreError - expected [b]

Expected behavior
The same results as InMemoryDocumentStore: [a] and [b].

Proposed fix
Cast both int and float to numeric. It is exact and accepts any JSON number. I have a small PR ready.

System: haystack-ai 3.4.1, pgvector-haystack main, PostgreSQL 17 + pgvector

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions