use arrow::array::ArrayRef;
use arrow::datatypes::{DataType, Field, Schema};
use crate::error::Result;
use crate::execution::physical_plan::PhysicalExpr;
use arrow::record_batch::RecordBatch;
use std::sync::Arc;
pub type ScalarUdf = Arc<dyn Fn(&[ArrayRef]) -> Result<ArrayRef> + Send + Sync>;
#[derive(Clone)]
pub struct ScalarFunction {
pub name: String,
pub args: Vec<Field>,
pub return_type: DataType,
pub fun: ScalarUdf,
}
impl ScalarFunction {
pub fn new(
name: &str,
args: Vec<Field>,
return_type: DataType,
fun: ScalarUdf,
) -> Self {
Self {
name: name.to_owned(),
args,
return_type,
fun,
}
}
}
pub struct ScalarFunctionExpr {
name: String,
fun: Box<ScalarUdf>,
args: Vec<Arc<dyn PhysicalExpr>>,
return_type: DataType,
}
impl ScalarFunctionExpr {
pub fn new(
name: &str,
fun: Box<ScalarUdf>,
args: Vec<Arc<dyn PhysicalExpr>>,
return_type: &DataType,
) -> Self {
Self {
name: name.to_owned(),
fun,
args,
return_type: return_type.clone(),
}
}
}
impl PhysicalExpr for ScalarFunctionExpr {
fn name(&self) -> String {
self.name.clone()
}
fn data_type(&self, _input_schema: &Schema) -> Result<DataType> {
Ok(self.return_type.clone())
}
fn nullable(&self, _input_schema: &Schema) -> Result<bool> {
Ok(true)
}
fn evaluate(&self, batch: &RecordBatch) -> Result<ArrayRef> {
let inputs = self
.args
.iter()
.map(|e| e.evaluate(batch))
.collect::<Result<Vec<_>>>()?;
let fun = self.fun.as_ref();
(fun)(&inputs)
}
}