Skip to content

Commit dc7e3e2

Browse files
committed
Switched to TypeDecorator for bit type for SQLAlchemy
1 parent 27a16c2 commit dc7e3e2

1 file changed

Lines changed: 11 additions & 29 deletions

File tree

pgvector/sqlalchemy/bit.py

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,24 @@
1-
from sqlalchemy.dialects.postgresql.base import ischema_names
2-
from sqlalchemy.types import UserDefinedType, TypeEngine, Float
3-
from sqlalchemy import Dialect, Operators
1+
from sqlalchemy.dialects.postgresql.base import PGBit
2+
from sqlalchemy.types import TypeDecorator, Float
3+
from sqlalchemy import Operators
44
from typing import Any
55

66

7-
class BIT(UserDefinedType[Any]):
7+
class BIT(TypeDecorator[Any]):
8+
impl = PGBit
89
cache_ok = True
910

10-
def __init__(self, length: int | None = None) -> None:
11-
super().__init__()
12-
self.length = length
13-
14-
def get_col_spec(self, **kw: Any) -> str:
15-
if self.length is None:
16-
return 'BIT'
17-
return 'BIT(%d)' % self.length
18-
19-
def bind_processor(self, dialect: Dialect) -> Any:
20-
if dialect.__class__.__name__ == 'PGDialect_asyncpg':
11+
def process_bind_param(self, value: Any, dialect: Any) -> Any:
12+
if dialect.__class__.__name__ == 'PGDialect_asyncpg' and isinstance(value, str):
2113
import asyncpg
14+
return asyncpg.BitString(value) # type: ignore
15+
return value
2216

23-
def process(value: Any) -> Any:
24-
if isinstance(value, str):
25-
return asyncpg.BitString(value) # type: ignore
26-
return value
27-
return process
28-
else:
29-
return super().bind_processor(dialect)
30-
31-
class Comparator(TypeEngine.Comparator[Any]):
17+
class Comparator(TypeDecorator.Comparator[Any]):
3218
def hamming_distance(self, other: object) -> Operators:
3319
return self.op('<~>', return_type=Float)(other)
3420

3521
def jaccard_distance(self, other: object) -> Operators:
3622
return self.op('<%>', return_type=Float)(other)
3723

38-
comparator_factory = Comparator
39-
40-
41-
# for reflection
42-
ischema_names['bit'] = BIT # type: ignore
24+
comparator_factory = Comparator # type: ignore

0 commit comments

Comments
 (0)