Skip to content

Commit

Permalink
Replacing pattern matching through downcast with trait method (#11257)
Browse files Browse the repository at this point in the history
  • Loading branch information
edmondop authored Jul 3, 2024
1 parent 0922d4a commit 1ffe053
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
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

0 comments on commit 1ffe053

Please sign in to comment.