diff --git a/ibis/backends/tests/test_aggregation.py b/ibis/backends/tests/test_aggregation.py index ddaea304de63..6e4ed54e6ae5 100644 --- a/ibis/backends/tests/test_aggregation.py +++ b/ibis/backends/tests/test_aggregation.py @@ -659,6 +659,45 @@ def test_first_last_ordered(alltypes, method, filtered, include_null): assert res == sol +@pytest.mark.notimpl( + ["databricks"], + raises=com.UnsupportedOperationError, +) +@pytest.mark.notimpl( + ["polars"], + raises=com.OperationNotDefinedError, +) +@pytest.mark.notimpl( + ["risingwave"], + raises=PsycoPg2InternalError, + reason="Feature is not yet implemented:", +) +@pytest.mark.parametrize( + "method,expected", + [ + pytest.param(lambda col: col.first(order_by="ob"), 4, id="first_asc"), + pytest.param(lambda col: col.last(order_by="ob"), 5, id="last_asc"), + pytest.param( + lambda col: col.first(order_by=ibis._.ob.desc()), 5, id="first_desc" + ), + pytest.param( + lambda col: col.last(order_by=ibis._.ob.desc()), 4, id="last_desc" + ), + ], +) +def test_first_last_ordered_in_mutate(alltypes, con, method, expected): + # originally reported in https://github.com/ibis-project/ibis/issues/11656 + t = alltypes.select( + a=ibis._.tinyint_col, val=ibis._.int_col, ob=ibis._.bigint_col + ).filter( + ((ibis._.val == 4) & (ibis._.ob == 40)) + | ((ibis._.val == 5) & (ibis._.ob == 50)) + ) + expr = t.mutate(new=method(t.val)).limit(10) + actual = con.to_pyarrow(expr.new).to_pylist() + assert actual == [expected] * 10 + + @pytest.mark.notimpl( [ "druid", diff --git a/ibis/expr/rewrites.py b/ibis/expr/rewrites.py index 1f4bee2de6fe..03ccd5fb16ed 100644 --- a/ibis/expr/rewrites.py +++ b/ibis/expr/rewrites.py @@ -261,7 +261,7 @@ def project_wrap_reduction(_, rel): if _.relations == {rel}: # The reduction is fully originating from the `rel`, so turn # it into a window function of `rel` - return ops.WindowFunction(_) + return ops.WindowFunction(_, order_by=getattr(_, "order_by", ())) else: # 1. The reduction doesn't depend on any table, constructed from # scalar values, so turn it into a scalar subquery.