use crate::expressions::try_cast;
use crate::PhysicalExpr;
use arrow::datatypes::Schema;
use datafusion_common::Result;
use datafusion_expr::{type_coercion, AggregateFunction, Signature};
use std::sync::Arc;
pub fn coerce_exprs(
agg_fun: &AggregateFunction,
input_exprs: &[Arc<dyn PhysicalExpr>],
schema: &Schema,
signature: &Signature,
) -> Result<Vec<Arc<dyn PhysicalExpr>>> {
if input_exprs.is_empty() {
return Ok(vec![]);
}
let input_types = input_exprs
.iter()
.map(|e| e.data_type(schema))
.collect::<Result<Vec<_>>>()?;
let coerced_types =
type_coercion::aggregates::coerce_types(agg_fun, &input_types, signature)?;
input_exprs
.iter()
.zip(coerced_types.into_iter())
.map(|(expr, coerced_type)| try_cast(expr.clone(), schema, coerced_type))
.collect::<Result<Vec<_>>>()
}