MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / evaluate_window_functions

Function evaluate_window_functions

nodedb-query/src/window/eval.rs:23–58  ·  view source on GitHub ↗

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],
)

Source from the content-addressed store, hash-verified

21/// reaching this dispatcher; an unrecognised name here is an internal bug
22/// and panics rather than silently dropping the projection.
23pub 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)]
61mod tests {

Callers 10

row_number_partitionedFunction · 0.85
running_sumFunction · 0.85
percent_rank_with_peersFunction · 0.85
cume_dist_distinct_keysFunction · 0.85
cume_dist_with_peersFunction · 0.85
execute_document_scanMethod · 0.85

Calls 12

build_partitionsFunction · 0.85
apply_row_numberFunction · 0.85
apply_rankFunction · 0.85
apply_dense_rankFunction · 0.85
apply_ntileFunction · 0.85
apply_percent_rankFunction · 0.85
apply_cume_distFunction · 0.85
apply_lagFunction · 0.85
apply_leadFunction · 0.85
apply_nth_valueFunction · 0.85
apply_aggregate_windowFunction · 0.85
as_strMethod · 0.45

Tested by 8

row_number_partitionedFunction · 0.68
running_sumFunction · 0.68
percent_rank_with_peersFunction · 0.68
cume_dist_distinct_keysFunction · 0.68
cume_dist_with_peersFunction · 0.68