Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More number operations #66

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions pylegend/core/databse/sql_to_string/db_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@
StringConcatExpression,
AbsoluteExpression,
PowerExpression,
CeilExpression,
FloorExpression,
SqrtExpression,
CbrtExpression,
ExpExpression,
LogExpression,
)


Expand Down Expand Up @@ -342,6 +348,18 @@ def expression_processor(
return extension.process_absolute_expression(expression, config)
elif isinstance(expression, PowerExpression):
return extension.process_power_expression(expression, config)
elif isinstance(expression, CeilExpression):
return extension.process_ceil_expression(expression, config)
elif isinstance(expression, FloorExpression):
return extension.process_floor_expression(expression, config)
elif isinstance(expression, SqrtExpression):
return extension.process_sqrt_expression(expression, config)
elif isinstance(expression, CbrtExpression):
return extension.process_cbrt_expression(expression, config)
elif isinstance(expression, ExpExpression):
return extension.process_exp_expression(expression, config)
elif isinstance(expression, LogExpression):
return extension.process_log_expression(expression, config)
else:
raise ValueError("Unsupported expression type: " + str(type(expression))) # pragma: no cover

Expand Down Expand Up @@ -954,6 +972,36 @@ def process_power_expression(self, expr: PowerExpression, config: SqlToStringCon
second=self.process_expression(expr.second, config)
)

def process_ceil_expression(self, expr: CeilExpression, config: SqlToStringConfig) -> str:
return "CEIL({value})".format(
value=self.process_expression(expr.value, config)
)

def process_floor_expression(self, expr: FloorExpression, config: SqlToStringConfig) -> str:
return "FLOOR({value})".format(
value=self.process_expression(expr.value, config)
)

def process_sqrt_expression(self, expr: SqrtExpression, config: SqlToStringConfig) -> str:
return "SQRT({value})".format(
value=self.process_expression(expr.value, config)
)

def process_cbrt_expression(self, expr: CbrtExpression, config: SqlToStringConfig) -> str:
return "CBRT({value})".format(
value=self.process_expression(expr.value, config)
)

def process_exp_expression(self, expr: ExpExpression, config: SqlToStringConfig) -> str:
return "EXP({value})".format(
value=self.process_expression(expr.value, config)
)

def process_log_expression(self, expr: LogExpression, config: SqlToStringConfig) -> str:
return "LN({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
127 changes: 127 additions & 0 deletions pylegend/core/language/operations/number_operation_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
)
from pylegend.core.language.expression import (
PyLegendExpressionNumberReturn,
PyLegendExpressionIntegerReturn,
PyLegendExpressionBooleanReturn,
)
from pylegend.core.language.operations.binary_expression import PyLegendBinaryExpression
Expand All @@ -35,6 +36,12 @@
from pylegend.core.sql.metamodel_extension import (
AbsoluteExpression,
PowerExpression,
CeilExpression,
FloorExpression,
SqrtExpression,
CbrtExpression,
ExpExpression,
LogExpression,
)


Expand All @@ -50,6 +57,12 @@
"PyLegendNumberNegativeExpression",
"PyLegendNumberAbsoluteExpression",
"PyLegendNumberPowerExpression",
"PyLegendNumberCeilExpression",
"PyLegendNumberFloorExpression",
"PyLegendNumberSqrtExpression",
"PyLegendNumberCbrtExpression",
"PyLegendNumberExpExpression",
"PyLegendNumberLogExpression",
]


Expand Down Expand Up @@ -278,3 +291,117 @@ def __init__(self, operand1: PyLegendExpressionNumberReturn, operand2: PyLegendE
operand2,
PyLegendNumberPowerExpression.__to_sql_func
)


class PyLegendNumberCeilExpression(PyLegendUnaryExpression, PyLegendExpressionIntegerReturn):

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

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


class PyLegendNumberFloorExpression(PyLegendUnaryExpression, PyLegendExpressionIntegerReturn):

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

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


class PyLegendNumberSqrtExpression(PyLegendUnaryExpression, PyLegendExpressionNumberReturn):

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

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


class PyLegendNumberCbrtExpression(PyLegendUnaryExpression, PyLegendExpressionNumberReturn):

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

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


class PyLegendNumberExpExpression(PyLegendUnaryExpression, PyLegendExpressionNumberReturn):

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

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


class PyLegendNumberLogExpression(PyLegendUnaryExpression, PyLegendExpressionNumberReturn):

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

def __init__(self, operand: PyLegendExpressionNumberReturn) -> None:
PyLegendExpressionNumberReturn.__init__(self)
PyLegendUnaryExpression.__init__(
self,
operand,
PyLegendNumberLogExpression.__to_sql_func
)
26 changes: 26 additions & 0 deletions pylegend/core/language/primitives/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
PyLegendNumberNegativeExpression,
PyLegendNumberAbsoluteExpression,
PyLegendNumberPowerExpression,
PyLegendNumberCeilExpression,
PyLegendNumberFloorExpression,
PyLegendNumberSqrtExpression,
PyLegendNumberCbrtExpression,
PyLegendNumberExpExpression,
PyLegendNumberLogExpression,
)
from pylegend.core.sql.metamodel import (
Expression,
Expand Down Expand Up @@ -192,6 +198,26 @@ def __rpow__(
other_op = PyLegendNumber.__convert_to_number_expr(other)
return PyLegendNumber(PyLegendNumberPowerExpression(other_op, self.__value))

def ceil(self) -> "PyLegendInteger":
from pylegend.core.language.primitives.integer import PyLegendInteger
return PyLegendInteger(PyLegendNumberCeilExpression(self.__value))

def floor(self) -> "PyLegendInteger":
from pylegend.core.language.primitives.integer import PyLegendInteger
return PyLegendInteger(PyLegendNumberFloorExpression(self.__value))

def sqrt(self) -> "PyLegendNumber":
return PyLegendNumber(PyLegendNumberSqrtExpression(self.__value))

def cbrt(self) -> "PyLegendNumber":
return PyLegendNumber(PyLegendNumberCbrtExpression(self.__value))

def exp(self) -> "PyLegendNumber":
return PyLegendNumber(PyLegendNumberExpExpression(self.__value))

def log(self) -> "PyLegendNumber":
return PyLegendNumber(PyLegendNumberLogExpression(self.__value))

@staticmethod
def __convert_to_number_expr(
val: PyLegendUnion[int, float, "PyLegendInteger", "PyLegendFloat", "PyLegendNumber"]
Expand Down
72 changes: 72 additions & 0 deletions pylegend/core/sql/metamodel_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
"StringConcatExpression",
"AbsoluteExpression",
"PowerExpression",
"CeilExpression",
"FloorExpression",
"SqrtExpression",
"CbrtExpression",
"ExpExpression",
"LogExpression",
]


Expand Down Expand Up @@ -152,3 +158,69 @@ def __init__(
super().__init__(_type="powerExpression")
self.first = first
self.second = second


class CeilExpression(Expression):
value: "Expression"

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


class FloorExpression(Expression):
value: "Expression"

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


class SqrtExpression(Expression):
value: "Expression"

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


class CbrtExpression(Expression):
value: "Expression"

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


class ExpExpression(Expression):
value: "Expression"

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


class LogExpression(Expression):
value: "Expression"

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