-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Add reduce ComputeNode
in new streaming engine
#17389
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1412ba0
wip: Add reduce
ritchie46 ce8f5b8
remove nth
ritchie46 f6976a1
remove nth
ritchie46 f621cb7
rebase and move to expr [skip ci]
ritchie46 257b0b3
more [skip ci]
ritchie46 2375d9d
send [skip ci]
ritchie46 3f4ff6c
more [skip ci]
ritchie46 5a16f3f
wip: [skip ci]
ritchie46 6a8de99
wrap-up
ritchie46 4d943f5
merge
ritchie46 2b8846f
feedback
ritchie46 5eeaf42
feedback
ritchie46 1f6cedc
c
ritchie46 6c7d6ee
features
ritchie46 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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,35 @@ | ||
use crate::datatypes::{AnyValue, TimeUnit}; | ||
use crate::prelude::{DataType, Scalar, MS_IN_DAY}; | ||
|
||
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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this supposed to be part of the PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe.. Then we can ensure that all warnings are fixed. But if CI doesn't like it, I will remove it.