Repository navigation
Initial support for running vector similarity searches from analyzer - #71545
Conversation
|
This is an automated comment for commit b34f28a with description of existing statuses. It's updated for the latest CI running ❌ Click here to open a full report in a separate page
Successful checks
|
This code is extremely shameful, don't look at it.
Only goal of this PR is to demonstrate that the analyzer is able to see
vector similarity indexes:
ip-10-19-83-181.eu-central-1.compute.internal :) explain plan indexes=1 SELECT id, vec, L2Distance(vec, [0.0, 2.0])
FROM tab
ORDER BY L2Distance(vec, [0.0, 2.0])
LIMIT 3 settings enable_analyzer = 1
EXPLAIN indexes = 1
SELECT
id,
vec,
L2Distance(vec, [0., 2.])
FROM tab
ORDER BY L2Distance(vec, [0., 2.]) ASC
LIMIT 3
SETTINGS enable_analyzer = 1
Query id: 086df46e-2e27-4342-a47c-6900762a7c8c
┌─explain─────────────────────────────────────────────────────────────────────────────────────────┐
1. │ Expression (Project names) │
2. │ Limit (preliminary LIMIT (without OFFSET)) │
3. │ Sorting (Sorting for ORDER BY) │
4. │ Expression ((Before ORDER BY + (Projection + Change column names to column identifiers))) │
5. │ ReadFromMergeTree (default.tab) │
6. │ Indexes: │
7. │ PrimaryKey │
8. │ Condition: true │
9. │ Parts: 1/1 │
10. │ Granules: 1/1 │
11. │ Skip │
12. │ Name: idx │
13. │ Description: vector_similarity GRANULARITY 100000000 │
14. │ Parts: 1/1 │
15. │ Granules: 1/1 │
└─────────────────────────────────────────────────────────────────────────────────────────────────┘
15 rows in set. Elapsed: 0.007 sec.
fa63535 to
20bc3fe
Compare
006422f to
dfa29b1
Compare
7d3ec87 to
b8a9c5b
Compare
|
@novikd This PR is ready and the test failure is unrelated. Like to take a look? Thanks. |
| ((parameters->distance_function == "L2Distance" && metric_kind == unum::usearch::metric_kind_t::l2sq_k) | ||
| || (parameters->distance_function == "cosineDistance" && metric_kind == unum::usearch::metric_kind_t::cos_k)); | ||
|
|
||
| if (distance_function_in_query_and_distance_function_in_index_match && (parameters->limit <= max_limit_for_ann_queries)) |
There was a problem hiding this comment.
I don't understand this condition. It differs from the previous one.
There was a problem hiding this comment.
I'll add a code comment!
There was a problem hiding this comment.
It's clear that we must match distance functions, but why do you check the limit here?
There was a problem hiding this comment.
Added a code comment which explains the reason.
There was a problem hiding this comment.
For me, it's unclear why we compare the limits here in the alwaysUnknownOrTrue method. I think the limit should be checked when we try to apply plan optimization.
| ((parameters->distance_function == "L2Distance" && metric_kind == unum::usearch::metric_kind_t::l2sq_k) | ||
| || (parameters->distance_function == "cosineDistance" && metric_kind == unum::usearch::metric_kind_t::cos_k)); | ||
|
|
||
| if (distance_function_in_query_and_distance_function_in_index_match && (parameters->limit <= max_limit_for_ann_queries)) |
There was a problem hiding this comment.
For me, it's unclear why we compare the limits here in the alwaysUnknownOrTrue method. I think the limit should be checked when we try to apply plan optimization.
| return updated_layers; | ||
|
|
||
| /// Extract N | ||
| size_t n = limit_step->getLimitForSorting(); |
There was a problem hiding this comment.
The limit should be checked somewhere here. We should not apply the optimization if n > max_limit_for_ann_queries.
|
@novikd Tnx, I'll merge for now (some people are waiting for this PR) and will follow-up separately. |
|
clang-build: #73265 ClickHouse Stateless tests (msan) [3/4]
|
The vector similarity index is a special secondary index which speeds up queries of the form
where
distance_functionis function 'L2Distance' or 'cosineDistance',vecis a column of tab (*),reference_vecis a literal of typeArray(Float32/Float64)(see here for more details).
Previously, the index analyzed the query AST and extracted
distance_function,vecandreference_vecfrom it (this was done by the code in src/Storages/MergeTree/VectorSimilarityCondition.h, deleted in this PR). As this approach did not work with the analyzer, this PR re-implements the logic as an optimization pass (--> src/Processors/QueryPlan/Optimizations/useVectorSearch.cpp). A new structVectorSearchParametersis added toReadFromMergeTreewhich contains all necessary data to perform a vector search.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Enable analyzer support for vector search