Skip to main content

nodedb_query/window/
spec.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Window function spec and frame types serialized over the SPSC bridge.
4
5use crate::expr::SqlExpr;
6
7/// A window function specification.
8#[derive(
9    Debug,
10    Clone,
11    serde::Serialize,
12    serde::Deserialize,
13    zerompk::ToMessagePack,
14    zerompk::FromMessagePack,
15)]
16pub struct WindowFuncSpec {
17    /// Output column name (e.g., "row_num", "running_sum").
18    pub alias: String,
19    /// Function name: row_number, rank, dense_rank, ntile, percent_rank,
20    /// cume_dist, lag, lead, nth_value, sum, count, avg, min, max,
21    /// first_value, last_value.
22    pub func_name: String,
23    /// Function arguments (e.g., `salary` for SUM(salary)). Empty for ROW_NUMBER.
24    pub args: Vec<SqlExpr>,
25    /// PARTITION BY column names. Empty = single partition (entire result set).
26    pub partition_by: Vec<String>,
27    /// ORDER BY within each partition: [(field, ascending)].
28    pub order_by: Vec<(String, bool)>,
29    /// Window frame specification.
30    pub frame: WindowFrame,
31}
32
33/// Window frame: defines which rows within the partition are visible to the function.
34#[derive(
35    Debug,
36    Clone,
37    serde::Serialize,
38    serde::Deserialize,
39    zerompk::ToMessagePack,
40    zerompk::FromMessagePack,
41)]
42pub struct WindowFrame {
43    /// Frame mode: "rows" or "range".
44    pub mode: String,
45    /// Start bound.
46    pub start: FrameBound,
47    /// End bound.
48    pub end: FrameBound,
49}
50
51impl Default for WindowFrame {
52    fn default() -> Self {
53        Self {
54            mode: "range".into(),
55            start: FrameBound::UnboundedPreceding,
56            end: FrameBound::CurrentRow,
57        }
58    }
59}
60
61/// Window frame boundary.
62#[derive(
63    Debug,
64    Clone,
65    serde::Serialize,
66    serde::Deserialize,
67    zerompk::ToMessagePack,
68    zerompk::FromMessagePack,
69)]
70pub enum FrameBound {
71    UnboundedPreceding,
72    Preceding(u64),
73    CurrentRow,
74    Following(u64),
75    UnboundedFollowing,
76}