-
Notifications
You must be signed in to change notification settings - Fork 696
feat: support merging zonemap index segments #7128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Xuanwo
wants to merge
9
commits into
main
Choose a base branch
from
xuanwo/zonemap-segment-merge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
868463b
feat: support merging zonemap index segments
Xuanwo ec6bfaa
fix: drop retired fragments when merging zonemap segments
Xuanwo d43ec04
refactor: simplify zonemap segment merge
Xuanwo 31c82c3
fix: handle retired fragments when replacing index segments
Xuanwo 3905198
test: keep index replacement orphan test on live fragments
Xuanwo 6342051
Merge branch 'main' into xuanwo/zonemap-segment-merge
Xuanwo 14e4629
Merge remote-tracking branch 'origin/main' into xuanwo/zonemap-segmen…
Xuanwo eb0e558
Merge remote-tracking branch 'origin/xuanwo/zonemap-segment-merge' in…
Xuanwo a84745e
fix: remove fully retired index segments on replacement
Xuanwo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright The Lance Authors | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use lance_index::metrics::NoOpMetricsCollector; | ||
| use lance_index::scalar::lance_format::LanceIndexStore; | ||
| use lance_index::scalar::zonemap::ZoneMapIndex; | ||
| use lance_table::format::IndexMetadata; | ||
| use roaring::RoaringBitmap; | ||
| use uuid::Uuid; | ||
|
|
||
| use crate::{Dataset, Error, Result, dataset::index::LanceIndexStoreExt}; | ||
|
|
||
| /// Merge one caller-defined group of source ZoneMap segments into a single segment. | ||
| pub(in crate::index) async fn merge_segments( | ||
| dataset: &Dataset, | ||
| segments: Vec<IndexMetadata>, | ||
| ) -> Result<IndexMetadata> { | ||
| if segments.is_empty() { | ||
| return Err(Error::index("No segment metadata was provided".to_string())); | ||
| } | ||
|
|
||
| let field_id = *segments[0].fields.first().ok_or_else(|| { | ||
| Error::invalid_input(format!( | ||
| "CreateIndex: segment {} is missing field ids", | ||
| segments[0].uuid | ||
| )) | ||
| })?; | ||
| let field_path = dataset.schema().field_path(field_id)?; | ||
|
|
||
| let mut scalar_indices = Vec::with_capacity(segments.len()); | ||
| let mut fragment_bitmap = RoaringBitmap::new(); | ||
| let dataset_fragments = dataset.fragment_bitmap.as_ref(); | ||
| for segment in &segments { | ||
| let effective = segment | ||
| .effective_fragment_bitmap(dataset_fragments) | ||
| .ok_or_else(|| { | ||
| Error::invalid_input(format!( | ||
| "CreateIndex: segment {} is missing fragment coverage", | ||
| segment.uuid | ||
| )) | ||
| })?; | ||
| fragment_bitmap |= effective; | ||
| let scalar_index = | ||
| super::open_scalar_index(dataset, &field_path, segment, &NoOpMetricsCollector).await?; | ||
| scalar_indices.push((segment.uuid, scalar_index)); | ||
| } | ||
|
|
||
| let mut source_indices = Vec::with_capacity(scalar_indices.len()); | ||
| for (segment_uuid, scalar_index) in &scalar_indices { | ||
| let zonemap_index = scalar_index | ||
| .as_any() | ||
| .downcast_ref::<ZoneMapIndex>() | ||
| .ok_or_else(|| { | ||
| Error::index(format!( | ||
| "merge_existing_index_segments: expected zonemap segment {}, got {:?}", | ||
| segment_uuid, | ||
| scalar_index.index_type() | ||
| )) | ||
| })?; | ||
| source_indices.push(zonemap_index); | ||
| } | ||
|
|
||
| let new_uuid = Uuid::new_v4(); | ||
| let new_store = LanceIndexStore::from_dataset_for_new(dataset, &new_uuid.to_string())?; | ||
| let created_index = lance_index::scalar::zonemap::merge_zonemap_indices( | ||
| &source_indices, | ||
| &new_store, | ||
| &fragment_bitmap, | ||
| ) | ||
| .await?; | ||
|
|
||
| Ok(IndexMetadata { | ||
| uuid: new_uuid, | ||
| fields: vec![field_id], | ||
| dataset_version: dataset.manifest.version, | ||
| fragment_bitmap: Some(fragment_bitmap), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a merge-then-commit regression test here? Since the merged segment keeps only live fragment coverage, committing it under the same index name can compare against the old segment's original coverage and report retired fragments as orphaned. |
||
| index_details: Some(Arc::new(created_index.index_details)), | ||
| index_version: created_index.index_version as i32, | ||
| created_at: Some(chrono::Utc::now()), | ||
| base_id: None, | ||
| files: created_index.files, | ||
| ..segments[0].clone() | ||
| }) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential issue: fully retired existing segments can reach this disjoint branch and be kept instead of removed. That leaves old zonemap segments referenced in the manifest after a merge, so the merge may not actually replace multiple physical segments with one.