feat: add array_contains and array_overlaps operators to pgvector filters#3459
Open
abhay23-AI wants to merge 1 commit into
Open
feat: add array_contains and array_overlaps operators to pgvector filters#3459abhay23-AI wants to merge 1 commit into
abhay23-AI wants to merge 1 commit into
Conversation
Contributor
Coverage report (pgvector)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Contributor
Author
|
hi @julian-risch, gentle nudge on this one when you get a chance. it adds the array_contains and array_overlaps pgvector operators you invited me to reopen from the old #1352, rebased and green (the py 3.10 and 3.14 checks pass). fwiw the auto labeler tagged it type:documentation but it's a feature, in case that helps triage. happy to adjust anything. |
…ters Adds two metadata filter operators for querying JSONB array fields in the pgvector document store: - array_contains: the meta array must contain ALL of the given values (SQL `meta->'field' @> %s`, value wrapped as Jsonb) - array_overlaps: the meta array must share AT LEAST ONE element with the given list of strings (SQL `meta->'field' ?| %s`) Both access the meta value as JSONB via the `->` accessor (the scalar operators use `->>` text extraction, which is incompatible with JSONB array operators) through a new `_treat_meta_field_as_jsonb` helper. array_overlaps validates that values are strings and both raise FilterError on non-list values. Field names stay embedded via SQLLiteral so injection is not possible. Resolves deepset-ai#1351
54abba0 to
720f070
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds two metadata filter operators to the pgvector document store for querying JSONB array fields, as requested in #1351:
array_contains: the meta array must contain all of the given valuesarray_overlaps: the meta array must share at least one element with the given listHow
meta is stored as a JSONB column, so list metadata lands as a native JSON array. The existing scalar operators access meta with the
->>(text) accessor, which can't drive the JSONB array operators, so I added a small_treat_meta_field_as_jsonbhelper that uses the->(jsonb) accessor with no cast:array_containsgeneratesmeta->'field' @> %swith the value wrapped asJsonb(...).jsonb @> jsonbis true when the left array contains every element of the right one.array_overlapsgeneratesmeta->'field' ?| %s.jsonb ?| text[]is true when any of the given strings exist as top level array elements.Field names are still embedded via
SQLLiteral, so this stays injection safe.array_overlapsvalidates that the values are strings (the?|operator only matches string elements) and both operators raiseFilterErroron a non list value, matching the style of the existingin/not inoperators.Context
This picks up the work from the earlier #1352, which got closed for inactivity. @julian-risch said back then to feel free to reopen, and since the pgvector integration was refactored in #1547 to build SQL with psycopg
Composableobjects, I redid it fresh against the current code rather than reviving the stale branch. The old undefinedJsonbimport concern from that PR is moot now sinceJsonbis already imported in filters.py.Testing
pgvector/pgvector:pg17container (the same image CI uses).Full pgvector suite locally: 162 integration passed, all unit passed,
fmt-checkandmypyclean.One open question for reviewers: I kept
array_overlapsstring only with a hardFilterErrorfor non string values, since?|only matches strings and the issue's use case is tags. If you'd rather support numeric/object arrays too I can switch it to ajsonb_array_elementsbased approach, just let me know.Resolves #1351