Skip to content

Commit

Permalink
Number cot operation
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 190f44e commit d1ed956
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pylegend/core/databse/sql_to_string/db_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
ArcCosineExpression,
TanExpression,
ArcTanExpression,
CotExpression,
)


Expand Down Expand Up @@ -384,6 +385,8 @@ def expression_processor(
return extension.process_tan_expression(expression, config)
elif isinstance(expression, ArcTanExpression):
return extension.process_arc_tan_expression(expression, config)
elif isinstance(expression, CotExpression):
return extension.process_cot_expression(expression, config)
else:
raise ValueError("Unsupported expression type: " + str(type(expression))) # pragma: no cover

Expand Down Expand Up @@ -1080,6 +1083,11 @@ def process_arc_tan_expression(self, expr: ArcTanExpression, config: SqlToString
value=self.process_expression(expr.value, config)
)

def process_cot_expression(self, expr: CotExpression, config: SqlToStringConfig) -> str:
return "COT({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
21 changes: 21 additions & 0 deletions pylegend/core/language/operations/number_operation_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
ArcCosineExpression,
TanExpression,
ArcTanExpression,
CotExpression,
)


Expand Down Expand Up @@ -79,6 +80,7 @@
"PyLegendNumberArcCosineExpression",
"PyLegendNumberTanExpression",
"PyLegendNumberArcTanExpression",
"PyLegendNumberCotExpression",
]


Expand Down Expand Up @@ -577,3 +579,22 @@ def __init__(self, operand: PyLegendExpressionNumberReturn) -> None:
operand,
PyLegendNumberArcTanExpression.__to_sql_func
)


class PyLegendNumberCotExpression(PyLegendUnaryExpression, PyLegendExpressionNumberReturn):

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

def __init__(self, operand: PyLegendExpressionNumberReturn) -> None:
PyLegendExpressionNumberReturn.__init__(self)
PyLegendUnaryExpression.__init__(
self,
operand,
PyLegendNumberCotExpression.__to_sql_func
)
4 changes: 4 additions & 0 deletions pylegend/core/language/primitives/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
PyLegendNumberArcCosineExpression,
PyLegendNumberTanExpression,
PyLegendNumberArcTanExpression,
PyLegendNumberCotExpression,
)
from pylegend.core.sql.metamodel import (
Expression,
Expand Down Expand Up @@ -259,6 +260,9 @@ def tan(self) -> "PyLegendNumber":
def atan(self) -> "PyLegendNumber":
return PyLegendNumber(PyLegendNumberArcTanExpression(self.__value))

def cot(self) -> "PyLegendNumber":
return PyLegendNumber(PyLegendNumberCotExpression(self.__value))

def round(
self,
n: PyLegendOptional[int] = None
Expand Down
12 changes: 12 additions & 0 deletions pylegend/core/sql/metamodel_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"ArcCosineExpression",
"TanExpression",
"ArcTanExpression",
"CotExpression",
]


Expand Down Expand Up @@ -327,3 +328,14 @@ def __init__(
) -> None:
super().__init__(_type="arcTanExpression")
self.value = value


class CotExpression(Expression):
value: "Expression"

def __init__(
self,
value: "Expression",
) -> None:
super().__init__(_type="cotExpression")
self.value = value
8 changes: 8 additions & 0 deletions pylegend/tests/core/database/test_sql_to_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
ArcCosineExpression,
TanExpression,
ArcTanExpression,
CotExpression,
)


Expand Down Expand Up @@ -1351,3 +1352,10 @@ def test_process_arc_tan_expression(self) -> None:

expr = ArcTanExpression(DoubleLiteral(0.5))
assert extension.process_expression(expr, config) == "ATAN(0.5)"

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

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

def test_number_cot_expr(self) -> None:
assert self.__generate_sql_string(lambda x: x.get_number("col2").cot()) == \
'COT("root".col2)'
assert self.__generate_sql_string(lambda x: (x.get_number("col2") + x.get_number("col1")).cot()) == \
'COT(("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 d1ed956

Please sign in to comment.