Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1569594

Browse files
committedOct 7, 2023
add first_value last_value
1 parent 9ef0a57 commit 1569594

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed
 

‎datafusion/tests/test_functions.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,29 @@ def test_case(df):
479479
assert result.column(2) == pa.array(["Hola", "Mundo", None])
480480

481481

482+
def test_first_last_value(df):
483+
df = df.aggregate(
484+
[],
485+
[
486+
f.first_value(column("a")),
487+
f.first_value(column("b")),
488+
f.first_value(column("d")),
489+
f.last_value(column("a")),
490+
f.last_value(column("b")),
491+
f.last_value(column("d")),
492+
],
493+
)
494+
495+
result = df.collect()
496+
result = result[0]
497+
assert result.column(0) == pa.array(["Hello"])
498+
assert result.column(1) == pa.array([4])
499+
assert result.column(2) == pa.array([datetime(2022, 12, 31)])
500+
assert result.column(3) == pa.array(["!"])
501+
assert result.column(4) == pa.array([6])
502+
assert result.column(5) == pa.array([datetime(2020, 7, 2)])
503+
504+
482505
def test_binary_string_functions(df):
483506
df = df.select(
484507
f.encode(column("a"), literal("base64")),

‎src/functions.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn digest(value: PyExpr, method: PyExpr) -> PyExpr {
5454
/// Concatenates the text representations of all the arguments.
5555
/// NULL arguments are ignored.
5656
#[pyfunction]
57-
#[pyo3(signature = (*args))]
57+
#[pyo3(signature = (* args))]
5858
fn concat(args: Vec<PyExpr>) -> PyResult<PyExpr> {
5959
let args = args.into_iter().map(|e| e.expr).collect::<Vec<_>>();
6060
Ok(datafusion_expr::concat(&args).into())
@@ -64,7 +64,7 @@ fn concat(args: Vec<PyExpr>) -> PyResult<PyExpr> {
6464
/// The first argument is used as the separator string, and should not be NULL.
6565
/// Other NULL arguments are ignored.
6666
#[pyfunction]
67-
#[pyo3(signature = (sep, *args))]
67+
#[pyo3(signature = (sep, * args))]
6868
fn concat_ws(sep: String, args: Vec<PyExpr>) -> PyResult<PyExpr> {
6969
let args = args.into_iter().map(|e| e.expr).collect::<Vec<_>>();
7070
Ok(datafusion_expr::concat_ws(lit(sep), args).into())
@@ -362,6 +362,8 @@ aggregate_function!(stddev_samp, Stddev);
362362
aggregate_function!(var, Variance);
363363
aggregate_function!(var_pop, VariancePop);
364364
aggregate_function!(var_samp, Variance);
365+
aggregate_function!(first_value, FirstValue);
366+
aggregate_function!(last_value, LastValue);
365367

366368
pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
367369
m.add_wrapped(wrap_pyfunction!(abs))?;
@@ -489,6 +491,8 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
489491
m.add_wrapped(wrap_pyfunction!(var_pop))?;
490492
m.add_wrapped(wrap_pyfunction!(var_samp))?;
491493
m.add_wrapped(wrap_pyfunction!(window))?;
494+
m.add_wrapped(wrap_pyfunction!(first_value))?;
495+
m.add_wrapped(wrap_pyfunction!(last_value))?;
492496

493497
//Binary String Functions
494498
m.add_wrapped(wrap_pyfunction!(encode))?;

0 commit comments

Comments
 (0)