Jefffrey commented on code in PR #10226: URL: https://github.com/apache/datafusion/pull/10226#discussion_r1580884824
########## datafusion/physical-expr/src/aggregate/median.rs: ########## @@ -196,6 +184,192 @@ impl<T: ArrowNumericType> Accumulator for MedianAccumulator<T> { } } +/// MEDIAN(DISTINCT) aggregate expression. Similar to MEDIAN but computes after taking +/// all unique values. This may use a lot of memory if the cardinality is high. +#[derive(Debug)] +pub struct DistinctMedian { + name: String, + expr: Arc<dyn PhysicalExpr>, + data_type: DataType, +} + +impl DistinctMedian { + /// Create a new MEDIAN(DISTINCT) aggregate function + pub fn new( + expr: Arc<dyn PhysicalExpr>, + name: impl Into<String>, + data_type: DataType, + ) -> Self { + Self { + name: name.into(), + expr, + data_type, + } + } +} + +impl AggregateExpr for DistinctMedian { + /// Return a reference to Any that can be used for downcasting + fn as_any(&self) -> &dyn Any { + self + } + + fn field(&self) -> Result<Field> { + Ok(Field::new(&self.name, self.data_type.clone(), true)) + } + + fn create_accumulator(&self) -> Result<Box<dyn Accumulator>> { + use arrow_array::types::*; + macro_rules! helper { + ($t:ty, $dt:expr) => { + Ok(Box::new(DistinctMedianAccumulator::<$t> { + data_type: $dt.clone(), + distinct_values: Default::default(), + })) + }; + } Review Comment: Removed this bit of duplication by folding distinct into Median -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org