Skip to content

Conversation

@A2va
Copy link

@A2va A2va commented Oct 21, 2025

This will enable some usage, as disccused in this issue

CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3072));
CREATE INDEX ON items USING hnsw ((embedding::halfvec(3072)) halfvec_cosine_ops);

-- and query with
SELECT * FROM items ORDER BY embedding::halfvec(3072) <=> '[1,2,3]' LIMIT 5;

Taken this snippet

let mut conn = establish_connection();
let embed = HalfVector::from_f32_slice(&[1.0, 2.0, 5.0]);
let neighbors = items::table
    .order(
        items::embedding
            .cast::<Nullable<pgvector::sql_types::HalfVector>>()
            .l2_distance(embed.clone()),
    )
    .load::<Item>(&mut conn)
    .unwrap();

It produces the following query:
SELECT "items"."id", "items_diesel"."embedding" FROM "items" ORDER BY CAST("items_diesel"."embedding" AS halfvec) <-> $1 LIMIT $2 -- binds: [HalfVector([1.0, 2.0, 5.0]), 5]
But I actually don't now if not providing the dimension have an impact ?

Second question on the binary vector, for the example in the following query present in the docs:
SELECT * FROM items ORDER BY binary_quantize(embedding)::bit(3) <~> binary_quantize('[1,-2,3]') LIMIT 5;
Is the cast necessary? The documentation mentioned that binary_quantize already returns a bit vector.

@ankane
Copy link
Member

ankane commented Oct 22, 2025

Hi @A2va, thanks for the PR. Casts need to include dimensions in both cases for Postgres to use a vector index, so I'm not sure it makes sense to add without that.

@A2va
Copy link
Author

A2va commented Oct 22, 2025

Ok, I will see what I can do.

@A2va
Copy link
Author

A2va commented Oct 22, 2025

Since it's not possible to use the diesel bultin cast function, I have added a cast_to_halfvec on VectorExpressionMethods

@ankane
Copy link
Member

ankane commented Oct 25, 2025

Thanks @A2va. I think it'd be better to submit a generic casting function to Diesel for this (as casting with a type modifier isn't specific to pgvector or Postgres).

@A2va A2va closed this Oct 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants