Restore 0.1.5 version from stash

This commit is contained in:
liaibo
2025-12-08 19:56:24 +08:00
parent de189e938d
commit 8db3f4e32d
8578 changed files with 2703426 additions and 217 deletions
@@ -0,0 +1,19 @@
from .bit import BIT
from .functions import avg, sum
from .halfvec import HALFVEC
from .sparsevec import SPARSEVEC
from .vector import VECTOR
from .vector import VECTOR as Vector
from ..utils import HalfVector, SparseVector
__all__ = [
'Vector',
'VECTOR',
'HALFVEC',
'BIT',
'SPARSEVEC',
'HalfVector',
'SparseVector',
'avg',
'sum'
]
@@ -0,0 +1,26 @@
from sqlalchemy.dialects.postgresql.base import ischema_names
from sqlalchemy.types import UserDefinedType, Float
class BIT(UserDefinedType):
cache_ok = True
def __init__(self, length=None):
super(UserDefinedType, self).__init__()
self.length = length
def get_col_spec(self, **kw):
if self.length is None:
return 'BIT'
return 'BIT(%d)' % self.length
class comparator_factory(UserDefinedType.Comparator):
def hamming_distance(self, other):
return self.op('<~>', return_type=Float)(other)
def jaccard_distance(self, other):
return self.op('<%>', return_type=Float)(other)
# for reflection
ischema_names['bit'] = BIT
@@ -0,0 +1,14 @@
# https://docs.sqlalchemy.org/en/20/core/functions.html
# include sum for a consistent API
from sqlalchemy.sql.functions import ReturnTypeFromArgs, sum
class avg(ReturnTypeFromArgs):
inherit_cache = True
package = 'pgvector'
__all__ = [
'avg',
'sum'
]
@@ -0,0 +1,51 @@
from sqlalchemy.dialects.postgresql.base import ischema_names
from sqlalchemy.types import UserDefinedType, Float, String
from ..utils import HalfVector
class HALFVEC(UserDefinedType):
cache_ok = True
_string = String()
def __init__(self, dim=None):
super(UserDefinedType, self).__init__()
self.dim = dim
def get_col_spec(self, **kw):
if self.dim is None:
return 'HALFVEC'
return 'HALFVEC(%d)' % self.dim
def bind_processor(self, dialect):
def process(value):
return HalfVector._to_db(value, self.dim)
return process
def literal_processor(self, dialect):
string_literal_processor = self._string._cached_literal_processor(dialect)
def process(value):
return string_literal_processor(HalfVector._to_db(value, self.dim))
return process
def result_processor(self, dialect, coltype):
def process(value):
return HalfVector._from_db(value)
return process
class comparator_factory(UserDefinedType.Comparator):
def l2_distance(self, other):
return self.op('<->', return_type=Float)(other)
def max_inner_product(self, other):
return self.op('<#>', return_type=Float)(other)
def cosine_distance(self, other):
return self.op('<=>', return_type=Float)(other)
def l1_distance(self, other):
return self.op('<+>', return_type=Float)(other)
# for reflection
ischema_names['halfvec'] = HALFVEC
@@ -0,0 +1,51 @@
from sqlalchemy.dialects.postgresql.base import ischema_names
from sqlalchemy.types import UserDefinedType, Float, String
from ..utils import SparseVector
class SPARSEVEC(UserDefinedType):
cache_ok = True
_string = String()
def __init__(self, dim=None):
super(UserDefinedType, self).__init__()
self.dim = dim
def get_col_spec(self, **kw):
if self.dim is None:
return 'SPARSEVEC'
return 'SPARSEVEC(%d)' % self.dim
def bind_processor(self, dialect):
def process(value):
return SparseVector._to_db(value, self.dim)
return process
def literal_processor(self, dialect):
string_literal_processor = self._string._cached_literal_processor(dialect)
def process(value):
return string_literal_processor(SparseVector._to_db(value, self.dim))
return process
def result_processor(self, dialect, coltype):
def process(value):
return SparseVector._from_db(value)
return process
class comparator_factory(UserDefinedType.Comparator):
def l2_distance(self, other):
return self.op('<->', return_type=Float)(other)
def max_inner_product(self, other):
return self.op('<#>', return_type=Float)(other)
def cosine_distance(self, other):
return self.op('<=>', return_type=Float)(other)
def l1_distance(self, other):
return self.op('<+>', return_type=Float)(other)
# for reflection
ischema_names['sparsevec'] = SPARSEVEC
@@ -0,0 +1,51 @@
from sqlalchemy.dialects.postgresql.base import ischema_names
from sqlalchemy.types import UserDefinedType, Float, String
from ..utils import Vector
class VECTOR(UserDefinedType):
cache_ok = True
_string = String()
def __init__(self, dim=None):
super(UserDefinedType, self).__init__()
self.dim = dim
def get_col_spec(self, **kw):
if self.dim is None:
return 'VECTOR'
return 'VECTOR(%d)' % self.dim
def bind_processor(self, dialect):
def process(value):
return Vector._to_db(value, self.dim)
return process
def literal_processor(self, dialect):
string_literal_processor = self._string._cached_literal_processor(dialect)
def process(value):
return string_literal_processor(Vector._to_db(value, self.dim))
return process
def result_processor(self, dialect, coltype):
def process(value):
return Vector._from_db(value)
return process
class comparator_factory(UserDefinedType.Comparator):
def l2_distance(self, other):
return self.op('<->', return_type=Float)(other)
def max_inner_product(self, other):
return self.op('<#>', return_type=Float)(other)
def cosine_distance(self, other):
return self.op('<=>', return_type=Float)(other)
def l1_distance(self, other):
return self.op('<+>', return_type=Float)(other)
# for reflection
ischema_names['vector'] = VECTOR