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,
)
| 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. |
| 83 | pub(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 | } |
no test coverage detected