Elffuss BraidHub

kikocis/lancedb public

feat(rust): remote rename table (#1703)

Adds rename to remote table. Pre-requisite for
https://github.com/lancedb/lancedb/pull/1701
Changesetef299fdfbf7f9ef3a71b26435b01a2cf80476e10c30d14c9270d5ad26d1615e1
AuthorWill Jones <willjones127@gmail.com> · 2024-09-27 09:37 -07:00
CommitterGitHub <noreply@github.com> · 2024-09-27 09:37 -07:00
Parentsf167fea780f1

Browse the files at this changeset

2 files changed

modified rust/lancedb/src/connection.rs +20 −0
@@ -431,6 +431,7 @@
431431 data: Box<dyn RecordBatchReader + Send>,
432432 ) -> Result<Table>;
433433 async fn do_open_table(&self, options: OpenTableBuilder) -> Result<Table>;
434+ async fn rename_table(&self, old_name: &str, new_name: &str) -> Result<()>;
434435 async fn drop_table(&self, name: &str) -> Result<()>;
435436 async fn drop_db(&self) -> Result<()>;
436437
@@ -513,6 +514,19 @@
513514 OpenTableBuilder::new(self.internal.clone(), name.into())
514515 }
515516
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+
516530 /// Drop a table in the database.
517531 ///
518532 /// # Arguments
@@ -1066,6 +1080,12 @@
10661080 Ok(Table::new(native_table))
10671081 }
10681082
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+
10691089 async fn drop_table(&self, name: &str) -> Result<()> {
10701090 let dir_name = format!("{}.{}", name, LANCE_EXTENSION);
10711091 let full_path = self.base_path.child(dir_name.clone());
modified rust/lancedb/src/remote/db.rs +33 −1
@@ -149,6 +149,16 @@
149149 ))))
150150 }
151151
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+
152162 async fn drop_table(&self, name: &str) -> Result<()> {
153163 let req = self.client.post(&format!("/v1/table/{}/drop/", name));
154164 let resp = self.client.send(req).await?;
@@ -174,7 +184,10 @@
174184 use arrow_array::{Int32Array, RecordBatch, RecordBatchIterator};
175185 use arrow_schema::{DataType, Field, Schema};
176186
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+ };
178191
179192 #[tokio::test]
180193 async fn test_table_names() {
@@ -334,4 +347,23 @@
334347 conn.drop_table("table1").await.unwrap();
335348 // NOTE: the API will return 200 even if the table does not exist. So we shouldn't expect 404.
336349 }
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+ }
337369 }