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;
// Provided methods
fn min_args(&self) -> i32 { ... }
fn max_args(&self) -> Option<i32> { ... }
fn accepts_arg_count(&self, num_args: i32) -> bool { ... }
}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.
Provided Methods§
Sourcefn min_args(&self) -> i32
fn min_args(&self) -> i32
Minimum accepted SQL argument count for variadic functions.
Some built-in window functions receive ORDER BY values internally even when their SQL call syntax accepts no visible arguments. These bounds describe SQL-visible arity, not the runtime step argument slice.
Sourcefn max_args(&self) -> Option<i32>
fn max_args(&self) -> Option<i32>
Maximum accepted SQL argument count, or None for unbounded variadic
functions.
Sourcefn accepts_arg_count(&self, num_args: i32) -> bool
fn accepts_arg_count(&self, num_args: i32) -> bool
Return whether this function accepts num_args SQL arguments.