Skip to main content

store_api/storage/
requests.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt::{Display, Formatter};
16
17use common_error::ext::BoxedError;
18use common_recordbatch::OrderOption;
19use datafusion_expr::expr::Expr;
20// Re-export vector types from datatypes to avoid duplication
21pub use datatypes::schema::{VectorDistanceMetric, VectorIndexEngineType};
22use strum::Display;
23
24use crate::storage::{ColumnId, SequenceNumber};
25
26/// A hint for KNN vector search.
27#[derive(Debug, Clone, PartialEq)]
28pub struct VectorSearchRequest {
29    /// Column ID of the vector column to search.
30    pub column_id: ColumnId,
31    /// The query vector to search for.
32    pub query_vector: Vec<f32>,
33    /// Number of nearest neighbors to return.
34    pub k: usize,
35    /// Distance metric to use (matches the index metric).
36    pub metric: VectorDistanceMetric,
37}
38
39/// Search results from vector index.
40#[derive(Debug, Clone, PartialEq)]
41pub struct VectorSearchMatches {
42    /// Keys (row offsets in the index).
43    pub keys: Vec<u64>,
44    /// Distances from the query vector.
45    pub distances: Vec<f32>,
46}
47
48/// Trait for vector index engines (HNSW implementations).
49///
50/// This trait defines the interface for pluggable vector index engines.
51/// Implementations (e.g., UsearchEngine) are provided by storage engines like mito2.
52pub trait VectorIndexEngine: Send + Sync {
53    /// Adds a vector with the given key.
54    fn add(&mut self, key: u64, vector: &[f32]) -> Result<(), BoxedError>;
55
56    /// Searches for k nearest neighbors.
57    fn search(&self, query: &[f32], k: usize) -> Result<VectorSearchMatches, BoxedError>;
58
59    /// Returns the serialized length.
60    fn serialized_length(&self) -> usize;
61
62    /// Serializes the index to a buffer.
63    fn save_to_buffer(&self, buffer: &mut [u8]) -> Result<(), BoxedError>;
64
65    /// Reserves capacity for vectors.
66    fn reserve(&mut self, capacity: usize) -> Result<(), BoxedError>;
67
68    /// Returns current size (number of vectors).
69    fn size(&self) -> usize;
70
71    /// Returns current capacity.
72    fn capacity(&self) -> usize;
73
74    /// Returns memory usage in bytes.
75    fn memory_usage(&self) -> usize;
76}
77
78/// A hint on how to select rows from a time-series.
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
80pub enum TimeSeriesRowSelector {
81    /// Only keep the last row of each time-series.
82    LastRow,
83}
84
85/// A hint on how to distribute time-series data on the scan output.
86#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
87pub enum TimeSeriesDistribution {
88    /// Data are distributed by time window first. The scanner will
89    /// return all data within one time window before moving to the next one.
90    TimeWindowed,
91    /// Data are organized by time-series first. The scanner will return
92    /// all data for one time-series before moving to the next one.
93    PerSeries,
94}
95
96#[derive(Default, Clone, Debug, PartialEq)]
97pub struct ScanRequest {
98    /// Indices of columns to read, `None` to read all columns. This indices is
99    /// based on table schema.
100    pub projection: Option<Vec<usize>>,
101    /// Filters pushed down
102    pub filters: Vec<Expr>,
103    /// Expected output ordering. This is only a hint and isn't guaranteed.
104    pub output_ordering: Option<Vec<OrderOption>>,
105    /// limit can be used to reduce the amount scanned
106    /// from the datasource as a performance optimization.
107    /// If set, it contains the amount of rows needed by the caller,
108    /// The data source should return *at least* this number of rows if available.
109    pub limit: Option<usize>,
110    /// Optional hint to select rows from time-series.
111    pub series_row_selector: Option<TimeSeriesRowSelector>,
112    /// Optional constraint on the sequence number of the rows to read.
113    /// If set, only rows with a sequence number **lesser or equal** to this value
114    /// will be returned.
115    /// This is the effective memtable upper bound used by the scan, whether provided
116    /// explicitly or bound on scan open.
117    pub memtable_max_sequence: Option<SequenceNumber>,
118    /// Optional constraint on the minimal sequence number in the memtable.
119    /// If set, only the memtables that contain sequences **greater than** this value will be scanned
120    pub memtable_min_sequence: Option<SequenceNumber>,
121    /// Optional constraint on the minimal sequence number in the SST files.
122    /// If set, only the SST files that contain sequences greater than this value will be scanned.
123    pub sst_min_sequence: Option<SequenceNumber>,
124    /// Whether to bind the effective snapshot upper bound when opening the scan.
125    pub snapshot_on_scan: bool,
126    /// Optional hint for the distribution of time-series data.
127    pub distribution: Option<TimeSeriesDistribution>,
128    /// Optional hint for KNN vector search. When set, the scan should use
129    /// vector index to find the k nearest neighbors.
130    pub vector_search: Option<VectorSearchRequest>,
131}
132
133impl Display for ScanRequest {
134    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
135        enum Delimiter {
136            None,
137            Init,
138        }
139
140        impl Delimiter {
141            fn as_str(&mut self) -> &str {
142                match self {
143                    Delimiter::None => {
144                        *self = Delimiter::Init;
145                        ""
146                    }
147                    Delimiter::Init => ", ",
148                }
149            }
150        }
151
152        let mut delimiter = Delimiter::None;
153
154        write!(f, "ScanRequest {{ ")?;
155        if let Some(projection) = &self.projection {
156            write!(f, "{}projection: {:?}", delimiter.as_str(), projection)?;
157        }
158        if !self.filters.is_empty() {
159            write!(
160                f,
161                "{}filters: [{}]",
162                delimiter.as_str(),
163                self.filters
164                    .iter()
165                    .map(|f| f.to_string())
166                    .collect::<Vec<_>>()
167                    .join(", ")
168            )?;
169        }
170        if let Some(output_ordering) = &self.output_ordering {
171            write!(
172                f,
173                "{}output_ordering: {:?}",
174                delimiter.as_str(),
175                output_ordering
176            )?;
177        }
178        if let Some(limit) = &self.limit {
179            write!(f, "{}limit: {}", delimiter.as_str(), limit)?;
180        }
181        if let Some(series_row_selector) = &self.series_row_selector {
182            write!(
183                f,
184                "{}series_row_selector: {}",
185                delimiter.as_str(),
186                series_row_selector
187            )?;
188        }
189        if let Some(sequence) = &self.memtable_max_sequence {
190            write!(f, "{}sequence: {}", delimiter.as_str(), sequence)?;
191        }
192        if let Some(sst_min_sequence) = &self.sst_min_sequence {
193            write!(
194                f,
195                "{}sst_min_sequence: {}",
196                delimiter.as_str(),
197                sst_min_sequence
198            )?;
199        }
200        if self.snapshot_on_scan {
201            write!(
202                f,
203                "{}snapshot_on_scan: {}",
204                delimiter.as_str(),
205                self.snapshot_on_scan
206            )?;
207        }
208        if let Some(distribution) = &self.distribution {
209            write!(f, "{}distribution: {}", delimiter.as_str(), distribution)?;
210        }
211        if let Some(vector_search) = &self.vector_search {
212            write!(
213                f,
214                "{}vector_search: column_id={}, k={}, metric={}",
215                delimiter.as_str(),
216                vector_search.column_id,
217                vector_search.k,
218                vector_search.metric
219            )?;
220        }
221        write!(f, " }}")
222    }
223}
224
225#[cfg(test)]
226mod tests {
227    use datafusion_expr::{Operator, binary_expr, col, lit};
228
229    use super::*;
230
231    #[test]
232    fn test_display_scan_request() {
233        let request = ScanRequest {
234            ..Default::default()
235        };
236        assert_eq!(request.to_string(), "ScanRequest {  }");
237
238        let request = ScanRequest {
239            projection: Some(vec![1, 2]),
240            filters: vec![
241                binary_expr(col("i"), Operator::Gt, lit(1)),
242                binary_expr(col("s"), Operator::Eq, lit("x")),
243            ],
244            limit: Some(10),
245            ..Default::default()
246        };
247        assert_eq!(
248            request.to_string(),
249            r#"ScanRequest { projection: [1, 2], filters: [i > Int32(1), s = Utf8("x")], limit: 10 }"#
250        );
251
252        let request = ScanRequest {
253            filters: vec![
254                binary_expr(col("i"), Operator::Gt, lit(1)),
255                binary_expr(col("s"), Operator::Eq, lit("x")),
256            ],
257            limit: Some(10),
258            ..Default::default()
259        };
260        assert_eq!(
261            request.to_string(),
262            r#"ScanRequest { filters: [i > Int32(1), s = Utf8("x")], limit: 10 }"#
263        );
264
265        let request = ScanRequest {
266            projection: Some(vec![1, 2]),
267            limit: Some(10),
268            ..Default::default()
269        };
270        assert_eq!(
271            request.to_string(),
272            "ScanRequest { projection: [1, 2], limit: 10 }"
273        );
274
275        let request = ScanRequest {
276            snapshot_on_scan: true,
277            ..Default::default()
278        };
279        assert_eq!(
280            request.to_string(),
281            "ScanRequest { snapshot_on_scan: true }"
282        );
283    }
284}