Skip to content

Added support for Bit objects with Psycopg 2 and pg8000 - #170

Open
astroknot-sheep wants to merge 1 commit into
pgvector:masterfrom
astroknot-sheep:bit-adapters-psycopg2-pg8000
Open

astroknot-sheep wants to merge 1 commit into
pgvector:masterfrom
astroknot-sheep:bit-adapters-psycopg2-pg8000

Conversation

@astroknot-sheep

Copy link
Copy Markdown

Fixes #169

Bit objects can be passed as query parameters with Psycopg 3, but not with Psycopg 2 or pg8000. This adds:

  • pgvector/psycopg2/bit.py with a BitAdapter, matching HalfvecAdapter / SparsevecAdapter, registered in register_vector
  • a Bit out adapter for pg8000 using to_text(), alongside the existing Vector one

asyncpg is unchanged (asyncpg.BitString per #110).

Tests: test_bit_class for Psycopg 2 and pg8000 (both fail on master). Full suite passes locally (527 passed, Postgres 17), plus mypy and ty check.

Written with AI assistance; I have reviewed and tested the changes.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@astroknot-sheep this works on both drivers and brings them in line with Psycopg 3, which already adapts Bit. Reverting both register.py files fails the new tests with the #169 errors, and <~>, = and & resolve against a Bit on both drivers.

# Postgres 17 + pgvector (docker.io/pgvector/pgvector:pg17), CPython 3.12, pgvector imported from the exported tree
$ python -m pytest tests -q -p no:cacheprovider        # head 4764b5b
519 passed, 8 skipped in 29.96s

$ python -m pytest tests/test_psycopg2.py tests/test_pg8000.py -q -p no:cacheprovider -k bit_class   # head, both register.py from e0f3ac3
E       psycopg2.ProgrammingError: can't adapt type 'Bit'
E           pg8000.exceptions.DatabaseError: {... 'M': '"i" is not a valid binary digit', ...}
2 failed, 15 deselected in 0.27s

# probe at head: SELECT '111'::bit(3) <~> p1, '1010'::varbit = p2, p3::bit(3) & '011'::bit(3)
# with p1..p3 = Bit("101"), Bit("1010"), Bit("110")
psycopg2 (1.0, True, '010')
pg8000   [[1.0, True, '010']]

# asyncpg 0.32.0 at head, after register_vector(conn)
SELECT $1::bit(3) -> DataError invalid input for query argument $1: Bit(101) (a bytes-like object is required, not 'Bit')

# not run: mypy, ty

non-blocking: asyncpg has the same gap, since its register_vector sets no codec for bit. I'd keep this PR to the two drivers #169 names and track asyncpg in its own issue. Shall I open it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Bit objects not supported with Psycopg 2 and pg8000

2 participants