Skip to content

Commit

Permalink
Moved expression methods to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed May 5, 2024
1 parent 3fccb3b commit 7c2aa1b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 44 deletions.
44 changes: 44 additions & 0 deletions src/diesel_ext/expression_methods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use diesel::expression::{AsExpression, Expression};
use diesel::pg::Pg;
use diesel::sql_types::{Double, SqlType};

diesel::infix_operator!(L2Distance, " <-> ", Double, backend: Pg);
diesel::infix_operator!(MaxInnerProduct, " <#> ", Double, backend: Pg);
diesel::infix_operator!(CosineDistance, " <=> ", Double, backend: Pg);
diesel::infix_operator!(L1Distance, " <+> ", Double, backend: Pg);

pub trait VectorExpressionMethods: Expression + Sized {
fn l2_distance<T>(self, other: T) -> L2Distance<Self, T::Expression>
where
Self::SqlType: SqlType,
T: AsExpression<Self::SqlType>,
{
L2Distance::new(self, other.as_expression())
}

fn max_inner_product<T>(self, other: T) -> MaxInnerProduct<Self, T::Expression>
where
Self::SqlType: SqlType,
T: AsExpression<Self::SqlType>,
{
MaxInnerProduct::new(self, other.as_expression())
}

fn cosine_distance<T>(self, other: T) -> CosineDistance<Self, T::Expression>
where
Self::SqlType: SqlType,
T: AsExpression<Self::SqlType>,
{
CosineDistance::new(self, other.as_expression())
}

fn l1_distance<T>(self, other: T) -> L1Distance<Self, T::Expression>
where
Self::SqlType: SqlType,
T: AsExpression<Self::SqlType>,
{
L1Distance::new(self, other.as_expression())
}
}

impl<T: Expression> VectorExpressionMethods for T {}
1 change: 1 addition & 0 deletions src/diesel_ext/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) mod expression_methods;
pub(crate) mod vector;

#[cfg(feature = "halfvec")]
Expand Down
44 changes: 1 addition & 43 deletions src/diesel_ext/vector.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use diesel::deserialize::{self, FromSql};
use diesel::expression::{AsExpression, Expression};
use diesel::pg::{Pg, PgValue};
use diesel::query_builder::QueryId;
use diesel::serialize::{self, IsNull, Output, ToSql};
use diesel::sql_types::{Double, SqlType};
use diesel::sql_types::SqlType;
use std::convert::TryFrom;
use std::io::Write;

Expand Down Expand Up @@ -33,47 +32,6 @@ impl FromSql<VectorType, Pg> for Vector {
}
}

diesel::infix_operator!(L2Distance, " <-> ", Double, backend: Pg);
diesel::infix_operator!(MaxInnerProduct, " <#> ", Double, backend: Pg);
diesel::infix_operator!(CosineDistance, " <=> ", Double, backend: Pg);
diesel::infix_operator!(L1Distance, " <+> ", Double, backend: Pg);

pub trait VectorExpressionMethods: Expression + Sized {
fn l2_distance<T>(self, other: T) -> L2Distance<Self, T::Expression>
where
Self::SqlType: SqlType,
T: AsExpression<Self::SqlType>,
{
L2Distance::new(self, other.as_expression())
}

fn max_inner_product<T>(self, other: T) -> MaxInnerProduct<Self, T::Expression>
where
Self::SqlType: SqlType,
T: AsExpression<Self::SqlType>,
{
MaxInnerProduct::new(self, other.as_expression())
}

fn cosine_distance<T>(self, other: T) -> CosineDistance<Self, T::Expression>
where
Self::SqlType: SqlType,
T: AsExpression<Self::SqlType>,
{
CosineDistance::new(self, other.as_expression())
}

fn l1_distance<T>(self, other: T) -> L1Distance<Self, T::Expression>
where
Self::SqlType: SqlType,
T: AsExpression<Self::SqlType>,
{
L1Distance::new(self, other.as_expression())
}
}

impl<T: Expression> VectorExpressionMethods for T {}

#[cfg(test)]
mod tests {
use crate::{Vector, VectorExpressionMethods};
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ pub mod sql_types {
}

#[cfg(feature = "diesel")]
pub use diesel_ext::vector::VectorExpressionMethods;
pub use diesel_ext::expression_methods::VectorExpressionMethods;

0 comments on commit 7c2aa1b

Please sign in to comment.