Skip to content

Commit

Permalink
Date Min aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
gs-ssh16 authored and gs-ssh16 committed Oct 22, 2023
1 parent 7065a84 commit 3d14991
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"PyLegendStrictDateMaxExpression",
"PyLegendStrictDateMinExpression",
"PyLegendDateMaxExpression",
"PyLegendDateMinExpression",
]


Expand Down Expand Up @@ -491,3 +492,22 @@ def __init__(self, operand: PyLegendExpressionDateReturn) -> None:
operand,
PyLegendDateMaxExpression.__to_sql_func
)


class PyLegendDateMinExpression(PyLegendUnaryExpression, PyLegendExpressionDateReturn):

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

def __init__(self, operand: PyLegendExpressionDateReturn) -> None:
PyLegendExpressionDateReturn.__init__(self)
PyLegendUnaryExpression.__init__(
self,
operand,
PyLegendDateMinExpression.__to_sql_func
)
8 changes: 8 additions & 0 deletions pylegend/core/language/primitive_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
PyLegendStrictDateMaxExpression,
PyLegendStrictDateMinExpression,
PyLegendDateMaxExpression,
PyLegendDateMinExpression,
)


Expand Down Expand Up @@ -271,6 +272,13 @@ def max(self) -> "PyLegendDate":
)
return PyLegendDate(PyLegendDateMaxExpression(nested_expr)) # type: ignore

def min(self) -> "PyLegendDate":
nested_expr = (
convert_literal_to_literal_expression(self.__nested) if isinstance(self.__nested, (date, datetime))
else self.__nested.value()
)
return PyLegendDate(PyLegendDateMinExpression(nested_expr)) # type: ignore


class PyLegendDateTimeCollection(PyLegendDateCollection):
__nested: PyLegendUnion[datetime, PyLegendDateTime]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,29 @@ def test_sql_gen_group_by_date_max_agg(self) -> None:
"root".col1'''
assert frame.to_sql_query(FrameToSqlConfig()) == dedent(expected)

def test_sql_gen_group_by_date_min_agg(self) -> None:
columns = [
PrimitiveTdsColumn.number_column("col1"),
PrimitiveTdsColumn.date_column("col2")
]
frame: LegendApiTdsFrame = LegendApiTableSpecInputFrame(['test_schema', 'test_table'], columns)
frame = frame.group_by(
["col1"],
[AggregateSpecification(lambda x: x["col2"], lambda y: y.min(), "Minimum")] # type: ignore
)
assert "[" + ", ".join([str(c) for c in frame.columns()]) + "]" == (
"[TdsColumn(Name: col1, Type: Number), TdsColumn(Name: Minimum, Type: Date)]"
)
expected = '''\
SELECT
"root".col1 AS "col1",
MIN("root".col2) AS "Minimum"
FROM
test_schema.test_table AS "root"
GROUP BY
"root".col1'''
assert frame.to_sql_query(FrameToSqlConfig()) == dedent(expected)

def test_e2e_group_by(self, legend_test_server: PyLegendDict[str, PyLegendUnion[int, ]]) -> None:
frame: LegendApiTdsFrame = simple_person_service_frame(legend_test_server['engine_port'])
frame = frame.group_by(
Expand Down Expand Up @@ -1570,3 +1593,25 @@ def test_e2e_group_by_date_max_agg(self, legend_test_server: PyLegendDict[str, P
{'values': ['Firm X', '2014-12-02T21:00:00.000000000+0000']}]}
res = frame.execute_frame_to_string()
assert json.loads(res)["result"] == expected

def test_e2e_group_by_date_min_agg(self, legend_test_server: PyLegendDict[str, PyLegendUnion[int, ]]) -> None:
frame: LegendApiTdsFrame = simple_trade_service_frame(legend_test_server['engine_port'])
frame = frame.group_by(
["Product/Name"],
[
AggregateSpecification(
lambda x: x['Settlement Date Time'],
lambda y: y.min(), # type: ignore
'Min Date Time'
)
]
)
assert "[" + ", ".join([str(c) for c in frame.columns()]) + "]" == \
"[TdsColumn(Name: Product/Name, Type: String), TdsColumn(Name: Min Date Time, Type: Date)]"
expected = {'columns': ['Product/Name', 'Min Date Time'],
'rows': [{'values': [None, None]},
{'values': ['Firm A', '2014-12-02T21:00:00.000000000+0000']},
{'values': ['Firm C', '2014-12-04T15:22:23.123456789+0000']},
{'values': ['Firm X', '2014-12-02T21:00:00.000000000+0000']}]}
res = frame.execute_frame_to_string()
assert json.loads(res)["result"] == expected

0 comments on commit 3d14991

Please sign in to comment.