use std::fmt::Debug;
use std::sync::Arc;
use vortex_array::aggregate_fn::AggregateFnRef;
use vortex_array::expr::BoundExpression;
use vortex_array::scalar_fn::ScalarFnId;
use vortex_session::VortexSession;
use crate::stats::bound;
use crate::stats::session::StatsSessionExt;
pub type StatDerivationRef = Arc<dyn StatDerivation>;
pub trait StatDerivation: Debug + Send + Sync + 'static {
fn scalar_fn_id(&self) -> ScalarFnId;
fn derive_stat(
&self,
expr: &BoundExpression,
aggregate_fn: &AggregateFnRef,
session: &VortexSession,
) -> Option<BoundExpression>;
}
pub fn stat_expr(
expr: &BoundExpression,
aggregate_fn: &AggregateFnRef,
session: &VortexSession,
) -> Option<BoundExpression> {
if let Some(scalar_fn) = expr.as_scalar()
&& let Some(derivation) = session.stats().stat_derivation_for(scalar_fn.id())
{
return derivation.derive_stat(expr, aggregate_fn, session);
}
aggregate_fn
.return_dtype(expr.dtype())
.is_some()
.then(|| bound::stat(expr.clone(), aggregate_fn.clone()))
}