use arrow::datatypes::DataType;
use datafusion_common::Result;
use datafusion_expr::function::AccumulatorArgs;
use datafusion_expr::{
Accumulator, AggregateUDFImpl, ColumnarValue, DistinctHandling, Expr,
ExpressionPlacement, ScalarFunctionArgs, ScalarUDF, ScalarUDFImpl, Signature,
TypeSignature, Volatility, lit,
};
use std::hash::{Hash, Hasher};
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct PlacementTestUDF {
signature: Signature,
placement: ExpressionPlacement,
id: usize,
}
impl Default for PlacementTestUDF {
fn default() -> Self {
Self::new()
}
}
impl PlacementTestUDF {
pub fn new() -> Self {
Self {
signature: Signature::new(
TypeSignature::OneOf(vec![TypeSignature::Any(1), TypeSignature::Any(2)]),
Volatility::Immutable,
),
placement: ExpressionPlacement::MoveTowardsLeafNodes,
id: 0,
}
}
pub fn with_placement(mut self, placement: ExpressionPlacement) -> Self {
self.placement = placement;
self
}
pub fn with_id(mut self, id: usize) -> Self {
self.id = id;
self
}
pub fn with_volatility(mut self, volatility: Volatility) -> Self {
self.signature.volatility = volatility;
self
}
}
impl ScalarUDFImpl for PlacementTestUDF {
fn name(&self) -> &str {
match self.placement {
ExpressionPlacement::MoveTowardsLeafNodes => "leaf_udf",
ExpressionPlacement::KeepInPlace => "keep_in_place_udf",
ExpressionPlacement::Column => "column_udf",
ExpressionPlacement::Literal => "literal_udf",
}
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
Ok(DataType::UInt32)
}
fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> {
panic!("PlacementTestUDF: not intended for execution")
}
fn placement(&self, _args: &[ExpressionPlacement]) -> ExpressionPlacement {
self.placement
}
}
pub fn leaf_udf_expr(arg: Expr) -> Expr {
let udf = ScalarUDF::new_from_impl(
PlacementTestUDF::new().with_placement(ExpressionPlacement::MoveTowardsLeafNodes),
);
udf.call(vec![arg])
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct GetFieldLikeUDF {
signature: Signature,
}
impl Default for GetFieldLikeUDF {
fn default() -> Self {
Self::new()
}
}
impl GetFieldLikeUDF {
pub fn new() -> Self {
Self {
signature: Signature::new(TypeSignature::Any(2), Volatility::Immutable),
}
}
}
impl ScalarUDFImpl for GetFieldLikeUDF {
fn name(&self) -> &str {
"get_field_like"
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
Ok(DataType::UInt32)
}
fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> {
panic!("GetFieldLikeUDF: not intended for execution")
}
fn placement(&self, args: &[ExpressionPlacement]) -> ExpressionPlacement {
match args.first() {
Some(
ExpressionPlacement::Column | ExpressionPlacement::MoveTowardsLeafNodes,
) if args[1..]
.iter()
.all(|p| matches!(p, ExpressionPlacement::Literal)) =>
{
ExpressionPlacement::MoveTowardsLeafNodes
}
_ => ExpressionPlacement::KeepInPlace,
}
}
}
pub fn get_field_like(base: Expr, key: &str) -> Expr {
let udf = ScalarUDF::new_from_impl(GetFieldLikeUDF::new());
udf.call(vec![base, lit(key)])
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DistinctHandlingTestUDAF {
name: &'static str,
handling: DistinctHandling,
signature: Signature,
}
impl DistinctHandlingTestUDAF {
pub fn new(name: &'static str, handling: DistinctHandling) -> Self {
Self {
name,
handling,
signature: Signature::any(1, Volatility::Immutable),
}
}
pub fn with_volatility(mut self, volatility: Volatility) -> Self {
self.signature.volatility = volatility;
self
}
}
impl Hash for DistinctHandlingTestUDAF {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
self.signature.hash(state);
}
}
impl AggregateUDFImpl for DistinctHandlingTestUDAF {
fn name(&self) -> &str {
self.name
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
Ok(DataType::UInt32)
}
fn accumulator(&self, _acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
panic!("DistinctHandlingTestUDAF: not intended for execution")
}
fn distinct_handling(&self) -> DistinctHandling {
self.handling
}
}