Skip to main content

query_planning/plan/vector_algorithm/
common.rs

1use std::fmt;
2
3use crate::plan::DenseVectorBinding;
4
5/// Row identity emitted alongside vector algorithm results.
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7pub enum VectorRowIdBinding {
8    /// Use the zero-based row ordinal of the evaluated relation.
9    RowIndex,
10    /// Use a non-null integral column from the evaluated relation.
11    Column(String),
12}
13
14impl VectorRowIdBinding {
15    /// Construct a column-backed row identity.
16    pub fn column(name: impl Into<String>) -> Self {
17        Self::Column(name.into())
18    }
19
20    /// Stable diagnostic label.
21    pub(crate) fn diagnostic_label(&self) -> &str {
22        match self {
23            Self::RowIndex => "row_index",
24            Self::Column(name) => name,
25        }
26    }
27}
28
29/// Binds one input relation of a vector algorithm to its row identity and dense vector.
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct VectorRelationBinding {
32    /// Position of the relation among the algorithm node inputs.
33    pub input: usize,
34    /// Row identity emitted by relational results.
35    pub row_id: VectorRowIdBinding,
36    /// Ordered dense vector binding.
37    pub vector: DenseVectorBinding,
38}
39
40impl VectorRelationBinding {
41    /// Construct a vector relation binding.
42    pub fn new(
43        input: usize,
44        row_id: VectorRowIdBinding,
45        feature_columns: impl IntoIterator<Item = impl Into<String>>,
46    ) -> Self {
47        Self { input, row_id, vector: DenseVectorBinding::columns(feature_columns) }
48    }
49
50    /// Construct a binding backed by one list/vector column.
51    pub fn new_vector_column(input: usize, row_id: VectorRowIdBinding, column: impl Into<String>) -> Self {
52        Self { input, row_id, vector: DenseVectorBinding::vector_column(column) }
53    }
54}
55
56/// Distance metric used by exact kNN and K-means.
57#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
58pub enum VectorDistanceMetric {
59    /// Expanded squared L2 distance.
60    L2Expanded,
61    /// Square root of expanded L2 distance.
62    L2SqrtExpanded,
63    /// Expanded cosine distance.
64    CosineExpanded,
65    /// Inner-product distance.
66    InnerProduct,
67}
68
69impl VectorDistanceMetric {
70    /// Stable diagnostic label.
71    pub(crate) const fn as_str(self) -> &'static str {
72        match self {
73            Self::L2Expanded => "l2_expanded",
74            Self::L2SqrtExpanded => "l2_sqrt_expanded",
75            Self::CosineExpanded => "cosine_expanded",
76            Self::InnerProduct => "inner_product",
77        }
78    }
79}
80
81impl fmt::Display for VectorDistanceMetric {
82    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83        f.write_str(self.as_str())
84    }
85}