Repository navigation
Fix immutable map index zero count - #8890
Conversation
… load When MmapMapIndex::open ORs the id-tracker's runtime deletion bitvec into the on-disk one at open time, ImmutableMapIndex::open_mmap could insert a zero-count entry into value_to_points for any value whose live points were all deleted, then immediately trip its own post-build sort assert. Skip such values, mirroring the runtime invariant maintained by remove_idx_from_value_list which already removes entries when their count drops to zero. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Builds an immutable segment with all 8 PayloadSchemaType variants indexed, snapshots every byte under payload_index/, then asserts byte-for-byte equality plus per-field query correctness after delete_point, flush, drop+reload, and a second deletes+flush on the reloaded segment. Each query exercises a different read path of an immutable index variant: map exact-match (keyword/uuid/integer), numeric range (float), datetime range, geo bounding box, full-text token match, bool match. Reproduces the regression fixed in the previous commit. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
📝 WalkthroughWalkthroughThis pull request modifies Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. 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. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.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/test_immutable_payload_index_files.rs (1)
773-774:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winUse checked conversions for
PointOffsetTypehere.
n_vectors as PointOffsetTypeandi as u32 - 1assume these values always fit in the offset type, but that bound is not enforced. A future test expansion could wrap silently.Suggested fix
- let deferred_internal_id = (n_deferred > 0).then_some(n_vectors as PointOffsetType); + let deferred_internal_id = if n_deferred > 0 { + Some(u32::try_from(n_vectors).expect("n_vectors fits in PointOffsetType")) + } else { + None + };- .with_view(|v| v.payload_by_offset(i as u32 - 1, &hw_counter)) + .with_view(|v| { + v.payload_by_offset( + u32::try_from(i - 1).expect("internal id fits in PointOffsetType"), + &hw_counter, + ) + })Based on learnings, in code that computes offsets or casts counts to PointOffsetType (u32), avoid unsafe or unchecked casts from usize to PointOffsetType.
Also applies to: 923-941
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/segment/src/segment/tests/test_immutable_payload_index_files.rs` around lines 773 - 774, Replace unchecked casts like `n_vectors as PointOffsetType` and `i as u32 - 1` with checked conversions using TryFrom/TryInto for PointOffsetType (or use usize::try_into()) and handle the Result (propagate or panic with a clear message). Locate the occurrences around the test function that uses the `n_vectors` variable and the loop index `i` (and the similar casts between lines ~923-941) and change them to use `PointOffsetType::try_from(n_vectors)` / `n_vectors.try_into()` and `PointOffsetType::try_from(i - 1)` (or `usize::try_into()`), unwrapping or returning an error with a descriptive message if the conversion fails so the test fails loudly instead of silently wrapping.
🤖 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/test_immutable_payload_index_files.rs`:
- Around line 773-774: Replace unchecked casts like `n_vectors as
PointOffsetType` and `i as u32 - 1` with checked conversions using
TryFrom/TryInto for PointOffsetType (or use usize::try_into()) and handle the
Result (propagate or panic with a clear message). Locate the occurrences around
the test function that uses the `n_vectors` variable and the loop index `i` (and
the similar casts between lines ~923-941) and change them to use
`PointOffsetType::try_from(n_vectors)` / `n_vectors.try_into()` and
`PointOffsetType::try_from(i - 1)` (or `usize::try_into()`), unwrapping or
returning an error with a descriptive message if the conversion fails so the
test fails loudly instead of silently wrapping.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 9d4d9327-29df-419f-abe9-d9712b2ad425
📒 Files selected for processing (3)
lib/segment/src/index/field_index/map_index/immutable_map_index.rslib/segment/src/segment/tests/mod.rslib/segment/src/segment/tests/test_immutable_payload_index_files.rs
* fix(segment): immutable map index skips values with no live points on load When MmapMapIndex::open ORs the id-tracker's runtime deletion bitvec into the on-disk one at open time, ImmutableMapIndex::open_mmap could insert a zero-count entry into value_to_points for any value whose live points were all deleted, then immediately trip its own post-build sort assert. Skip such values, mirroring the runtime invariant maintained by remove_idx_from_value_list which already removes entries when their count drops to zero. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * test(segment): file-system immutability test for payload indices Builds an immutable segment with all 8 PayloadSchemaType variants indexed, snapshots every byte under payload_index/, then asserts byte-for-byte equality plus per-field query correctness after delete_point, flush, drop+reload, and a second deletes+flush on the reloaded segment. Each query exercises a different read path of an immutable index variant: map exact-match (keyword/uuid/integer), numeric range (float), datetime range, geo bounding box, full-text token match, bool match. Reproduces the regression fixed in the previous commit. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
This PR fixes
ImmutableMapIndex::open_mmapso that it no longer inserts zero-count value_to_points entries for values whose live points are all deleted at open time.The constructor now matches the runtime invariant maintained by
remove_idx_from_value_list(entries with count == 0 don't exist), preventing a debug_assert! panic on the post-build sort loop after a delete-flush-reload cycle.A new payload_index_files_are_immutable_after_build covering all 8 PayloadSchemaType variants (every immutable index variant) as been introduced.
It snapshots every byte under payload_index/ after build, then asserts byte equality + per-field query correctness through delete_point, flush, drop+reload, and another deletes+flush.
Repro (without the fix)
Build an immutable map index with a value held by a single point, delete that point, flush, drop, reload → panic at immutable_map_index.rs:119 reading value not found in value_to_points.
Triggered when
MmapMapIndex::openORs the id-tracker's runtime deletion bitvec into its own at open time and the constructor naively inserts an entry whose live count is now zero.Test plan