diff --git a/rust/lancedb/src/table/optimize.rs b/rust/lancedb/src/table/optimize.rs index 0888ca4c4..e19443759 100644 --- a/rust/lancedb/src/table/optimize.rs +++ b/rust/lancedb/src/table/optimize.rs @@ -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> { + /// 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;