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
initial_statecreates a fresh accumulator.- For each row in the frame:
stepadds,inverseremoves. - After each step/inverse:
valuereturns the current result. - At partition end:
finalizeconsumes state and returns the final value.
Required Associated Types§
Required Methods§
Sourcefn initial_state(&self) -> Self::State
fn initial_state(&self) -> Self::State
Create a fresh accumulator.
Sourcefn step(&self, state: &mut Self::State, args: &[SqliteValue]) -> Result<()>
fn step(&self, state: &mut Self::State, args: &[SqliteValue]) -> Result<()>
Add a row to the window frame.
Sourcefn inverse(&self, state: &mut Self::State, args: &[SqliteValue]) -> Result<()>
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.
Sourcefn value(&self, state: &Self::State) -> Result<SqliteValue>
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.
Sourcefn finalize(&self, state: Self::State) -> Result<SqliteValue>
fn finalize(&self, state: Self::State) -> Result<SqliteValue>
Consume the accumulator and produce the final result.