Skip to content

Commit 1ffe053

Browse files
authored
Replacing pattern matching through downcast with trait method (#11257)
1 parent 0922d4a commit 1ffe053

File tree

3 files changed

+21
-8
lines changed
  • datafusion
    • physical-expr/src/aggregate
    • physical-expr-common/src/aggregate
    • physical-plan/src/aggregates

3 files changed

+21
-8
lines changed

datafusion/physical-expr-common/src/aggregate/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,17 @@ pub trait AggregateExpr: Send + Sync + Debug + PartialEq<dyn Any> {
221221
) -> Option<Arc<dyn AggregateExpr>> {
222222
None
223223
}
224+
225+
/// If this function is max, return (output_field, true)
226+
/// if the function is min, return (output_field, false)
227+
/// otherwise return None (the default)
228+
///
229+
/// output_field is the name of the column produced by this aggregate
230+
///
231+
/// Note: this is used to use special aggregate implementations in certain conditions
232+
fn get_minmax_desc(&self) -> Option<(Field, bool)> {
233+
None
234+
}
224235
}
225236

226237
/// Stores the physical expressions used inside the `AggregateExpr`.

datafusion/physical-expr/src/aggregate/min_max.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ impl AggregateExpr for Max {
266266
fn create_sliding_accumulator(&self) -> Result<Box<dyn Accumulator>> {
267267
Ok(Box::new(SlidingMaxAccumulator::try_new(&self.data_type)?))
268268
}
269+
270+
fn get_minmax_desc(&self) -> Option<(Field, bool)> {
271+
Some((self.field().ok()?, true))
272+
}
269273
}
270274

271275
impl PartialEq<dyn Any> for Max {
@@ -1018,6 +1022,10 @@ impl AggregateExpr for Min {
10181022
fn create_sliding_accumulator(&self) -> Result<Box<dyn Accumulator>> {
10191023
Ok(Box::new(SlidingMinAccumulator::try_new(&self.data_type)?))
10201024
}
1025+
1026+
fn get_minmax_desc(&self) -> Option<(Field, bool)> {
1027+
Some((self.field().ok()?, false))
1028+
}
10211029
}
10221030

10231031
impl PartialEq<dyn Any> for Min {

datafusion/physical-plan/src/aggregates/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use datafusion_execution::TaskContext;
4141
use datafusion_expr::Accumulator;
4242
use datafusion_physical_expr::{
4343
equivalence::{collapse_lex_req, ProjectionMapping},
44-
expressions::{Column, Max, Min, UnKnownColumn},
44+
expressions::{Column, UnKnownColumn},
4545
physical_exprs_contains, AggregateExpr, EquivalenceProperties, LexOrdering,
4646
LexRequirement, PhysicalExpr, PhysicalSortRequirement,
4747
};
@@ -484,13 +484,7 @@ impl AggregateExec {
484484
/// Finds the DataType and SortDirection for this Aggregate, if there is one
485485
pub fn get_minmax_desc(&self) -> Option<(Field, bool)> {
486486
let agg_expr = self.aggr_expr.iter().exactly_one().ok()?;
487-
if let Some(max) = agg_expr.as_any().downcast_ref::<Max>() {
488-
Some((max.field().ok()?, true))
489-
} else if let Some(min) = agg_expr.as_any().downcast_ref::<Min>() {
490-
Some((min.field().ok()?, false))
491-
} else {
492-
None
493-
}
487+
agg_expr.get_minmax_desc()
494488
}
495489

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

0 commit comments

Comments
 (0)