uni_plugin/traits/window.rs
1//! Cypher window-function plugins.
2//!
3//! Window functions evaluate over partitions of rows defined by `PARTITION
4//! BY` / `ORDER BY` clauses, producing one output per input row. Unlike
5//! aggregates they preserve the input cardinality.
6
7use arrow_array::ArrayRef;
8use arrow_schema::SchemaRef;
9use datafusion::arrow::record_batch::RecordBatch;
10use datafusion::logical_expr::Volatility;
11
12use crate::errors::FnError;
13use crate::traits::scalar::ArgType;
14
15/// A Cypher window-function plugin.
16pub trait WindowPluginFn: Send + Sync {
17 /// Static signature.
18 fn signature(&self) -> &WindowSignature;
19
20 /// Evaluate the window function over a partition.
21 ///
22 /// `partition` is the partition's rows, already sorted per `ORDER BY`.
23 /// `frame` describes the relative window over which each row's value is
24 /// computed. Implementations return an `ArrayRef` of length
25 /// `partition.num_rows()`.
26 ///
27 /// # Errors
28 ///
29 /// Returns [`FnError`] if the partition cannot be evaluated.
30 fn evaluate(&self, partition: &RecordBatch, frame: WindowFrame) -> Result<ArrayRef, FnError>;
31}
32
33/// Static signature of a window-function plugin.
34#[derive(Clone, Debug)]
35pub struct WindowSignature {
36 /// Argument types, in declaration order.
37 pub args: Vec<ArgType>,
38 /// Output type.
39 pub returns: ArgType,
40 /// DataFusion volatility.
41 pub volatility: Volatility,
42}
43
44/// Descriptor for the active window over a partition row.
45#[derive(Clone, Debug)]
46pub struct WindowFrame {
47 /// Schema of the partition (for column resolution by name).
48 pub schema: SchemaRef,
49 /// Inclusive start row index relative to the partition (0-based).
50 pub start: usize,
51 /// Exclusive end row index relative to the partition.
52 pub end: usize,
53 /// Column indices participating in `ORDER BY`.
54 pub order_by_indices: Vec<usize>,
55 /// Column indices participating in `PARTITION BY`.
56 pub partition_by_indices: Vec<usize>,
57}