From 1b5dbba763537c07f7a7d5228a846350f20a559a Mon Sep 17 00:00:00 2001 From: WHIPLASH-25210 Date: Sat, 21 Nov 2026 19:19:22 +0530 Subject: [PATCH 1/2] fix(sqlalchemy): support BIT literal binds --- pgvector/sqlalchemy/bit.py | 17 +++++++++++++++-- tests/test_sqlalchemy.py | 11 ++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/pgvector/sqlalchemy/bit.py b/pgvector/sqlalchemy/bit.py index a5af3b0..fca0412 100644 --- a/pgvector/sqlalchemy/bit.py +++ b/pgvector/sqlalchemy/bit.py @@ -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) diff --git a/tests/test_sqlalchemy.py b/tests/test_sqlalchemy.py index dc58aeb..4469a13 100644 --- a/tests/test_sqlalchemy.py +++ b/tests/test_sqlalchemy.py @@ -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 @@ -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])) From 02e4c157aca219fadad5675534207bfc4b963cf8 Mon Sep 17 00:00:00 2001 From: WHIPLASH-25210 Date: Sat, 21 Nov 2026 19:19:22 +0530 Subject: [PATCH 2/2] docs: document fork changes --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 26dccfb..0f55711 100644 --- a/README.md +++ b/README.md @@ -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`. +