Skip to content

fix: skip vector search optimization for LIMIT WITH TIES to prevent dropped rows - #110453

Merged
rschu1ze merged 4 commits into
ClickHouse:masterfrom
tamish560:fix/vector-search-with-ties
Jul 20, 2026
Merged

rschu1ze merged 4 commits into
ClickHouse:masterfrom
tamish560:fix/vector-search-with-ties

Conversation

@tamish560

@tamish560 tamish560 commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

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

Changelog entry:

Skips vector search optimization when LIMIT ... WITH TIES is used, because the optimization bounds the ANN search to exactly n candidates and rows tied with the n-th row are dropped.

Related to #110407
Closes #110405

Version info

  • Merged into: 26.7.1.1241 (included in 26.7 and later)

@CLAassistant

CLAassistant commented Jul 14, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@PedroTadim

This comment was marked as resolved.

@tamish560

This comment was marked as resolved.

@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Jul 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b01f9d7]

Summary: ✅


AI Review

Summary

This PR makes useVectorSearch skip the vector_similarity optimization for LIMIT ... WITH TIES, which addresses the wrong-results case from #110405. I cannot approve it yet because the same optimization still ignores LimitStep::alwaysReadTillEnd(), so SET exact_rows_before_limit = 1 can still return an incorrect rows_before_limit_at_least once the ANN path truncates the upstream stream.

Findings

⚠️ Majors

[src/Processors/QueryPlan/Optimizations/useVectorSearch.cpp:119-123] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/110453#discussion_r3585723495] exact_rows_before_limit still promises an exact rows_before_limit_at_least by reading the full pre-LIMIT stream (src/Core/Settings.cpp:3488-3490, src/Planner/Planner.cpp:1445-1461), but this optimizer now skips only withTies(). The ANN path still sizes the search to parameters->limit (src/Storages/MergeTree/MergeTreeIndexVectorSimilarity.cpp:560-570) and then filters the reader output down to those neighbor offsets (src/Storages/MergeTree/MergeTreeRangeReader.cpp:1724-1742), so a query like ... LIMIT 3 SETTINGS exact_rows_before_limit = 1 can expose only three upstream rows and undercount rows_before_limit_at_least. The earlier dismissal came from the original comment naming the wrong setting, but the underlying alwaysReadTillEnd() contract is still real, and sibling approximating optimizations already guard it (src/Processors/QueryPlan/Optimizations/useVectorSearchWithQuantizedCodes.cpp:159-162, src/Processors/QueryPlan/Optimizations/optimizeTopK.cpp:42-46). Suggested fix: skip useVectorSearch whenever limit_step->alwaysReadTillEnd() is set, and add a focused regression that asserts rows_before_limit_at_least under SET exact_rows_before_limit = 1.

Final Verdict

Status: ⚠️ Request changes

Minimum required actions: add the missing alwaysReadTillEnd() guard (or otherwise prove the vector-search reader still exposes the full pre-LIMIT stream for exact row counting), and add a regression for SET exact_rows_before_limit = 1 so the setting contract is covered alongside LIMIT WITH TIES.

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

This comment was marked as resolved.

/// LIMIT ... WITH TIES can return more rows than n. The vector search optimization
/// bounds the ANN search to exactly n candidates, so rows tied with the n-th row
/// are never retrieved. Skip the optimization and fall back to brute force.
if (limit_step->withTies())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

exact_rows_before_limit is still unsafe here. That setting sets LimitStep::alwaysReadTillEnd() because the query promises an exact rows_before_limit_at_least, but this optimizer still programs vector_search_parameters.limit = getLimitForSorting() and the read path later filters the source rows down to those ANN neighbors (MergeTreeDataSelectExecutor / MergeTreeRangeReader). The counter above LIMIT can therefore observe only the truncated vector-search subset instead of the full pre-LIMIT stream.

This needs the same alwaysReadTillEnd() guard that optimizeTopK and topKThroughJoin already have, plus a focused regression with SET exact_rows_before_limit = 1.

This comment was marked as low quality.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is still present in the current code. LimitStep::alwaysReadTillEnd() is not a rows_before_limit_at_least setting; src/Planner/Planner.cpp:1443-1459 sets it for exact_rows_before_limit (and some totals cases). But src/Processors/QueryPlan/Optimizations/useVectorSearch.cpp:117-126 now only guards withTies(), while the vector-search path still caps the ANN fetch at parameters->limit in src/Storages/MergeTree/MergeTreeIndexVectorSimilarity.cpp:559-563 and then filters the reader output down to those ANN rows in src/Storages/MergeTree/MergeTreeRangeReader.cpp:1690-1694. So SET exact_rows_before_limit = 1 can still undercount rows_before_limit_at_least unless this optimization also skips alwaysReadTillEnd().

@PedroTadim

Copy link
Copy Markdown
Member

Let's wait for the review

@rschu1ze rschu1ze self-assigned this Jul 15, 2026
@tamish560

This comment was marked as low quality.

@rschu1ze

rschu1ze commented Jul 15, 2026 •

Copy link
Copy Markdown
Member

@tamish560 Can you please ask your Large Language Model (*) to not re-introduce changes which have been reverted upon explicit human request. I'm looking at the test for exact_rows_before_limit.

(*) https://github.com/tamish560

image

@tamish560

This comment was marked as low quality.

@rschu1ze

Copy link
Copy Markdown
Member

@tamish560 The tests are failing, could you check please?

@tamish560

Copy link
Copy Markdown
Contributor Author

Looking into it, checking the CI results now.

@tamish560

This comment was marked as low quality.

@tamish560

This comment was marked as low quality.

@tamish560

This comment was marked as low quality.

@tamish560

This comment was marked as low quality.

@tamish560

This comment was marked as low quality.

@PedroTadim

Copy link
Copy Markdown
Member

@tamish560 maybe you should squash the commits, they are quite many

@tamish560
tamish560 force-pushed the fix/vector-search-with-ties branch from e1af0f8 to f5a0837 Compare July 16, 2026 09:24
@tamish560

Copy link
Copy Markdown
Contributor Author

Squashed. The PR now has a single commit.

)
WHERE explain LIKE '%vector_similarity%';

SELECT '-- Plain LIMIT 3: index usage expected';

@rschu1ze rschu1ze Jul 16, 2026 •

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.

@tamish560 Could you please ask the Large Language Model to remove everything from l. 39 to the end? This stuff is already tested elsewhere.

@tamish560
tamish560 force-pushed the fix/vector-search-with-ties branch from 8772ae6 to 1e866f2 Compare July 16, 2026 09:30
@tamish560

This comment was marked as low quality.

@tamish560

Copy link
Copy Markdown
Contributor Author

@rschu1ze All requested changes are done:

  1. Test cases moved to new file 02354_vector_search_bug110405.sql/reference
  2. Removed everything after line 191 from the existing test file
  3. Removed the rows_before_limit_at_least guard (lines 125-127) from useVectorSearch.cpp
  4. Removed everything from line 39 onward from the new test file per your latest comment
  5. Squashed to a single commit

The CI failures (arm_binary flaky test, amd_llvm_coverage server crash, amd_tsan data race) are all pre-existing and unrelated to this change. Could you re-review when you get a chance?

@tamish560

Copy link
Copy Markdown
Contributor Author

done. removed lines 39+ from the test file. the DROP TABLE cleanup at the end was redundant since stateless tests handle their own teardown.

@clickhouse-gh

clickhouse-gh Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 78.30% 78.20% -0.10%

Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code

Full report · Diff report

@rschu1ze
rschu1ze added this pull request to the merge queue Jul 20, 2026
Merged via the queue into ClickHouse:master with commit 42fe598 Jul 20, 2026
177 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 20, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…-with-ties

fix: skip vector search optimization for LIMIT WITH TIES to prevent dropped rows
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 pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong results: vector similarity index drops tied rows for LIMIT ... WITH TIES

5 participants