Skip to content

feat: strategized plan compaction#5233

Merged
wjones127 merged 9 commits intolance-format:mainfrom
zhangyue19921010:feat_strategized_plan_compaction_v2
Dec 9, 2025
Merged

feat: strategized plan compaction#5233
wjones127 merged 9 commits intolance-format:mainfrom
zhangyue19921010:feat_strategized_plan_compaction_v2

Conversation

@zhangyue19921010
Copy link
Copy Markdown
Contributor

Close #5186

@github-actions github-actions Bot added the enhancement New feature or request label Nov 13, 2025
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Nov 13, 2025

Codecov Report

❌ Patch coverage is 95.77465% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
rust/lance/src/dataset/optimize.rs 95.77% 1 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Member

@westonpace westonpace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few (mostly minor) suggestions

Comment thread rust/lance/src/dataset/optimize.rs Outdated
Comment on lines +214 to +229
// get all fragments by default
fn get_fragments(&self, dataset: &Dataset, _options: &CompactionOptions) -> Vec<FileFragment> {
// get_fragments should be returning fragments in sorted order (by id)
// and fragment ids should be unique
dataset.get_fragments()
}

// no filter by default
async fn filter_fragments(
&self,
_dataset: &Dataset,
fragments: Vec<FileFragment>,
_options: &CompactionOptions,
) -> Result<Vec<FileFragment>> {
Ok(fragments)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these really need to be trait methods? I think we can probably leave them out and just let individual implementations use them if they want to. It will keep the trait simpler.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed.

Comment thread rust/lance/src/dataset/optimize.rs Outdated
Ok(fragments)
}

async fn plan(&self, dataset: &Dataset, options: &CompactionOptions) -> Result<CompactionPlan>;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's document this method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added docs

Comment thread rust/lance/src/dataset/optimize.rs Outdated
Ok(fragments)
}

async fn plan(&self, dataset: &Dataset, options: &CompactionOptions) -> Result<CompactionPlan>;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will CompactionOptions be flexible enough for all possible strategies? Should we maybe accept options as a JSON string or a Map<String, String>? This way different strategies can expose their own custom options. That would leave the API a little less defined but it would be more flexible.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we even need to take CompactionOptions? Maybe it should be a argument to the constructor of the individual structs. That way each could have their own arguments but also be strongly typed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will CompactionOptions be flexible enough for all possible strategies? Should we maybe accept options as a JSON string or a Map<String, String>? This way different strategies can expose their own custom options. That would leave the API a little less defined but it would be more flexible.

Hi @westonpace Thanks a lot for your review. Added Map<String, String> for flexible.

Do we even need to take CompactionOptions? Maybe it should be a argument to the constructor of the individual structs. That way each could have their own arguments but also be strongly typed.

Hi @wjones127 Thanks a lot for your review.

I have tried several ways to eliminate the CompactionOptions parameter in the plan method, but none of them are perfect :( The main contradiction is that during users start planning compaction based on the built planner, they may dynamically adjust the options parameters on certain conditions, such as

https://github.com/lancedb/lance/blob/254a8217ac26666585983aa7ec8c4234f4c3f99f/rust/lance/src/dataset/optimize.rs#L225

If options are passed in when building the planner, then after modifying the options subsequently, it must also be ensured that the options in the planner can be seen. Therefore, we need Arc + mutex and cannot use clone.

On the contrary, it might be simpler and more flexible to pass in the desired options each time the plan method is called here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If options are passed in when building the planner, then after modifying the options subsequently, it must also be ensured that the options in the planner can be seen. Therefore, we need Arc + mutex and cannot use clone.

I don't understand this. The logic of validate() can live in the planner and be internal.

If I were to rewrite compact_files, I would do:

pub async fn compact_files(
    dataset: &mut Dataset,
    mut options: CompactionOptions,
    remap_options: Option<Arc<dyn IndexRemapperOptions>>, // These will be deprecated later
) -> Result<CompactionMetrics> {
    info!(target: TRACE_DATASET_EVENTS, event=DATASET_COMPACTING_EVENT, uri = &dataset.uri);
    // .validate() now happens inside of `from_options`
    let planner = DefaultCompactionPlanner::from_options(options);

    compact_files_with_planner(dataset, &planner, remap_options).await
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed as you suggested ~

Comment thread rust/lance/src/dataset/optimize.rs Outdated
/// Compacts the files in the dataset without reordering them.
///
/// This does a few things:
/// By default, his does a few things:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// By default, his does a few things:
/// By default, this does a few things:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed.

pub async fn compact_files(
dataset: &mut Dataset,
options: CompactionOptions,
remap_options: Option<Arc<dyn IndexRemapperOptions>>, // These will be deprecated later
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@zhangyue19921010
Copy link
Copy Markdown
Contributor Author

Hi @westonpace and @wjones127 Thanks a lot for your review. All comments are addressed. PTAL :)

Copy link
Copy Markdown
Contributor

@wjones127 wjones127 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, I accidentally left my comments pending. I'm still not sure about the design. It seems like it could be simplified further.

Comment thread rust/lance/src/dataset/optimize.rs Outdated
Comment on lines +214 to +219
// get all fragments by default
fn get_fragments(&self, dataset: &Dataset, _options: &CompactionOptions) -> Vec<FileFragment> {
// get_fragments should be returning fragments in sorted order (by id)
// and fragment ids should be unique
dataset.get_fragments()
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was already commented on, but do we need this? It seems like individual implementations can just call dataset.get_fragments() and then do whatever filtering they would like.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

Comment thread rust/lance/src/dataset/optimize.rs Outdated
Ok(fragments)
}

async fn plan(&self, dataset: &Dataset, options: &CompactionOptions) -> Result<CompactionPlan>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If options are passed in when building the planner, then after modifying the options subsequently, it must also be ensured that the options in the planner can be seen. Therefore, we need Arc + mutex and cannot use clone.

I don't understand this. The logic of validate() can live in the planner and be internal.

If I were to rewrite compact_files, I would do:

pub async fn compact_files(
    dataset: &mut Dataset,
    mut options: CompactionOptions,
    remap_options: Option<Arc<dyn IndexRemapperOptions>>, // These will be deprecated later
) -> Result<CompactionMetrics> {
    info!(target: TRACE_DATASET_EVENTS, event=DATASET_COMPACTING_EVENT, uri = &dataset.uri);
    // .validate() now happens inside of `from_options`
    let planner = DefaultCompactionPlanner::from_options(options);

    compact_files_with_planner(dataset, &planner, remap_options).await
}

@zhangyue19921010
Copy link
Copy Markdown
Contributor Author

Sorry for the delay, I accidentally left my comments pending. I'm still not sure about the design. It seems like it could be simplified further.

No worried @westonpace . Really appreciate for your response. Also all comments are addressed. PTAL~

Copy link
Copy Markdown
Contributor

@wjones127 wjones127 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just got back from vacation, so sorry for the delay.

This is headed in the right direction. I still have some comments on the API.

Comment thread rust/lance/src/dataset/optimize.rs Outdated
Comment on lines +230 to +231

fn get_compaction_options(&self) -> &CompactionOptions;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary? The CompactionPlan already contains the CompactionOptions, so isn't the return value of plan sufficient?

Comment thread rust/lance/src/dataset/optimize.rs Outdated
async fn plan(
&self,
dataset: &Dataset,
configs: HashMap<String, String>,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need configs? The point of my earlier suggestion was to get rid of string typed configurations in favor of proper constructors. For example:

pub trait CompactionPlanner: Send + Sync {
    async fn plan(&self, dataset: &Dataset) -> Result<CompactionPlan>;
}

pub struct DefaultCompactionPlanner {
    options: CompactionOptions,
}

impl CompactionPlanner for DefaultCompactionPlanner {
    async fn plan(&self, dataset: &Dataset) -> Result<CompactionPlan> {
        let tasks = todo!();
        Ok(CompactionPlan {
            tasks,
            read_version: dataset.manifest.version,
            options: self.options.clone()
        })
    }
}

pub struct CustomPlanner {
    config_a: i32,
    config_b: Duration,
    options: CompactionOptions
}

impl CustomPlanner {
    fn new(config_a: i32, config_b: Duration, options: CompactionOptions) -> {
        todo!()
    }
}

impl CompactionPlanner for CustomPlanner {
    async fn plan(&self, dataset: &Dataset) -> Result<CompactionPlan> {
        let tasks = todo!("Use config_a and config_b to plan tasks");
        Ok(CompactionPlan {
            tasks,
            read_version: dataset.manifest.version,
            options: self.options.clone()
        })
    }
}

Notice how CustomPlanner:new() takes strongly-typed parameters. We no long have to validate string inputs, which makes the API safer and easier to use.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW we should probably distinguish from configurations for planning and configurations for executing. The reason we need CompactionOptions later is they are needed for execution. But the options that will vary for planners are the planning options.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW we should probably distinguish from configurations for planning and configurations for executing. The reason we need CompactionOptions later is they are needed for execution. But the options that will vary for planners are the planning options.

I quickly glanced through it, and there are quite a few parts that need to be modified. I will try to create a new PR to solve this problem :)

@zhangyue19921010
Copy link
Copy Markdown
Contributor Author

Just got back from vacation, so sorry for the delay.

This is headed in the right direction. I still have some comments on the API.

Hi @wjones127. Thanks for your response and wish u have a fantastic vacation! I will address the comments asap :)

Copilot AI review requested due to automatic review settings December 9, 2025 07:13
@github-actions github-actions Bot added the java label Dec 9, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a strategy pattern for compaction planning in Lance, allowing users to implement custom compaction strategies via the new CompactionPlanner trait. The existing compaction logic has been refactored into a DefaultCompactionPlanner implementation, maintaining backward compatibility while enabling extensibility.

  • Adds CompactionPlanner trait for custom compaction strategies
  • Refactors existing compaction planning logic into DefaultCompactionPlanner
  • Introduces compact_files_with_planner API to enable custom planner usage

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 3 comments.

File Description
rust/lance/src/dataset/optimize.rs Adds CompactionPlanner trait and DefaultCompactionPlanner struct; refactors plan_compaction logic into the default planner; adds compact_files_with_planner function; updates documentation; adds test for default planner
java/lance-jni/Cargo.lock Updates lance-namespace-impls version from 1.0.0-beta.4 to 1.0.0-beta.7

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread rust/lance/src/dataset/optimize.rs Outdated
Comment thread rust/lance/src/dataset/optimize.rs Outdated
Comment thread rust/lance/src/dataset/optimize.rs
@zhangyue19921010
Copy link
Copy Markdown
Contributor Author

Hi @wjones127 Really appreciate for your help. Comments are all addressed . PTAL ~

Copy link
Copy Markdown
Contributor

@wjones127 wjones127 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. Thanks for working with me on this.

@wjones127 wjones127 merged commit b9129d8 into lance-format:main Dec 9, 2025
25 of 27 checks passed
@zhangyue19921010
Copy link
Copy Markdown
Contributor Author

This looks great. Thanks for working with me on this.

Thanks @wjones127 :) Will optimize the compaction plan strategy, based on the horizontal and tiered vertical strategies we discuss before as next step ~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request java

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strategized Compaction Plan

5 participants