Repository navigation
fix: skip vector search optimization for LIMIT WITH TIES to prevent dropped rows - #110453
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
|
Workflow [PR], commit [b01f9d7] Summary: ✅
AI ReviewSummaryThis PR makes Findings
Final VerdictStatus: Minimum required actions: add the missing |
This comment was marked as resolved.
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()) |
There was a problem hiding this comment.
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.
This comment was marked as low quality.
Sorry, something went wrong.
There was a problem hiding this comment.
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().
|
Let's wait for the review |
This comment was marked as low quality.
This comment was marked as low quality.
|
@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 (*) https://github.com/tamish560
|
This comment was marked as low quality.
This comment was marked as low quality.
|
@tamish560 The tests are failing, could you check please? |
|
Looking into it, checking the CI results now. |
This comment was marked as low quality.
This comment was marked as low quality.
This comment was marked as low quality.
This comment was marked as low quality.
This comment was marked as low quality.
This comment was marked as low quality.
This comment was marked as low quality.
This comment was marked as low quality.
This comment was marked as low quality.
This comment was marked as low quality.
|
@tamish560 maybe you should squash the commits, they are quite many |
e1af0f8 to
f5a0837
Compare
|
Squashed. The PR now has a single commit. |
| ) | ||
| WHERE explain LIKE '%vector_similarity%'; | ||
|
|
||
| SELECT '-- Plain LIMIT 3: index usage expected'; |
There was a problem hiding this comment.
@tamish560 Could you please ask the Large Language Model to remove everything from l. 39 to the end? This stuff is already tested elsewhere.
8772ae6 to
1e866f2
Compare
This comment was marked as low quality.
This comment was marked as low quality.
|
@rschu1ze All requested changes are done:
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? |
|
done. removed lines 39+ from the test file. the |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code |
…-with-ties fix: skip vector search optimization for LIMIT WITH TIES to prevent dropped rows

Changelog category (leave one):
Changelog entry:
Skips vector search optimization when
LIMIT ... WITH TIESis 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
26.7.1.1241(included in26.7and later)