-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add reduce
ComputeNode
in new streaming engine (#17389)
- Loading branch information
Showing
23 changed files
with
763 additions
and
239 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
pub mod reduce; | ||
|
||
use crate::datatypes::{AnyValue, DataType}; | ||
use crate::prelude::Series; | ||
|
||
#[derive(Clone)] | ||
pub struct Scalar { | ||
dtype: DataType, | ||
value: AnyValue<'static>, | ||
} | ||
|
||
impl Scalar { | ||
pub fn new(dtype: DataType, value: AnyValue<'static>) -> Self { | ||
Self { dtype, value } | ||
} | ||
|
||
pub fn value(&self) -> &AnyValue<'static> { | ||
&self.value | ||
} | ||
|
||
pub fn as_any_value(&self) -> AnyValue { | ||
self.value | ||
.strict_cast(&self.dtype) | ||
.unwrap_or_else(|| self.value.clone()) | ||
} | ||
|
||
pub fn into_series(self, name: &str) -> Series { | ||
Series::from_any_values_and_dtype(name, &[self.as_any_value()], &self.dtype, true).unwrap() | ||
} | ||
|
||
pub fn dtype(&self) -> &DataType { | ||
&self.dtype | ||
} | ||
|
||
pub fn update(&mut self, value: AnyValue<'static>) { | ||
self.value = value; | ||
} | ||
} |
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,37 @@ | ||
use crate::datatypes::{AnyValue, TimeUnit}; | ||
#[cfg(feature = "dtype-date")] | ||
use crate::prelude::MS_IN_DAY; | ||
use crate::prelude::{DataType, Scalar}; | ||
|
||
pub fn mean_reduce(value: Option<f64>, dtype: DataType) -> Scalar { | ||
match dtype { | ||
DataType::Float32 => { | ||
let val = value.map(|m| m as f32); | ||
Scalar::new(dtype, val.into()) | ||
}, | ||
dt if dt.is_numeric() || dt.is_decimal() || dt.is_bool() => { | ||
Scalar::new(DataType::Float64, value.into()) | ||
}, | ||
#[cfg(feature = "dtype-date")] | ||
DataType::Date => { | ||
let val = value.map(|v| (v * MS_IN_DAY as f64) as i64); | ||
Scalar::new(DataType::Datetime(TimeUnit::Milliseconds, None), val.into()) | ||
}, | ||
#[cfg(feature = "dtype-datetime")] | ||
dt @ DataType::Datetime(_, _) => { | ||
let val = value.map(|v| v as i64); | ||
Scalar::new(dt, val.into()) | ||
}, | ||
#[cfg(feature = "dtype-duration")] | ||
dt @ DataType::Duration(_) => { | ||
let val = value.map(|v| v as i64); | ||
Scalar::new(dt, val.into()) | ||
}, | ||
#[cfg(feature = "dtype-time")] | ||
dt @ DataType::Time => { | ||
let val = value.map(|v| v as i64); | ||
Scalar::new(dt, val.into()) | ||
}, | ||
dt => Scalar::new(dt, AnyValue::Null), | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod expressions; | ||
pub mod planner; | ||
pub mod prelude; | ||
pub mod reduce; | ||
pub mod state; | ||
|
||
pub use crate::planner::{create_physical_expr, ExpressionConversionState}; |
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,82 @@ | ||
use polars_core::error::feature_gated; | ||
use polars_plan::prelude::*; | ||
use polars_utils::arena::{Arena, Node}; | ||
|
||
use super::extrema::*; | ||
use super::sum::SumReduce; | ||
use super::*; | ||
use crate::reduce::mean::MeanReduce; | ||
|
||
pub fn can_convert_into_reduction(node: Node, expr_arena: &Arena<AExpr>) -> bool { | ||
match expr_arena.get(node) { | ||
AExpr::Agg(agg) => matches!( | ||
agg, | ||
IRAggExpr::Min { .. } | ||
| IRAggExpr::Max { .. } | ||
| IRAggExpr::Mean { .. } | ||
| IRAggExpr::Sum(_) | ||
), | ||
_ => false, | ||
} | ||
} | ||
|
||
pub fn into_reduction( | ||
node: Node, | ||
expr_arena: &Arena<AExpr>, | ||
schema: &Schema, | ||
) -> PolarsResult<Option<(Box<dyn Reduction>, Node)>> { | ||
let e = expr_arena.get(node); | ||
let field = e.to_field(schema, Context::Default, expr_arena)?; | ||
let out = match expr_arena.get(node) { | ||
AExpr::Agg(agg) => match agg { | ||
IRAggExpr::Sum(node) => ( | ||
Box::new(SumReduce::new(field.dtype.clone())) as Box<dyn Reduction>, | ||
*node, | ||
), | ||
IRAggExpr::Min { | ||
propagate_nans, | ||
input, | ||
} => { | ||
if *propagate_nans && field.dtype.is_float() { | ||
feature_gated!("propagate_nans", { | ||
let out: Box<dyn Reduction> = match field.dtype { | ||
DataType::Float32 => Box::new(MinNanReduce::<Float32Type>::new()), | ||
DataType::Float64 => Box::new(MinNanReduce::<Float64Type>::new()), | ||
_ => unreachable!(), | ||
}; | ||
(out, *input) | ||
}) | ||
} else { | ||
( | ||
Box::new(MinReduce::new(field.dtype.clone())) as Box<dyn Reduction>, | ||
*input, | ||
) | ||
} | ||
}, | ||
IRAggExpr::Max { | ||
propagate_nans, | ||
input, | ||
} => { | ||
if *propagate_nans && field.dtype.is_float() { | ||
feature_gated!("propagate_nans", { | ||
let out: Box<dyn Reduction> = match field.dtype { | ||
DataType::Float32 => Box::new(MaxNanReduce::<Float32Type>::new()), | ||
DataType::Float64 => Box::new(MaxNanReduce::<Float64Type>::new()), | ||
_ => unreachable!(), | ||
}; | ||
(out, *input) | ||
}) | ||
} else { | ||
(Box::new(MaxReduce::new(field.dtype.clone())) as _, *input) | ||
} | ||
}, | ||
IRAggExpr::Mean(input) => { | ||
let out: Box<dyn Reduction> = Box::new(MeanReduce::new(field.dtype.clone())); | ||
(out, *input) | ||
}, | ||
_ => return Ok(None), | ||
}, | ||
_ => return Ok(None), | ||
}; | ||
Ok(Some(out)) | ||
} |
Oops, something went wrong.