Skip to content

Commit

Permalink
Change last_value to use function builder instead of explicitly passi…
Browse files Browse the repository at this point in the history
…ng values
  • Loading branch information
timsaucer committed Aug 13, 2024
1 parent 18501fa commit cbdddf1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 43 deletions.
25 changes: 8 additions & 17 deletions python/datafusion/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1701,23 +1701,13 @@ def first_value(
)


def last_value(
arg: Expr,
distinct: bool = False,
filter: bool = None,
order_by: Expr | None = None,
null_treatment: common.NullTreatment | None = None,
) -> Expr:
"""Returns the last value in a group of values."""
return Expr(
f.last_value(
arg.expr,
distinct=distinct,
filter=filter,
order_by=order_by,
null_treatment=null_treatment,
)
)
def last_value(arg: Expr) -> Expr:
"""Returns the last value in a group of values.
To set parameters on this expression, use ``.order_by()``, ``.distinct()``,
``.filter()``, or ``.null_treatment()``.
"""
return Expr(f.last_value(arg.expr))


def bit_and(arg: Expr, distinct: bool = False) -> Expr:
Expand Down Expand Up @@ -1746,4 +1736,5 @@ def bool_or(arg: Expr, distinct: bool = False) -> Expr:


def lead(arg: Expr, shift_offset: int = 1, default_value: pa.Scalar | None = None):
"""Create a lead window function."""
return Expr(f.lead(arg.expr, shift_offset, default_value))
28 changes: 2 additions & 26 deletions src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,32 +349,8 @@ pub fn first_value(
}

#[pyfunction]
pub fn last_value(
expr: PyExpr,
distinct: bool,
filter: Option<PyExpr>,
order_by: Option<Vec<PyExpr>>,
null_treatment: Option<NullTreatment>,
) -> PyResult<PyExpr> {
let agg_fn = functions_aggregate::expr_fn::last_value(vec![expr.expr]);

// luckily, I can guarantee initializing a builder with an `order_by` default of empty vec
let order_by = order_by
.map(|x| x.into_iter().map(|x| x.expr).collect::<Vec<_>>())
.unwrap_or_default();
let mut builder = agg_fn.order_by(order_by);

if distinct {
builder = builder.distinct();
}

if let Some(filter) = filter {
builder = builder.filter(filter.expr);
}

builder = builder.null_treatment(null_treatment.map(DFNullTreatment::from));

Ok(builder.build()?.into())
pub fn last_value(expr: PyExpr) -> PyExpr {
functions_aggregate::expr_fn::last_value(vec![expr.expr]).into()
}

#[pyfunction]
Expand Down

0 comments on commit cbdddf1

Please sign in to comment.