Repository navigation
Fix vector search without rescoring returning rows not selected by the index - #126135
Conversation
…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]>
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).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261015-210600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261015-203100 |
|
Workflow [PR], commit [bf987fd] Summary: ✅
AI ReviewSummaryThe PR makes Missing context / blind spots
Final VerdictStatus: ✅ Approve. |
|
cc @shankar-iyer @Algunenano, could you review this, and add the |
329715c
Backport #126135 to 26.10: Fix vector search without rescoring returning rows not selected by the index
…ing rows not selected by the index
Closes: #124230
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed vector search with a
vector_similarityindex and without rescoring (the default) returning rows that the index did not select, with a_distanceof 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 3over 4 rows with distances 0, 2000, 3000, 4000 returns0, 3, 1instead of0, 1, 2.MergeTreeRangeReaderturns the row offsets returned by the index into a row filter, and fills_distancefor the selected rows (999999.99 for the others).executePrewhereActionsAndFilterColumnspassed this filter toReadResult::optimizewithmust_apply_filter = false, so when 60% or more of the block's rows pass, the filter was only kept infinal_filterfor a later step to apply. No later step re-checks it (the reader chain applies a deferredfinal_filteronly after a PREWHERE step), so the other rows reachedORDER 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 withvector_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 1000row, 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
26.11.1.26-master(included in26.11and later)