Path: blob/main/crates/polars-expr/src/expressions/alias.rs
6940 views
use polars_core::prelude::*;12use super::*;3use crate::expressions::{AggregationContext, PartitionedAggregation, PhysicalExpr};45pub struct AliasExpr {6pub(crate) physical_expr: Arc<dyn PhysicalExpr>,7pub(crate) name: PlSmallStr,8expr: Expr,9}1011impl AliasExpr {12pub fn new(physical_expr: Arc<dyn PhysicalExpr>, name: PlSmallStr, expr: Expr) -> Self {13Self {14physical_expr,15name,16expr,17}18}1920fn finish(&self, input: Column) -> Column {21input.with_name(self.name.clone())22}23}2425impl PhysicalExpr for AliasExpr {26fn as_expression(&self) -> Option<&Expr> {27Some(&self.expr)28}2930fn evaluate(&self, df: &DataFrame, state: &ExecutionState) -> PolarsResult<Column> {31let series = self.physical_expr.evaluate(df, state)?;32Ok(self.finish(series))33}3435#[allow(clippy::ptr_arg)]36fn evaluate_on_groups<'a>(37&self,38df: &DataFrame,39groups: &'a GroupPositions,40state: &ExecutionState,41) -> PolarsResult<AggregationContext<'a>> {42let mut ac = self.physical_expr.evaluate_on_groups(df, groups, state)?;43let c = ac.take();44let c = self.finish(c);4546if ac.is_literal() {47ac.with_literal(c);48} else {49ac.with_values(c, ac.is_aggregated(), Some(&self.expr))?;50}51Ok(ac)52}5354fn to_field(&self, input_schema: &Schema) -> PolarsResult<Field> {55Ok(Field::new(56self.name.clone(),57self.physical_expr.to_field(input_schema)?.dtype().clone(),58))59}6061fn is_literal(&self) -> bool {62self.physical_expr.is_literal()63}6465fn is_scalar(&self) -> bool {66self.physical_expr.is_scalar()67}6869fn as_partitioned_aggregator(&self) -> Option<&dyn PartitionedAggregation> {70Some(self)71}72}7374impl PartitionedAggregation for AliasExpr {75fn evaluate_partitioned(76&self,77df: &DataFrame,78groups: &GroupPositions,79state: &ExecutionState,80) -> PolarsResult<Column> {81let agg = self.physical_expr.as_partitioned_aggregator().unwrap();82let s = agg.evaluate_partitioned(df, groups, state)?;83Ok(s.with_name(self.name.clone()))84}8586fn finalize(87&self,88partitioned: Column,89groups: &GroupPositions,90state: &ExecutionState,91) -> PolarsResult<Column> {92let agg = self.physical_expr.as_partitioned_aggregator().unwrap();93let s = agg.finalize(partitioned, groups, state)?;94Ok(s.with_name(self.name.clone()))95}96}979899