diff --git a/rust/lancedb/src/connection/create_table.rs b/rust/lancedb/src/connection/create_table.rs index 77003dc30..10633adb9 100644 --- a/rust/lancedb/src/connection/create_table.rs +++ b/rust/lancedb/src/connection/create_table.rs @@ -235,7 +235,8 @@ impl CreateTableBuilder { #[cfg(test)] mod tests { use arrow_array::{ - Array, FixedSizeListArray, Float32Array, RecordBatch, RecordBatchIterator, record_batch, + Array, FixedSizeListArray, Float32Array, Int32Array, RecordBatch, RecordBatchIterator, + cast::AsArray, record_batch, types::Int32Type, }; use arrow_schema::{ArrowError, DataType, Field, Schema}; use futures::TryStreamExt; @@ -492,6 +493,105 @@ mod tests { assert_eq!(other_schema, overwritten.schema().await.unwrap()); } + #[tokio::test] + #[rstest::rstest] + #[case::non_nullable_list_item(DataType::new_fixed_size_list(DataType::Float32, 2, false))] + #[case::renamed_list_item(DataType::FixedSizeList( + Arc::new(Field::new("element", DataType::Float32, true)), + 2 + ))] + #[case::string_view(DataType::Utf8View)] + async fn test_create_table_exist_ok_with_lance_normalized_type(#[case] data_type: DataType) { + let tmp_dir = tempdir().unwrap(); + let uri = tmp_dir.path().to_str().unwrap(); + let db = connect(uri).execute().await.unwrap(); + let schema = Arc::new(Schema::new(vec![Field::new("x", data_type.clone(), false)])); + db.create_empty_table("test", schema.clone()) + .execute() + .await + .unwrap(); + db.create_empty_table("test", schema) + .mode(CreateTableMode::exist_ok(|req| req)) + .execute() + .await + .unwrap(); + + let nullable_schema = Arc::new(Schema::new(vec![Field::new("x", data_type, true)])); + let result = db + .create_empty_table("test", nullable_schema) + .mode(CreateTableMode::exist_ok(|req| req)) + .execute() + .await; + assert!(matches!(result, Err(Error::Schema { .. }))); + } + + /// `exist_ok` must reopen the table at the requested location, not the one at the + /// default location of a table with the same name. + #[tokio::test] + #[rstest::rstest] + #[case::original_schema(false)] + #[case::normalized_schema(true)] + async fn test_create_table_exist_ok_opens_requested_location(#[case] nullable_item: bool) { + let tmp_dir = tempdir().unwrap(); + let db = connect(tmp_dir.path().join("db").to_str().unwrap()) + .execute() + .await + .unwrap(); + let location = tmp_dir.path().join("elsewhere").join("test.lance"); + let location = location.to_str().unwrap(); + + let item = Arc::new(Field::new("item", DataType::Float32, nullable_item)); + let schema = Arc::new(Schema::new(vec![ + Field::new("id", DataType::Int32, false), + Field::new("vector", DataType::FixedSizeList(item.clone(), 2), false), + ])); + let batch_with_id = |id: i32| { + let vector = FixedSizeListArray::new( + item.clone(), + 2, + Arc::new(Float32Array::from(vec![1.0, 2.0])), + None, + ); + RecordBatch::try_new( + schema.clone(), + vec![Arc::new(Int32Array::from(vec![id])), Arc::new(vector)], + ) + .unwrap() + }; + db.create_table("test", batch_with_id(10)) + .execute() + .await + .unwrap(); + db.create_table("test", batch_with_id(20)) + .location(location) + .execute() + .await + .unwrap(); + + let table = db + .create_empty_table("test", schema.clone()) + .location(location) + .mode(CreateTableMode::exist_ok(|req| req)) + .execute() + .await + .unwrap(); + + assert_eq!(table.uri().await.unwrap(), location); + let batches = table + .query() + .execute() + .await + .unwrap() + .try_collect::>() + .await + .unwrap(); + let ids = batches + .iter() + .flat_map(|batch| batch["id"].as_primitive::().values().to_vec()) + .collect::>(); + assert_eq!(ids, [20]); + } + #[tokio::test] #[rstest::rstest] #[case(LanceFileVersion::Legacy)] diff --git a/rust/lancedb/src/database/listing.rs b/rust/lancedb/src/database/listing.rs index 17ecfadff..d85dfbe69 100644 --- a/rust/lancedb/src/database/listing.rs +++ b/rust/lancedb/src/database/listing.rs @@ -876,6 +876,7 @@ impl ListingDatabase { &self, table_name: &str, namespace_path: Vec, + location: Option, mode: CreateTableMode, data_schema: &arrow_schema::Schema, ) -> Result> { @@ -889,7 +890,7 @@ impl ListingDatabase { namespace_path: namespace_path.clone(), index_cache_size: None, lance_read_params: None, - location: None, + location, namespace_client: None, managed_versioning: None, }; @@ -897,8 +898,13 @@ impl ListingDatabase { let table = self.open_table(req).await?; let table_schema = table.schema().await?; + // Lance doesn't store every Arrow detail (such as the name and nullability of a + // fixed-size list's child), so also accept a schema that it stores as this one. + let matches = table_schema.as_ref() == data_schema + || lance::datatypes::Schema::try_from(data_schema) + .is_ok_and(|schema| arrow_schema::Schema::from(&schema) == *table_schema); - if table_schema.as_ref() != data_schema { + if !matches { return Err(Error::Schema { message: "Provided schema does not match existing table schema".to_string(), }); @@ -1093,6 +1099,7 @@ impl Database for ListingDatabase { self.handle_table_exists( &request.name, request.namespace_path.clone(), + request.location, request.mode, &data_schema, )