Skip to content

Commit

Permalink
Number abs operation
Browse files Browse the repository at this point in the history
  • Loading branch information
gs-ssh16 authored and gs-ssh16 committed Jul 26, 2023
1 parent a7b7dcb commit 0d796fa
Show file tree
Hide file tree
Showing 6 changed files with 61 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 @@ -82,6 +82,7 @@
StringTrimExpression,
StringPosExpression,
StringConcatExpression,
AbsoluteExpression,
)


Expand Down Expand Up @@ -336,6 +337,8 @@ def expression_processor(
return extension.process_string_pos_expression(expression, config)
elif isinstance(expression, StringConcatExpression):
return extension.process_string_concat_expression(expression, config)
elif isinstance(expression, AbsoluteExpression):
return extension.process_absolute_expression(expression, config)
else:
raise ValueError("Unsupported expression type: " + str(type(expression))) # pragma: no cover

Expand Down Expand Up @@ -937,6 +940,11 @@ def process_string_concat_expression(self, expr: StringConcatExpression, config:
second=self.process_expression(expr.second, config)
)

def process_absolute_expression(self, expr: AbsoluteExpression, config: SqlToStringConfig) -> str:
return "ABS({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
23 changes: 23 additions & 0 deletions pylegend/core/language/operations/number_operation_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
ComparisonExpression,
NegativeExpression,
)
from pylegend.core.sql.metamodel_extension import (
AbsoluteExpression,
)


__all__: PyLegendSequence[str] = [
Expand All @@ -44,6 +47,7 @@
"PyLegendNumberGreaterThanExpression",
"PyLegendNumberGreaterThanEqualExpression",
"PyLegendNumberNegativeExpression",
"PyLegendNumberAbsoluteExpression",
]


Expand Down Expand Up @@ -232,3 +236,22 @@ def __init__(self, operand: PyLegendExpressionNumberReturn) -> None:
operand,
PyLegendNumberNegativeExpression.__to_sql_func
)


class PyLegendNumberAbsoluteExpression(PyLegendUnaryExpression, PyLegendExpressionNumberReturn):

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

def __init__(self, operand: PyLegendExpressionNumberReturn) -> None:
PyLegendExpressionNumberReturn.__init__(self)
PyLegendUnaryExpression.__init__(
self,
operand,
PyLegendNumberAbsoluteExpression.__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 @@ -37,6 +37,7 @@
PyLegendNumberGreaterThanExpression,
PyLegendNumberGreaterThanEqualExpression,
PyLegendNumberNegativeExpression,
PyLegendNumberAbsoluteExpression,
)
from pylegend.core.sql.metamodel import (
Expression,
Expand Down Expand Up @@ -171,6 +172,9 @@ def __pos__(self) -> "PyLegendNumber":
def __neg__(self) -> "PyLegendNumber":
return PyLegendNumber(PyLegendNumberNegativeExpression(self.__value))

def __abs__(self) -> "PyLegendNumber":
return PyLegendNumber(PyLegendNumberAbsoluteExpression(self.__value))

@staticmethod
def __convert_to_number_expr(
val: PyLegendUnion[int, float, "PyLegendInteger", "PyLegendFloat", "PyLegendNumber"]
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 @@ -29,6 +29,7 @@
"StringTrimExpression",
"StringPosExpression",
"StringConcatExpression",
"AbsoluteExpression",
]


Expand Down Expand Up @@ -125,3 +126,14 @@ def __init__(
super().__init__(_type="stringConcatExpression")
self.first = first
self.second = second


class AbsoluteExpression(Expression):
value: "Expression"

def __init__(
self,
value: "Expression",
) -> None:
super().__init__(_type="absoluteExpression")
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 @@ -79,6 +79,7 @@
StringTrimExpression,
StringPosExpression,
StringConcatExpression,
AbsoluteExpression,
)


Expand Down Expand Up @@ -1208,3 +1209,10 @@ def test_process_string_concat_expression(self) -> None:

expr = StringConcatExpression(StringLiteral("Hello", quoted=False), StringLiteral("World", quoted=False))
assert extension.process_expression(expr, config) == "CONCAT('Hello', 'World')"

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

expr = AbsoluteExpression(IntegerLiteral(-1))
assert extension.process_expression(expr, config) == "ABS(-1)"
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 @@ -113,6 +113,12 @@ def test_number_neg_expr(self) -> None:
assert self.__generate_sql_string(lambda x: -(x.get_number("col2") + x.get_number("col1"))) == \
'(0 - ("root".col2 + "root".col1))'

def test_number_abs_expr(self) -> None:
assert self.__generate_sql_string(lambda x: abs(x.get_number("col2"))) == \
'ABS("root".col2)'
assert self.__generate_sql_string(lambda x: abs(x.get_number("col2") + x.get_number("col1"))) == \
'ABS(("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 0d796fa

Please sign in to comment.