fix(rig-postgres): filtered and thresholded searches generate valid SQL - #2377
Open
jackthepunished wants to merge 5 commits into
Open
fix(rig-postgres): filtered and thresholded searches generate valid SQL#2377jackthepunished wants to merge 5 commits into
jackthepunished wants to merge 5 commits into
Conversation
The threshold predicate was rendered as `distance > $N` inside the inner select, where `distance` is only a select-list alias, so Postgres rejected every thresholded search. It also compared a pgvector distance with `>`, which would have kept the least similar rows. Compare a per-operator similarity expression against the threshold instead, and separate the `WHERE` keyword from the first condition, which a single-condition filter used to abut.
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.
Fixes #2376
rig-postgresproduced SQL that Postgres rejects for anyVectorSearchRequestthat carries amemberfilter, a single-condition filter, or athreshold; and the threshold compared in the wrong direction. Found by auditing the vector-store crates againstrig-sqlite, which gets all of this right.PgSearchFilter::memberrendersid is in ($3, $4)— not a Postgres operatordistance > $Nin the innerWHERE, wheredistanceis only a select-list alias →column "distance" does not exist>on a pgvector distance whilethresholdis documented as a minimum similarity → would keep the worst rows"WHERE" + conditionwith no separator →WHEREprice >= $3for any single-condition filterGenerated SQL on
origin/main, as printed by the new tests when run there:and on this branch:
Fix
memberrendersIN.1 - (embedding <=> $1)for cosine and jaccard, the negated distance for inner product / L2 / L1 / hamming (pgvector's<#>is already the negative inner product). This is the approachrig-sqlitetakes. It is composed after the$renumbering pass so the$1inside the expression survives, and its bind value is appended after the filter's.WHEREis separated from its first condition.Non-breaking: returned scores are still raw pgvector distances in ascending order; only requests that previously errored change behavior.
Tests
origin/mainand pass here (threshold_is_a_minimum_similarity_on_the_repeated_expression,threshold_placeholder_follows_the_filter_placeholders,single_condition_filter_is_separated_from_where, plus the existingevery_parameterised_operator_uses_dollar_placeholders, whose assertion pinnedis in). They call the privatesearch_queryon a store built with a lazy pool andrig_core::test_utils::MockEmbeddingModel, so no database is needed.similarity_expression_inverts_each_distance_operatorcovers all six operators.Verified locally:
cargo fmt --all -- --check,cargo clippy -p rig-postgres --all-targets --all-features,cargo test -p rig-postgres.