Open kj3moraes opened 1 year ago
Diesel only supports binary format https://github.com/diesel-rs/diesel/issues/2087 now and it seems unchanged yet.
But Pgvecto.rs not support binary result format now.
Might related: https://github.com/tensorchord/pgvecto.rs/issues/100
For now, You would meet ERROR: no binary output function available for type vector
.
Here is a partial finished example for Diesel
, insert
is fine while select
is still ill.
#[macro_use]
extern crate diesel;
use std::{env, vec};
use chunks::emb;
use diesel::expression::{AsExpression, Expression, TypedExpressionType};
use diesel::{insert_into, Connection, PgConnection, QueryDsl, RunQueryDsl};
use diesel::{pg::Pg, sql_types::*};
diesel::infix_operator!(Emb, " <-> ", backend: Pg);
pub fn dis<T, U, ST>(left: T, right: U) -> Emb<T, U::Expression>
where
T: Expression<SqlType = ST>,
U: AsExpression<ST>,
ST: SqlType + TypedExpressionType,
{
Emb::new(left, right.as_expression())
}
table! {
chunks {
id -> Integer,
emb -> Array<Float>,
}
}
#[derive(Insertable, Queryable, Identifiable, Debug, PartialEq, Selectable)]
#[diesel(table_name = chunks)]
#[diesel(check_for_backend(Pg))]
struct VectorTable {
id: i32,
emb: Vec<f32>,
}
fn main() {
// let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let database_url = "postgresql://postgres:mysecretpassword@localhost:5432".to_string();
let mut connection = PgConnection::establish(&database_url)
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url));
let values: Vec<VectorTable> = vec![
VectorTable {
id: 0,
emb: vec![0.1, 0.2, 0.3],
},
VectorTable {
id: 1,
emb: vec![0.1, 0.2, 0.3],
},
];
let _ = insert_into(chunks::table)
.values(&values)
.execute(&mut connection)
.expect("insert failed");
let filter = chunks::table
.select(emb)
.order(dis(emb, vec![0.1, 0.2, 0.3]))
.load::<Vec<f32>>(&mut connection)
.expect("select failed");
for v in filter {
println!("{:?}", v)
}
}
Will there be support added for Diesel ? It is easier to use Diesel to generate SQL queries.