Trait datafusion::logical_expr::WindowUDFImpl

source ·
pub trait WindowUDFImpl: Debug + Send + Sync {
    // Required methods
    fn as_any(&self) -> &(dyn Any + 'static);
    fn name(&self) -> &str;
    fn signature(&self) -> &Signature;
    fn return_type(
        &self,
        arg_types: &[DataType]
    ) -> Result<DataType, DataFusionError>;
    fn partition_evaluator(
        &self
    ) -> Result<Box<dyn PartitionEvaluator>, DataFusionError>;

    // Provided method
    fn aliases(&self) -> &[String] { ... }
}
Expand description

Trait for implementing WindowUDF.

This trait exposes the full API for implementing user defined window functions and can be used to implement any function.

See advanced_udwf.rs for a full example with complete implementation and WindowUDF for other available options.

§Basic Example

#[derive(Debug, Clone)]
struct SmoothIt {
  signature: Signature
};

impl SmoothIt {
  fn new() -> Self {
    Self {
      signature: Signature::uniform(1, vec![DataType::Int32], Volatility::Immutable)
     }
  }
}

/// Implement the WindowUDFImpl trait for AddOne
impl WindowUDFImpl for SmoothIt {
   fn as_any(&self) -> &dyn Any { self }
   fn name(&self) -> &str { "smooth_it" }
   fn signature(&self) -> &Signature { &self.signature }
   fn return_type(&self, args: &[DataType]) -> Result<DataType> {
     if !matches!(args.get(0), Some(&DataType::Int32)) {
       return plan_err!("smooth_it only accepts Int32 arguments");
     }
     Ok(DataType::Int32)
   }
   // The actual implementation would add one to the argument
   fn partition_evaluator(&self) -> Result<Box<dyn PartitionEvaluator>> { unimplemented!() }
}

// Create a new WindowUDF from the implementation
let smooth_it = WindowUDF::from(SmoothIt::new());

// Call the function `add_one(col)`
let expr = smooth_it.call(
    vec![col("speed")],                 // smooth_it(speed)
    vec![col("car")],                   // PARTITION BY car
    vec![col("time").sort(true, true)], // ORDER BY time ASC
    WindowFrame::new(None),
);

Required Methods§

source

fn as_any(&self) -> &(dyn Any + 'static)

Returns this object as an Any trait object

source

fn name(&self) -> &str

Returns this function’s name

source

fn signature(&self) -> &Signature

Returns the function’s Signature for information about what input types are accepted and the function’s Volatility.

source

fn return_type( &self, arg_types: &[DataType] ) -> Result<DataType, DataFusionError>

What DataType will be returned by this function, given the types of the arguments

source

fn partition_evaluator( &self ) -> Result<Box<dyn PartitionEvaluator>, DataFusionError>

Invoke the function, returning the PartitionEvaluator instance

Provided Methods§

source

fn aliases(&self) -> &[String]

Returns any aliases (alternate names) for this function.

Note: aliases should only include names other than Self::name. Defaults to [] (no aliases)

Implementors§