Changesets · Branches · Tags · Compare
| Changeset | ef299fdfbf7f9ef3a71b26435b01a2cf80476e10c30d14c9270d5ad26d1615e1 |
|---|---|
| Author | Will Jones <willjones127@gmail.com> · 2024-09-27 09:37 -07:00 |
| Committer | GitHub <noreply@github.com> · 2024-09-27 09:37 -07:00 |
| Parents | f167fea780f1 |
2 files changed
modified rust/lancedb/src/connection.rs +20 −0
| @@ -431,6 +431,7 @@ | ||
| 431 | 431 | data: Box<dyn RecordBatchReader + Send>, |
| 432 | 432 | ) -> Result<Table>; |
| 433 | 433 | async fn do_open_table(&self, options: OpenTableBuilder) -> Result<Table>; |
| 434 | + async fn rename_table(&self, old_name: &str, new_name: &str) -> Result<()>; | |
| 434 | 435 | async fn drop_table(&self, name: &str) -> Result<()>; |
| 435 | 436 | async fn drop_db(&self) -> Result<()>; |
| 436 | 437 | |
| @@ -513,6 +514,19 @@ | ||
| 513 | 514 | OpenTableBuilder::new(self.internal.clone(), name.into()) |
| 514 | 515 | } |
| 515 | 516 | |
| 517 | + /// Rename a table in the database. | |
| 518 | + /// | |
| 519 | + /// This is only supported in LanceDB Cloud. | |
| 520 | + pub async fn rename_table( | |
| 521 | + &self, | |
| 522 | + old_name: impl AsRef<str>, | |
| 523 | + new_name: impl AsRef<str>, | |
| 524 | + ) -> Result<()> { | |
| 525 | + self.internal | |
| 526 | + .rename_table(old_name.as_ref(), new_name.as_ref()) | |
| 527 | + .await | |
| 528 | + } | |
| 529 | + | |
| 516 | 530 | /// Drop a table in the database. |
| 517 | 531 | /// |
| 518 | 532 | /// # Arguments |
| @@ -1066,6 +1080,12 @@ | ||
| 1066 | 1080 | Ok(Table::new(native_table)) |
| 1067 | 1081 | } |
| 1068 | 1082 | |
| 1083 | + async fn rename_table(&self, _old_name: &str, _new_name: &str) -> Result<()> { | |
| 1084 | + Err(Error::NotSupported { | |
| 1085 | + message: "rename_table is not supported in LanceDB OSS".to_string(), | |
| 1086 | + }) | |
| 1087 | + } | |
| 1088 | + | |
| 1069 | 1089 | async fn drop_table(&self, name: &str) -> Result<()> { |
| 1070 | 1090 | let dir_name = format!("{}.{}", name, LANCE_EXTENSION); |
| 1071 | 1091 | let full_path = self.base_path.child(dir_name.clone()); |
modified rust/lancedb/src/remote/db.rs +33 −1
| @@ -149,6 +149,16 @@ | ||
| 149 | 149 | )))) |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | + async fn rename_table(&self, current_name: &str, new_name: &str) -> Result<()> { | |
| 153 | + let req = self | |
| 154 | + .client | |
| 155 | + .post(&format!("/v1/table/{}/rename/", current_name)); | |
| 156 | + let req = req.json(&serde_json::json!({ "new_table_name": new_name })); | |
| 157 | + let resp = self.client.send(req).await?; | |
| 158 | + self.client.check_response(resp).await?; | |
| 159 | + Ok(()) | |
| 160 | + } | |
| 161 | + | |
| 152 | 162 | async fn drop_table(&self, name: &str) -> Result<()> { |
| 153 | 163 | let req = self.client.post(&format!("/v1/table/{}/drop/", name)); |
| 154 | 164 | let resp = self.client.send(req).await?; |
| @@ -174,7 +184,10 @@ | ||
| 174 | 184 | use arrow_array::{Int32Array, RecordBatch, RecordBatchIterator}; |
| 175 | 185 | use arrow_schema::{DataType, Field, Schema}; |
| 176 | 186 | |
| 177 | - use crate::{remote::db::ARROW_STREAM_CONTENT_TYPE, Connection}; | |
| 187 | + use crate::{ | |
| 188 | + remote::{ARROW_STREAM_CONTENT_TYPE, JSON_CONTENT_TYPE}, | |
| 189 | + Connection, | |
| 190 | + }; | |
| 178 | 191 | |
| 179 | 192 | #[tokio::test] |
| 180 | 193 | async fn test_table_names() { |
| @@ -334,4 +347,23 @@ | ||
| 334 | 347 | conn.drop_table("table1").await.unwrap(); |
| 335 | 348 | // NOTE: the API will return 200 even if the table does not exist. So we shouldn't expect 404. |
| 336 | 349 | } |
| 350 | + | |
| 351 | + #[tokio::test] | |
| 352 | + async fn test_rename_table() { | |
| 353 | + let conn = Connection::new_with_handler(|request| { | |
| 354 | + assert_eq!(request.method(), &reqwest::Method::POST); | |
| 355 | + assert_eq!(request.url().path(), "/v1/table/table1/rename/"); | |
| 356 | + assert_eq!( | |
| 357 | + request.headers().get("Content-Type").unwrap(), | |
| 358 | + JSON_CONTENT_TYPE | |
| 359 | + ); | |
| 360 | + | |
| 361 | + let body = request.body().unwrap().as_bytes().unwrap(); | |
| 362 | + let body: serde_json::Value = serde_json::from_slice(body).unwrap(); | |
| 363 | + assert_eq!(body["new_table_name"], "table2"); | |
| 364 | + | |
| 365 | + http::Response::builder().status(200).body("").unwrap() | |
| 366 | + }); | |
| 367 | + conn.rename_table("table1", "table2").await.unwrap(); | |
| 368 | + } | |
| 337 | 369 | } |