Skip to content

Commit

Permalink
don't automatically flatten. If an apply needs a flatten, just add a …
Browse files Browse the repository at this point in the history
…flatten to the expr
  • Loading branch information
ritchie46 committed Nov 20, 2021
1 parent 4dc472f commit 3ea632e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
27 changes: 2 additions & 25 deletions polars/polars-lazy/src/physical_plan/expressions/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ impl PhysicalAggregation for ApplyExpr {
let mut container = [Default::default()];
let name = ac.series().name().to_string();

let mut all_unit_length = true;

let mut ca: ListChunked = ac
.aggregated()
.list()
Expand All @@ -190,21 +188,11 @@ impl PhysicalAggregation for ApplyExpr {
.map(|opt_s| {
opt_s.and_then(|s| {
container[0] = s;
let out = self.function.call_udf(&mut container).ok();

if let Some(s) = &out {
if s.len() != 1 {
all_unit_length = false;
}
}
out
self.function.call_udf(&mut container).ok()
})
})
.collect();
ca.rename(&name);
if all_unit_length {
return Ok(Some(ca.explode()?));
}
Ok(Some(ca.into_series()))
}
ApplyOptions::ApplyFlat => {
Expand Down Expand Up @@ -232,7 +220,6 @@ impl PhysicalAggregation for ApplyExpr {

match self.collect_groups {
ApplyOptions::ApplyGroups => {
let mut all_unit_length = true;
let mut container = vec![Default::default(); acs.len()];
let name = acs[0].series().name().to_string();

Expand All @@ -259,20 +246,10 @@ impl PhysicalAggregation for ApplyExpr {
Some(s) => container.push(s),
}
}
let out = self.function.call_udf(&mut container).ok();

if let Some(s) = &out {
if s.len() != 1 {
all_unit_length = false;
}
}
out
self.function.call_udf(&mut container).ok()
})
.collect();
ca.rename(&name);
if all_unit_length {
return Ok(Some(ca.explode()?));
}
Ok(Some(ca.into_series()))
}
ApplyOptions::ApplyFlat => {
Expand Down
19 changes: 19 additions & 0 deletions polars/polars-lazy/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2201,3 +2201,22 @@ fn test_groupby_on_lists() -> Result<()> {

Ok(())
}

#[test]
fn test_single_group_result() -> Result<()> {
// the argsort should not auto explode
let df = df![
"a" => [1, 2],
"b" => [1, 1]
]?;

let out = df
.lazy()
.select([col("a").arg_sort(false).over([col("a")]).flatten()])
.collect()?;

let a = out.column("a")?.u32()?;
assert_eq!(Vec::from(a), &[Some(0), Some(0)]);

Ok(())
}

0 comments on commit 3ea632e

Please sign in to comment.