pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #793: Casting bit vectors to floating-point representations
3+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #793: Casting bit vectors to floating-point representations
@ 2025-03-05 06:51  "kevinhu (@kevinhu)" <noreply+kevinhu@github.com>
  0 siblings, 0 replies; 3+ messages in thread

From: kevinhu (@kevinhu) @ 2025-03-05 06:51 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

I'm wondering if there is a recommended method for casting a `bit` type to `vector` or `halfvec` types. When working with binary embeddings, a common step is to rescore by taking the dot product of a float-based query vector with binary candidates. A cast would make this an easy operation to perform within Postgres.

^ permalink  raw  [nested|flat] 3+ messages in thread

* Re: [pgvector/pgvector] issue #793: Casting bit vectors to floating-point representations
@ 2025-03-05 15:14  "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2025-03-05 15:14 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @kevinhu, you can create SQL functions for this:

```sql
CREATE FUNCTION bit_to_vector(v bit) RETURNS vector AS $$
    SELECT array_agg(get_bit(v, i) * 2 - 1) FROM generate_series(0, bit_length(v) - 1) i
$$ LANGUAGE SQL;

CREATE FUNCTION bit_to_halfvec(v bit) RETURNS halfvec AS $$
    SELECT array_agg(get_bit(v, i) * 2 - 1) FROM generate_series(0, bit_length(v) - 1) i
$$ LANGUAGE SQL;
```

and use:

```sql
SELECT bit_to_vector('101');
SELECT bit_to_halfvec('101');
```

^ permalink  raw  [nested|flat] 3+ messages in thread

* Re: [pgvector/pgvector] issue #793: Casting bit vectors to floating-point representations
@ 2025-03-05 15:14  "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2025-03-05 15:14 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2025-03-05 15:14 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-03-05 06:51 [pgvector/pgvector] issue #793: Casting bit vectors to floating-point representations "kevinhu (@kevinhu)" <noreply+kevinhu@github.com>
2025-03-05 15:14 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-03-05 15:14 ` "ankane (@ankane)" <noreply+ankane@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox