Skip to content

Commit

Permalink
fix groups state in complex aggregation (#3656)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jun 10, 2022
1 parent 24d0cd0 commit 50e54fb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions polars/polars-lazy/src/physical_plan/expressions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ impl<'a> AggregationContext<'a> {
/// Calling this function will ensure both are sortened. This will be a no-op
/// if already aggregated.
pub(crate) fn sort_by_groups(&mut self) {
// make sure that the groups are updated before we use them to sort.
self.groups();
match &self.state {
AggState::NotAggregated(s) => {
// We should not aggregate literals!!
Expand Down
26 changes: 26 additions & 0 deletions polars/tests/it/lazy/groupby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,29 @@ fn test_list_arithmetic_in_groupby() -> Result<()> {

Ok(())
}

#[test]
fn test_filter_diff_arithmetic() -> Result<()> {
let df = df![
"user" => [1, 1, 1, 1, 2],
"group" => [1, 2, 1, 1, 2],
"value" => [1, 5, 14, 17, 20]
]?;

let out = df
.lazy()
.groupby([col("user")])
.agg([(col("value")
.filter(col("group").eq(lit(1)))
.diff(1, Default::default())
* lit(2))
.alias("diff")])
.sort("user", Default::default())
.explode([col("diff")])
.collect()?;

let out = out.column("diff")?;
assert_eq!(out, &Series::new("diff", &[None, Some(26), Some(6), None]));

Ok(())
}

0 comments on commit 50e54fb

Please sign in to comment.