Skip to content

Commit 344bd82

Browse files
docs: add context, results, and Windows/Docker setup to README
1 parent 02e4c15 commit 344bd82

2 files changed

Lines changed: 103 additions & 5 deletions

File tree

‎CONTEXT_AND_RESULTS.md‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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` |

‎README.md‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -839,13 +839,23 @@ Everyone is encouraged to help improve this project. Here are a few ways you can
839839
To get started with development:
840840

841841
```sh
842-
git clone https://github.com/pgvector/pgvector-python.git
842+
git clone https://github.com/shikhar-gangwar/pgvector-python.git
843843
cd pgvector-python
844-
pip install --group dev
844+
pip install -e . --group dev --group numpy --group scipy
845845
createdb pgvector_python_test
846846
pytest
847847
```
848848

849+
On Windows with Docker Desktop (`pgvector/pgvector:pg17`), start PostgreSQL and run the full suite with `WindowsSelectorEventLoopPolicy` (required for `psycopg` async tests on Windows):
850+
851+
```powershell
852+
docker run -d --name pgvector-test -e POSTGRES_USER=bit -e POSTGRES_DB=pgvector_python_test -e POSTGRES_HOST_AUTH_METHOD=trust -p 5432:5432 pgvector/pgvector:pg17
853+
docker exec -it pgvector-test createdb -U bit pgvector_example
854+
$env:PGHOST = "localhost"
855+
$env:PGUSER = "bit"
856+
.\.venv\Scripts\python.exe -c "import asyncio, sys, pytest; asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()); sys.exit(pytest.main())"
857+
```
858+
849859
To run an example:
850860

851861
```sh
@@ -855,13 +865,17 @@ createdb pgvector_example
855865
python3 example.py
856866
```
857867

858-
## Fork Attribution and Changes
868+
## Fork Attribution, Context, and Results
869+
870+
This repository is an attributed fork of [pgvector/pgvector-python](https://github.com/pgvector/pgvector-python) by Andrew Kane, licensed under the MIT License ([LICENSE.txt](LICENSE.txt)). All original copyright notices, license conditions, and upstream attribution are preserved.
859871

860-
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.
872+
For full technical context, implementation notes, and verified test/example outputs, see [CONTEXT_AND_RESULTS.md](CONTEXT_AND_RESULTS.md).
861873

862-
### Specific Changes in this Fork
874+
### Specific Changes in this Repository
863875
- **SQLAlchemy `BIT` literal binds and object support (`pgvector/sqlalchemy/bit.py`)**:
864876
- 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`.
865877
- 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.
866878
- Added regression test assertions to `test_literal_binds` in `tests/test_sqlalchemy.py`.
879+
- **Context, Verification, and Documentation (`CONTEXT_AND_RESULTS.md`, `README.md`)**:
880+
- Verified all **581 tests** pass against `pgvector/pgvector:pg17` (including all sync/async driver and ORM suites when using `WindowsSelectorEventLoopPolicy` on Windows) and verified `examples/loading/example.py` loading 1,000,000 rows into PostgreSQL.
867881

0 commit comments

Comments
 (0)