diff --git a/rust/lancedb/src/database/namespace.rs b/rust/lancedb/src/database/namespace.rs index d603a878b..2bcc5339e 100644 --- a/rust/lancedb/src/database/namespace.rs +++ b/rust/lancedb/src/database/namespace.rs @@ -577,8 +577,12 @@ impl Database for LanceNamespaceDatabase { self.namespace .rename_table(rename_request) .await - .map_err(|e| Error::Runtime { - message: format!("Failed to rename table: {}", e), + .map_err(|e| match map_namespace_lance_error(e, cur_name) { + // A conflict here means the destination name is taken. + Error::TableAlreadyExists { .. } => Error::TableAlreadyExists { + name: new_name.to_string(), + }, + other => other, })?; Ok(()) @@ -1693,6 +1697,79 @@ mod tests { assert!(open_result.is_err()); } + #[tokio::test] + async fn test_namespace_rename_missing_table_returns_not_found() { + let tmp_dir = tempdir().unwrap(); + let mut properties = HashMap::new(); + properties.insert( + "root".to_string(), + tmp_dir.path().to_str().unwrap().to_string(), + ); + let conn = connect_namespace("dir", properties) + .execute() + .await + .expect("Failed to connect to namespace"); + + conn.create_namespace(CreateNamespaceRequest { + id: Some(vec!["test_ns".into()]), + ..Default::default() + }) + .await + .expect("Failed to create namespace"); + + let error = conn + .rename_table( + "missing", + "renamed", + &["test_ns".into()], + &["test_ns".into()], + ) + .await + .expect_err("renaming a missing table should fail"); + assert!( + matches!(error, Error::TableNotFound { ref name, .. } if name == "missing"), + "expected TableNotFound, got: {error:?}" + ); + } + + #[tokio::test] + async fn test_namespace_rename_to_existing_table_returns_already_exists() { + let tmp_dir = tempdir().unwrap(); + let mut properties = HashMap::new(); + properties.insert( + "root".to_string(), + tmp_dir.path().to_str().unwrap().to_string(), + ); + let conn = connect_namespace("dir", properties) + .execute() + .await + .expect("Failed to connect to namespace"); + + conn.create_namespace(CreateNamespaceRequest { + id: Some(vec!["test_ns".into()]), + ..Default::default() + }) + .await + .expect("Failed to create namespace"); + + for name in ["source", "taken"] { + conn.create_table(name, create_test_data()) + .namespace(vec!["test_ns".into()]) + .execute() + .await + .expect("Failed to create table"); + } + + let error = conn + .rename_table("source", "taken", &["test_ns".into()], &["test_ns".into()]) + .await + .expect_err("renaming onto an existing table should fail"); + assert!( + matches!(error, Error::TableAlreadyExists { ref name } if name == "taken"), + "expected TableAlreadyExists for the destination, got: {error:?}" + ); + } + #[tokio::test] async fn test_table_names_at_root() { // Test that table_names at root (empty namespace) works correctly