Evaluate window functions over sorted, partitioned rows. `rows` is the sorted result set. Each row is a `(doc_id, serde_json::Value)`. The same rows are mutated in place with window columns appended to each document. Unknown window function names must be rejected by the planner before reaching this dispatcher; an unrecognised name here is an internal bug and panics rather than silently dropping
(
rows: &mut [(String, serde_json::Value)],
specs: &[WindowFuncSpec],
)
| 21 | /// reaching this dispatcher; an unrecognised name here is an internal bug |
| 22 | /// and panics rather than silently dropping the projection. |
| 23 | pub fn evaluate_window_functions( |
| 24 | rows: &mut [(String, serde_json::Value)], |
| 25 | specs: &[WindowFuncSpec], |
| 26 | ) { |
| 27 | for spec in specs { |
| 28 | let partitions = build_partitions(rows, &spec.partition_by); |
| 29 | |
| 30 | for partition_indices in &partitions { |
| 31 | match spec.func_name.as_str() { |
| 32 | "row_number" => apply_row_number(rows, partition_indices, &spec.alias), |
| 33 | "rank" => apply_rank(rows, partition_indices, &spec.alias, &spec.order_by), |
| 34 | "dense_rank" => { |
| 35 | apply_dense_rank(rows, partition_indices, &spec.alias, &spec.order_by) |
| 36 | } |
| 37 | "ntile" => apply_ntile(rows, partition_indices, spec), |
| 38 | "percent_rank" => { |
| 39 | apply_percent_rank(rows, partition_indices, &spec.alias, &spec.order_by) |
| 40 | } |
| 41 | "cume_dist" => { |
| 42 | apply_cume_dist(rows, partition_indices, &spec.alias, &spec.order_by) |
| 43 | } |
| 44 | "lag" => apply_lag(rows, partition_indices, spec), |
| 45 | "lead" => apply_lead(rows, partition_indices, spec), |
| 46 | "nth_value" => apply_nth_value(rows, partition_indices, spec), |
| 47 | "sum" | "count" | "avg" | "min" | "max" | "first_value" | "last_value" => { |
| 48 | apply_aggregate_window(rows, partition_indices, spec) |
| 49 | } |
| 50 | other => { |
| 51 | unreachable!( |
| 52 | "invariant: SQL planner validates window function names before dispatch; '{other}' is unrecognized and should have been rejected at planning time" |
| 53 | ) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[cfg(test)] |
| 61 | mod tests { |