Skip to content

Commit 31295dd

Browse files
cpcloudjcrist
authored andcommitted
fix(ir): convert analytic functions to window functions in filters
1 parent b63e0fd commit 31295dd

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

ibis/expr/rewrites.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def lower_stringslice(_, **kwargs):
223223

224224

225225
@replace(p.Analytic)
226-
def project_wrap_analytic(_, rel):
226+
def wrap_analytic(_, **__):
227227
# Wrap analytic functions in a window function
228228
return ops.WindowFunction(_)
229229

@@ -250,7 +250,7 @@ def rewrite_project_input(value, relation):
250250
# or scalar subqueries depending on whether they are originating from the
251251
# relation
252252
return value.replace(
253-
project_wrap_analytic | project_wrap_reduction,
253+
wrap_analytic | project_wrap_reduction,
254254
filter=p.Value & ~p.WindowFunction,
255255
context={"rel": relation},
256256
)
@@ -272,7 +272,9 @@ def filter_wrap_reduction(_):
272272

273273

274274
def rewrite_filter_input(value):
275-
return value.replace(filter_wrap_reduction, filter=p.Value & ~p.WindowFunction)
275+
return value.replace(
276+
wrap_analytic | filter_wrap_reduction, filter=p.Value & ~p.WindowFunction
277+
)
276278

277279

278280
@replace(p.Analytic | p.Reduction)

ibis/expr/tests/test_newrels.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,3 +1829,12 @@ def test_expr_in_join_projection():
18291829
"lit6": "int64",
18301830
}
18311831
)
1832+
1833+
1834+
def test_analytic_dereference():
1835+
t = ibis.table({"a": "int"})
1836+
ix = ibis.row_number()
1837+
expr = t.mutate(ix=ix).filter(ix == 5)
1838+
assert expr.op().predicates == (
1839+
ops.Equals(ops.WindowFunction(ops.RowNumber()), ops.Literal(5, dtype="int8")),
1840+
)

0 commit comments

Comments
 (0)