Skip to content

Commit 5f08588

Browse files
convert first_value and last_value to UDFs
Ref apache/datafusion#10648
1 parent f36558e commit 5f08588

1 file changed

Lines changed: 44 additions & 2 deletions

File tree

src/functions.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,50 @@ pub fn var(y: PyExpr) -> PyExpr {
6767
var_samp(y)
6868
}
6969

70+
#[pyfunction]
71+
#[pyo3(signature = (*args, distinct = false, filter = None, order_by = None))]
72+
pub fn first_value(
73+
args: Vec<PyExpr>,
74+
distinct: bool,
75+
filter: Option<PyExpr>,
76+
order_by: Option<Vec<PyExpr>>,
77+
) -> PyExpr {
78+
// TODO: allow user to select null_treatment
79+
let null_treatment = None;
80+
let args = args.into_iter().map(|x| x.expr).collect::<Vec<_>>();
81+
let order_by = order_by.map(|x| x.into_iter().map(|x| x.expr).collect::<Vec<_>>());
82+
functions_aggregate::expr_fn::first_value(
83+
args,
84+
distinct,
85+
filter.map(|x| Box::new(x.expr)),
86+
order_by,
87+
null_treatment,
88+
)
89+
.into()
90+
}
91+
92+
#[pyfunction]
93+
#[pyo3(signature = (*args, distinct = false, filter = None, order_by = None))]
94+
pub fn last_value(
95+
args: Vec<PyExpr>,
96+
distinct: bool,
97+
filter: Option<PyExpr>,
98+
order_by: Option<Vec<PyExpr>>,
99+
) -> PyExpr {
100+
// TODO: allow user to select null_treatment
101+
let null_treatment = None;
102+
let args = args.into_iter().map(|x| x.expr).collect::<Vec<_>>();
103+
let order_by = order_by.map(|x| x.into_iter().map(|x| x.expr).collect::<Vec<_>>());
104+
functions_aggregate::expr_fn::last_value(
105+
args,
106+
distinct,
107+
filter.map(|x| Box::new(x.expr)),
108+
order_by,
109+
null_treatment,
110+
)
111+
.into()
112+
}
113+
70114
#[pyfunction]
71115
fn in_list(expr: PyExpr, value: Vec<PyExpr>, negated: bool) -> PyExpr {
72116
datafusion_expr::in_list(
@@ -614,8 +658,6 @@ aggregate_function!(regr_slope, RegrSlope);
614658
aggregate_function!(regr_sxx, RegrSXX);
615659
aggregate_function!(regr_sxy, RegrSXY);
616660
aggregate_function!(regr_syy, RegrSYY);
617-
aggregate_function!(first_value, FirstValue);
618-
aggregate_function!(last_value, LastValue);
619661
aggregate_function!(bit_and, BitAnd);
620662
aggregate_function!(bit_or, BitOr);
621663
aggregate_function!(bit_xor, BitXor);

0 commit comments

Comments
 (0)