Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-sql/tests/ops_distinct_on.rs
6939 views
1
use polars_core::chunked_array::ops::SortMultipleOptions;
2
use polars_core::df;
3
use polars_lazy::prelude::*;
4
use polars_sql::*;
5
6
#[test]
7
fn test_distinct_on() {
8
let df = df! {
9
"Name" => ["Bob", "Pete", "Pete", "Pete", "Martha", "Martha"],
10
"Record Date" => [1, 1, 2, 4, 1, 3],
11
"Score" => [8, 2, 9, 3, 2, 6]
12
}
13
.unwrap()
14
.lazy();
15
let mut ctx = SQLContext::new();
16
17
ctx.register("df", df.clone());
18
let sql = r#"
19
SELECT DISTINCT ON ("Name")
20
"Name",
21
"Record Date",
22
"Score"
23
FROM
24
df
25
ORDER BY
26
"Name",
27
"Record Date" DESC;"#;
28
let lf = ctx.execute(sql).unwrap();
29
let actual = lf.collect().unwrap();
30
let expected = df
31
.sort_by_exprs(
32
vec![col("Name"), col("Record Date")],
33
SortMultipleOptions::default()
34
.with_order_descending_multi([false, true])
35
.with_maintain_order(true),
36
)
37
.group_by_stable(vec![col("Name")])
38
.agg(vec![col("*").first()]);
39
let expected = expected.collect().unwrap();
40
assert!(actual.equals(&expected))
41
}
42
43