Repository navigation
Fix wrong top-k for dotProduct vector search without rescoring - #126128
alexey-milovidov merged 1 commit into
Conversation
USearch computes the `ip_k` distance (the metric behind a `dotProduct` vector similarity index) as `1 - dotProduct`, on both its autovectorized and its SimSIMD paths. With `vector_search_with_rescoring = 0` (the default), `optimizeVectorSearchWithVectorIndexSecondPass` replaces `dotProduct(vec, ref)` by the `_distance` virtual column, but converted the index value back to the function value only for `L2Distance` (`sqrt`). For `dotProduct` the raw `1 - dot` was returned as the function value and the `DESC` sort ranked by ascending dot product, so the query returned the least similar candidates of the per-part shortlists, in reverse order, and reported `1 - dot` as the result. Convert the value next to the `L2Distance` branch: `1. - _distance`, then the existing CAST to the function's result type. `cosineDistance` needs no conversion (the USearch `cos` distance is `1 - cos`). Closes: ClickHouse#124229 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings), plus my own cold review (0 blockers, 0 majors).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261015-202900 |
|
Workflow [PR], commit [6632d2c] Summary: ✅
AI ReviewSummaryPR #126128 fixes incorrect ranking and projected values for indexed Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested; complete the outstanding CI validation before merging. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261015-200700 |
|
cc @rschu1ze @shankar-iyer, could you review this, and add the |
59ec4c6
Backport #126128 to 26.10: Fix wrong top-k for `dotProduct` vector search without rescoring
Closes: #124229
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix vector search queries on a
vector_similarityindex with thedotProductdistance function whenvector_search_with_rescoringis disabled (the default): the query returned the least similar candidates in reverse order and reporteddotProductas1 - dotProduct. Closes #124229.Description
On 26.8.18.2 (also 26.6.8.7 and 26.5.7.64), with a
vector_similarity('hnsw', 'dotProduct', 2)index and default settings,SELECT id, dotProduct(vec, [0., 1.]) FROM tab ORDER BY dotProduct(vec, [0., 1.]) DESC LIMIT 3on 20 rows(number, [0, number + 1])split into two parts returns7 -7 / 8 -8 / 9 -9instead of19 20 / 18 19 / 17 18.Root cause: USearch computes the
ip_kdistance as1 - dotProduct. Without rescoring,optimizeVectorSearchWithVectorIndexSecondPassreplacesdotProduct(vec, ref)by the_distancevirtual column and converts the index value back only forL2Distance(sqrt, #105495). FordotProductthe raw1 - dotbecame the function value, so theDESCsort ranked by ascending dot product.Change: in the same place,
dotProductis mapped to1. - _distance(then the existing CAST to the function's result type).cosineDistanceneeds no conversion, since the USearchcosdistance is1 - cos.Validation: new test
05401_vector_search_dot_product_no_rescoringfails on master and passes with the fix (50/50 randomized runs); the02354_vector_search_*tests give the same results as on master.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/126128]
Version info
26.11.1.470-master(included in26.11and later)