-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for REAL_VECTOR utility functions
Closes #325
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# pylint: disable=invalid-name | ||
"""Custom SQL functions for SAP HANA.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Generic | ||
|
||
from sqlalchemy import Float, Integer | ||
from sqlalchemy.sql.functions import GenericFunction | ||
|
||
from sqlalchemy_hana.types import _RV, REAL_VECTOR | ||
|
||
|
||
class cardinality(GenericFunction[int]): | ||
type = Integer() | ||
inherit_cache = True | ||
_has_args = True | ||
|
||
|
||
class cosine_similarity(GenericFunction[float]): | ||
type = Float() | ||
inherit_cache = True | ||
_has_args = True | ||
|
||
|
||
class l2distance(GenericFunction[float]): | ||
type = Float() | ||
inherit_cache = True | ||
_has_args = True | ||
|
||
|
||
class to_real_vector(GenericFunction[_RV], Generic[_RV]): | ||
type = REAL_VECTOR[_RV]() | ||
inherit_cache = True | ||
_has_args = True | ||
|
||
|
||
__all__ = ("cardinality", "cosine_similarity", "l2distance", "to_real_vector") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""SQL function tests.""" | ||
|
||
from __future__ import annotations | ||
|
||
from sqlalchemy import select | ||
from sqlalchemy.testing.assertions import eq_ | ||
from sqlalchemy.testing.fixtures import TestBase | ||
|
||
from sqlalchemy_hana.functions import ( | ||
cardinality, | ||
cosine_similarity, | ||
l2distance, | ||
to_real_vector, | ||
) | ||
|
||
|
||
class FunctionTest(TestBase): | ||
|
||
def test_cardinality(self, connection): | ||
res = connection.execute(select(cardinality(to_real_vector("[1, 2, 3]")))).one() | ||
eq_(res, (3,)) | ||
|
||
def test_cosine_similarity(self, connection): | ||
res = connection.execute( | ||
select( | ||
cosine_similarity( | ||
to_real_vector("[1, 0, 0]"), to_real_vector("[0.5, 0.8660254, 0]") | ||
) | ||
) | ||
).one() | ||
eq_(res, (0.5,)) | ||
|
||
def test_l2distance(self, connection): | ||
res = connection.execute( | ||
select(l2distance(to_real_vector("[2, 3, 5]"), to_real_vector("[6, 6, 5]"))) | ||
).one() | ||
eq_(res, (5,)) |