Repository navigation
Add reproducing test for spurious geo index warning on duplicate geo values - #8404
Conversation
…values When a point has multiple geo values that produce the same max-precision geohash (e.g. duplicate coordinates in a multi-value geo field), `InMemoryGeoMapIndex::remove_point` logs a spurious warning: "Geo index error: no points for hash X was found". The root cause is an asymmetry between `point_to_values` (stores all values including duplicates) and `points_map` (uses a HashSet, so duplicates are collapsed). During removal the loop processes each value individually — the first iteration removes the `points_map` entry, and subsequent iterations for the same hash can't find it. This commit adds: - A `debug_assert!` in `remove_point` (matching the existing pattern in `decrement_hash_value_counts`) to make the issue detectable in tests. - `test_remove_point_with_duplicate_geo_values` — reproduces the bug by adding a point with `[BERLIN, BERLIN]` then removing it. - `test_frequent_add_remove_geo_points` — exercises repeated add/remove cycles to cover the user-reported scenario. Made-with: Cursor
📝 WalkthroughWalkthroughReplaces the removed-geo-hash storage from a Vec to an AHashSet to deduplicate hashes during removal and adjust related call sites and helper signature to accept an iterator (impl IntoIterator<Item = GeoHash>). Removal now skips duplicate hashes early and uses debug_assert when a hash is missing from the points map instead of emitting a warning; behavior still returns false for is_last in that branch. Adds two new tests exercising removal of points with duplicate geo values and repeated add/remove cycles to validate multi-value geo handling and index stability. Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
📝 Coding Plan
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 Tip CodeRabbit can use OpenGrep to find security vulnerabilities and bugs across 17+ programming languages.OpenGrep is compatible with Semgrep configurations. Add an |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@lib/segment/src/index/field_index/geo_index/mutable_geo_index.rs`:
- Around line 503-508: The remove_point path in mutable_geo_index::remove_point
currently unconditionally removes the geohash entry from points_map and triggers
debug_assert! when a duplicate value with the same geohash is processed; change
remove_point so it maintains per-value counts and only removes the points_map
entry for a given geohash when the last stored value for that geohash is
removed. Concretely, in remove_point (and any helper like decrement/remove
logic), look up the entry in points_map using the Entry API or get_mut,
decrement the counter for the specific value (or remove the value key) and only
remove the entire geohash key from points_map if the inner map/set becomes
empty; remove the unconditional debug_assert! that assumed the geohash must
exist on second duplicate removal and instead handle the absent-entry case
gracefully (no panic) while preserving correct per-value count semantics.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 3572e81c-337f-4cb7-98c7-8ae2a1396415
📒 Files selected for processing (2)
lib/segment/src/index/field_index/geo_index/mod.rslib/segment/src/index/field_index/geo_index/mutable_geo_index.rs
|
I've had Claude spending quite a bit of time on this. Here is its assessment of the current PR: |
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/index/field_index/geo_index/mutable_geo_index.rs (1)
497-521:⚠️ Potential issue | 🔴 Critical
AHashSet::insert()returnstruefor newly inserted values, making the condition logic inverted.At Line 497,
insert()returnstruewhen the hash is first seen, but the code continues (skips) ontrue. This causes the first occurrence to bypasspoints_mapremoval anddecrement_hash_value_counts(), leaving stale entries and incorrect counts. Only duplicates are processed, which is backwards.Rename to
is_new_hashfor clarity and move the processing block insideif is_new_hashto handle only the first occurrence. Always calldecrement_hash_value_counts()for every removed value (including duplicates).Proposed fix
- let already_seen = removed_geo_hashes.insert(removed_geo_hash); - - if already_seen { - continue; - } - - let is_last = if let Some(hash_ids) = self.points_map.get_mut(&removed_geo_hash) { - hash_ids.remove(&idx); - hash_ids.is_empty() - } else { - debug_assert!( - false, - "Geo index error: no points for hash {removed_geo_hash} was found", - ); - false - }; - - if is_last { - self.points_map.remove(&removed_geo_hash); - } - - self.decrement_hash_value_counts(&removed_geo_hash); + let is_new_hash = removed_geo_hashes.insert(removed_geo_hash); + + if is_new_hash { + let is_last = if let Some(hash_ids) = self.points_map.get_mut(&removed_geo_hash) { + hash_ids.remove(&idx); + hash_ids.is_empty() + } else { + debug_assert!( + false, + "Geo index error: no points for hash {removed_geo_hash} was found", + ); + false + }; + + if is_last { + self.points_map.remove(&removed_geo_hash); + } + } + + self.decrement_hash_value_counts(&removed_geo_hash);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/segment/src/index/field_index/geo_index/mutable_geo_index.rs` around lines 497 - 521, The logic using removed_geo_hashes.insert(...) is inverted: AHashSet::insert returns true for a newly-seen hash, so rename the boolean to is_new_hash = removed_geo_hashes.insert(removed_geo_hash) and move the points_map removal/cleanup (the block that gets &mut hash_ids, removes idx, checks is_empty and possibly self.points_map.remove) inside if is_new_hash so the first occurrence does the map cleanup; additionally ensure self.decrement_hash_value_counts(&removed_geo_hash) is called for every removed value (keep it outside the if so duplicates also decrement), and leave the final self.decrement_hash_point_counts(removed_geo_hashes) call as-is.
🤖 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/index/field_index/geo_index/mutable_geo_index.rs`:
- Around line 497-521: The logic using removed_geo_hashes.insert(...) is
inverted: AHashSet::insert returns true for a newly-seen hash, so rename the
boolean to is_new_hash = removed_geo_hashes.insert(removed_geo_hash) and move
the points_map removal/cleanup (the block that gets &mut hash_ids, removes idx,
checks is_empty and possibly self.points_map.remove) inside if is_new_hash so
the first occurrence does the map cleanup; additionally ensure
self.decrement_hash_value_counts(&removed_geo_hash) is called for every removed
value (keep it outside the if so duplicates also decrement), and leave the final
self.decrement_hash_point_counts(removed_geo_hashes) call as-is.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 32f298b7-313c-44d1-b39c-80de4f0c0240
📒 Files selected for processing (1)
lib/segment/src/index/field_index/geo_index/mutable_geo_index.rs
|
@robcaulk should fix now. |
…values (#8404) * Add reproducing test for spurious geo index warning on duplicate geo values When a point has multiple geo values that produce the same max-precision geohash (e.g. duplicate coordinates in a multi-value geo field), `InMemoryGeoMapIndex::remove_point` logs a spurious warning: "Geo index error: no points for hash X was found". The root cause is an asymmetry between `point_to_values` (stores all values including duplicates) and `points_map` (uses a HashSet, so duplicates are collapsed). During removal the loop processes each value individually — the first iteration removes the `points_map` entry, and subsequent iterations for the same hash can't find it. This commit adds: - A `debug_assert!` in `remove_point` (matching the existing pattern in `decrement_hash_value_counts`) to make the issue detectable in tests. - `test_remove_point_with_duplicate_geo_values` — reproduces the bug by adding a point with `[BERLIN, BERLIN]` then removing it. - `test_frequent_add_remove_geo_points` — exercises repeated add/remove cycles to cover the user-reported scenario. Made-with: Cursor * fix: redulplicate hash IDs, replace warn with debug assertion * fmt --------- Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: generall <[email protected]>
…values (#8404) * Add reproducing test for spurious geo index warning on duplicate geo values When a point has multiple geo values that produce the same max-precision geohash (e.g. duplicate coordinates in a multi-value geo field), `InMemoryGeoMapIndex::remove_point` logs a spurious warning: "Geo index error: no points for hash X was found". The root cause is an asymmetry between `point_to_values` (stores all values including duplicates) and `points_map` (uses a HashSet, so duplicates are collapsed). During removal the loop processes each value individually — the first iteration removes the `points_map` entry, and subsequent iterations for the same hash can't find it. This commit adds: - A `debug_assert!` in `remove_point` (matching the existing pattern in `decrement_hash_value_counts`) to make the issue detectable in tests. - `test_remove_point_with_duplicate_geo_values` — reproduces the bug by adding a point with `[BERLIN, BERLIN]` then removing it. - `test_frequent_add_remove_geo_points` — exercises repeated add/remove cycles to cover the user-reported scenario. Made-with: Cursor * fix: redulplicate hash IDs, replace warn with debug assertion * fmt --------- Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: generall <[email protected]>
Summary
InMemoryGeoMapIndex::remove_pointlogs spurious warnings:"Geo index error: no points for hash X was found"when a point has multiple geo values that produce the same max-precision geohash (e.g. duplicate coordinates in a multi-value geo field).point_to_valuesstores all values including duplicates, butpoints_mapuses aHashSetso duplicates are collapsed into one entry. During removal the loop processes each value individually — the first iteration removes thepoints_mapentry, and the second iteration for the same hash can't find it.debug_assert!inremove_point(matching the existing pattern indecrement_hash_value_counts) so the issue is caught in debug/test builds.test_remove_point_with_duplicate_geo_values— directly reproduces the bug (currently fails via thedebug_assert!).test_frequent_add_remove_geo_points— exercises repeated add/remove cycles to cover the user-reported scenario (passes).Test plan
test_remove_point_with_duplicate_geo_valuesreproduces the bug (panics ondebug_assert!)test_frequent_add_remove_geo_pointspassesFix for code: