use std::{
fmt::{self, Debug, Display, Formatter},
sync::Arc,
};
use crate::{
Expr, PartitionEvaluatorFactory, ReturnTypeFunction, Signature, WindowFrame,
};
#[derive(Clone)]
pub struct WindowUDF {
pub name: String,
pub signature: Signature,
pub return_type: ReturnTypeFunction,
pub partition_evaluator_factory: PartitionEvaluatorFactory,
}
impl Debug for WindowUDF {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("WindowUDF")
.field("name", &self.name)
.field("signature", &self.signature)
.field("return_type", &"<func>")
.field("partition_evaluator_factory", &"<func>")
.finish_non_exhaustive()
}
}
impl Display for WindowUDF {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.name)
}
}
impl PartialEq for WindowUDF {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.signature == other.signature
}
}
impl Eq for WindowUDF {}
impl std::hash::Hash for WindowUDF {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.name.hash(state);
self.signature.hash(state);
}
}
impl WindowUDF {
pub fn new(
name: &str,
signature: &Signature,
return_type: &ReturnTypeFunction,
partition_evaluator_factory: &PartitionEvaluatorFactory,
) -> Self {
Self {
name: name.to_owned(),
signature: signature.clone(),
return_type: return_type.clone(),
partition_evaluator_factory: partition_evaluator_factory.clone(),
}
}
pub fn call(
&self,
args: Vec<Expr>,
partition_by: Vec<Expr>,
order_by: Vec<Expr>,
window_frame: WindowFrame,
) -> Expr {
let fun = crate::WindowFunction::WindowUDF(Arc::new(self.clone()));
Expr::WindowFunction(crate::expr::WindowFunction {
fun,
args,
partition_by,
order_by,
window_frame,
})
}
}