Skip to content

Commit

Permalink
fix(snowflake): bring back default nth behavior from before the-epic-…
Browse files Browse the repository at this point in the history
…split
  • Loading branch information
cpcloud authored and gforsyth committed Mar 11, 2024
1 parent 8c8b565 commit e943667
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
42 changes: 42 additions & 0 deletions ibis/backends/snowflake/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,48 @@ def visit_Xor(self, op, *, left, right):
# boolxor accepts numerics ... and returns a boolean? wtf?
return self.f.boolxor(self.cast(left, dt.int8), self.cast(right, dt.int8))

def visit_Window(self, op, *, how, func, start, end, group_by, order_by):
if start is None:
start = {}
if end is None:
end = {}

start_value = start.get("value", "UNBOUNDED")
start_side = start.get("side", "PRECEDING")
end_value = end.get("value", "UNBOUNDED")
end_side = end.get("side", "FOLLOWING")

if getattr(start_value, "this", None) == "0":
start_value = "CURRENT ROW"
start_side = None

if getattr(end_value, "this", None) == "0":
end_value = "CURRENT ROW"
end_side = None

spec = sge.WindowSpec(
kind=how.upper(),
start=start_value,
start_side=start_side,
end=end_value,
end_side=end_side,
over="OVER",
)
order = sge.Order(expressions=order_by) if order_by else None

orig_spec = spec
spec = self._minimize_spec(op.start, op.end, orig_spec)

# due to https://docs.snowflake.com/en/sql-reference/functions-analytic#window-frame-usage-notes
# we need to make the default window rows (since range isn't supported)
# and we need to make the default frame unbounded preceding to current
# row
if spec is None and isinstance(op.func, (ops.First, ops.Last, ops.NthValue)):
spec = orig_spec
spec.args["kind"] = "ROWS"

return sge.Window(this=func, partition_by=group_by, order=order, spec=spec)

def visit_WindowBoundary(self, op, *, value, preceding):
if not isinstance(op.value, ops.Literal):
raise com.OperationNotDefinedError(
Expand Down
5 changes: 0 additions & 5 deletions ibis/backends/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,6 @@ def calc_zscore(s):
pytest.mark.notimpl(["dask"], raises=com.OperationNotDefinedError),
pytest.mark.notimpl(["flink"], raises=com.OperationNotDefinedError),
pytest.mark.notimpl(["risingwave"], raises=PsycoPg2InternalError),
pytest.mark.broken(
["snowflake"],
raises=AssertionError,
reason="behavior of windows with nth_value has changed; unclear whether that is in ibis or in snowflake itself",
),
],
),
param(
Expand Down

0 comments on commit e943667

Please sign in to comment.