Repository navigation
refactor: read only nullindex - #9019
Conversation
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@lib/segment/src/common/flags/read_only_roaring_flags.rs`:
- Around line 42-94: The compiler error occurs because
read_only_roaring_flags::open reads DynamicFlagsStatus and calls .len() on its
private field; fix by exposing the length via a public accessor on
DynamicFlagsStatus (e.g., add a pub fn len(&self) -> usize in
dynamic_stored_flags.rs) and use that accessor here (call
status_storage.read_whole()?[0].len()), or alternatively make the len field
pub/pub(super) in DynamicFlagsStatus and recompile; update references to use the
new public API so the code no longer tries to access a private field directly.
In `@lib/segment/src/common/flags/roaring_flags.rs`:
- Around line 37-43: iter_falses currently does an unchecked cast of self.len()
into PointOffsetType which can silently truncate large lengths; replace the raw
"as" cast with a checked conversion (e.g. use
PointOffsetType::try_from(self.len()) or usize::try_into()) and handle the Err
case explicitly (either return an empty iterator or panic with a clear message)
before creating the range and calling get_bitmap(); update the code in
iter_falses to use the checked value (len_u32) when building (0..len_u32) so no
silent truncation occurs.
In `@lib/segment/src/index/field_index/null_index/shared.rs`:
- Around line 34-36: The doc comment currently uses the misspelled word
"Implementors"; change it to "Implementers" in the comment that documents the
trait exposing `has_values_flags`, `is_null_flags`, `total_point_count`, and
`telemetry_index_type` (the comment block in shared.rs), so the line reads
"Implementers expose four required pieces ..." to satisfy codespell.
- Around line 128-149: The code casts has_values_flags.len(),
is_null_flags.len(), and total_point_count to PointOffsetType (u32) without
checking for overflow; add explicit checked conversions (e.g.,
PointOffsetType::try_from(...) or usize::try_into()) for the values used in the
range expressions in shared.rs so overflows are detected before casting, and
propagate/handle the error (return an Err or a safe fallback iterator) instead
of silently truncating; update the two range constructions that use
end..total_point_count as PointOffsetType and any uses of end = ... as
PointOffsetType to use the checked conversion and appropriate error handling.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: a36ba527-6ec2-4f5f-a2ab-ed6fcb9c7017
📒 Files selected for processing (12)
lib/segment/src/common/flags/dynamic_stored_flags.rslib/segment/src/common/flags/mod.rslib/segment/src/common/flags/read_only_roaring_flags.rslib/segment/src/common/flags/roaring_flags.rslib/segment/src/index/field_index/bool_index/mutable_bool_index.rslib/segment/src/index/field_index/field_index_base/field_index.rslib/segment/src/index/field_index/field_index_base/field_index_read_impl.rslib/segment/src/index/field_index/null_index/immutable_null_index.rslib/segment/src/index/field_index/null_index/mod.rslib/segment/src/index/field_index/null_index/mutable_null_index.rslib/segment/src/index/field_index/null_index/read_only_null_index.rslib/segment/src/index/field_index/null_index/shared.rs
| fn iter_falses(&self) -> Box<dyn Iterator<Item = PointOffsetType> + '_> { | ||
| // potential optimization: | ||
| // Create custom iterator which leverages bitmap's iterator for knowing ranges where the flags are false. | ||
| // This will help by not checking the bitmap for indices that are already known to be false. | ||
| let len = self.len() as PointOffsetType; | ||
| let bitmap = self.get_bitmap(); | ||
| Box::new((0..len).filter(move |i| !bitmap.contains(*i))) |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
# First, read the specific lines mentioned in the review
cat -n lib/segment/src/common/flags/roaring_flags.rs | sed -n '35,45p'Repository: qdrant/qdrant
Length of output: 640
🏁 Script executed:
# Find the definition of PointOffsetType
rg "type PointOffsetType|PointOffsetType\s*=" --type rs -A 2 | head -30Repository: qdrant/qdrant
Length of output: 84
🏁 Script executed:
# Check if there's validation of length before the cast
rg "iter_falses" -B 5 -A 15 lib/segment/src/common/flags/roaring_flags.rsRepository: qdrant/qdrant
Length of output: 1992
🏁 Script executed:
# Find PointOffsetType definition
rg "type PointOffsetType" --multiline-dotall -A 2 | head -30Repository: qdrant/qdrant
Length of output: 279
🏁 Script executed:
# Also check in common module
rg "PointOffsetType" lib/segment/src/ -l | head -5Repository: qdrant/qdrant
Length of output: 417
🏁 Script executed:
# Look at the whole file to understand context better
head -100 lib/segment/src/common/flags/roaring_flags.rsRepository: qdrant/qdrant
Length of output: 3292
🏁 Script executed:
# Search for constraints on segment/point count or PointOffsetType limits
rg "u32::MAX|PointOffsetType::MAX|MAX_POINTS|POINT_OFFSET" lib/segment/src/ | head -20Repository: qdrant/qdrant
Length of output: 2184
🏁 Script executed:
# Check how RoaringBitmap is constructed and if there are constraints
rg "RoaringBitmap::new|insert|len" lib/segment/src/common/flags/ -B 2 -A 2 | head -40Repository: qdrant/qdrant
Length of output: 2753
🏁 Script executed:
# Look for any segment size limits or validation
rg "max.*point|point.*max|segment.*size" lib/segment/src/ -i | head -20Repository: qdrant/qdrant
Length of output: 2197
Guard the usize → PointOffsetType cast in iter_falses.
Line 41 narrows self.len() with an unchecked as cast. If the roaring flags length ever exceeds PointOffsetType::MAX (u32::MAX), this silently truncates the iterator range and skips false entries. The codebase demonstrates awareness of this boundary in id_tracker/compressed/versions_store.rs where it explicitly validates the u32::MAX constraint.
Suggested fix
- let len = self.len() as PointOffsetType;
+ let len = PointOffsetType::try_from(self.len())
+ .expect("roaring flags length must fit into PointOffsetType");🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@lib/segment/src/common/flags/roaring_flags.rs` around lines 37 - 43,
iter_falses currently does an unchecked cast of self.len() into PointOffsetType
which can silently truncate large lengths; replace the raw "as" cast with a
checked conversion (e.g. use PointOffsetType::try_from(self.len()) or
usize::try_into()) and handle the Err case explicitly (either return an empty
iterator or panic with a clear message) before creating the range and calling
get_bitmap(); update the code in iter_falses to use the checked value (len_u32)
when building (0..len_u32) so no silent truncation occurs.
| let result: Option<Box<dyn Iterator<Item = PointOffsetType> + 'a>> = | ||
| if let Some(is_empty) = is_empty { | ||
| if *is_empty { | ||
| // Return points that don't have values | ||
| Some(Box::new(has_values_flags.iter_falses().chain({ | ||
| let end = has_values_flags.len() as PointOffsetType; | ||
| end..total_point_count as u32 | ||
| }))) | ||
| } else { | ||
| // Return points that have values | ||
| Some(Box::new(has_values_flags.iter_trues())) | ||
| } | ||
| } else if let Some(is_null) = is_null { | ||
| if *is_null { | ||
| // Return points that have null values | ||
| Some(Box::new(is_null_flags.iter_trues())) | ||
| } else { | ||
| // Return points that don't have null values | ||
| Some(Box::new(is_null_flags.iter_falses().chain({ | ||
| let end = is_null_flags.len() as PointOffsetType; | ||
| end..total_point_count as u32 | ||
| }))) |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
# First, let's look at the actual file to verify the code matches the snippet
cat -n lib/segment/src/index/field_index/null_index/shared.rs | sed -n '120,155p'Repository: qdrant/qdrant
Length of output: 1625
🏁 Script executed:
# Find PointOffsetType definition
rg "type PointOffsetType|PointOffsetType\s*=" --type rs -A 2 | head -20Repository: qdrant/qdrant
Length of output: 84
🏁 Script executed:
# Check imports and context of the file
head -50 lib/segment/src/index/field_index/null_index/shared.rsRepository: qdrant/qdrant
Length of output: 2274
🏁 Script executed:
# Find PointOffsetType definition
find . -name "*.rs" -type f | xargs grep -l "type PointOffsetType\|pub.*PointOffsetType" | head -5Repository: qdrant/qdrant
Length of output: 317
🏁 Script executed:
# Search for PointOffsetType in common module
rg "type PointOffsetType|PointOffsetType\s*=" --max-count 10Repository: qdrant/qdrant
Length of output: 1067
🏁 Script executed:
# Check if there are other similar patterns with try_from or checked conversions
rg "try_from.*PointOffsetType|PointOffsetType.*try_from" -A 2 -B 2 | head -40Repository: qdrant/qdrant
Length of output: 968
🏁 Script executed:
# Check how other parts of code handle usize to u32 conversions
rg "as PointOffsetType|as u32" lib/segment/src/index/field_index/ -B 2 -A 2 | head -50Repository: qdrant/qdrant
Length of output: 4417
🏁 Script executed:
# Check if there are any comments or constraints about maximum point count
rg "PointOffsetType::MAX|u32::MAX" -B 2 -A 2 | head -40Repository: qdrant/qdrant
Length of output: 3669
🏁 Script executed:
# Check documentation/comments about segment size constraints
rg "segment.*size|max.*point|point.*count" lib/segment/src/ -i -B 1 | grep -A 2 -B 2 "u32\|PointOffsetType" | head -50Repository: qdrant/qdrant
Length of output: 3843
🏁 Script executed:
# Check if there are invariants or assertions about point count fitting in u32
rg "assert.*point.*count|unwrap|expect.*point" lib/segment/src/index/field_index/ -B 2 -A 1 | head -40Repository: qdrant/qdrant
Length of output: 3382
🏁 Script executed:
# Look at the context of how total_point_count is set in NullIndexRead implementations
rg "total_point_count\(\)" lib/segment/src/index/field_index/null_index/ -B 5 -A 2Repository: qdrant/qdrant
Length of output: 2843
🏁 Script executed:
# Check if there are any constraints or documentation about maximum segment size
rg "segment.*capacity|max.*segment|segment.*limit" lib/collection/src/ -i -B 2 -A 2 | head -50Repository: qdrant/qdrant
Length of output: 4519
🏁 Script executed:
# Look at how segments are created and if there are size limits
rg "create.*segment|new.*segment" lib/collection/src/ -B 2 -A 3 -i | head -60Repository: qdrant/qdrant
Length of output: 4737
🏁 Script executed:
# Check if there's documentation about PointOffsetType being u32 and any guarantees
rg "PointOffsetType|u32" lib/common/common/src/types.rs -B 5 -A 5Repository: qdrant/qdrant
Length of output: 962
🏁 Script executed:
# Check if there's more context about segment size constraints
rg "max_segment_size|segment.*capacity" lib/collection/src/config.rs -B 3 -A 3Repository: qdrant/qdrant
Length of output: 327
🏁 Script executed:
# Look for similar range construction patterns with try_from or checked conversions elsewhere
rg "\.\..*as (u32|PointOffsetType)" lib/segment/src/index/field_index/ -B 2 -A 1Repository: qdrant/qdrant
Length of output: 4498
Add bounds checking before casting usize to PointOffsetType.
Lines 133, 147-148 cast to u32 without validation. Silent truncation occurs if has_values_flags.len() or total_point_count exceeds u32::MAX, causing the range to incorrectly truncate the tail of the segment.
Suggested fix
+fn to_point_offset(value: usize, what: &'static str) -> PointOffsetType {
+ PointOffsetType::try_from(value).expect(what)
+}
+
pub(super) fn filter<'a, N: NullIndexRead>(
idx: &'a N,
condition: &'a FieldCondition,
) -> OperationResult<Option<Box<dyn Iterator<Item = PointOffsetType> + 'a>>> {
@@
// Return points that don't have values
Some(Box::new(has_values_flags.iter_falses().chain({
- let end = has_values_flags.len() as PointOffsetType;
- end..total_point_count as u32
+ let end = to_point_offset(
+ has_values_flags.len(),
+ "has_values length must fit into PointOffsetType",
+ );
+ end..to_point_offset(
+ total_point_count,
+ "total_point_count must fit into PointOffsetType",
+ )
})))
@@
// Return points that don't have null values
Some(Box::new(is_null_flags.iter_falses().chain({
- let end = is_null_flags.len() as PointOffsetType;
- end..total_point_count as u32
+ let end = to_point_offset(
+ is_null_flags.len(),
+ "is_null length must fit into PointOffsetType",
+ );
+ end..to_point_offset(
+ total_point_count,
+ "total_point_count must fit into PointOffsetType",
+ )
})))📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| let result: Option<Box<dyn Iterator<Item = PointOffsetType> + 'a>> = | |
| if let Some(is_empty) = is_empty { | |
| if *is_empty { | |
| // Return points that don't have values | |
| Some(Box::new(has_values_flags.iter_falses().chain({ | |
| let end = has_values_flags.len() as PointOffsetType; | |
| end..total_point_count as u32 | |
| }))) | |
| } else { | |
| // Return points that have values | |
| Some(Box::new(has_values_flags.iter_trues())) | |
| } | |
| } else if let Some(is_null) = is_null { | |
| if *is_null { | |
| // Return points that have null values | |
| Some(Box::new(is_null_flags.iter_trues())) | |
| } else { | |
| // Return points that don't have null values | |
| Some(Box::new(is_null_flags.iter_falses().chain({ | |
| let end = is_null_flags.len() as PointOffsetType; | |
| end..total_point_count as u32 | |
| }))) | |
| fn to_point_offset(value: usize, what: &'static str) -> PointOffsetType { | |
| PointOffsetType::try_from(value).expect(what) | |
| } | |
| pub(super) fn filter<'a, N: NullIndexRead>( | |
| idx: &'a N, | |
| condition: &'a FieldCondition, | |
| ) -> OperationResult<Option<Box<dyn Iterator<Item = PointOffsetType> + 'a>>> { | |
| let result: Option<Box<dyn Iterator<Item = PointOffsetType> + 'a>> = | |
| if let Some(is_empty) = is_empty { | |
| if *is_empty { | |
| // Return points that don't have values | |
| Some(Box::new(has_values_flags.iter_falses().chain({ | |
| let end = to_point_offset( | |
| has_values_flags.len(), | |
| "has_values length must fit into PointOffsetType", | |
| ); | |
| end..to_point_offset( | |
| total_point_count, | |
| "total_point_count must fit into PointOffsetType", | |
| ) | |
| }))) | |
| } else { | |
| // Return points that have values | |
| Some(Box::new(has_values_flags.iter_trues())) | |
| } | |
| } else if let Some(is_null) = is_null { | |
| if *is_null { | |
| // Return points that have null values | |
| Some(Box::new(is_null_flags.iter_trues())) | |
| } else { | |
| // Return points that don't have null values | |
| Some(Box::new(is_null_flags.iter_falses().chain({ | |
| let end = to_point_offset( | |
| is_null_flags.len(), | |
| "is_null length must fit into PointOffsetType", | |
| ); | |
| end..to_point_offset( | |
| total_point_count, | |
| "total_point_count must fit into PointOffsetType", | |
| ) | |
| }))) | |
| } | |
| } else { | |
| None | |
| }; |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@lib/segment/src/index/field_index/null_index/shared.rs` around lines 128 -
149, The code casts has_values_flags.len(), is_null_flags.len(), and
total_point_count to PointOffsetType (u32) without checking for overflow; add
explicit checked conversions (e.g., PointOffsetType::try_from(...) or
usize::try_into()) for the values used in the range expressions in shared.rs so
overflows are detected before casting, and propagate/handle the error (return an
Err or a safe fallback iterator) instead of silently truncating; update the two
range constructions that use end..total_point_count as PointOffsetType and any
uses of end = ... as PointOffsetType to use the checked conversion and
appropriate error handling.
📝 WalkthroughWalkthroughAdds RoaringFlagsRead and NullIndexRead traits to centralize read-only flag and null-index APIs, exposes a few dynamic-stored flag constants/helpers as pub(super), implements ReadOnlyRoaringFlags and ReadOnlyNullIndex that load persisted roaring bitmaps into memory, moves shared null-index query logic into read_ops free functions (filter, estimate_cardinality, condition_checker), and refactors mutable/immutable null index types to implement and delegate through the new read traits. Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 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.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@lib/segment/src/index/field_index/null_index/read_ops.rs`:
- Around line 132-133: The range creation uses unchecked casts from usize to
PointOffsetType (u32) for values like has_values_flags.len(),
is_null_flags.len(), and total_point_count which can truncate; before
constructing ranges in read_ops.rs (the code that computes end =
has_values_flags.len() as PointOffsetType and similar lines), validate that each
usize value is <= PointOffsetType::MAX and return or propagate an error (or
panic with a clear message) if it isn't, otherwise safely cast to
PointOffsetType; reference the has_values_flags, is_null_flags,
total_point_count variables and the PointOffsetType type when adding the guard.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 1dd296a6-5f5e-44d8-8187-1de4577d2ff3
📒 Files selected for processing (3)
lib/segment/src/common/flags/read_only_roaring_flags.rslib/segment/src/index/field_index/null_index/read_only_null_index.rslib/segment/src/index/field_index/null_index/read_ops.rs
| let end = has_values_flags.len() as PointOffsetType; | ||
| end..total_point_count as u32 |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Verify where usize->PointOffsetType/u32 casts are used and whether bounds are asserted nearby.
rg -nP 'as\s+PointOffsetType|as\s+u32' lib/segment/src/index/field_index/null_index/read_ops.rs
rg -nP 'total_point_count|PointOffsetType::MAX|u32::MAX|try_from\(' lib/segment/src/index/field_index/null_index/*.rsRepository: qdrant/qdrant
Length of output: 4767
🏁 Script executed:
#!/bin/bash
# Get the definition of PointOffsetType
rg -n 'type PointOffsetType|PointOffsetType =' lib/segment/src/ | head -20
# Get full context of the filter function
sed -n '108,155p' lib/segment/src/index/field_index/null_index/read_ops.rs
# Check if there are any guards or assertions around total_point_count
sed -n '120,135p' lib/segment/src/index/field_index/null_index/read_ops.rs
sed -n '140,150p' lib/segment/src/index/field_index/null_index/read_ops.rsRepository: qdrant/qdrant
Length of output: 3424
🏁 Script executed:
#!/bin/bash
# Search for PointOffsetType type definition in the entire codebase
rg -n 'type PointOffsetType|^pub.*type PointOffsetType' --type rs lib/
# Also check imports to see where PointOffsetType comes from
rg -n 'use.*PointOffsetType|PointOffsetType' lib/segment/src/index/field_index/null_index/read_ops.rs | head -5Repository: qdrant/qdrant
Length of output: 396
🏁 Script executed:
#!/bin/bash
# Find PointOffsetType definition in common module
find . -type f -name "*.rs" | xargs grep -l 'type PointOffsetType' 2>/dev/null | head -5
# Direct search without -t flag
rg 'type PointOffsetType' --max-count 5Repository: qdrant/qdrant
Length of output: 153
🏁 Script executed:
#!/bin/bash
# Check if there are segment size limits documented or enforced
rg -i 'segment.*size|max.*point|point.*limit' lib/segment/src/ --max-count 10
# Check if total_point_count is ever validated
rg 'total_point_count.*>' lib/segment/src/index/field_index/null_index/Repository: qdrant/qdrant
Length of output: 19595
Guard usize → PointOffsetType casts before building iterator ranges.
Lines 132–133 and 146–147 cast usize values to PointOffsetType (which is u32) without bounds checking. If has_values_flags.len(), is_null_flags.len(), or total_point_count exceed u32::MAX, the casts silently truncate, returning incorrect point IDs.
Proposed fix
- let end = has_values_flags.len() as PointOffsetType;
- end..total_point_count as u32
+ let end = PointOffsetType::try_from(has_values_flags.len())
+ .expect("has_values_flags length exceeds PointOffsetType::MAX");
+ let total = PointOffsetType::try_from(total_point_count)
+ .expect("total_point_count exceeds PointOffsetType::MAX");
+ end..total
@@
- let end = is_null_flags.len() as PointOffsetType;
- end..total_point_count as u32
+ let end = PointOffsetType::try_from(is_null_flags.len())
+ .expect("is_null_flags length exceeds PointOffsetType::MAX");
+ let total = PointOffsetType::try_from(total_point_count)
+ .expect("total_point_count exceeds PointOffsetType::MAX");
+ end..total📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| let end = has_values_flags.len() as PointOffsetType; | |
| end..total_point_count as u32 | |
| let end = PointOffsetType::try_from(has_values_flags.len()) | |
| .expect("has_values_flags length exceeds PointOffsetType::MAX"); | |
| let total = PointOffsetType::try_from(total_point_count) | |
| .expect("total_point_count exceeds PointOffsetType::MAX"); | |
| end..total |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@lib/segment/src/index/field_index/null_index/read_ops.rs` around lines 132 -
133, The range creation uses unchecked casts from usize to PointOffsetType (u32)
for values like has_values_flags.len(), is_null_flags.len(), and
total_point_count which can truncate; before constructing ranges in read_ops.rs
(the code that computes end = has_values_flags.len() as PointOffsetType and
similar lines), validate that each usize value is <= PointOffsetType::MAX and
return or propagate an error (or panic with a clear message) if it isn't,
otherwise safely cast to PointOffsetType; reference the has_values_flags,
is_null_flags, total_point_count variables and the PointOffsetType type when
adding the guard.
* feat: add read only null index * fix: linter * chore: remove unused impl * chore: revert impl deletion * fix: linter errors and rename shared->read_ops * fix: linter * review fixes --------- Co-authored-by: generall <[email protected]>
All Submissions:
devbranch (notmaster) and my branch was created fromdev.New Feature Submissions:
cargo +nightly fmt --allcommand prior to submission?cargo clippy --workspace --all-featurescommand?Changes to Core Features: