-
-
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.
count expression that does not require column name (#2558)
- Loading branch information
Showing
12 changed files
with
126 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::physical_plan::state::ExecutionState; | ||
use crate::prelude::*; | ||
use polars_arrow::utils::CustomIterTools; | ||
use polars_core::prelude::*; | ||
use polars_core::utils::NoNull; | ||
use std::borrow::Cow; | ||
|
||
pub struct CountExpr { | ||
expr: Expr, | ||
} | ||
|
||
impl CountExpr { | ||
pub(crate) fn new() -> Self { | ||
Self { expr: Expr::Count } | ||
} | ||
} | ||
|
||
impl PhysicalExpr for CountExpr { | ||
fn as_expression(&self) -> &Expr { | ||
&self.expr | ||
} | ||
|
||
fn evaluate(&self, df: &DataFrame, _state: &ExecutionState) -> Result<Series> { | ||
Ok(Series::new("count", [df.height() as u32])) | ||
} | ||
|
||
fn evaluate_on_groups<'a>( | ||
&self, | ||
_df: &DataFrame, | ||
groups: &'a GroupsProxy, | ||
_state: &ExecutionState, | ||
) -> Result<AggregationContext<'a>> { | ||
let mut ca = match groups { | ||
GroupsProxy::Idx(groups) => { | ||
let ca: NoNull<UInt32Chunked> = groups | ||
.all() | ||
.iter() | ||
.map(|g| g.len() as u32) | ||
.collect_trusted(); | ||
ca.into_inner() | ||
} | ||
GroupsProxy::Slice(groups) => { | ||
let ca: NoNull<UInt32Chunked> = groups.iter().map(|g| g[1]).collect_trusted(); | ||
ca.into_inner() | ||
} | ||
}; | ||
ca.rename("count"); | ||
let s = ca.into_series(); | ||
|
||
Ok(AggregationContext::new(s, Cow::Borrowed(groups), true)) | ||
} | ||
fn to_field(&self, _input_schema: &Schema) -> Result<Field> { | ||
Ok(Field::new("count", DataType::UInt32)) | ||
} | ||
} | ||
|
||
impl PhysicalAggregation for CountExpr { | ||
fn aggregate( | ||
&self, | ||
df: &DataFrame, | ||
groups: &GroupsProxy, | ||
state: &ExecutionState, | ||
) -> Result<Option<Series>> { | ||
let mut ac = self.evaluate_on_groups(df, groups, state)?; | ||
let s = ac.aggregated(); | ||
Ok(Some(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
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
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