Skip to content
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

Replacing pattern matching through downcast with trait method #11257

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions datafusion/physical-expr-common/src/aggregate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ pub trait AggregateExpr: Send + Sync + Debug + PartialEq<dyn Any> {
) -> Option<Arc<dyn AggregateExpr>> {
None
}

/// If this function is max, return (output_field, true)
/// if the function is min, return (output_field, false)
/// otherwise return None (the default)
///
/// output_field is the name of the column produced by this aggregate
///
/// Note: this is used to use special aggregate implementations in certain conditions
fn get_minmax_desc(&self) -> Option<(Field, bool)> {
None
}
}

/// Stores the physical expressions used inside the `AggregateExpr`.
Expand Down
8 changes: 8 additions & 0 deletions datafusion/physical-expr/src/aggregate/min_max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ impl AggregateExpr for Max {
fn create_sliding_accumulator(&self) -> Result<Box<dyn Accumulator>> {
Ok(Box::new(SlidingMaxAccumulator::try_new(&self.data_type)?))
}

fn get_minmax_desc(&self) -> Option<(Field, bool)> {
Some((self.field().ok()?, true))
}
}

impl PartialEq<dyn Any> for Max {
Expand Down Expand Up @@ -1018,6 +1022,10 @@ impl AggregateExpr for Min {
fn create_sliding_accumulator(&self) -> Result<Box<dyn Accumulator>> {
Ok(Box::new(SlidingMinAccumulator::try_new(&self.data_type)?))
}

fn get_minmax_desc(&self) -> Option<(Field, bool)> {
Some((self.field().ok()?, false))
}
}

impl PartialEq<dyn Any> for Min {
Expand Down
10 changes: 2 additions & 8 deletions datafusion/physical-plan/src/aggregates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use datafusion_execution::TaskContext;
use datafusion_expr::Accumulator;
use datafusion_physical_expr::{
equivalence::{collapse_lex_req, ProjectionMapping},
expressions::{Column, Max, Min, UnKnownColumn},
expressions::{Column, UnKnownColumn},
physical_exprs_contains, AggregateExpr, EquivalenceProperties, LexOrdering,
LexRequirement, PhysicalExpr, PhysicalSortRequirement,
};
Expand Down Expand Up @@ -484,13 +484,7 @@ impl AggregateExec {
/// Finds the DataType and SortDirection for this Aggregate, if there is one
pub fn get_minmax_desc(&self) -> Option<(Field, bool)> {
let agg_expr = self.aggr_expr.iter().exactly_one().ok()?;
if let Some(max) = agg_expr.as_any().downcast_ref::<Max>() {
Some((max.field().ok()?, true))
} else if let Some(min) = agg_expr.as_any().downcast_ref::<Min>() {
Some((min.field().ok()?, false))
} else {
None
}
agg_expr.get_minmax_desc()
}

/// true, if this Aggregate has a group-by with no required or explicit ordering,
Expand Down