use super::type_coercion::coerce;
use crate::{PhysicalExpr, ScalarFunctionExpr};
use arrow::datatypes::Schema;
use datafusion_common::Result;
pub use datafusion_expr::ScalarUDF;
use std::sync::Arc;
pub fn create_physical_expr(
fun: &ScalarUDF,
input_phy_exprs: &[Arc<dyn PhysicalExpr>],
input_schema: &Schema,
) -> Result<Arc<dyn PhysicalExpr>> {
let coerced_phy_exprs = coerce(input_phy_exprs, input_schema, &fun.signature)?;
let coerced_exprs_types = coerced_phy_exprs
.iter()
.map(|e| e.data_type(input_schema))
.collect::<Result<Vec<_>>>()?;
Ok(Arc::new(ScalarFunctionExpr::new(
&fun.name,
fun.fun.clone(),
coerced_phy_exprs,
(fun.return_type)(&coerced_exprs_types)?.as_ref(),
)))
}