Skip to content

Commit

Permalink
refactor(clickhouse): use a pattern for one-to-zero index conversion …
Browse files Browse the repository at this point in the history
…of ranking window functions
  • Loading branch information
cpcloud committed Oct 12, 2023
1 parent 8df3ea1 commit 732c031
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
9 changes: 9 additions & 0 deletions ibis/backends/clickhouse/compiler/core.py
Expand Up @@ -113,13 +113,22 @@ def fn(node, _, **kwargs):
replace_notany_with_min_not = p.NotAny(x, where=y) >> c.Min(c.Not(x), where=y)
replace_notall_with_max_not = p.NotAll(x, where=y) >> c.Max(c.Not(x), where=y)

# subtract one from ranking functions to convert from 1-indexed to 0-indexed
subtract_one_from_ranking_functions = p.WindowFunction(
p.RankBase | p.NTile
) >> c.Subtract(_, 1)

add_one_to_nth_value_input = p.NthValue >> _.copy(nth=c.Add(_.nth, 1))

op = op.replace(
replace_literals
| replace_in_column_with_table_array_view
| replace_empty_in_values_with_false
| replace_notexists_subquery_with_not_exists
| replace_notany_with_min_not
| replace_notall_with_max_not
| subtract_one_from_ranking_functions
| add_one_to_nth_value_input
)
# apply translate rules in topological order
results = op.map(fn, filter=(ops.TableNode, ops.Value))
Expand Down
10 changes: 3 additions & 7 deletions ibis/backends/clickhouse/compiler/values.py
Expand Up @@ -569,7 +569,7 @@ def _struct_field(op, *, arg, field: str, **_):

@translate_val.register(ops.NthValue)
def _nth_value(op, *, arg, nth, **_):
return F.nth_value(arg, _parenthesize(op.nth, nth) + 1)
return F.nth_value(arg, _parenthesize(op.nth, nth))


@translate_val.register(ops.Repeat)
Expand Down Expand Up @@ -898,12 +898,8 @@ def _window_frame(op, *, group_by, order_by, start, end, max_lookback=None, **_)

@translate_val.register(ops.WindowFunction)
def _window(op: ops.WindowFunction, *, func, frame, **_: Any):
window = frame(this=func)

# preserve zero-based indexing
if isinstance(op.func, ops.RankBase):
return window - 1
return window
# frame is a partial call to sg.exp.Window
return frame(this=func)


def shift_like(op_class, func):
Expand Down

0 comments on commit 732c031

Please sign in to comment.