Path: blob/main/crates/polars-sql/tests/ops_distinct_on.rs
6939 views
use polars_core::chunked_array::ops::SortMultipleOptions;1use polars_core::df;2use polars_lazy::prelude::*;3use polars_sql::*;45#[test]6fn test_distinct_on() {7let df = df! {8"Name" => ["Bob", "Pete", "Pete", "Pete", "Martha", "Martha"],9"Record Date" => [1, 1, 2, 4, 1, 3],10"Score" => [8, 2, 9, 3, 2, 6]11}12.unwrap()13.lazy();14let mut ctx = SQLContext::new();1516ctx.register("df", df.clone());17let sql = r#"18SELECT DISTINCT ON ("Name")19"Name",20"Record Date",21"Score"22FROM23df24ORDER BY25"Name",26"Record Date" DESC;"#;27let lf = ctx.execute(sql).unwrap();28let actual = lf.collect().unwrap();29let expected = df30.sort_by_exprs(31vec![col("Name"), col("Record Date")],32SortMultipleOptions::default()33.with_order_descending_multi([false, true])34.with_maintain_order(true),35)36.group_by_stable(vec![col("Name")])37.agg(vec![col("*").first()]);38let expected = expected.collect().unwrap();39assert!(actual.equals(&expected))40}414243