From a6159eaf26d71f02aeeef80e7247f9dbde39887a Mon Sep 17 00:00:00 2001 From: Orson Peters Date: Wed, 23 Oct 2024 18:49:36 +0200 Subject: [PATCH] fmt --- crates/polars-compute/src/var_cov.rs | 2 +- crates/polars-expr/src/reduce/convert.rs | 8 +++-- crates/polars-expr/src/reduce/mean.rs | 16 +++++++-- crates/polars-expr/src/reduce/min_max.rs | 34 ++++++++++++++----- crates/polars-expr/src/reduce/mod.rs | 34 +++++++++++++++---- crates/polars-expr/src/reduce/var_std.rs | 16 +++++---- .../src/physical_plan/lower_expr.rs | 3 +- 7 files changed, 83 insertions(+), 30 deletions(-) diff --git a/crates/polars-compute/src/var_cov.rs b/crates/polars-compute/src/var_cov.rs index 75e5ade3bb6b..d6c0267faec6 100644 --- a/crates/polars-compute/src/var_cov.rs +++ b/crates/polars-compute/src/var_cov.rs @@ -75,7 +75,7 @@ impl VarState { dp: alg_sum(x.iter().map(|&xi| (xi - mean) * (xi - mean))), } } - + pub fn add_one(&mut self, x: f64) { // Just a specialized version of // self.combine(&Self { weight: 1.0, mean: x, dp: 0.0 }) diff --git a/crates/polars-expr/src/reduce/convert.rs b/crates/polars-expr/src/reduce/convert.rs index b5570d869cb8..55a4b325bda1 100644 --- a/crates/polars-expr/src/reduce/convert.rs +++ b/crates/polars-expr/src/reduce/convert.rs @@ -33,8 +33,12 @@ pub fn into_reduction( propagate_nans, input, } => (new_max_reduction(get_dt(*input)?, *propagate_nans), *input), - IRAggExpr::Var(input, ddof) => (new_var_std_reduction(get_dt(*input)?, false, *ddof), *input), - IRAggExpr::Std(input, ddof) => (new_var_std_reduction(get_dt(*input)?, true, *ddof), *input), + IRAggExpr::Var(input, ddof) => { + (new_var_std_reduction(get_dt(*input)?, false, *ddof), *input) + }, + IRAggExpr::Std(input, ddof) => { + (new_var_std_reduction(get_dt(*input)?, true, *ddof), *input) + }, _ => todo!(), }, AExpr::Len => { diff --git a/crates/polars-expr/src/reduce/mean.rs b/crates/polars-expr/src/reduce/mean.rs index ea32f2905a9d..4a8ec962f237 100644 --- a/crates/polars-expr/src/reduce/mean.rs +++ b/crates/polars-expr/src/reduce/mean.rs @@ -107,7 +107,12 @@ where v.1 += ca.len() - ca.null_count(); } - fn finish(&self, v: Vec, m: Option, dtype: &DataType) -> PolarsResult { + fn finish( + &self, + v: Vec, + m: Option, + dtype: &DataType, + ) -> PolarsResult { assert!(m.is_none()); Ok(finish_output(v, dtype)) } @@ -121,7 +126,7 @@ impl Reducer for BoolMeanReducer { type Value = (usize, usize); #[inline(always)] - fn init(&self, ) -> Self::Value { + fn init(&self) -> Self::Value { (0, 0) } @@ -142,7 +147,12 @@ impl Reducer for BoolMeanReducer { v.1 += ca.len() - ca.null_count(); } - fn finish(&self, v: Vec, m: Option, dtype: &DataType) -> PolarsResult { + fn finish( + &self, + v: Vec, + m: Option, + dtype: &DataType, + ) -> PolarsResult { assert!(m.is_none()); assert!(dtype == &DataType::Boolean); let ca: Float64Chunked = v diff --git a/crates/polars-expr/src/reduce/min_max.rs b/crates/polars-expr/src/reduce/min_max.rs index 1c943ac7688f..f4541d7a88a1 100644 --- a/crates/polars-expr/src/reduce/min_max.rs +++ b/crates/polars-expr/src/reduce/min_max.rs @@ -18,9 +18,13 @@ pub fn new_min_reduction(dtype: DataType, propagate_nans: bool) -> Box Box::new(BoolMinGroupedReduction::default()), #[cfg(feature = "propagate_nans")] - Float32 if propagate_nans => Box::new(VMGR::new(dtype, NumReducer::>::new())), + Float32 if propagate_nans => { + Box::new(VMGR::new(dtype, NumReducer::>::new())) + }, #[cfg(feature = "propagate_nans")] - Float64 if propagate_nans => Box::new(VMGR::new(dtype, NumReducer::>::new())), + Float64 if propagate_nans => { + Box::new(VMGR::new(dtype, NumReducer::>::new())) + }, Float32 => Box::new(VMGR::new(dtype, NumReducer::>::new())), Float64 => Box::new(VMGR::new(dtype, NumReducer::>::new())), String | Binary => Box::new(VecGroupedReduction::new(dtype, BinaryMinReducer)), @@ -41,9 +45,13 @@ pub fn new_max_reduction(dtype: DataType, propagate_nans: bool) -> Box Box::new(BoolMaxGroupedReduction::default()), #[cfg(feature = "propagate_nans")] - Float32 if propagate_nans => Box::new(VMGR::new(dtype, NumReducer::>::new())), + Float32 if propagate_nans => { + Box::new(VMGR::new(dtype, NumReducer::>::new())) + }, #[cfg(feature = "propagate_nans")] - Float64 if propagate_nans => Box::new(VMGR::new(dtype, NumReducer::>::new())), + Float64 if propagate_nans => { + Box::new(VMGR::new(dtype, NumReducer::>::new())) + }, Float32 => Box::new(VMGR::new(dtype, NumReducer::>::new())), Float64 => Box::new(VMGR::new(dtype, NumReducer::>::new())), String | Binary => Box::new(VecGroupedReduction::new(dtype, BinaryMaxReducer)), @@ -171,7 +179,7 @@ impl Reducer for BinaryMinReducer { type Dtype = BinaryType; type Value = Option>; // TODO: evaluate SmallVec. - fn init(&self, ) -> Self::Value { + fn init(&self) -> Self::Value { None } @@ -201,7 +209,12 @@ impl Reducer for BinaryMinReducer { self.reduce_one(v, ca.min_binary()) } - fn finish(&self, v: Vec, m: Option, dtype: &DataType) -> PolarsResult { + fn finish( + &self, + v: Vec, + m: Option, + dtype: &DataType, + ) -> PolarsResult { assert!(m.is_none()); // This should only be used with VecGroupedReduction. let ca: BinaryChunked = v.into_iter().collect_ca(PlSmallStr::EMPTY); ca.into_series().cast(dtype) @@ -213,7 +226,7 @@ impl Reducer for BinaryMaxReducer { type Value = Option>; // TODO: evaluate SmallVec. #[inline(always)] - fn init(&self, ) -> Self::Value { + fn init(&self) -> Self::Value { None } @@ -247,7 +260,12 @@ impl Reducer for BinaryMaxReducer { } #[inline(always)] - fn finish(&self, v: Vec, m: Option, dtype: &DataType) -> PolarsResult { + fn finish( + &self, + v: Vec, + m: Option, + dtype: &DataType, + ) -> PolarsResult { assert!(m.is_none()); // This should only be used with VecGroupedReduction. let ca: BinaryChunked = v.into_iter().collect_ca(PlSmallStr::EMPTY); ca.into_series().cast(dtype) diff --git a/crates/polars-expr/src/reduce/mod.rs b/crates/polars-expr/src/reduce/mod.rs index 5096341f3d0d..170ee6abff6c 100644 --- a/crates/polars-expr/src/reduce/mod.rs +++ b/crates/polars-expr/src/reduce/mod.rs @@ -69,9 +69,18 @@ pub trait Reducer: Send + Sync + Clone + 'static { Cow::Borrowed(s) } fn combine(&self, a: &mut Self::Value, b: &Self::Value); - fn reduce_one(&self, a: &mut Self::Value, b: Option<::Physical<'_>>); + fn reduce_one( + &self, + a: &mut Self::Value, + b: Option<::Physical<'_>>, + ); fn reduce_ca(&self, v: &mut Self::Value, ca: &ChunkedArray); - fn finish(&self, v: Vec, m: Option, dtype: &DataType) -> PolarsResult; + fn finish( + &self, + v: Vec, + m: Option, + dtype: &DataType, + ) -> PolarsResult; } pub trait NumericReduction: Send + Sync + 'static { @@ -118,7 +127,11 @@ impl Reducer for NumReducer { } #[inline(always)] - fn reduce_one(&self, a: &mut Self::Value, b: Option<::Physical<'_>>) { + fn reduce_one( + &self, + a: &mut Self::Value, + b: Option<::Physical<'_>>, + ) { if let Some(b) = b { *a = ::combine(*a, b); } @@ -131,7 +144,12 @@ impl Reducer for NumReducer { } } - fn finish(&self, v: Vec, m: Option, dtype: &DataType) -> PolarsResult { + fn finish( + &self, + v: Vec, + m: Option, + dtype: &DataType, + ) -> PolarsResult { let arr = Box::new(PrimitiveArray::::from_vec(v).with_validity(m)); Ok(unsafe { Series::from_chunks_and_dtype_unchecked(PlSmallStr::EMPTY, vec![arr], dtype) }) } @@ -173,7 +191,8 @@ where assert!(values.dtype() == &self.in_dtype); let values = self.reducer.cast_series(values); let ca: &ChunkedArray = values.as_ref().as_ref().as_ref(); - self.reducer.reduce_ca(&mut self.values[group_idx as usize], ca); + self.reducer + .reduce_ca(&mut self.values[group_idx as usize], ca); Ok(()) } @@ -196,7 +215,7 @@ where } else { let mut offset = 0; for arr in ca.downcast_iter() { - let subgroup = &group_idxs[offset..offset+arr.len()]; + let subgroup = &group_idxs[offset..offset + arr.len()]; for (g, v) in subgroup.iter().zip(arr.values_iter()) { let grp = self.values.get_unchecked_mut(*g as usize); self.reducer.reduce_one(grp, Some(v)); @@ -278,7 +297,8 @@ where assert!(values.dtype() == &self.in_dtype); let values = values.to_physical_repr(); let ca: &ChunkedArray = values.as_ref().as_ref().as_ref(); - self.reducer.reduce_ca(&mut self.values[group_idx as usize], ca); + self.reducer + .reduce_ca(&mut self.values[group_idx as usize], ca); if ca.len() != ca.null_count() { self.mask.set(group_idx as usize, true); } diff --git a/crates/polars-expr/src/reduce/var_std.rs b/crates/polars-expr/src/reduce/var_std.rs index 1bd69c8461dd..7993c06e6a4e 100644 --- a/crates/polars-expr/src/reduce/var_std.rs +++ b/crates/polars-expr/src/reduce/var_std.rs @@ -22,12 +22,15 @@ pub fn new_var_std_reduction(dtype: DataType, is_std: bool, ddof: u8) -> Box Box::new(VGR::new(dtype, VarStdReducer:: { - is_std, - ddof, - needs_cast: true, - _phantom: PhantomData, - })), + Decimal(_, _) => Box::new(VGR::new( + dtype, + VarStdReducer:: { + is_std, + ddof, + needs_cast: true, + _phantom: PhantomData, + }, + )), Duration(..) => todo!(), _ => unimplemented!(), } @@ -106,7 +109,6 @@ impl Reducer for VarStdReducer { } } - #[derive(Clone)] struct BoolVarStdReducer { is_std: bool, diff --git a/crates/polars-stream/src/physical_plan/lower_expr.rs b/crates/polars-stream/src/physical_plan/lower_expr.rs index 5d464ac7c38b..2505e033aeec 100644 --- a/crates/polars-stream/src/physical_plan/lower_expr.rs +++ b/crates/polars-stream/src/physical_plan/lower_expr.rs @@ -575,8 +575,7 @@ fn lower_exprs_with_ctx( | IRAggExpr::Sum(ref mut inner) | IRAggExpr::Mean(ref mut inner) | IRAggExpr::Var(ref mut inner, _ /* ddof */) - | IRAggExpr::Std(ref mut inner, _ /* ddof */) - => { + | IRAggExpr::Std(ref mut inner, _ /* ddof */) => { let (trans_input, trans_exprs) = lower_exprs_with_ctx(input, &[*inner], ctx)?; *inner = trans_exprs[0];