Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -854,3 +854,14 @@ pip install --group dev
createdb pgvector_example
python3 example.py
```

## Fork Attribution and Changes

This repository is an attributed fork of [pgvector/pgvector-python](https://github.com/pgvector/pgvector-python) by Andrew Kane, licensed under the MIT License. All original copyright notices, license conditions, and upstream attribution are preserved.

### Specific Changes in this Fork
- **SQLAlchemy `BIT` literal binds and object support (`pgvector/sqlalchemy/bit.py`)**:
- Implemented `literal_processor` for `BIT` in SQLAlchemy to support compiling queries with `compile_kwargs={'literal_binds': True}`. Previously, while `VECTOR`, `HALFVEC`, and `SPARSEVEC` implemented `literal_processor`, `BIT` inherited the default from `PGBit` (which returns `None`), causing `sqlalchemy.exc.CompileError: No literal value renderer is available for literal value "'101'" with datatype BIT`.
- Added support in `BIT.process_bind_param` and `BIT.literal_processor` for `pgvector.Bit` instances by converting them to bit string text, allowing `Bit` objects to be passed cleanly across SQLAlchemy queries and expressions.
- Added regression test assertions to `test_literal_binds` in `tests/test_sqlalchemy.py`.

17 changes: 15 additions & 2 deletions pgvector/sqlalchemy/bit.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
from sqlalchemy.dialects.postgresql.base import PGBit
from sqlalchemy.types import TypeDecorator, Float
from sqlalchemy import Operators
from sqlalchemy.types import TypeDecorator, Float, String
from sqlalchemy import Dialect, Operators
from typing import Any
from .. import Bit


class BIT(TypeDecorator[Any]):
impl = PGBit
cache_ok = True
_string = String()

def process_bind_param(self, value: Any, dialect: Any) -> Any:
if isinstance(value, Bit):
value = value.to_text()
if dialect.__class__.__name__ == 'PGDialect_asyncpg' and isinstance(value, str):
import asyncpg
return asyncpg.BitString(value) # type: ignore
return value

def literal_processor(self, dialect: Dialect) -> Any:
string_literal_processor = self._string._cached_literal_processor(dialect)

def process(value: Any) -> Any:
if isinstance(value, Bit):
value = value.to_text()
return string_literal_processor(value) # type: ignore
return process

class Comparator(TypeDecorator.Comparator[Any]): # type: ignore
def hamming_distance(self, other: object, /) -> Operators:
return self.op('<~>', return_type=Float)(other)
Expand Down
11 changes: 10 additions & 1 deletion tests/test_sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncpg
from getpass import getuser
from pgvector import SparseVector
from pgvector import Bit, SparseVector
from pgvector.sqlalchemy import VECTOR, HALFVEC, BIT, SPARSEVEC, avg, sum
import pytest
from sqlalchemy import create_engine, event, insert, inspect, literal, select, text, MetaData, Table, Column, Index, Integer, ARRAY, Engine
Expand Down Expand Up @@ -518,6 +518,15 @@ def test_literal_binds(self, engine: Engine) -> None:
sql = select(Item).order_by(Item.embedding.l2_distance([1, 2, 3])).compile(engine, compile_kwargs={'literal_binds': True})
assert "embedding <-> '[1.0,2.0,3.0]'" in str(sql)

sql = select(Item).order_by(Item.binary_embedding.hamming_distance('101')).compile(engine, compile_kwargs={'literal_binds': True})
assert "binary_embedding <~> '101'" in str(sql)

sql = select(Item).order_by(Item.binary_embedding.hamming_distance(Bit('101'))).compile(engine, compile_kwargs={'literal_binds': True})
assert "binary_embedding <~> '101'" in str(sql)

sql = select(Item).order_by(Item.binary_embedding.jaccard_distance('101')).compile(engine, compile_kwargs={'literal_binds': True})
assert "binary_embedding <%> '101'" in str(sql) or "binary_embedding <%%> '101'" in str(sql)

def test_insert(self, engine: Engine) -> None:
with Session(engine) as session:
session.execute(insert(Item).values(embedding=[1, 2, 3]))
Expand Down