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

Function apply_nth_value

nodedb-query/src/window/offset.rs:83–99  ·  view source on GitHub ↗

PostgreSQL `nth_value(expr, n)` — value of `expr` at the n'th row of the window frame, NULL if the frame doesn't yet contain n rows. Default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, so the first n-1 rows of each partition return NULL and rows from the n'th onward return the value of `expr` at the n'th row.

(
    rows: &mut [(String, serde_json::Value)],
    indices: &[usize],
    spec: &WindowFuncSpec,
)

Source from the content-addressed store, hash-verified

81/// rows of each partition return NULL and rows from the n'th onward return
82/// the value of `expr` at the n'th row.
83pub(super) fn apply_nth_value(
84 rows: &mut [(String, serde_json::Value)],
85 indices: &[usize],
86 spec: &WindowFuncSpec,
87) {
88 let field = col_arg(spec, 0);
89 let n = usize_arg(spec, 1, 1).max(1);
90
91 for (pos, &i) in indices.iter().enumerate() {
92 let val = if pos + 1 >= n {
93 get_field(&rows[indices[n - 1]].1, field)
94 } else {
95 serde_json::Value::Null
96 };
97 set_window_col(&mut rows[i].1, &spec.alias, val);
98 }
99}

Callers 1

Calls 5

col_argFunction · 0.85
get_fieldFunction · 0.85
set_window_colFunction · 0.85
usize_argFunction · 0.70
iterMethod · 0.45

Tested by

no test coverage detected