Skip to content

Commit 2b5e53d

Browse files
committed
feat: add processor methods to BIT class
1 parent 05349f9 commit 2b5e53d

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

pgvector/sqlalchemy/bit.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import asyncpg
2+
from sqlalchemy.dialects.postgresql.asyncpg import PGDialect_asyncpg
13
from sqlalchemy.dialects.postgresql.base import ischema_names
24
from sqlalchemy.types import UserDefinedType, Float
3-
5+
from .. import Bit
46

57
class BIT(UserDefinedType):
68
cache_ok = True
@@ -14,6 +16,21 @@ def get_col_spec(self, **kw):
1416
return 'BIT'
1517
return 'BIT(%d)' % self.length
1618

19+
def bind_processor(self, dialect):
20+
def process(value):
21+
value = Bit._to_db(value)
22+
if value and isinstance(dialect, PGDialect_asyncpg):
23+
return asyncpg.BitString(value)
24+
return value
25+
return process
26+
27+
def result_processor(self, dialect, coltype):
28+
def process(value):
29+
if value and isinstance(dialect, PGDialect_asyncpg):
30+
return value.as_string()
31+
return Bit._from_db(value).to_text()
32+
return process
33+
1734
class comparator_factory(UserDefinedType.Comparator):
1835
def hamming_distance(self, other):
1936
return self.op('<~>', return_type=Float)(other)

0 commit comments

Comments
 (0)