Elffuss BraidHub

kikocis/lancedb public

feat(rust): `list_indices` in remote SDK (#1726)

Implements `list_indices`.

---------

Co-authored-by: Weston Pace <weston.pace@gmail.com>
Changesete6c3bef0cf811f7acd72409b6c8ffae451a7f1ea968cb7eed133fc10c7fdef75
AuthorWill Jones <willjones127@gmail.com> · 2024-10-08 20:45 -07:00
CommitterGitHub <noreply@github.com> · 2024-10-08 21:45 -06:00
Parentsfb6326207146

Browse the files at this changeset

1 file changed

modified rust/lancedb/src/remote/table.rs +112 −3
@@ -525,11 +525,57 @@
525525 message: "drop_columns is not yet supported.".into(),
526526 })
527527 }
528+
528529 async fn list_indices(&self) -> Result<Vec<IndexConfig>> {
529- Err(Error::NotSupported {
530- message: "list_indices is not yet supported.".into(),
531- })
530+ // Make request to list the indices
531+ let request = self
532+ .client
533+ .post(&format!("/v1/table/{}/index/list/", self.name));
534+ let response = self.client.send(request, true).await?;
535+ let response = self.check_table_response(response).await?;
536+
537+ #[derive(Deserialize)]
538+ struct ListIndicesResponse {
539+ indexes: Vec<IndexConfigResponse>,
540+ }
541+
542+ #[derive(Deserialize)]
543+ struct IndexConfigResponse {
544+ index_name: String,
545+ columns: Vec<String>,
546+ }
547+
548+ let body = response.text().await?;
549+ let body: ListIndicesResponse = serde_json::from_str(&body).map_err(|err| Error::Http {
550+ message: format!(
551+ "Failed to parse list_indices response: {}, body: {}",
552+ err, body
553+ ),
554+ })?;
555+
556+ // Make request to get stats for each index, so we get the index type.
557+ // This is a bit inefficient, but it's the only way to get the index type.
558+ let mut futures = Vec::with_capacity(body.indexes.len());
559+ for index in body.indexes {
560+ let future = async move {
561+ match self.index_stats(&index.index_name).await {
562+ Ok(Some(stats)) => Ok(Some(IndexConfig {
563+ name: index.index_name,
564+ index_type: stats.index_type,
565+ columns: index.columns,
566+ })),
567+ Ok(None) => Ok(None), // The index must have been deleted since we listed it.
568+ Err(e) => Err(e),
569+ }
570+ };
571+ futures.push(future);
572+ }
573+ let results = futures::future::try_join_all(futures).await?;
574+ let index_configs = results.into_iter().flatten().collect();
575+
576+ Ok(index_configs)
532577 }
578+
533579 async fn index_stats(&self, index_name: &str) -> Result<Option<IndexStatistics>> {
534580 let request = self.client.post(&format!(
535581 "/v1/table/{}/index/{}/stats/",
@@ -1181,6 +1227,69 @@
11811227 }
11821228 }
11831229
1230+ #[tokio::test]
1231+ async fn test_list_indices() {
1232+ let table = Table::new_with_handler("my_table", |request| {
1233+ assert_eq!(request.method(), "POST");
1234+
1235+ let response_body = match request.url().path() {
1236+ "/v1/table/my_table/index/list/" => {
1237+ serde_json::json!({
1238+ "indexes": [
1239+ {
1240+ "index_name": "vector_idx",
1241+ "index_uuid": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
1242+ "columns": ["vector"],
1243+ "index_status": "done",
1244+ },
1245+ {
1246+ "index_name": "my_idx",
1247+ "index_uuid": "34255f64-5717-4562-b3fc-2c963f66afa6",
1248+ "columns": ["my_column"],
1249+ "index_status": "done",
1250+ },
1251+ ]
1252+ })
1253+ }
1254+ "/v1/table/my_table/index/vector_idx/stats/" => {
1255+ serde_json::json!({
1256+ "num_indexed_rows": 100000,
1257+ "num_unindexed_rows": 0,
1258+ "index_type": "IVF_PQ",
1259+ "distance_type": "l2"
1260+ })
1261+ }
1262+ "/v1/table/my_table/index/my_idx/stats/" => {
1263+ serde_json::json!({
1264+ "num_indexed_rows": 100000,
1265+ "num_unindexed_rows": 0,
1266+ "index_type": "LABEL_LIST"
1267+ })
1268+ }
1269+ path => panic!("Unexpected path: {}", path),
1270+ };
1271+ http::Response::builder()
1272+ .status(200)
1273+ .body(serde_json::to_string(&response_body).unwrap())
1274+ .unwrap()
1275+ });
1276+
1277+ let indices = table.list_indices().await.unwrap();
1278+ let expected = vec![
1279+ IndexConfig {
1280+ name: "vector_idx".into(),
1281+ index_type: IndexType::IvfPq,
1282+ columns: vec!["vector".into()],
1283+ },
1284+ IndexConfig {
1285+ name: "my_idx".into(),
1286+ index_type: IndexType::LabelList,
1287+ columns: vec!["my_column".into()],
1288+ },
1289+ ];
1290+ assert_eq!(indices, expected);
1291+ }
1292+
11841293 #[tokio::test]
11851294 async fn test_index_stats() {
11861295 let table = Table::new_with_handler("my_table", |request| {