1use std::any::Any;
8use std::fmt::Debug;
9use std::{collections::HashMap, sync::Arc};
10
11use arrow_array::{ArrayRef, Float32Array, RecordBatch, UInt32Array};
12use arrow_schema::Field;
13use async_trait::async_trait;
14use datafusion::execution::SendableRecordBatchStream;
15use deepsize::DeepSizeOf;
16use ivf::storage::IvfModel;
17use lance_core::{Result, ROW_ID_FIELD};
18use lance_io::traits::Reader;
19use lance_linalg::distance::DistanceType;
20use quantizer::{QuantizationType, Quantizer};
21use std::sync::LazyLock;
22use v3::subindex::SubIndexType;
23
24pub mod bq;
25pub mod flat;
26pub mod graph;
27pub mod hnsw;
28pub mod ivf;
29pub mod kmeans;
30pub mod pq;
31pub mod quantizer;
32pub mod residual;
33pub mod sq;
34pub mod storage;
35pub mod transform;
36pub mod utils;
37pub mod v3;
38
39use super::pb;
40use crate::metrics::MetricsCollector;
41use crate::{prefilter::PreFilter, Index};
42
43pub const DIST_COL: &str = "_distance";
45pub const DISTANCE_TYPE_KEY: &str = "distance_type";
46pub const INDEX_UUID_COLUMN: &str = "__index_uuid";
47pub const PART_ID_COLUMN: &str = "__ivf_part_id";
48pub const DIST_Q_C_COLUMN: &str = "__dist_q_c";
49pub const CENTROID_DIST_COLUMN: &str = "__centroid_dist";
51pub const PQ_CODE_COLUMN: &str = "__pq_code";
52pub const SQ_CODE_COLUMN: &str = "__sq_code";
53pub const LOSS_METADATA_KEY: &str = "_loss";
54
55pub static VECTOR_RESULT_SCHEMA: LazyLock<arrow_schema::SchemaRef> = LazyLock::new(|| {
56 arrow_schema::SchemaRef::new(arrow_schema::Schema::new(vec![
57 Field::new(DIST_COL, arrow_schema::DataType::Float32, false),
58 ROW_ID_FIELD.clone(),
59 ]))
60});
61
62pub static PART_ID_FIELD: LazyLock<arrow_schema::Field> = LazyLock::new(|| {
63 arrow_schema::Field::new(PART_ID_COLUMN, arrow_schema::DataType::UInt32, true)
64});
65
66pub static CENTROID_DIST_FIELD: LazyLock<arrow_schema::Field> = LazyLock::new(|| {
67 arrow_schema::Field::new(CENTROID_DIST_COLUMN, arrow_schema::DataType::Float32, true)
68});
69
70#[derive(Debug, Clone)]
72pub struct Query {
73 pub column: String,
75
76 pub key: ArrayRef,
78
79 pub k: usize,
81
82 pub lower_bound: Option<f32>,
84
85 pub upper_bound: Option<f32>,
87
88 pub minimum_nprobes: usize,
91
92 pub maximum_nprobes: Option<usize>,
95
96 pub ef: Option<usize>,
99
100 pub refine_factor: Option<u32>,
103
104 pub metric_type: DistanceType,
106
107 pub use_index: bool,
109
110 pub dist_q_c: f32,
113}
114
115impl From<pb::VectorMetricType> for DistanceType {
116 fn from(proto: pb::VectorMetricType) -> Self {
117 match proto {
118 pb::VectorMetricType::L2 => Self::L2,
119 pb::VectorMetricType::Cosine => Self::Cosine,
120 pb::VectorMetricType::Dot => Self::Dot,
121 pb::VectorMetricType::Hamming => Self::Hamming,
122 }
123 }
124}
125
126impl From<DistanceType> for pb::VectorMetricType {
127 fn from(mt: DistanceType) -> Self {
128 match mt {
129 DistanceType::L2 => Self::L2,
130 DistanceType::Cosine => Self::Cosine,
131 DistanceType::Dot => Self::Dot,
132 DistanceType::Hamming => Self::Hamming,
133 }
134 }
135}
136
137#[async_trait]
145#[allow(clippy::redundant_pub_crate)]
146pub trait VectorIndex: Send + Sync + std::fmt::Debug + Index {
147 async fn search(
164 &self,
165 query: &Query,
166 pre_filter: Arc<dyn PreFilter>,
167 metrics: &dyn MetricsCollector,
168 ) -> Result<RecordBatch>;
169
170 fn find_partitions(&self, query: &Query) -> Result<(UInt32Array, Float32Array)>;
179
180 fn total_partitions(&self) -> usize;
182
183 async fn search_in_partition(
188 &self,
189 partition_id: usize,
190 query: &Query,
191 pre_filter: Arc<dyn PreFilter>,
192 metrics: &dyn MetricsCollector,
193 ) -> Result<RecordBatch>;
194
195 fn is_loadable(&self) -> bool;
198
199 fn use_residual(&self) -> bool;
201
202 async fn load(
207 &self,
208 reader: Arc<dyn Reader>,
209 offset: usize,
210 length: usize,
211 ) -> Result<Box<dyn VectorIndex>>;
212
213 async fn load_partition(
215 &self,
216 reader: Arc<dyn Reader>,
217 offset: usize,
218 length: usize,
219 _partition_id: usize,
220 ) -> Result<Box<dyn VectorIndex>> {
221 self.load(reader, offset, length).await
222 }
223
224 async fn partition_reader(
226 &self,
227 _partition_id: usize,
228 _with_vector: bool,
229 _metrics: &dyn MetricsCollector,
230 ) -> Result<SendableRecordBatchStream> {
231 unimplemented!("only for IVF")
232 }
233
234 async fn to_batch_stream(&self, with_vector: bool) -> Result<SendableRecordBatchStream>;
236
237 fn num_rows(&self) -> u64;
238
239 fn row_ids(&self) -> Box<dyn Iterator<Item = &'_ u64> + '_>;
241
242 async fn remap(&mut self, mapping: &HashMap<u64, Option<u64>>) -> Result<()>;
251
252 fn metric_type(&self) -> DistanceType;
254
255 fn ivf_model(&self) -> &IvfModel;
256 fn quantizer(&self) -> Quantizer;
257
258 fn sub_index_type(&self) -> (SubIndexType, QuantizationType);
260}
261
262pub trait VectorIndexCacheEntry: Debug + Send + Sync + DeepSizeOf {
264 fn as_any(&self) -> &dyn Any;
265}