From 1bf2842dca1d595d80f6cd9f56d84cc3872c8e17 Mon Sep 17 00:00:00 2001 From: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Date: Fri, 20 May 2022 13:04:10 -0700 Subject: [PATCH] feat(duckdb): implement NthValue --- ibis/backends/base/sql/alchemy/registry.py | 6 ++++++ ibis/backends/tests/test_window.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/ibis/backends/base/sql/alchemy/registry.py b/ibis/backends/base/sql/alchemy/registry.py index 5b63ca1eec5e..34af082ea2cd 100644 --- a/ibis/backends/base/sql/alchemy/registry.py +++ b/ibis/backends/base/sql/alchemy/registry.py @@ -580,6 +580,11 @@ def _string_join(t, expr): } +def _nth_value(t, expr): + op = expr.op() + return sa.func.nth_value(t.translate(op.arg), t.translate(op.nth) + 1) + + sqlalchemy_window_functions_registry = { ops.Lag: _lag, ops.Lead: _lead, @@ -590,6 +595,7 @@ def _string_join(t, expr): ops.DenseRank: unary(lambda arg: sa.func.dense_rank()), ops.MinRank: unary(lambda arg: sa.func.rank()), ops.PercentRank: unary(lambda arg: sa.func.percent_rank()), + ops.NthValue: _nth_value, ops.Window: _window, ops.CumulativeOp: _window, ops.CumulativeMax: unary(sa.func.max), diff --git a/ibis/backends/tests/test_window.py b/ibis/backends/tests/test_window.py index 75f576837451..9c629a45b5fd 100644 --- a/ibis/backends/tests/test_window.py +++ b/ibis/backends/tests/test_window.py @@ -69,6 +69,23 @@ def calc_zscore(s): lambda t: t.float_col.transform('last'), id='last', ), + param( + lambda t, win: t.float_col.nth(3).over(win), + lambda t: t.float_col.apply( + lambda s: pd.concat( + [ + pd.Series(np.nan, index=s.index[:3], dtype="float32"), + pd.Series( + s.iloc[3], + index=s.index[3:], + dtype="float32", + ), + ] + ) + ), + id="nth", + marks=pytest.mark.notimpl(["pandas"]), + ), param( lambda _, win: ibis.row_number().over(win), lambda t: t.cumcount(),