Skip to main content

Module window

Module window 

Source
Expand description

SQL:2003 window-function evaluation kernel.

This module implements a self-contained window-evaluation engine over an already-materialized set of rows. It is intentionally decoupled from the SQL parser/planner: callers drive it directly by constructing a WindowFunction and a WindowSpec, then calling evaluate_window (closure-driven) or evaluate_window_batch (over a RecordBatch).

§Supported functions

  • ROW_NUMBER() — sequential 1-based ordinal within each partition.
  • RANK() — competition ranking: equal order keys share a rank and the next distinct key skips ranks (1, 1, 3, ...).
  • DENSE_RANK() — like RANK() but without gaps (1, 1, 2, ...).
  • LAG(expr[, offset[, default]]) — value of expr offset rows before the current row inside the sorted partition; out-of-range yields the supplied default or NULL.
  • LEAD(expr[, offset[, default]]) — symmetric to LAG but forward.
  • FIRST_VALUE(expr) / LAST_VALUE(expr) — first / last value of expr in the sorted partition.
  • NTH_VALUE(expr, n) — the n-th (1-based) value of expr in the sorted partition, or NULL when the partition has fewer than n rows.

§Row model

The crate stores data column-by-column (RecordBatch / ColumnData), so there is no concrete “row” struct to borrow. The kernel therefore addresses values by (row_index, column_index) pairs and pulls them through a value_at closure. Column indices in WindowSpec and the target_column arguments are positions into that columnar model, exactly as in crate::executor::aggregate and crate::executor::sort.

§Value ordering

Ordering follows the same rules as crate::executor::sort: real values compare naturally, NULLs sort after non-NULLs in ascending order, and a descending OrderKey simply reverses the per-key ordering. Numeric values of differing widths are compared after promotion (matching the coercion used by crate::executor::filter). The comparison never assumes the full set of Value variants, so newer variants degrade gracefully to “equal”.

Structs§

OrderKey
A single ORDER BY key inside an OVER (... ORDER BY ...) clause.
WindowSpec
The OVER (PARTITION BY ... ORDER BY ...) specification.

Enums§

WindowFunction
A window function to evaluate over partitioned, ordered rows.

Functions§

evaluate_window
Evaluate a window function over num_rows rows.
evaluate_window_batch
Evaluate a window function directly over a RecordBatch.