Skip to content

Commit

Permalink
Number cos/acos operations
Browse files Browse the repository at this point in the history
  • Loading branch information
gs-ssh16 authored and gs-ssh16 committed Jul 27, 2023
1 parent 9f6a76c commit 3a403c4
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pylegend/core/databse/sql_to_string/db_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
RoundExpression,
SineExpression,
ArcSineExpression,
CosineExpression,
ArcCosineExpression,
)


Expand Down Expand Up @@ -372,6 +374,10 @@ def expression_processor(
return extension.process_sine_expression(expression, config)
elif isinstance(expression, ArcSineExpression):
return extension.process_arc_sine_expression(expression, config)
elif isinstance(expression, CosineExpression):
return extension.process_cosine_expression(expression, config)
elif isinstance(expression, ArcCosineExpression):
return extension.process_arc_cosine_expression(expression, config)
else:
raise ValueError("Unsupported expression type: " + str(type(expression))) # pragma: no cover

Expand Down Expand Up @@ -1048,6 +1054,16 @@ def process_arc_sine_expression(self, expr: ArcSineExpression, config: SqlToStri
value=self.process_expression(expr.value, config)
)

def process_cosine_expression(self, expr: CosineExpression, config: SqlToStringConfig) -> str:
return "COS({value})".format(
value=self.process_expression(expr.value, config)
)

def process_arc_cosine_expression(self, expr: ArcCosineExpression, config: SqlToStringConfig) -> str:
return "ACOS({value})".format(
value=self.process_expression(expr.value, config)
)

def process_qualified_name(self, qualified_name: QualifiedName, config: SqlToStringConfig) -> str:
return qualified_name_processor(qualified_name, self, config)

Expand Down
42 changes: 42 additions & 0 deletions pylegend/core/language/operations/number_operation_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
RoundExpression,
SineExpression,
ArcSineExpression,
CosineExpression,
ArcCosineExpression,
)


Expand All @@ -71,6 +73,8 @@
"PyLegendNumberRoundExpression",
"PyLegendNumberSineExpression",
"PyLegendNumberArcSineExpression",
"PyLegendNumberCosineExpression",
"PyLegendNumberArcCosineExpression",
]


Expand Down Expand Up @@ -493,3 +497,41 @@ def __init__(self, operand: PyLegendExpressionNumberReturn) -> None:
operand,
PyLegendNumberArcSineExpression.__to_sql_func
)


class PyLegendNumberCosineExpression(PyLegendUnaryExpression, PyLegendExpressionNumberReturn):

@staticmethod
def __to_sql_func(
expression: Expression,
frame_name_to_base_query_map: PyLegendDict[str, QuerySpecification],
config: FrameToSqlConfig
) -> Expression:
return CosineExpression(expression)

def __init__(self, operand: PyLegendExpressionNumberReturn) -> None:
PyLegendExpressionNumberReturn.__init__(self)
PyLegendUnaryExpression.__init__(
self,
operand,
PyLegendNumberCosineExpression.__to_sql_func
)


class PyLegendNumberArcCosineExpression(PyLegendUnaryExpression, PyLegendExpressionNumberReturn):

@staticmethod
def __to_sql_func(
expression: Expression,
frame_name_to_base_query_map: PyLegendDict[str, QuerySpecification],
config: FrameToSqlConfig
) -> Expression:
return ArcCosineExpression(expression)

def __init__(self, operand: PyLegendExpressionNumberReturn) -> None:
PyLegendExpressionNumberReturn.__init__(self)
PyLegendUnaryExpression.__init__(
self,
operand,
PyLegendNumberArcCosineExpression.__to_sql_func
)
8 changes: 8 additions & 0 deletions pylegend/core/language/primitives/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
PyLegendNumberRoundExpression,
PyLegendNumberSineExpression,
PyLegendNumberArcSineExpression,
PyLegendNumberCosineExpression,
PyLegendNumberArcCosineExpression,
)
from pylegend.core.sql.metamodel import (
Expression,
Expand Down Expand Up @@ -243,6 +245,12 @@ def sin(self) -> "PyLegendNumber":
def asin(self) -> "PyLegendNumber":
return PyLegendNumber(PyLegendNumberArcSineExpression(self.__value))

def cos(self) -> "PyLegendNumber":
return PyLegendNumber(PyLegendNumberCosineExpression(self.__value))

def acos(self) -> "PyLegendNumber":
return PyLegendNumber(PyLegendNumberArcCosineExpression(self.__value))

def round(
self,
n: PyLegendOptional[int] = None
Expand Down
24 changes: 24 additions & 0 deletions pylegend/core/sql/metamodel_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
"RoundExpression",
"SineExpression",
"ArcSineExpression",
"CosineExpression",
"ArcCosineExpression",
]


Expand Down Expand Up @@ -279,3 +281,25 @@ def __init__(
) -> None:
super().__init__(_type="arcSineExpression")
self.value = value


class CosineExpression(Expression):
value: "Expression"

def __init__(
self,
value: "Expression",
) -> None:
super().__init__(_type="cosineExpression")
self.value = value


class ArcCosineExpression(Expression):
value: "Expression"

def __init__(
self,
value: "Expression",
) -> None:
super().__init__(_type="arcCosineExpression")
self.value = value
16 changes: 16 additions & 0 deletions pylegend/tests/core/database/test_sql_to_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
RoundExpression,
SineExpression,
ArcSineExpression,
CosineExpression,
ArcCosineExpression,
)


Expand Down Expand Up @@ -1319,3 +1321,17 @@ def test_process_arc_sine_expression(self) -> None:

expr = ArcSineExpression(IntegerLiteral(10))
assert extension.process_expression(expr, config) == "ASIN(10)"

def test_process_cosine_expression(self) -> None:
extension = SqlToStringDbExtension()
config = SqlToStringConfig(SqlToStringFormat(pretty=False))

expr = CosineExpression(IntegerLiteral(10))
assert extension.process_expression(expr, config) == "COS(10)"

def test_process_arc_cosine_expression(self) -> None:
extension = SqlToStringDbExtension()
config = SqlToStringConfig(SqlToStringFormat(pretty=False))

expr = ArcCosineExpression(IntegerLiteral(10))
assert extension.process_expression(expr, config) == "ACOS(10)"
12 changes: 12 additions & 0 deletions pylegend/tests/core/language/primitives/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ def test_number_arc_sine_expr(self) -> None:
assert self.__generate_sql_string(lambda x: (x.get_number("col2") + x.get_number("col1")).asin()) == \
'ASIN(("root".col2 + "root".col1))'

def test_number_cosine_expr(self) -> None:
assert self.__generate_sql_string(lambda x: x.get_number("col2").cos()) == \
'COS("root".col2)'
assert self.__generate_sql_string(lambda x: (x.get_number("col2") + x.get_number("col1")).cos()) == \
'COS(("root".col2 + "root".col1))'

def test_number_arc_cosine_expr(self) -> None:
assert self.__generate_sql_string(lambda x: x.get_number("col2").acos()) == \
'ACOS("root".col2)'
assert self.__generate_sql_string(lambda x: (x.get_number("col2") + x.get_number("col1")).acos()) == \
'ACOS(("root".col2 + "root".col1))'

def __generate_sql_string(self, f) -> str: # type: ignore
return self.db_extension.process_expression(
f(self.tds_row).to_sql_expression({"t": self.base_query}, self.frame_to_sql_config),
Expand Down

0 comments on commit 3a403c4

Please sign in to comment.