Skip to content

Commit

Permalink
Handle window function frame cause appropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Dec 19, 2024
1 parent 9d56d33 commit 4390e63
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions metricflow-semantics/metricflow_semantics/sql/sql_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,20 @@ def requires_ordering(self) -> bool:
else:
assert_values_exhausted(self)

@property
def allows_frame_clause(self) -> bool:
"""Whether the function allows a frame clause, e.g., 'ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING'."""
if (
self is SqlWindowFunction.FIRST_VALUE
or self is SqlWindowFunction.LAST_VALUE
or self is SqlWindowFunction.AVERAGE
):
return True
if self is SqlWindowFunction.ROW_NUMBER or self is SqlWindowFunction.LAG:
return False
else:
assert_values_exhausted(self)

@classmethod
def get_window_function_for_period_agg(cls, period_agg: PeriodAggregation) -> SqlWindowFunction:
"""Get the window function to use for given period agg option."""
Expand Down
2 changes: 1 addition & 1 deletion metricflow/sql/render/expr_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def visit_window_function_expr(self, node: SqlWindowFunctionExpression) -> SqlEx
)
)

if len(order_by_args_rendered) > 0:
if len(order_by_args_rendered) > 0 and node.sql_function.allows_frame_clause:
window_string_lines.append("ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING")

window_string = "\n".join(window_string_lines)
Expand Down

0 comments on commit 4390e63

Please sign in to comment.