-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathhalfvec.py
More file actions
30 lines (21 loc) · 993 Bytes
/
Copy pathhalfvec.py
File metadata and controls
30 lines (21 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from psycopg2.extensions import adapt, connection, cursor, new_array_type, new_type, register_adapter, register_type
from typing import Any
from .. import HalfVector
class HalfvecAdapter:
def __init__(self, value: HalfVector) -> None:
self._value = value
def getquoted(self) -> Any:
return adapt(self._value.to_text()).getquoted()
def cast_halfvec(value: str | bytes | None, cur: cursor) -> HalfVector | None:
if value is None:
return None
if isinstance(value, bytes):
raise ValueError('expected str')
return HalfVector.from_text(value)
def register_halfvec_info(oid: int, array_oid: int | None, scope: connection | cursor | None, /) -> None:
halfvec = new_type((oid,), 'HALFVEC', cast_halfvec)
register_type(halfvec, scope)
if array_oid is not None:
halfvecarray = new_array_type((array_oid,), 'HALFVECARRAY', halfvec)
register_type(halfvecarray, scope)
register_adapter(HalfVector, HalfvecAdapter)