Skip to content

Commit 3d20b82

Browse files
committed
Expose row_number window function
1 parent 6a1b3ea commit 3d20b82

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

python/datafusion/functions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,3 +1781,13 @@ def lag(arg: Expr, shift_offset: int = 1, default_value: Optional[Any] = None) -
17811781
default_value = pa.scalar(default_value)
17821782

17831783
return Expr(f.lag(arg.expr, shift_offset, default_value))
1784+
1785+
1786+
def row_number() -> Expr:
1787+
"""Create a row number window function.
1788+
1789+
Returns the row number of the window function. To set window function parameters
1790+
use the window builder approach described in the ref:`_window_functions` online
1791+
documentation.
1792+
"""
1793+
return Expr(f.row_number())

python/datafusion/tests/test_dataframe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def test_distinct():
279279

280280

281281
data_test_window_functions = [
282-
("row", f.window("row_number", [], order_by=[f.order_by(column("c"))]), [2, 1, 3]),
282+
("row", f.row_number().order_by(column("c").sort()).build(), [2, 1, 3]),
283283
("rank", f.window("rank", [], order_by=[f.order_by(column("c"))]), [2, 1, 2]),
284284
(
285285
"dense_rank",

src/functions.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,11 @@ pub fn lag(arg: PyExpr, shift_offset: i64, default_value: Option<ScalarValue>) -
869869
window_function::lag(arg.expr, Some(shift_offset), default_value).into()
870870
}
871871

872+
#[pyfunction]
873+
pub fn row_number() -> PyExpr {
874+
window_function::row_number().into()
875+
}
876+
872877
pub(crate) fn init_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
873878
m.add_wrapped(wrap_pyfunction!(abs))?;
874879
m.add_wrapped(wrap_pyfunction!(acos))?;
@@ -1055,6 +1060,7 @@ pub(crate) fn init_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
10551060

10561061
m.add_wrapped(wrap_pyfunction!(lead))?;
10571062
m.add_wrapped(wrap_pyfunction!(lag))?;
1063+
m.add_wrapped(wrap_pyfunction!(row_number))?;
10581064

10591065
Ok(())
10601066
}

0 commit comments

Comments
 (0)