Repository navigation
Re-land exact row filtering for vector search rescoring queries - #108846
shankar-iyer merged 11 commits into
Conversation
…rescoring-fastpath Apply exact row-positioning for vector search rescoring queries
…-rescoring-row-filter
|
Workflow [PR], commit [dcce702] Summary: ✅
AI ReviewSummaryThis PR re-lands row-level candidate filtering for vector-search rescoring, updates the Findings❌ Blockers
Final Verdict❌ Changes required until the existing-setting behavior change has an explicit compatibility path. |
may add PK-overlapping ranges after vector index analysis via . The rescoring row filter only has row offsets for the original vector candidates, so applying it before the final merge can drop rows added by exact-mode recovery and let an older row survive. Keep mark-level vector index selection and exact-mode range recovery, but do not enable for rescoring queries with . Non- rescoring still uses the candidate-row filter. Add a regression case to where the reference query and vector-index query must both return the newer visible row after . Related: ClickHouse#108846 Tested locally: - env -u CFLAGS -u CXXFLAGS -u LDFLAGS -u CMAKE_C_FLAGS -u CMAKE_CXX_FLAGS -u CMAKE_EXE_LINKER_FLAGS -u CMAKE_SHARED_LINKER_FLAGS -u CMAKE_MODULE_LINKER_FLAGS ninja -C build_verify programs/clickhouse - tests/clickhouse-test -b build_verify/programs/clickhouse --queries tests/queries --no-stateful --no-random-settings --no-random-merge-tree-settings --no-zookeeper --no-shard --timeout 180 02354_vector_search_rescoring 02354_vector_search_rescoring_distance_in_select_list 02354_vector_search_rescoring_and_prewhere 02354_vector_search_incident1654 - build_verify/programs/clickhouse client --multiquery < tmp/pr108846_final_probe4.sql
|
The What changed:
Local verification:
|
The reland stores `use_vector_search_result_filter` in shared read hints. `prepared_index` reuses the main `data_part_info_for_read`, and `MergeTreeReadTask::createReaders` also builds non-main readers for index and PREWHERE stages. If those readers interpret the rescoring flag, they can build and apply the same row-offset filter before the final data reader. Gate the rescoring row filter on `main_reader` in `MergeTreeRangeReader::fillVirtualColumns` and `MergeTreeRangeReader::executePrewhereActionsAndFilterColumns`. Keep the existing `_distance` path unchanged so vector index reads that explicitly request `_distance` still populate and apply the virtual-column filter. Review thread: ClickHouse#108846 (comment) Local verification: - `ninja -C build_verify programs/clickhouse` - `tests/clickhouse-test -b build_verify/programs/clickhouse --queries tests/queries --no-stateful --no-random-settings --no-random-merge-tree-settings --no-zookeeper --no-shard --timeout 180 02354_vector_search_rescoring 02354_vector_search_rescoring_distance_in_select_list 02354_vector_search_rescoring_and_prewhere 02354_vector_search_lazy_materialization 02354_vector_search_incident1654`
Add a focused stateless test for the multi-reader topology from the review thread: vector-search rescoring with lazy materialization enabled and a selected mark that contains spillover rows beyond the top-k candidates. The test verifies that the plan uses `LazilyReadFromMergeTree`, that the vector similarity index is used, and that late materialization of an extra payload column returns the expected candidate rows under `vector_search_with_rescoring = 1`. Review thread: ClickHouse#108846 (comment) Local verification: - `tests/clickhouse-test -b build_verify/programs/clickhouse --queries tests/queries --no-stateful --no-random-settings --no-random-merge-tree-settings --no-zookeeper --no-shard --timeout 180 04489_vector_search_rescoring_lazy_materialization` - `tests/clickhouse-test -b build_verify/programs/clickhouse --queries tests/queries --no-stateful --no-random-settings --no-random-merge-tree-settings --no-zookeeper --no-shard --timeout 180 02354_vector_search_rescoring 02354_vector_search_rescoring_distance_in_select_list 02354_vector_search_rescoring_and_prewhere 02354_vector_search_lazy_materialization 02354_vector_search_incident1654 04489_vector_search_rescoring_lazy_materialization`
used as a proxy for vector second-pass execution. That proxy is too broad: rescoring queries can later skip the candidate-row filter, for example when PK pruning leaves a non-empty read range without vector-search row hints, so implicit was disabled without any vector-search replacement benefit. Make the suppression depend on the read-hint conditions that the second pass needs: local index analysis, non- rescoring reads, vector-search result hints for rescoring, and distance-bearing hints for the non-rescoring rewrite. Add to cover both sides: PK-pruned rescoring restores implicit , while an eligible rescoring query still suppresses it for the row-filter path. Review thread: ClickHouse#108846 (comment) Verification: - ninja -C build_verify programs/clickhouse - tests/clickhouse-test --binary build_verify/programs/clickhouse --testname 04490_vector_search_rescoring_prewhere_eligibility - tests/clickhouse-test --binary build_verify/programs/clickhouse --testname 02354_vector_search_rescoring 02354_vector_search_rescoring_and_prewhere 02354_vector_search_rescoring_distance_in_select_list 02354_vector_search_lazy_materialization 02354_vector_search_incident1654 04489_vector_search_rescoring_lazy_materialization 04490_vector_search_rescoring_prewhere_eligibility - git diff --check
`optimizePrewhere` now suppresses implicit `PREWHERE` only when read hints make the vector second pass eligible, but non-rescoring vector search has one additional upper-plan rejection: the `_distance` rewrite is disabled when the query still needs the physical vector column in the output. When the second pass reaches such a late rejection and no vector optimization is applied, run `optimizePrewhere` once for the filter node with vector suppression disabled. Keep this gated by `query_plan_optimize_prewhere` through `Optimization::ExtraSettings`, so the fallback does not move conditions when the query-plan `PREWHERE` optimization is disabled. Extend `04490_vector_search_rescoring_prewhere_eligibility` with both sides of that late-rejection case: selecting `vec` restores implicit `PREWHERE`, while `query_plan_optimize_prewhere = 0` keeps it disabled. Verification: - `ninja -C build_verify programs/clickhouse` - `tests/clickhouse-test --binary build_verify/programs/clickhouse --testname 04490_vector_search_rescoring_prewhere_eligibility` - `tests/clickhouse-test --binary build_verify/programs/clickhouse --testname 02354_vector_search_rescoring 02354_vector_search_rescoring_and_prewhere 02354_vector_search_rescoring_distance_in_select_list 02354_vector_search_lazy_materialization 02354_vector_search_incident1654 04489_vector_search_rescoring_lazy_materialization 04490_vector_search_rescoring_prewhere_eligibility` - `git diff --check`
After gating implicit `PREWHERE` suppression on actual vector second-pass eligibility, indexed auto/postfilter queries in `02354_vector_search_nonindexed_columns` no longer keep the `WHERE` predicate as a plain filter when the PK filter leaves no usable vector row hints. The second pass does not apply the vector rewrite in that shape, so implicit `PREWHERE` is restored. Update the reference and test comment to match the new intended behavior. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108846&sha=f254dac3784c858c9287cba471f5ebf6a44cd9f8&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20targeted%29 PR: ClickHouse#108846 Verification: - `tests/clickhouse-test --binary build_verify/programs/clickhouse --testname 02354_vector_search_nonindexed_columns` - `tests/clickhouse-test --binary build_verify/programs/clickhouse --testname 02354_vector_search_nonindexed_columns 02354_vector_search_rescoring 02354_vector_search_rescoring_and_prewhere 02354_vector_search_rescoring_distance_in_select_list 02354_vector_search_lazy_materialization 02354_vector_search_incident1654 04489_vector_search_rescoring_lazy_materialization 04490_vector_search_rescoring_prewhere_eligibility` - `git diff --check`
`part_offsets_filter_for_vector_search` is built over physical rows in the current granules. When an earlier filter such as lightweight delete has already been applied, `ReadResult::num_rows` is smaller than `total_rows_per_granule`, while `final_filter` still keeps the physical row shape. Passing the physical vector filter directly to `ReadResult::optimize` either skipped candidate-row filtering with the old `num_rows` gate or violated `applyFilter` size invariants when the gate was widened. Convert the vector filter through `final_filter` before calling `optimize` in that state, matching the existing `combineFilters` contract for already-applied filters. Add a lightweight-delete regression test that asserts rescoring does not return a surviving granule mate outside vector candidates. Also make `02354_vector_search_nonindexed_columns` assert that indexed `auto`/`postfilter` paths still use the vector index even when implicit `PREWHERE` is restored. Tests: - `env -u CFLAGS -u CXXFLAGS -u LDFLAGS -u CPPFLAGS ninja -C build_verify programs/clickhouse` - `tests/clickhouse-test --binary build_verify/programs/clickhouse --testname 04491_vector_search_rescoring_lightweight_delete_filter` - `tests/clickhouse-test --binary build_verify/programs/clickhouse --testname 02354_vector_search_nonindexed_columns` - `tests/clickhouse-test --binary build_verify/programs/clickhouse --testname 04491_vector_search_rescoring_lightweight_delete_filter 02354_vector_search_rescoring 04490_vector_search_rescoring_prewhere_eligibility 02354_vector_search_nonindexed_columns 04489_vector_search_rescoring_lazy_materialization 02354_vector_search_rescoring_and_prewhere 02354_vector_search_rescoring_distance_in_select_list 02354_vector_search_lazy_materialization 02354_vector_search_incident1654` - `git diff --check -- src/Storages/MergeTree/MergeTreeRangeReader.cpp tests/queries/0_stateless/02354_vector_search_nonindexed_columns.sql tests/queries/0_stateless/02354_vector_search_nonindexed_columns.reference tests/queries/0_stateless/04491_vector_search_rescoring_lightweight_delete_filter.sql tests/queries/0_stateless/04491_vector_search_rescoring_lightweight_delete_filter.reference`
|
📊 Cloud Performance Report ✅ AI verdict: This change is entirely scoped to vector-similarity search rescoring: it rewrites the PREWHERE/vector-index second-pass optimizer and the MergeTree row-filter path, all gated on a query actually using a vector_similarity index. ClickBench Q15 and Q32 use no vector index, so neither flagged delta can be attributed to this PR. The deterministic envelope correctly held Q15's -13.1% at no_change as run-to-run variance, and Q32's -5.7% is borderline and off-path; both readings are consistent with the diff. No real performance impact is expected from this change on these benchmarks. clickbenchFlagged queries (1 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 153/167 (91.62%) · Uncovered code |
|
Green CI at dcce702 |
may add PK-overlapping ranges after vector index analysis via . The rescoring row filter only has row offsets for the original vector candidates, so applying it before the final merge can drop rows added by exact-mode recovery and let an older row survive. Keep mark-level vector index selection and exact-mode range recovery, but do not enable for rescoring queries with . Non- rescoring still uses the candidate-row filter. Add a regression case to where the reference query and vector-index query must both return the newer visible row after . Related: ClickHouse#108846 Tested locally: - env -u CFLAGS -u CXXFLAGS -u LDFLAGS -u CMAKE_C_FLAGS -u CMAKE_CXX_FLAGS -u CMAKE_EXE_LINKER_FLAGS -u CMAKE_SHARED_LINKER_FLAGS -u CMAKE_MODULE_LINKER_FLAGS ninja -C build_verify programs/clickhouse - tests/clickhouse-test -b build_verify/programs/clickhouse --queries tests/queries --no-stateful --no-random-settings --no-random-merge-tree-settings --no-zookeeper --no-shard --timeout 180 02354_vector_search_rescoring 02354_vector_search_rescoring_distance_in_select_list 02354_vector_search_rescoring_and_prewhere 02354_vector_search_incident1654 - build_verify/programs/clickhouse client --multiquery < tmp/pr108846_final_probe4.sql
The reland stores `use_vector_search_result_filter` in shared read hints. `prepared_index` reuses the main `data_part_info_for_read`, and `MergeTreeReadTask::createReaders` also builds non-main readers for index and PREWHERE stages. If those readers interpret the rescoring flag, they can build and apply the same row-offset filter before the final data reader. Gate the rescoring row filter on `main_reader` in `MergeTreeRangeReader::fillVirtualColumns` and `MergeTreeRangeReader::executePrewhereActionsAndFilterColumns`. Keep the existing `_distance` path unchanged so vector index reads that explicitly request `_distance` still populate and apply the virtual-column filter. Review thread: ClickHouse#108846 (comment) Local verification: - `ninja -C build_verify programs/clickhouse` - `tests/clickhouse-test -b build_verify/programs/clickhouse --queries tests/queries --no-stateful --no-random-settings --no-random-merge-tree-settings --no-zookeeper --no-shard --timeout 180 02354_vector_search_rescoring 02354_vector_search_rescoring_distance_in_select_list 02354_vector_search_rescoring_and_prewhere 02354_vector_search_lazy_materialization 02354_vector_search_incident1654`
Add a focused stateless test for the multi-reader topology from the review thread: vector-search rescoring with lazy materialization enabled and a selected mark that contains spillover rows beyond the top-k candidates. The test verifies that the plan uses `LazilyReadFromMergeTree`, that the vector similarity index is used, and that late materialization of an extra payload column returns the expected candidate rows under `vector_search_with_rescoring = 1`. Review thread: ClickHouse#108846 (comment) Local verification: - `tests/clickhouse-test -b build_verify/programs/clickhouse --queries tests/queries --no-stateful --no-random-settings --no-random-merge-tree-settings --no-zookeeper --no-shard --timeout 180 04489_vector_search_rescoring_lazy_materialization` - `tests/clickhouse-test -b build_verify/programs/clickhouse --queries tests/queries --no-stateful --no-random-settings --no-random-merge-tree-settings --no-zookeeper --no-shard --timeout 180 02354_vector_search_rescoring 02354_vector_search_rescoring_distance_in_select_list 02354_vector_search_rescoring_and_prewhere 02354_vector_search_lazy_materialization 02354_vector_search_incident1654 04489_vector_search_rescoring_lazy_materialization`
used as a proxy for vector second-pass execution. That proxy is too broad: rescoring queries can later skip the candidate-row filter, for example when PK pruning leaves a non-empty read range without vector-search row hints, so implicit was disabled without any vector-search replacement benefit. Make the suppression depend on the read-hint conditions that the second pass needs: local index analysis, non- rescoring reads, vector-search result hints for rescoring, and distance-bearing hints for the non-rescoring rewrite. Add to cover both sides: PK-pruned rescoring restores implicit , while an eligible rescoring query still suppresses it for the row-filter path. Review thread: ClickHouse#108846 (comment) Verification: - ninja -C build_verify programs/clickhouse - tests/clickhouse-test --binary build_verify/programs/clickhouse --testname 04490_vector_search_rescoring_prewhere_eligibility - tests/clickhouse-test --binary build_verify/programs/clickhouse --testname 02354_vector_search_rescoring 02354_vector_search_rescoring_and_prewhere 02354_vector_search_rescoring_distance_in_select_list 02354_vector_search_lazy_materialization 02354_vector_search_incident1654 04489_vector_search_rescoring_lazy_materialization 04490_vector_search_rescoring_prewhere_eligibility - git diff --check
After gating implicit `PREWHERE` suppression on actual vector second-pass eligibility, indexed auto/postfilter queries in `02354_vector_search_nonindexed_columns` no longer keep the `WHERE` predicate as a plain filter when the PK filter leaves no usable vector row hints. The second pass does not apply the vector rewrite in that shape, so implicit `PREWHERE` is restored. Update the reference and test comment to match the new intended behavior. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108846&sha=f254dac3784c858c9287cba471f5ebf6a44cd9f8&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20targeted%29 PR: ClickHouse#108846 Verification: - `tests/clickhouse-test --binary build_verify/programs/clickhouse --testname 02354_vector_search_nonindexed_columns` - `tests/clickhouse-test --binary build_verify/programs/clickhouse --testname 02354_vector_search_nonindexed_columns 02354_vector_search_rescoring 02354_vector_search_rescoring_and_prewhere 02354_vector_search_rescoring_distance_in_select_list 02354_vector_search_lazy_materialization 02354_vector_search_incident1654 04489_vector_search_rescoring_lazy_materialization 04490_vector_search_rescoring_prewhere_eligibility` - `git diff --check`
…tor-rescoring-row-filter Re-land exact row filtering for vector search rescoring queries
After a lightweight `DELETE`, a vector search returned fewer rows than its `LIMIT` and missed true
nearest neighbours:
INSERT INTO hv SELECT number, [toFloat32(number % 97), toFloat32(number % 31)] FROM numbers(10000);
DELETE FROM hv WHERE id IN (5, 3012, 6019, 9026); -- the exact matches of [5, 5]
SELECT count() FROM (SELECT id FROM hv ORDER BY L2Distance(v, [5.0, 5.0]) LIMIT 10); -- 6
Deleting a whole cluster of near neighbours emptied the result. The rows that were returned were also
not the nearest survivors: the deleted candidates shadowed them.
A lightweight delete does not rebuild a secondary index, so the vector index keeps returning the row
ids of deleted rows. Both vector-search optimizations then restrict the read to exactly those rows -
the rewritten plan without rescoring, and the exact row filter with rescoring - so `_row_exists`
removes the deleted candidates afterwards with nothing left to take their place. Before exact row
filtering was added (ClickHouse#108846, 26.7) the read covered the candidates' granules, so the surviving rows
there were rescored and the query still satisfied its `LIMIT`.
Skip both optimizations for a query whose parts have a delete the index does not know about: a
materialized lightweight delete, a delete mutation that is applied on the fly, or a patch part (which
can carry an update of `_row_exists`). The read then falls back to the candidates' granules, as before
26.7, until a merge or a mutation rebuilds the index.
`04491_vector_search_rescoring_lightweight_delete_filter` asserted the old behaviour - that the row
filter excludes a surviving granule mate after a lightweight delete, without checking that the `LIMIT`
is still satisfied - and now asserts that the index answer equals the bruteforce one.
Closes: ClickHouse#117286
Caused by: ClickHouse#108846
Related: #105591
Related: #108423
Related: #108812
Re-land #105591 on top of the
_distanceguard from #108423.The previous merge was reverted by #108812 because the old test coverage expected explicit
_distanceselection to work. This version keeps_distanceinternal, removes the conflicting direct-selection expectations, and preserves the row-level vector-index candidate filter forvector_search_with_rescoring = 1.For
vector_search_with_rescoring = 1, exact rescoring is restricted to rows returned by vector-index candidate selection. It no longer brute-force rescores neighbouring rows from the sameMergeTreegranule when those rows were not returned by the vector index.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Vector search queries using rescoring now restrict exact distance calculation to vector-index candidate rows instead of brute-force rescoring neighbouring rows from the same
MergeTreegranule.Version info
26.7.1.1181(included in26.7and later)