Changesets · Branches · Tags · Compare
| Changeset | 70d43ccc8e377886000a80205a91f8dd1c7acdd8a03ed15c149ac55b548aade4 |
|---|---|
| Author | Will Jones <willjones127@gmail.com> · 2024-10-01 09:16 -07:00 |
| Committer | GitHub <noreply@github.com> · 2024-10-01 09:16 -07:00 |
| Parents | cdf1aa093b13 |
3 files changed
modified python/python/tests/test_index.py +4 −6
| @@ -63,9 +63,8 @@ | ||
| 63 | 63 | @pytest.mark.asyncio |
| 64 | 64 | async def test_create_bitmap_index(some_table: AsyncTable): |
| 65 | 65 | await some_table.create_index("id", config=Bitmap()) |
| 66 | - # TODO: Fix via https://github.com/lancedb/lance/issues/2039 | |
| 67 | - # indices = await some_table.list_indices() | |
| 68 | - # assert str(indices) == '[Index(Bitmap, columns=["id"])]' | |
| 66 | + indices = await some_table.list_indices() | |
| 67 | + assert str(indices) == '[Index(Bitmap, columns=["id"])]' | |
| 69 | 68 | indices = await some_table.list_indices() |
| 70 | 69 | assert len(indices) == 1 |
| 71 | 70 | index_name = indices[0].name |
| @@ -80,9 +79,8 @@ | ||
| 80 | 79 | @pytest.mark.asyncio |
| 81 | 80 | async def test_create_label_list_index(some_table: AsyncTable): |
| 82 | 81 | await some_table.create_index("tags", config=LabelList()) |
| 83 | - # TODO: Fix via https://github.com/lancedb/lance/issues/2039 | |
| 84 | - # indices = await some_table.list_indices() | |
| 85 | - # assert str(indices) == '[Index(LabelList, columns=["id"])]' | |
| 82 | + indices = await some_table.list_indices() | |
| 83 | + assert str(indices) == '[Index(LabelList, columns=["tags"])]' | |
| 86 | 84 | |
| 87 | 85 | |
| 88 | 86 | @pytest.mark.asyncio |
modified rust/lancedb/src/index.rs +20 −1
| @@ -18,7 +18,7 @@ | ||
| 18 | 18 | use serde::Deserialize; |
| 19 | 19 | use serde_with::skip_serializing_none; |
| 20 | 20 | |
| 21 | -use crate::{table::TableInternal, DistanceType, Result}; | |
| 21 | +use crate::{table::TableInternal, DistanceType, Error, Result}; | |
| 22 | 22 | |
| 23 | 23 | use self::{ |
| 24 | 24 | scalar::{BTreeIndexBuilder, BitmapIndexBuilder, LabelListIndexBuilder}, |
| @@ -136,6 +136,25 @@ | ||
| 136 | 136 | } |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | +impl std::str::FromStr for IndexType { | |
| 140 | + type Err = Error; | |
| 141 | + | |
| 142 | + fn from_str(value: &str) -> Result<Self> { | |
| 143 | + match value.to_uppercase().as_str() { | |
| 144 | + "BTREE" => Ok(Self::BTree), | |
| 145 | + "BITMAP" => Ok(Self::Bitmap), | |
| 146 | + "LABEL_LIST" | "LABELLIST" => Ok(Self::LabelList), | |
| 147 | + "FTS" => Ok(Self::FTS), | |
| 148 | + "IVF_PQ" => Ok(Self::IvfPq), | |
| 149 | + "IVF_HNSW_PQ" => Ok(Self::IvfHnswPq), | |
| 150 | + "IVF_HNSW_SQ" => Ok(Self::IvfHnswSq), | |
| 151 | + _ => Err(Error::InvalidInput { | |
| 152 | + message: format!("the input value {} is not a valid IndexType", value), | |
| 153 | + }), | |
| 154 | + } | |
| 155 | + } | |
| 156 | +} | |
| 157 | + | |
| 139 | 158 | /// A description of an index currently configured on a column |
| 140 | 159 | #[derive(Debug, PartialEq, Clone)] |
| 141 | 160 | pub struct IndexConfig { |
modified rust/lancedb/src/table.rs +17 −16
| @@ -25,6 +25,7 @@ | ||
| 25 | 25 | use async_trait::async_trait; |
| 26 | 26 | use datafusion_physical_plan::display::DisplayableExecutionPlan; |
| 27 | 27 | use datafusion_physical_plan::ExecutionPlan; |
| 28 | +use futures::{StreamExt, TryStreamExt}; | |
| 28 | 29 | use lance::dataset::builder::DatasetBuilder; |
| 29 | 30 | use lance::dataset::cleanup::RemovalStats; |
| 30 | 31 | use lance::dataset::optimize::{compact_files, CompactionMetrics, IndexRemapperOptions}; |
| @@ -2023,28 +2024,28 @@ | ||
| 2023 | 2024 | async fn list_indices(&self) -> Result<Vec<IndexConfig>> { |
| 2024 | 2025 | let dataset = self.dataset.get().await?; |
| 2025 | 2026 | let indices = dataset.load_indices().await?; |
| 2026 | - indices.iter().map(|idx| { | |
| 2027 | - let mut is_vector = false; | |
| 2027 | + futures::stream::iter(indices.as_slice()).then(|idx| async { | |
| 2028 | + let stats = dataset.index_statistics(idx.name.as_str()).await?; | |
| 2029 | + let stats: serde_json::Value = serde_json::from_str(&stats).map_err(|e| Error::Runtime { | |
| 2030 | + message: format!("error deserializing index statistics: {}", e), | |
| 2031 | + })?; | |
| 2032 | + let index_type = stats.get("index_type").and_then(|v| v.as_str()) | |
| 2033 | + .ok_or_else(|| Error::Runtime { | |
| 2034 | + message: "index statistics was missing index type".to_string(), | |
| 2035 | + })?; | |
| 2036 | + let index_type: crate::index::IndexType = index_type.parse().map_err(|e| Error::Runtime { | |
| 2037 | + message: format!("error parsing index type: {}", e), | |
| 2038 | + })?; | |
| 2039 | + | |
| 2028 | 2040 | let mut columns = Vec::with_capacity(idx.fields.len()); |
| 2029 | 2041 | for field_id in &idx.fields { |
| 2030 | 2042 | let field = dataset.schema().field_by_id(*field_id).ok_or_else(|| Error::Runtime { message: format!("The index with name {} and uuid {} referenced a field with id {} which does not exist in the schema", idx.name, idx.uuid, field_id) })?; |
| 2031 | - if field.data_type().is_nested() { | |
| 2032 | - // Temporary hack to determine if an index is scalar or vector | |
| 2033 | - // Should be removed in https://github.com/lancedb/lance/issues/2039 | |
| 2034 | - is_vector = true; | |
| 2035 | - } | |
| 2036 | 2043 | columns.push(field.name.clone()); |
| 2037 | 2044 | } |
| 2038 | 2045 | |
| 2039 | - let index_type = if is_vector { | |
| 2040 | - crate::index::IndexType::IvfPq | |
| 2041 | - } else { | |
| 2042 | - crate::index::IndexType::BTree | |
| 2043 | - }; | |
| 2044 | - | |
| 2045 | 2046 | let name = idx.name.clone(); |
| 2046 | 2047 | Ok(IndexConfig { index_type, columns, name }) |
| 2047 | - }).collect::<Result<Vec<_>>>() | |
| 2048 | + }).try_collect::<Vec<_>>().await | |
| 2048 | 2049 | } |
| 2049 | 2050 | |
| 2050 | 2051 | fn dataset_uri(&self) -> &str { |
| @@ -2803,7 +2804,7 @@ | ||
| 2803 | 2804 | let index_configs = table.list_indices().await.unwrap(); |
| 2804 | 2805 | assert_eq!(index_configs.len(), 1); |
| 2805 | 2806 | let index = index_configs.into_iter().next().unwrap(); |
| 2806 | - assert_eq!(index.index_type, crate::index::IndexType::IvfPq); | |
| 2807 | + assert_eq!(index.index_type, crate::index::IndexType::IvfHnswSq); | |
| 2807 | 2808 | assert_eq!(index.columns, vec!["embeddings".to_string()]); |
| 2808 | 2809 | assert_eq!(table.count_rows(None).await.unwrap(), 512); |
| 2809 | 2810 | assert_eq!(table.name(), "test"); |
| @@ -2867,7 +2868,7 @@ | ||
| 2867 | 2868 | let index_configs = table.list_indices().await.unwrap(); |
| 2868 | 2869 | assert_eq!(index_configs.len(), 1); |
| 2869 | 2870 | let index = index_configs.into_iter().next().unwrap(); |
| 2870 | - assert_eq!(index.index_type, crate::index::IndexType::IvfPq); | |
| 2871 | + assert_eq!(index.index_type, crate::index::IndexType::IvfHnswPq); | |
| 2871 | 2872 | assert_eq!(index.columns, vec!["embeddings".to_string()]); |
| 2872 | 2873 | assert_eq!(table.count_rows(None).await.unwrap(), 512); |
| 2873 | 2874 | assert_eq!(table.name(), "test"); |