Repository navigation
Conversation
|
Hi @alexey-milovidov @rschu1ze, the current implementation follows the basic SPANN structure, where centroids in memory via HNSW, full vectors in a per-part posting list substream on disk, query path wired up end-to-end. The main limitation right now is that posting lists are fully loaded per granule at read time, which means we're not yet getting the memory savings SPANN promises. Doing proper selective IO (only reading posting lists for matched centroids) would require changes to the IMergeTreeIndexCondition interface or a dedicated branch in filterMarksUsingIndex, since calculateApproximateNearestNeighbors currently has no access to any file reader. Is this the kind of infrastructure change being discussed internally for vector similarity index 2.0? I would be happy to go in whatever direction is most useful. |
|
@zex-hyd Thanks. Please add design documentation (what data is stored where and why in what way) in the PR description and as code comments. Yes, I can read the code to figure these things out all by myself, but there are choices which I don't understand and I will need to guess around.
So far, no decision about the overall architecture / algorithm / data structures for the vector index 2.0 has been made. I would say we can consider this PR as a POC that SPANN can in principle be implemented (even thouth I doubt the decision to use HNSW for indexing the in-memory vector and all other optimizations from the SPANN paper are not implemented in this PR). Like I wrote here, we will likely implement the new index on our own. |
|
Hi @rschu1ze, added design documentation to the PR description covering the on-disk layout, build pipeline, query path, and current limitations. HBC and closure expansion are not implemented yet since the goal for the first few commit was to get the index structure and end-to-end path working first On the selective IO gap: |
|
We'll implement SPANN by ourselves, thanks for sending a PR anyways. |
Either you provide a link to a new PR, or we will take this one as the way to go. |
|
Workflow [PR], commit [b30a3bc] Summary: ❌
AI ReviewSummaryThis PR adds an experimental Findings
Tests
Final Verdict
|
|
Hi @alexey-milovidov, thanks for checking this PR! The current state has the multi-stream on-disk format, build pipeline, and end-to-end query path working. HBC, closure expansion, and selective IO are not done yet since posting lists are fully loaded per granule and calculateApproximateNearestNeighbors only receives the granule, not any file reader. Only Array(Float32) for now. Please let me know how you'd like to proceed with this! |
# Conflicts: # src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp
`clickhouse-gh` review flagged that `vector_spann` was copied into the `UInt32` part-row guards borrowed from `vector_similarity`, both in merge selection (`getMaxResultPartRowsCount`) and in `ALTER TABLE ... MATERIALIZE INDEX` (`hasTextIndexMaterialization`). Unlike `text` and `vector_similarity`, whose on-disk row ids are part-wide `UInt32`, `vector_spann` stores `UInt64` posting-list row ids that are local to a skip-index granule (`SpannPostingEntry::row_id`, reset per granule in `getGranuleAndReset`) and converts them to part-wide `UInt64` offsets at query time via `convertNearestNeighboursToPartOffsets`/`granule_begin_row`. It therefore imposes no part-wide row limit, and the blanket `part->rows_count > UInt32::max()` cap wrongly stopped large `vector_spann` tables from compacting and rejected their index materialization with `SUPPORT_IS_DISABLED` for no format-level reason. Remove `vector_spann` from both guards.
|
Merged current
CI failures on the previous commit:
|
|
The exact failing assertion on the latest tsan flaky-check run (commit Thrown at Two things in the CIDB history reframe this away from a tsan race:
Inference from the data (not yet debugger-confirmed): test #10 uses So the productive next step is to inspect Separately, to avoid conflation: the |
…tiplier lower bound
…nn-merge-tree-index Integrate remote review/master updates with the local readPostingList lazy-seek and index_fetch_multiplier fixes.
|
|
||
| const size_t first_row_in_index_granule = part->index_granularity->getMarkStartingRow(index_mark * skip_index_granularity); | ||
| for (size_t result_pos = 0; result_pos < vector_search_results.rows.size(); ++result_pos) | ||
| auto granule_results = condition->calculateApproximateNearestNeighbors(granule); |
There was a problem hiding this comment.
This new per-granule merge path is still unreachable as soon as primary-key pruning narrows the part, because filterMarksUsingIndex returns early on index_helper->isVectorSimilarityIndex() && !all_match before we get here. That blanket bailout is necessary for whole-part vector_similarity, but it disables the new vector_spann GRANULARITY 1 form too, so queries like WHERE id >= ... ORDER BY L2Distance(vec, ref) LIMIT n silently fall back to brute force even though the index can localize candidates mark-by-mark. Could we keep the !all_match guard for whole-part vector indexes, but let the multi-granule vector_spann case reach this merge path?
There was a problem hiding this comment.
Small clarification: #104754 mainly covers the existing vector_similarity partial-PK case.
In this PR, I’d like to keep vector_spann focused on the default whole-part granularity path. Supporting explicit small GRANULARITY together with partial primary-key pruning is useful, but seems better as a follow-up instead of expanding this PR further.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 604/715 (84.48%) · Uncovered code |
a6fe7c9 to
d601ca0
Compare
68187b4 to
d601ca0
Compare
| /// FINAL can change the visible row set after vector index analysis. | ||
| /// The optimized `_distance` rewrite also enables exact-row filtering in | ||
| /// MergeTreeRangeReader, so keep it disabled under FINAL like rescoring. | ||
| bool optimize_plan = !settings.vector_search_with_rescoring && !read_from_mergetree_step->isQueryWithFinal(); |
There was a problem hiding this comment.
02354_vector_search_rescoring.sql already covers FINAL, but only in rescoring mode. This new !isQueryWithFinal() guard is what keeps the optimized _distance / row-hint path from running for vector_spann when vector_search_with_rescoring = 0, and there is still no regression that exercises that exact branch.
Please add a small ReplacingMergeTree FINAL (or CollapsingMergeTree FINAL) case for vector_spann with vector_search_with_rescoring = 0 that would fail if the optimized rewrite were re-enabled under FINAL. Right now a later planner refactor could silently bring this bug back without tripping the suite.
Resolve the conflict with the move of the settings changes history into the setting declarations (ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as trailing arguments of the `DECLARE` of each setting. - retargeted PR record [email protected] to 26.10 (settings-history check rule) - semantic merge fixes in non-conflicting files: src/Storages/MergeTree/MergeTreeIndexVectorSpann.cpp, src/Storages/MergeTree/MergeTreeIndexVectorSpann.h Also resolved conflicts in: - `src/Processors/QueryPlan/ReadFromMergeTree.cpp` - `src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp` - `src/Storages/MergeTree/MergeTreeIndexVectorSimilarity.cpp` - `src/Core/Settings.cpp` `MergeTreeIndexVectorSpann` overrides `getPhysicalFormat`, because master made `getDeserializedFormat` non-virtual. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
| /// FINAL can change the visible row set after vector index analysis. | ||
| /// The optimized `_distance` rewrite also enables exact-row filtering in | ||
| /// MergeTreeRangeReader, so keep it disabled under FINAL like rescoring. | ||
| bool optimize_plan = !settings.vector_search_with_rescoring && !read_from_mergetree_step->isQueryWithFinal(); |
There was a problem hiding this comment.
vector_spann becomes eligible for the optimized _distance rewrite through this path, but the guard below still only disables the optimization when the vector column is selected directly.
That misses any other live consumer of vec in the ExpressionStep / FilterStep, for example SELECT length(vec) ... ORDER BY L2Distance(vec, ref) LIMIT 10 or WHERE arrayExists(x -> x > 0, vec) .... replaceVectorColumnWithDistanceColumn then unconditionally removes vec from ReadFromMergeTree::all_column_names, while those downstream DAG nodes still expect it, so the optimized plan can fail at execution time.
Can we gate optimize_plan on whether any surviving action in the main/filter DAG depends on the search column, rather than only checking for a direct vec output?
|
We invested quite some effort to implement IVF (here: #112309) which is now the fourth vector search method besides brute force, hnsw, and qbit. SPANN and IVF cover the same class of large vector indexes but the IVF implementation will give us more flexibility going forward. Therefore closing this PR. Thanks for sending this PR anyways @zex-hyd |
Resolves #102146
This is an early prototype implementing the basic SPANN-style index structure in MergeTree, following up on #94545.
Currently supports
Array(Float32)only. Centroid selection uses deterministic evenly-spaced sampling for now — hierarchical balanced clustering, closure expansion, query-aware dynamic pruning, and selective posting-list reads are planned as follow-ups. Posting lists are currently deserialized and scanned in memory during search.Supported metrics:
L2Distance,cosineDistance,dotProduct.Current scope vs. full SPANN
vector_spannindex typeSpannPostingListsChangelog category (leave one):
Changelog entry:
Add experimental
vector_spannindex type, a memory-disk hybrid ANN index inspired by SPANN (NeurIPS 2021). Centroids are kept in memory via HNSW; full vectors are stored in per-part posting-list files on disk.Documentation entry for user-facing changes
Note
High Risk
High risk because it introduces a new on-disk index format with multi-stream serialization and changes MergeTree index selection/query-path logic for vector search, which can affect query correctness and storage compatibility.
Overview
Adds a new experimental MergeTree secondary index type
vector_spann(gated byallow_experimental_vector_spann_indexandUSE_USEARCH) with its own build/search implementation, multi-stream serialization (newSpannPostingListssubstream), and validation for supported metrics/args andArray(Float32)columns.Updates vector-search integration to treat
vector_spannas a vector-search-capable index alongsidevector_similarity(planner detection, default granularity behavior, index condition creation, and various checks around ALTER/mutations/compaction row limits), and includes a new stateless test suite covering basic ANN behavior and input/argument validation.Reviewed by Cursor Bugbot for commit 687e5dc. Bugbot is set up for automated code reviews on this repo. Configure here.
Workflow [PR]
Sync PR [sync-upstream/pr/103112]