Skip to content

fix(postgres): use value type instead of float() to detect numeric metadata filters - #1

Open
kumarpriyanshu09 wants to merge 3 commits into
mainfrom
fix/postgres-numeric-filter-type-15962
Open

kumarpriyanshu09 wants to merge 3 commits into
mainfrom
fix/postgres-numeric-filter-type-15962

Conversation

@kumarpriyanshu09

Copy link
Copy Markdown
Owner

Description

PGVectorStore._build_filter_clause decided whether a comparison filter was numeric by calling float(filter_.value). Python's float() accepts strings that Postgres' ::float cast rejects (for example "2024_123", which Python reads as 2024123.0). So a plain string filter such as MetadataFilter(key="file_name", value="2024_123") emitted (metadata_->>'file_name')::float = 2024123.0, and the query failed with DataError: invalid input syntax for type double precision. Strings like "nan" or "inf" also produced invalid SQL.

The check now uses the value's Python type, which MetadataFilter preserves through StrictInt/StrictFloat/StrictStr:

  • int/float values (excluding bool) keep the existing ::float comparison. The emitted SQL is unchanged.
  • str values are always compared as text. Pass an int/float for numeric comparisons.

Only the EQ/NE/GT/GTE/LT/LTE fallback branch changes. IN/NIN/ANY/ALL/CONTAINS/TEXT_MATCH/IS_EMPTY are untouched. This follows the same idea as the idle draft run-llama#22367 (thanks @pctablet505).

Fixes run-llama#15962

New Package?

  • Yes
  • No

Version Bump?

  • Yes (llama-index-vector-stores-postgres 0.9.0 → 0.9.1)
  • No

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • I added new unit tests to cover this change

DB-free unit tests in tests/test_vector_stores_postgres.py call _build_filter_clause on a store created with PGVectorStore.__new__:

  • Numeric-looking strings ("2024_123", "123", "0.5", "1e5", "nan", "inf") × all six comparison operators produce text comparisons with no ::float.
  • int/float values keep the exact ::float SQL.
  • A bool is never cast.
  • IN/NIN output is unchanged.

uv run -- pytest in the package: 125 passed, 90 skipped (DB tests skip without Postgres).

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I ran uv run make format; uv run make lint to appease the lint gods
Open in Web Open in Cursor 

…ama#15962)

Co-authored-by: Techboy bebop  <kumarpriyanshu09@users.noreply.github.com>
…lters (run-llama#15962)

Co-authored-by: Techboy bebop  <kumarpriyanshu09@users.noreply.github.com>
Co-authored-by: Techboy bebop  <kumarpriyanshu09@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Sep 29, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: c9a8cd06-ec06-43ed-80b1-b231c0f3c460


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: PGVectorStore incorrectly handles string metadata that resembles numbers

1 participant