Skip to content

Commit

Permalink
Lazy: fix inconsistency in apply_flat
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 15, 2021
1 parent bb88016 commit 2f15726
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
18 changes: 16 additions & 2 deletions polars/polars-lazy/src/physical_plan/expressions/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl PhysicalExpr for ApplyExpr {
Ok(ac)
}
ApplyOptions::ApplyFlat => {
let s = self.function.call_udf(&mut [ac.take()])?;
let s = self.function.call_udf(&mut [ac.flat().into_owned()])?;
ac.with_series(s);
Ok(ac)
}
Expand Down Expand Up @@ -143,7 +143,21 @@ impl PhysicalAggregation for ApplyExpr {
}
Ok(Some(ca.into_series()))
}
ApplyOptions::ApplyFlat => self.function.call_udf(&mut [ac.take()]).map(Some),
ApplyOptions::ApplyFlat => {
// the function needs to be called on a flat series
// but the series may be flat or aggregated
// if its flat, we just apply and return
// if not flat, the flattening sorts by group, so we must create new group tuples
// and again aggregate.
let out = self.function.call_udf(&mut [ac.flat().into_owned()]);
if ac.is_flat() {
out.map(Some)
} else {
// TODO! maybe just apply over list?
ac.with_update_groups(UpdateGroups::WithGroupsLen);
Ok(out?.agg_list(ac.groups()))
}
}
ApplyOptions::ApplyList => self
.function
.call_udf(&mut [ac.aggregated().into_owned()])
Expand Down
4 changes: 4 additions & 0 deletions polars/polars-lazy/src/physical_plan/expressions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ impl<'a> AggregationContext<'a> {
}
}

pub(crate) fn is_flat(&self) -> bool {
matches!(&self.series, AggState::Flat(_))
}

pub(crate) fn combine_groups(&mut self, other: AggregationContext) -> &mut Self {
if let (Cow::Borrowed(_), Cow::Owned(a)) = (&self.groups, other.groups) {
self.groups = Cow::Owned(a);
Expand Down
22 changes: 22 additions & 0 deletions polars/polars-lazy/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1848,3 +1848,25 @@ fn test_round_after_agg() -> Result<()> {
assert!(out.column("foo")?.f64().is_ok());
Ok(())
}

#[test]
fn test_power_in_agg_list() -> Result<()> {
let df = fruits_cars();

let out = df
.lazy()
.groupby(vec![col("fruits")])
.agg(vec![col("A")
.rolling_min(1, None, false, 0)
.pow(2.0)
.alias("foo")])
.sort("fruits", true)
.collect()?;

let agg = out.column("foo")?.list()?;
let first = agg.get(0).unwrap();
let vals = first.f64()?;
assert_eq!(Vec::from(vals), &[Some(1.0), Some(4.0), Some(25.0)]);

Ok(())
}

0 comments on commit 2f15726

Please sign in to comment.