-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
146 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
polars/polars-lazy/src/physical_plan/expressions/unique.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use crate::physical_plan::state::ExecutionState; | ||
use crate::prelude::*; | ||
use polars_core::frame::groupby::GroupTuples; | ||
use polars_core::prelude::*; | ||
use std::sync::Arc; | ||
|
||
pub struct UniqueExpr { | ||
pub(crate) physical_expr: Arc<dyn PhysicalExpr>, | ||
expr: Expr, | ||
} | ||
|
||
impl PhysicalExpr for UniqueExpr { | ||
fn as_expression(&self) -> &Expr { | ||
&self.expr | ||
} | ||
|
||
fn evaluate(&self, df: &DataFrame, state: &ExecutionState) -> Result<Series> { | ||
let series = self.physical_expr.evaluate(df, state)?; | ||
series.unique() | ||
} | ||
|
||
#[allow(clippy::ptr_arg)] | ||
fn evaluate_on_groups<'a>( | ||
&self, | ||
df: &DataFrame, | ||
groups: &'a GroupTuples, | ||
state: &ExecutionState, | ||
) -> Result<AggregationContext<'a>> { | ||
let mut ac = self.physical_expr.evaluate_on_groups(df, groups, state)?; | ||
let series = ac.flat().into_owned(); | ||
|
||
let groups = ac | ||
.groups | ||
.iter() | ||
.map(|(_first, idx)| { | ||
// Safety: | ||
// Group tuples are always in bounds | ||
let group = | ||
unsafe { series.take_iter_unchecked(&mut idx.iter().map(|i| *i as usize)) }; | ||
|
||
let unique_idx = group.arg_unique()?; | ||
|
||
let new_idx: Vec<_> = unique_idx | ||
.cont_slice() | ||
.unwrap() | ||
.iter() | ||
.map(|&i| { | ||
debug_assert!(idx.get(i as usize).is_some()); | ||
unsafe { *idx.get_unchecked(i as usize) } | ||
}) | ||
.collect(); | ||
(new_idx[0], new_idx) | ||
}) | ||
.collect(); | ||
|
||
ac.with_groups(groups); | ||
|
||
Ok(ac) | ||
} | ||
|
||
fn to_field(&self, input_schema: &Schema) -> Result<Field> { | ||
self.physical_expr.to_field(input_schema) | ||
} | ||
|
||
fn as_agg_expr(&self) -> Result<&dyn PhysicalAggregation> { | ||
Ok(self) | ||
} | ||
} | ||
impl PhysicalAggregation for UniqueExpr { | ||
// As a final aggregation a Unique returns a list array. | ||
fn aggregate( | ||
&self, | ||
df: &DataFrame, | ||
groups: &GroupTuples, | ||
state: &ExecutionState, | ||
) -> Result<Option<Series>> { | ||
let mut ac = self.physical_expr.evaluate_on_groups(df, groups, state)?; | ||
let agg_s = ac.aggregated(); | ||
let agg_s = agg_s | ||
.list() | ||
.unwrap() | ||
.apply_amortized(|s| s.as_ref().unique()) | ||
.into_series(); | ||
Ok(Some(agg_s)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters