Coder Social home page Coder Social logo

Diesel Support about pgvecto.rs HOT 1 OPEN

tensorchord avatar tensorchord commented on May 21, 2024
Diesel Support

from pgvecto.rs.

Comments (1)

cutecutecat avatar cutecutecat commented on May 21, 2024

Diesel only supports binary format diesel-rs/diesel#2087 now and it seems unchanged yet.
But Pgvecto.rs not support binary result format now.
Might related: #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)
    }
}

from pgvecto.rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.