Repository navigation
Don't insert deferred points into sparse index - #8435
Conversation
| .is_some_and(|deferred| id >= deferred); | ||
|
|
||
| // do not upsert empty or deferred vectors into the index | ||
| if !vector.is_empty() && !point_is_deferred { |
There was a problem hiding this comment.
The main change of this PR, ignoring deferred points during insertion.
f00401c to
7d1c435
Compare
d163a15 to
a4ee5cd
Compare
7d1c435 to
4f157e9
Compare
a4ee5cd to
c2f6f48
Compare
4f157e9 to
839a3b9
Compare
c2f6f48 to
cdc54a8
Compare
839a3b9 to
c55cd4f
Compare
cdc54a8 to
ce6e5e5
Compare
|
|
| .is_some_and(|deferred| id >= deferred); | ||
|
|
||
| // do not upsert empty or deferred vectors into the index | ||
| if !vector.is_empty() && !point_is_deferred { |
There was a problem hiding this comment.
I propose to return because of deferred earlier before the conversion into SparseVector, we perform a couple of unnecessary allocations
There was a problem hiding this comment.
Hmmm... Let's discuss if it's safe to just return before upserting into the index. I'm not deep in the context but I think we are good just to call the return statement before upsertion
| if let Some(deferred_internal_id) = deferred_internal_id | ||
| && segment.is_appendable() | ||
| { | ||
| if let Some(deferred_internal_id) = deferred_internal_id { |
There was a problem hiding this comment.
I like that you refactored the is_appendable check.
Maybe as a next change after release, but I think we can go deeper.
We can move calculate_deleted_deferred_point_count into if tracker trait (as a default fn implementation) with deferred internal id as an argument. It will allow us to remove deferred_point_status: None while segment struct construction, and we can remove this ugly part where we modify just the created segment
| } | ||
|
|
||
| // Ensure we don't use the sparse vector index in future because it's missing proper setup of deferred points. | ||
| drop(index); |
There was a problem hiding this comment.
I like this drop call to mark in the code that we don't use the created index with the tmp deferred internal ID param.
IvanPleshkov
left a comment
There was a problem hiding this comment.
I didn't check these changes manually, but there are already unit tests from #8299, and I trust them.
Also, I compared with the old pr and approved that we cleaned the old code correctly.
2482aa9 to
4dde688
Compare
📝 WalkthroughWalkthroughThis PR moves deferred-ID responsibility from SearchContext into SparseVectorIndex by adding Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
lib/segment/src/segment/tests.rs (1)
1423-1446:⚠️ Potential issue | 🟠 MajorTempDir scoping issue will cause segment to operate on deleted directory.
When
need_rebuilt_segmentis true (non-Windows), the innerdircreated at line 1432 goes out of scope at line 1436 (end ofifblock), causingTempDir::dropto delete the temporary directory. Thesegmentcreated from this directory continues to be used on lines 1438-1442, but now operates on a deleted path.The original
dirclosed at line 1445 is not the one holding the rebuilt segment.🐛 Proposed fix: keep the inner TempDir alive
fn assert_deferred_points_excluded<F, R, T>( name: &str, operation: F, to_external_id: R, test_with_filter: bool, need_rebuilt_segment: bool, ) where F: Fn(&Segment, Option<&Filter>) -> Vec<T>, R: Fn(&T) -> ExtendedPointId, { // ... existing code ... for n_deferred in [0, 1, 10, 300] { for (filter_set_id, filter_set) in set_of_filters.iter().enumerate() { log::debug!(" => deferred points = {n_deferred}; filter-set ID = {filter_set_id}",); let dir = Builder::new().prefix("segment_dir").tempdir().unwrap(); let mut segment = create_deferred_segment(&dir, 5, N_POINTS, n_deferred); // Search with deferred mode let search_res_deferred = operation(&segment, filter_set.filter.as_ref()); assert!(!search_res_deferred.is_empty()); assert_eq!(search_res_deferred.len(), filter_set.expected_visible); // All points in result must always be non deferred. for id_t in search_res_deferred.iter() { let external_id = to_external_id(id_t); assert!(!segment.point_is_deferred(external_id)); } // Disable deferred points and search again. + let rebuilt_dir; if need_rebuilt_segment { // Don't run this on windows because this test is already extremely slow (~100s). // Recreating the segment here would double that time. if cfg!(target_os = "windows") { drop(segment); dir.close().unwrap(); continue; } - let dir = Builder::new().prefix("segment_dir_2").tempdir().unwrap(); - segment = create_deferred_segment(&dir, 5, N_POINTS + n_deferred, 0); + rebuilt_dir = Some(Builder::new().prefix("segment_dir_2").tempdir().unwrap()); + segment = create_deferred_segment(rebuilt_dir.as_ref().unwrap(), 5, N_POINTS + n_deferred, 0); } else { segment.deferred_point_status = None; + rebuilt_dir = None; } let search_res_normal = operation(&segment, filter_set.filter.as_ref()); assert_eq!( search_res_normal.len(), filter_set.expected_visible + (filter_set.expected_deferred)(n_deferred) ); drop(segment); + if let Some(rebuilt) = rebuilt_dir { + rebuilt.close().unwrap(); + } dir.close().unwrap(); } } }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/segment/src/segment/tests.rs` around lines 1423 - 1446, The rebuilt-segment TempDir is dropped too early because the inner `dir` created when `need_rebuilt_segment` is true is scoped inside the `if` and deleted before `segment` is used; to fix, ensure the `TempDir` that backs the rebuilt segment lives until after the segment is dropped by creating/assigning the tempdir to the same outer `dir` variable (or another variable with scope outside the `if`) instead of a new local `dir` inside the block, and use that long-lived `dir` when calling `create_deferred_segment`; keep the final `dir.close().unwrap()` where it is so the directory is only removed after `segment` is dropped.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@lib/segment/src/segment/tests.rs`:
- Around line 1423-1446: The rebuilt-segment TempDir is dropped too early
because the inner `dir` created when `need_rebuilt_segment` is true is scoped
inside the `if` and deleted before `segment` is used; to fix, ensure the
`TempDir` that backs the rebuilt segment lives until after the segment is
dropped by creating/assigning the tempdir to the same outer `dir` variable (or
another variable with scope outside the `if`) instead of a new local `dir`
inside the block, and use that long-lived `dir` when calling
`create_deferred_segment`; keep the final `dir.close().unwrap()` where it is so
the directory is only removed after `segment` is dropped.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 23ed65d7-dbc4-4d57-800a-7a9b67e941dc
📒 Files selected for processing (14)
lib/segment/benches/sparse_index_build.rslib/segment/benches/sparse_index_search.rslib/segment/src/fixtures/sparse_fixtures.rslib/segment/src/index/sparse_index/sparse_vector_index.rslib/segment/src/segment/tests.rslib/segment/src/segment_constructor/segment_builder.rslib/segment/src/segment_constructor/segment_constructor_base.rslib/segment/tests/integration/sparse_discover_test.rslib/segment/tests/integration/sparse_vector_index_search_tests.rslib/sparse/benches/search.rslib/sparse/src/index/search_context.rslib/sparse/src/index/tests/hw_counter_test.rslib/sparse/src/index/tests/indexed_vs_plain_test.rslib/sparse/src/index/tests/search_context_tests.rs
💤 Files with no reviewable changes (3)
- lib/sparse/src/index/tests/indexed_vs_plain_test.rs
- lib/sparse/src/index/tests/search_context_tests.rs
- lib/sparse/src/index/tests/hw_counter_test.rs
# Conflicts: # lib/segment/src/segment/tests.rs # lib/segment/src/segment_constructor/segment_constructor_base.rs # Conflicts: # lib/segment/src/segment_constructor/segment_constructor_base.rs
4dde688 to
49cc0eb
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (2)
lib/segment/src/index/sparse_index/sparse_vector_index.rs (2)
705-705: Minor: Comment is slightly misleading.The comment says "do not upsert empty or deferred vectors", but deferred vectors are handled by the early return at lines 697-699 and never reach this code path. This
if !vector.is_empty()check only guards against empty vectors.Consider updating to just "do not upsert empty vectors into the index" for accuracy.
✏️ Suggested comment fix
- // do not upsert empty or deferred vectors into the index + // do not upsert empty vectors into the index🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/segment/src/index/sparse_index/sparse_vector_index.rs` at line 705, The comment above the if !vector.is_empty() check is misleading because deferred vectors are already returned earlier; update the comment to accurately state that this branch only prevents upserting empty vectors (e.g., change it to "do not upsert empty vectors into the index") near the if !vector.is_empty() check in the function that performs the upsert so it's clear deferred vectors are handled by the earlier early-return.
577-582: Consider usingdebug_assert_eq!for production performance.While this assertion correctly verifies the consistency between
SparseVectorIndexandVectorQueryContext(as requested in the PR comments), usingassert_eq!will add overhead to every search operation in release builds. If this invariant is expected to always hold after initial validation, considerdebug_assert_eq!to avoid the runtime cost in production while still catching violations during development and testing.That said, if catching this inconsistency in production is critical (e.g., for data integrity), keeping
assert_eq!is justified.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/segment/src/index/sparse_index/sparse_vector_index.rs` around lines 577 - 582, Replace the runtime assertion with a debug-only assertion: in the consistency check between SparseVectorIndex and VectorQueryContext comparing deferred_internal_id (the assert_eq! that runs in the search path), use debug_assert_eq! instead of assert_eq! so the check is enabled during development/tests but omitted in release builds to avoid search-time overhead; update the assertion at the point where self.deferred_internal_id is compared to query_context.deferred_internal_id() (within SparseVectorIndex search logic) unless you deliberately want to keep a production-level assert for critical integrity checks.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@lib/segment/src/index/sparse_index/sparse_vector_index.rs`:
- Line 705: The comment above the if !vector.is_empty() check is misleading
because deferred vectors are already returned earlier; update the comment to
accurately state that this branch only prevents upserting empty vectors (e.g.,
change it to "do not upsert empty vectors into the index") near the if
!vector.is_empty() check in the function that performs the upsert so it's clear
deferred vectors are handled by the earlier early-return.
- Around line 577-582: Replace the runtime assertion with a debug-only
assertion: in the consistency check between SparseVectorIndex and
VectorQueryContext comparing deferred_internal_id (the assert_eq! that runs in
the search path), use debug_assert_eq! instead of assert_eq! so the check is
enabled during development/tests but omitted in release builds to avoid
search-time overhead; update the assertion at the point where
self.deferred_internal_id is compared to query_context.deferred_internal_id()
(within SparseVectorIndex search logic) unless you deliberately want to keep a
production-level assert for critical integrity checks.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: b578aff8-ecfa-4364-9c79-e2795f36eb7b
📒 Files selected for processing (14)
lib/segment/benches/sparse_index_build.rslib/segment/benches/sparse_index_search.rslib/segment/src/fixtures/sparse_fixtures.rslib/segment/src/index/sparse_index/sparse_vector_index.rslib/segment/src/segment/tests.rslib/segment/src/segment_constructor/segment_builder.rslib/segment/src/segment_constructor/segment_constructor_base.rslib/segment/tests/integration/sparse_discover_test.rslib/segment/tests/integration/sparse_vector_index_search_tests.rslib/sparse/benches/search.rslib/sparse/src/index/search_context.rslib/sparse/src/index/tests/hw_counter_test.rslib/sparse/src/index/tests/indexed_vs_plain_test.rslib/sparse/src/index/tests/search_context_tests.rs
💤 Files with no reviewable changes (3)
- lib/sparse/src/index/tests/search_context_tests.rs
- lib/sparse/src/index/tests/indexed_vs_plain_test.rs
- lib/sparse/src/index/tests/hw_counter_test.rs
✅ Files skipped from review due to trivial changes (3)
- lib/segment/benches/sparse_index_build.rs
- lib/sparse/benches/search.rs
- lib/segment/src/fixtures/sparse_fixtures.rs
🚧 Files skipped from review as they are similar to previous changes (5)
- lib/segment/src/segment_constructor/segment_constructor_base.rs
- lib/segment/src/segment_constructor/segment_builder.rs
- lib/segment/tests/integration/sparse_discover_test.rs
- lib/segment/tests/integration/sparse_vector_index_search_tests.rs
- lib/segment/src/segment/tests.rs
|
I am really on the fence regarding the duplication of the source of truth.
How bad is it to change the trait to pass the deferred_id there? |
It would be required to change in these 4 places: qdrant/lib/segment/src/index/hnsw_index/hnsw.rs Line 1405 in 6ddb81d I think it is a potential footgun to make it a parameter in the trait signature and then only apply it in one of the implementations. So if we'd add |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lib/segment/src/index/sparse_index/sparse_vector_index.rs (1)
705-706: Consider refining the comment placement.The comment "do not upsert empty or deferred vectors into the index" is slightly misleading at this location since deferred vectors have already been handled by the early return at lines 697-699. By the time execution reaches line 706, we know the vector is not deferred.
Consider either:
- Moving "or deferred" to a comment near the early return (lines 697-699), or
- Keeping this comment focused only on empty vectors.
Suggested clarification
if point_is_deferred { + // Deferred points are not indexed in the sparse inverted index return Ok(()); } let vector = SparseVector::try_from(new_vector)?; let old_vector: Option<SparseVector> = old_vector.map(SparseVector::try_from).transpose()?; - // do not upsert empty or deferred vectors into the index + // do not upsert empty vectors into the index if !vector.is_empty() {🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/segment/src/index/sparse_index/sparse_vector_index.rs` around lines 705 - 706, The comment above the empty-check on `vector` is misleading because deferred vectors are already handled by the earlier early-return that exits for deferred vectors; update comments so the `if !vector.is_empty()` comment only references empty vectors (e.g., "do not upsert empty vectors into the index"), and move the "deferred" wording to the comment immediately adjacent to the early-return that handles deferred vectors (the block that returns for deferred vectors) so each check's comment accurately describes that specific branch.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@lib/segment/src/index/sparse_index/sparse_vector_index.rs`:
- Around line 705-706: The comment above the empty-check on `vector` is
misleading because deferred vectors are already handled by the earlier
early-return that exits for deferred vectors; update comments so the `if
!vector.is_empty()` comment only references empty vectors (e.g., "do not upsert
empty vectors into the index"), and move the "deferred" wording to the comment
immediately adjacent to the early-return that handles deferred vectors (the
block that returns for deferred vectors) so each check's comment accurately
describes that specific branch.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: eb61bc78-f771-4c0e-91bd-2611c94c92fa
📒 Files selected for processing (1)
lib/segment/src/index/sparse_index/sparse_vector_index.rs
agourlay
left a comment
There was a problem hiding this comment.
Approving although I am still not a fan of the duplication issue here.
An alternative solution would be to push the deferred_id into the IdTracker so that it is central and accessible from other the sparse index and co.
We can tackle this larger refactoring in a future PR to unblock this one.
* Don't insert deferred points into sparse index # Conflicts: # lib/segment/src/segment/tests.rs # lib/segment/src/segment_constructor/segment_constructor_base.rs # Conflicts: # lib/segment/src/segment_constructor/segment_constructor_base.rs * Clippy * Assert consistency of deferred_internal_id var * Return before SparseVector conversion in case of deferred point * Use debug_assert instead
* Don't insert deferred points into sparse index # Conflicts: # lib/segment/src/segment/tests.rs # lib/segment/src/segment_constructor/segment_constructor_base.rs # Conflicts: # lib/segment/src/segment_constructor/segment_constructor_base.rs * Clippy * Assert consistency of deferred_internal_id var * Return before SparseVector conversion in case of deferred point * Use debug_assert instead
Depends on #8415
This PR reverts changes to sparse vector search from #8299 and instead of filtering, we now completely ignoring inserting deferred points into the sparse index. This reduces code complexity and potentially improves on search performance.
Design decision
To prevent passing
deferred_internal_idin theVectorIndextrait, theSparseVectorIndexstruct now holds a copy of that value.The disadvantage of this approach is having multiple source of truths and having to adjust the existing unit tests.
However, it greatly improves on the necessary changes and overall code style.
Potential issues
An inconsistency issue that must be considered here is the scenario of a user changing
prevent_unoptimizedand then restarting Qdrant. Since we don't persist the parameter, we will show all previously deferred points except sparse vectors, since they haven't been inserted into the index in the beginning. This means search operations could become inconsistent depending on which vector was used during search.The optimizer will deal with this inconsistency and create proper sparse indices after optimization. With other words, this inconsistency scenario will only be present for a limited amount of time.
Note
The changes in
lib/sparse/src/index/search_context.rshave been checked out to the latest commit before #8299. Therefore this file can be easily reviewed by file comparison.