Skip to content

Fix wrong top-k for dotProduct vector search without rescoring - #126128

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-dotproduct-vector-search-no-rescoring
Oct 21, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-dotproduct-vector-search-no-rescoring

Conversation

@groeneai

@groeneai groeneai commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #124229

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix vector search queries on a vector_similarity index with the dotProduct distance function when vector_search_with_rescoring is disabled (the default): the query returned the least similar candidates in reverse order and reported dotProduct as 1 - 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 3 on 20 rows (number, [0, number + 1]) split into two parts returns 7 -7 / 8 -8 / 9 -9 instead of 19 20 / 18 19 / 17 18.

Root cause: USearch computes the ip_k distance as 1 - dotProduct. Without rescoring, optimizeVectorSearchWithVectorIndexSecondPass replaces dotProduct(vec, ref) by the _distance virtual column and converts the index value back only for L2Distance (sqrt, #105495). For dotProduct the raw 1 - dot became the function value, so the DESC sort ranked by ascending dot product.

Change: in the same place, dotProduct is mapped to 1. - _distance (then the existing CAST to the function's result type). cosineDistance needs no conversion, since the USearch cos distance is 1 - cos.

Validation: new test 05401_vector_search_dot_product_no_rescoring fails on master and passes with the fix (50/50 randomized runs); the 02354_vector_search_* tests give the same results as on master.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/126128]

Version info

  • Merged into: 26.11.1.470-master (included in 26.11 and later)

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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
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).

# Sev Finding Verdict Evidence / action
- - No findings - -

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-slot-9:20261015-202900

@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6632d2c]

Summary: ✅


AI Review

Summary

PR #126128 fixes incorrect ranking and projected values for indexed dotProduct queries without rescoring by converting USearch's distance back to similarity. The regression test covers multiple parts and both Float32 and Float64 reference vectors. No actionable findings in commit 6632d2ca39a77470bc75f72990dc9e3540714494.

Missing context / blind spots
  • ⚠️ Independent runtime validation remains incomplete: no local binary/build was available, fast test skipped 05401_vector_search_dot_product_no_rescoring, and bugfix validation jobs were dropped after an earlier configuration failure. The author reports that the test fails before the patch and passes afterward, including 50 randomized runs. Re-running the USearch-enabled stateless and bugfix validation jobs would close this gap. CI report.
Final Verdict

Status: ✅ Approve. No code changes requested; complete the outstanding CI validation before merging.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 15, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. clickhouse local --queries-file 05401_vector_search_dot_product_no_rescoring.sql on master prints 7 -7 / 8 -8 / 9 -9 every time. Also wrong on official 26.8.18.2, 26.6.8.7, 26.5.7.64.
b Root cause explained? USearch ip_k distance is 1 - dotProduct. Without rescoring, the second-pass rewrite aliases dotProduct(vec, ref) to the raw _distance and converts only L2Distance (sqrt), so the kept DESC sort ranks by ascending dot product and projects 1 - dot.
c Fix matches root cause? Yes. The value is converted (1. - _distance) at the one place that maps _distance to the function value, next to the existing sqrt.
d Test intent preserved / new tests added? New test: two parts, Float64 and Float32 references, plus an EXPLAIN check that _distance is used (so it cannot pass through the exact path).
e Both directions demonstrated? Fails on master, passes with the fix, via clickhouse local and via clickhouse-test; 50/50 randomized runs pass.
f Fix is general across code paths? The second-pass rewrite is the only _distance to function-value mapping. L2Distance is unchanged; cosineDistance needs none (USearch cos distance is 1 - cos); the quantized-codes path does not accept dotProduct.
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked f64/f32/f16/bf16 quantization, Array(Float32)/Array(Float64) columns and references, one and two parts, a negative reference: all match the exact result. With i8 the ranking now follows the index's own quantized similarity, which is the same approximation L2Distance has with i8.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; only results of dotProduct indexes at vector_search_with_rescoring = 0 change, from wrong to correct.
i Invariants and contracts preserved? The rewritten output keeps its position and the original result type (existing CAST); _distance keeps one meaning; the added constant is not an output, so headers do not change.

Session id: cron:clickhouse-impl-slot-7:20261015-200700

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @rschu1ze @shankar-iyer, could you review this, and add the can be tested label so CI runs (I cannot add labels right now)? Without rescoring, the dotProduct value was the raw USearch distance 1 - dotProduct, so ORDER BY dotProduct(...) DESC LIMIT k returned the least similar candidates; the fix converts _distance back with 1 - _distance, next to the existing L2Distance sqrt conversion.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 15, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-vector-index Vector similarity index in MergeTree: ANN/HNSW index build/query, usearch integration, vector ind... label Oct 15, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good.

@alexey-milovidov alexey-milovidov self-assigned this Oct 21, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 21, 2026
Merged via the queue into ClickHouse:master with commit 59ec4c6 Oct 21, 2026
352 of 354 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 21, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 3, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 3, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 3, 2026
Backport #126128 to 26.10: Fix wrong top-k for `dotProduct` vector search without rescoring
leshikus pushed a commit that referenced this pull request Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-vector-index Vector similarity index in MergeTree: ANN/HNSW index build/query, usearch integration, vector ind... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dotProduct no-rescoring rewrite sorts USearch 1 - dot DESC: inverted top-k

5 participants