mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
docs(rust): document and test num_threads for index optimization (#5359)
This PR fixes [issue #4960](https://github.com/lancedb/lancedb/issues/4960). Fixes #4960 The issue asks for a `num_threads` option on `OptimizeOptions`, like `CompactionOptions::num_threads`, so index optimization can be held to a CPU budget. LanceDB re-exports `OptimizeOptions` from `lance-index`, and Lance added `OptimizeOptions::num_threads` / `with_num_threads` in lance-format/lance#11154. The bump to Lance v16.0.0-beta.7 (#5352) brought it in, and `OptimizeAction::Index(options)` already hands the options through to `Dataset::optimize_indices` unchanged. So the Rust option works today, but nothing in LanceDB says so or checks it. This PR: - documents the option on `OptimizeAction::Index`, with a compile-checked example (`OptimizeOptions::default().with_num_threads(2)`), what it limits (compute stages of index optimization, not a process-wide thread cap), that zero is rejected, and that index optimization runs on LanceDB Embedded tables only; - adds `test_optimize_index_with_num_threads`: on a table with 100 indexed and 100 unindexed rows, `with_num_threads(0)` returns an error naming `num_threads` and leaves the index untouched, and `with_num_threads(1)` then indexes all 200 rows. No code paths change. Possible SDK parity gap: the Python and TypeScript `optimize()` convenience methods run the index step with default `OptimizeOptions`, so they don't expose this limit yet. That would be a separate binding change; I left it out of this PR. ## Testing On macOS arm64, Rust 1.100.0 stable: ``` cargo test --profile ci -p lancedb --lib table::optimize::tests # 24 passed, incl. test_optimize_index_with_num_threads cargo test --profile ci -p lancedb --doc OptimizeAction # 2 passed, incl. the new OptimizeAction::Index example cargo fmt --all -- --check # clean ``` As a check that the test exercises the pass-through, I temporarily made `optimize_indices` ignore the caller's options and use `OptimizeOptions::default()`; `test_optimize_index_with_num_threads` then fails at the zero-value assertion. The test can't observe the thread count itself, so the `with_num_threads(1)` case only shows a limited run completes and indexes every row. `cargo clippy -p lancedb --tests` on Rust 1.100 reports dead-code and `if`-guard lints in other files (`job/profile.rs`, `table/add_data.rs`, `table/write_progress.rs`, `table/query.rs`, `table/computed_columns.rs`) with the default features; none in `table/optimize.rs`. Co-authored-by: Lenin Mookiah <2639584+leninworld@users.noreply.github.com>
This commit is contained in:
co-authored by
Lenin Mookiah
parent
feff86a08d
commit
787ad381bf
@@ -383,6 +383,23 @@ pub enum OptimizeAction {
|
||||
///
|
||||
/// For example, when using IVF, an index will create clusters. Optimizing an index assigns unindexed
|
||||
/// data to the existing clusters, but it does not move the clusters or create new clusters.
|
||||
///
|
||||
/// Use [`OptimizeOptions::with_num_threads`] to bound the compute-stage parallelism of
|
||||
/// index optimization, like [`CompactionOptions::num_threads`] for compaction. The value
|
||||
/// must be greater than zero. This is not a process-wide CPU or thread limit: I/O and
|
||||
/// independent stages may still run concurrently. Index optimization runs only on LanceDB
|
||||
/// Embedded tables.
|
||||
///
|
||||
/// ```
|
||||
/// use lancedb::table::{OptimizeAction, OptimizeOptions};
|
||||
/// # use lancedb::Table;
|
||||
/// # async fn optimize(table: &Table) -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// table
|
||||
/// .optimize(OptimizeAction::Index(OptimizeOptions::default().with_num_threads(2)))
|
||||
/// .await?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
Index(OptimizeOptions),
|
||||
}
|
||||
|
||||
@@ -580,7 +597,7 @@ mod tests {
|
||||
vector::{IvfHnswSqIndexBuilder, IvfRqIndexBuilder},
|
||||
};
|
||||
use crate::query::{ExecutableQuery, QueryBase};
|
||||
use crate::table::{CompactionOptions, OptimizeAction, OptimizeStats};
|
||||
use crate::table::{CompactionOptions, OptimizeAction, OptimizeOptions, OptimizeStats};
|
||||
use futures::TryStreamExt;
|
||||
|
||||
/// Regression test for https://github.com/lancedb/lancedb/issues/2866.
|
||||
@@ -1202,6 +1219,64 @@ mod tests {
|
||||
assert_eq!(final_row_count, 200);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_optimize_index_with_num_threads() {
|
||||
let conn = connect("memory://").execute().await.unwrap();
|
||||
|
||||
let schema = Arc::new(Schema::new(vec![Field::new("i", DataType::Int32, false)]));
|
||||
let batch = RecordBatch::try_new(
|
||||
schema.clone(),
|
||||
vec![Arc::new(Int32Array::from_iter_values(0..100))],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let table = conn
|
||||
.create_table("test_index_optimize_with_num_threads", batch)
|
||||
.execute()
|
||||
.await
|
||||
.unwrap();
|
||||
table
|
||||
.create_index(&["i"], Index::BTree(BTreeIndexBuilder::default()))
|
||||
.execute()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let batch = RecordBatch::try_new(
|
||||
schema,
|
||||
vec![Arc::new(Int32Array::from_iter_values(100..200))],
|
||||
)
|
||||
.unwrap();
|
||||
table.add(batch).execute().await.unwrap();
|
||||
|
||||
let indices = table.list_indices().await.unwrap();
|
||||
assert_eq!(indices.len(), 1);
|
||||
let index_name = indices[0].name.clone();
|
||||
|
||||
let error = table
|
||||
.optimize(OptimizeAction::Index(
|
||||
OptimizeOptions::default().with_num_threads(0),
|
||||
))
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(error.to_string().contains("num_threads"));
|
||||
|
||||
let stats_after_error = table.index_stats(&index_name).await.unwrap().unwrap();
|
||||
assert_eq!(stats_after_error.num_indexed_rows, 100);
|
||||
assert_eq!(stats_after_error.num_unindexed_rows, 100);
|
||||
|
||||
table
|
||||
.optimize(OptimizeAction::Index(
|
||||
OptimizeOptions::default().with_num_threads(1),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let stats_after = table.index_stats(&index_name).await.unwrap().unwrap();
|
||||
assert_eq!(stats_after.num_indexed_rows, 200);
|
||||
assert_eq!(stats_after.num_unindexed_rows, 0);
|
||||
assert_eq!(table.count_rows(None).await.unwrap(), 200);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_optimize_vector_index_after_delete_with_stable_row_ids() {
|
||||
const NUM_ROWS: i32 = 400;
|
||||
|
||||
Reference in New Issue
Block a user