Skip to content

Fix vector search without rescoring returning rows not selected by the index - #126135

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

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

Conversation

@groeneai

@groeneai groeneai commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #124230

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):

Fixed vector search with a vector_similarity index and without rescoring (the default) returning rows that the index did not select, with a _distance of 1000, when the selected rows made up most of a granule. When the true distances were above 1000, these rows displaced the real nearest neighbours. Closes #124230.

Description

On 26.9.19.9 (also 26.8.18.2 and 26.3.42.3), the reproducer from the issue returns a row the index did not select: SELECT id FROM tab ORDER BY L2Distance(vec, [0., 0.]) LIMIT 3 over 4 rows with distances 0, 2000, 3000, 4000 returns 0, 3, 1 instead of 0, 1, 2.

MergeTreeRangeReader turns the row offsets returned by the index into a row filter, and fills _distance for the selected rows (999999.99 for the others). executePrewhereActionsAndFilterColumns passed this filter to ReadResult::optimize with must_apply_filter = false, so when 60% or more of the block's rows pass, the filter was only kept in final_filter for a later step to apply. No later step re-checks it (the reader chain applies a deferred final_filter only after a PREWHERE step), so the other rows reached ORDER BY ... LIMIT.

The fix passes must_apply_filter = true, as the filter-only index reader already does for its exact filter. This takes the path that already runs when fewer than 60% of the rows are selected. The cost is one filter pass over the columns of such dense blocks; reads without vector search hints are unaffected. The same filter restricts the read with vector_search_with_rescoring = 1, so there dense blocks now return only the selected rows, as sparse blocks already did.

The new test fails on master (extra 3 1000 row, wrong order) and passes with the fix, 50 of 50 runs with randomized settings. The 86 existing vector search tests that run locally pass unchanged.


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

Version info

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

…e index

The vector search candidate filter was passed to ReadResult::optimize with
must_apply_filter = false. When 60% or more of a block's rows are candidates,
optimize only keeps such a filter in final_filter for a later step. Nothing
re-checks the candidates later (the reader chain applies a deferred
final_filter only after a PREWHERE step), so rows the index did not select
reached ORDER BY ... LIMIT with the default _distance of 999999.99, shown as
1000 after sqrt. With true distances above 1000 they displaced real nearest
neighbours.

Apply the filter in the reader, as the filter-only index reader already does
for its exact filter. With rescoring, the same filter now also restricts
dense blocks to the candidates, as it already did for sparse blocks.

Closes ClickHouse#124230

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; 2 findings) plus my own cold review (1 finding).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test does not pin query_plan_try_use_vector_search; a randomized compatibility < 25.1 would turn vector search off DISAGREE compatibility is randomized only in the Stress job (stress.py:550) and the AST fuzzer, and neither compares test output with references (the stress smoke check runs only 00001_select_1 and 00234_...). Stateless random settings do not include compatibility.
2 💡 The comment on use_vector_search_result_filter (RangesInDataPart.h:98) says a false flag means no row filtering, but the _distance path filters rows AGREE, not blocking The comment was already inaccurate before this PR (sparse blocks were always filtered); no behaviour impact.
3 💡 optimize_move_to_prewhere = 0 in the first query is redundant, PREWHERE is suppressed for vector search anyway AGREE, not blocking Kept: it pins the no-PREWHERE read the bug needs.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-10:20261015-210600

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The reproducer from #124230 run with clickhouse local prints the extra 3 1000 row and the order 0, 3, 1 on every run (master, 26.9.19.9, 26.8.18.2, 26.3.42.3).
b Root cause explained? Yes. The vector search candidate filter was passed to ReadResult::optimize with must_apply_filter = false. When 60% or more of the block's rows are candidates it is only kept in final_filter, and with no PREWHERE step nothing applies it later, so non-candidate rows keep the default _distance 999999.99 and reach ORDER BY ... LIMIT.
c Fix matches root cause? Yes. The filter is applied where it is created (must_apply_filter = true), the same as the filter-only index reader does for its exact filter. No symptom patch on the distance value.
d Test intent preserved / new tests added? Yes. New 05401_vector_search_no_rescoring_dense_candidates: a postfilter case, a rescoring case and a case with distances above 1000.
e Both directions demonstrated? Yes. Through tests/clickhouse-test on two servers from the same tree: FAIL without the fix (10 of 10 randomized runs), OK with it (50 of 50 randomized runs).
f Fix is general across code paths? Yes. The other optimize callers are the filter-only index reader (already applies its exact filter) and the PREWHERE step (re-checked by WHERE). The rescoring row filter goes through the same line and is fixed with it.
g Fix generalizes across inputs (params/datatypes/wrappers)? The defect depends on the share of candidate rows, not on data types. Checked with and without a WHERE filter, with rescoring, with lazy materialization and PREWHERE moving on and off, with randomized settings, and in Ordinary, Replicated and parallel-replicas modes.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change. Only rows the index did not select stop being returned; with rescoring, dense blocks now behave as sparse blocks already did.
i Invariants and contracts preserved? Yes. optimize keeps num_rows equal to the ones in final_filter and filters all columns together (including patch columns), the same path that already runs when fewer than 60% of rows are candidates. 86 existing vector search tests pass unchanged.

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

@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bf987fd]

Summary: ✅


AI Review

Summary

The PR makes MergeTreeRangeReader apply vector-search candidate filters even on dense blocks, preventing unselected rows with fabricated _distance values from displacing nearest neighbours. The new stateless test covers postfiltering, rescoring, and distances above 1000. No actionable findings.

Missing context / blind spots
  • ⚠️ Functional and bugfix-validation CI for bf987fde4bfb has not completed. No local ClickHouse binary or build is available; runtime evidence comes from the author's reported baseline failures and patched passes. Completed CI would independently confirm those results.
Final Verdict

Status: ✅ Approve.

@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

cc @shankar-iyer @Algunenano, could you review this, and add the can be tested label so CI runs (I cannot add labels right now)? Without rescoring, the vector search candidate filter went to ReadResult::optimize with must_apply_filter = false, so on dense blocks it was only kept in final_filter, which nothing applies without a PREWHERE step; the fix applies it where it is built, as the filter-only index reader already does.

@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 self-assigned this Oct 16, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 16, 2026
Merged via the queue into ClickHouse:master with commit 329715c Oct 16, 2026
351 of 353 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 16, 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 #126135 to 26.10: Fix vector search without rescoring returning rows not selected by the index
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.

No-rescoring vector search returns non-candidate rows with fake distance 1000

5 participants