|
| 1 | +# Context and Results |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This repository ([shikhar-gangwar/pgvector-python](https://github.com/shikhar-gangwar/pgvector-python)) is an attributed MIT-licensed fork of [pgvector/pgvector-python](https://github.com/pgvector/pgvector-python) by Andrew Kane. It preserves all upstream functionality, copyright notices, and MIT license terms while adding SQLAlchemy `BIT` literal-bind compilation support, `pgvector.Bit` value normalization, documentation, and verified local Windows + Docker execution results. |
| 6 | + |
| 7 | +## Problem Context and Implemented Fix |
| 8 | + |
| 9 | +### Root Cause |
| 10 | +In `pgvector/sqlalchemy/bit.py`, `VECTOR`, `HALFVEC`, and `SPARSEVEC` implemented `literal_processor` for SQLAlchemy, whereas `BIT` inherited the default from SQLAlchemy's `PGBit` (which returns `None`). Compiling queries with `compile_kwargs={'literal_binds': True}` (for example, `hamming_distance` or `jaccard_distance` expressions on a `BIT` column) failed with: |
| 11 | + |
| 12 | +```text |
| 13 | +sqlalchemy.exc.CompileError: No literal value renderer is available for literal value "'101'" with datatype BIT |
| 14 | +``` |
| 15 | + |
| 16 | +Additionally, passing a `pgvector.Bit` instance directly into SQLAlchemy `BIT` bind/literal paths required explicit conversion to bit-string text. |
| 17 | + |
| 18 | +### Code Changes (`pgvector/sqlalchemy/bit.py` & `tests/test_sqlalchemy.py`) |
| 19 | +- **`pgvector/sqlalchemy/bit.py`**: |
| 20 | + - Added `_string = String()` and implemented `BIT.literal_processor(self, dialect: Dialect)` using `self._string._cached_literal_processor(dialect)` while converting `pgvector.Bit` instances via `.to_text()`. |
| 21 | + - Updated `BIT.process_bind_param` so `pgvector.Bit` instances are converted via `.to_text()` before `asyncpg.BitString` conversion or return. |
| 22 | +- **`tests/test_sqlalchemy.py`**: |
| 23 | + - Added regression assertions inside `TestSqlalchemy.test_literal_binds` verifying `compile_kwargs={'literal_binds': True}` for: |
| 24 | + - `Item.binary_embedding.hamming_distance('101')` |
| 25 | + - `Item.binary_embedding.hamming_distance(Bit('101'))` |
| 26 | + - `Item.binary_embedding.jaccard_distance('101')` |
| 27 | + |
| 28 | +## Local Environment and Step-by-Step Execution (Windows + Docker) |
| 29 | + |
| 30 | +### Environment Verified |
| 31 | +- **OS / Shell**: Windows (PowerShell) |
| 32 | +- **Python**: `3.14.3` inside `pgvector-python\.venv` |
| 33 | +- **Package / Dependencies**: Editable install of `pgvector-0.5.1` with `dev`, `numpy`, and `scipy` dependency groups from `pyproject.toml` |
| 34 | +- **Database**: Docker container `pgvector/pgvector:pg17` exposed on `localhost:5432` |
| 35 | + |
| 36 | +### Step-by-Step Setup & Run Commands |
| 37 | + |
| 38 | +1. **Start PostgreSQL with `pgvector` in Docker** |
| 39 | + ```powershell |
| 40 | + docker run -d --name pgvector-test ` |
| 41 | + -e POSTGRES_USER=bit ` |
| 42 | + -e POSTGRES_DB=pgvector_python_test ` |
| 43 | + -e POSTGRES_HOST_AUTH_METHOD=trust ` |
| 44 | + -p 5432:5432 ` |
| 45 | + pgvector/pgvector:pg17 |
| 46 | + docker exec -it pgvector-test createdb -U bit pgvector_example |
| 47 | + ``` |
| 48 | + |
| 49 | +2. **Create and populate the virtual environment (from `pgvector-python`)** |
| 50 | + ```powershell |
| 51 | + cd "pgvector-python" |
| 52 | + python -m venv .venv |
| 53 | + .\.venv\Scripts\python.exe -m pip install -e . --group dev --group numpy --group scipy |
| 54 | + ``` |
| 55 | + |
| 56 | +3. **Run offline unit tests (no database required)** |
| 57 | + ```powershell |
| 58 | + .\.venv\Scripts\python.exe -m pytest tests/test_bit.py tests/test_half_vector.py tests/test_sparse_vector.py tests/test_vector.py |
| 59 | + ``` |
| 60 | + |
| 61 | +4. **Run the full 581-test suite on Windows** |
| 62 | + On Windows, Python's default `asyncio.ProactorEventLoop` is rejected by `psycopg` async connections (`psycopg.InterfaceError`). Running `pytest` with `WindowsSelectorEventLoopPolicy` enables all sync and async tests across all drivers and ORMs: |
| 63 | + ```powershell |
| 64 | + $env:PGHOST = "localhost" |
| 65 | + $env:PGUSER = "bit" |
| 66 | + .\.venv\Scripts\python.exe -c "import asyncio, sys, pytest; asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()); sys.exit(pytest.main())" |
| 67 | + ``` |
| 68 | + |
| 69 | +5. **Run the bulk loading example (`examples/loading/example.py`)** |
| 70 | + ```powershell |
| 71 | + $env:PGHOST = "localhost" |
| 72 | + $env:PGUSER = "bit" |
| 73 | + .\.venv\Scripts\python.exe examples\loading\example.py |
| 74 | + ``` |
| 75 | + |
| 76 | +## Observed Test and Run Results |
| 77 | + |
| 78 | +| Check | Command / Mode | Result | Notes | |
| 79 | +| --- | --- | --- | --- | |
| 80 | +| Offline unit tests | `pytest tests/test_bit.py tests/test_half_vector.py tests/test_sparse_vector.py tests/test_vector.py` | **122 passed** (`1.25s`) | Pure Python, NumPy, and SciPy vector/bit tests | |
| 81 | +| Offline SQLAlchemy `BIT` compilation | `Item.binary_embedding.hamming_distance('101' / Bit('101'))` & `jaccard_distance('101')` with `literal_binds=True` | **Passed** | Compiled cleanly to `<~> '101'` and `<%%> '101'` | |
| 82 | +| Full suite (default Windows event loop) | `.\.venv\Scripts\python.exe -m pytest` | **568 passed, 13 failed** (`67.54s`) | All 13 failures were `psycopg` async tests due to Windows default `ProactorEventLoop` | |
| 83 | +| Full suite (`WindowsSelectorEventLoopPolicy`) | `pytest.main()` with `WindowsSelectorEventLoopPolicy` | **581 passed** (`25.43s`) | All 12 test modules passed (`asyncpg`, `bit`, `django`, `half_vector`, `peewee`, `pg8000`, `psycopg`, `psycopg2`, `sparse_vector`, `sqlalchemy`, `sqlmodel`, `vector`) | |
| 84 | +| Bulk loading example | `.\.venv\Scripts\python.exe examples\loading\example.py` | **Success** | Loaded `1000000` 128-dimensional vector rows via binary `COPY` into `pgvector_example` | |
0 commit comments