Skip to main content

WindowFunction

Trait WindowFunction 

Source
pub trait WindowFunction: Send + Sync {
    type State: Send;

    // Required methods
    fn initial_state(&self) -> Self::State;
    fn step(&self, state: &mut Self::State, args: &[SqliteValue]) -> Result<()>;
    fn inverse(
        &self,
        state: &mut Self::State,
        args: &[SqliteValue],
    ) -> Result<()>;
    fn value(&self, state: &Self::State) -> Result<SqliteValue>;
    fn finalize(&self, state: Self::State) -> Result<SqliteValue>;
    fn num_args(&self) -> i32;
    fn name(&self) -> &str;
}
Expand description

A window SQL function (e.g. SUM() OVER (...), custom moving averages).

Window functions extend aggregates with:

  • inverse: remove a row from the frame (enables O(1) sliding windows)
  • value: peek at the current result without consuming state

This trait is open (user-implementable).

§State Lifecycle

  1. initial_state creates a fresh accumulator.
  2. For each row in the frame: step adds, inverse removes.
  3. After each step/inverse: value returns the current result.
  4. At partition end: finalize consumes state and returns the final value.

Required Associated Types§

Source

type State: Send

The per-partition accumulator type.

Required Methods§

Source

fn initial_state(&self) -> Self::State

Create a fresh accumulator.

Source

fn step(&self, state: &mut Self::State, args: &[SqliteValue]) -> Result<()>

Add a row to the window frame.

Source

fn inverse(&self, state: &mut Self::State, args: &[SqliteValue]) -> Result<()>

Remove a row from the window frame (sliding window support).

This is the key difference from AggregateFunction: by supporting removal, the engine can maintain a running window in O(1) time per row rather than recomputing the entire frame.

Source

fn value(&self, state: &Self::State) -> Result<SqliteValue>

Return the current result without consuming state.

Called after each step/inverse to provide the windowed value for the current row. Must be callable multiple times.

Source

fn finalize(&self, state: Self::State) -> Result<SqliteValue>

Consume the accumulator and produce the final result.

Source

fn num_args(&self) -> i32

The number of arguments this function accepts (-1 = variadic).

Source

fn name(&self) -> &str

The function name, used in error messages and EXPLAIN output.

Implementors§