velesdb_core/velesql/ast/window.rs
1//! Window function types for VelesQL (Issue #386).
2//!
3//! Phase 1: `ROW_NUMBER`, `RANK`, `DENSE_RANK` with `PARTITION BY` + `ORDER BY`.
4
5use serde::{Deserialize, Serialize};
6
7/// Window function type (Phase 1: ranking functions).
8///
9/// Marked `#[non_exhaustive]` so future phases (`LAG`, `LEAD`,
10/// `FIRST_VALUE`, `NTILE`, aggregate windows, …) can be added
11/// without a semver break. Downstream crates must include a
12/// wildcard (`_ =>`) arm when matching on this enum.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
14#[non_exhaustive]
15pub enum WindowFunctionType {
16 /// `ROW_NUMBER()` — sequential numbering 1..N within each partition.
17 RowNumber,
18 /// `RANK()` — ranking with gaps on ties (e.g., 1, 2, 2, 4).
19 Rank,
20 /// `DENSE_RANK()` — ranking without gaps on ties (e.g., 1, 2, 2, 3).
21 DenseRank,
22}
23
24impl WindowFunctionType {
25 /// Returns the default column alias for this function type.
26 #[must_use]
27 pub fn default_alias(&self) -> &'static str {
28 match self {
29 Self::RowNumber => "row_number",
30 Self::Rank => "rank",
31 Self::DenseRank => "dense_rank",
32 }
33 }
34}
35
36/// `ORDER BY` item inside a window `OVER` clause.
37#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
38pub struct WindowOrderBy {
39 /// Column to sort by within the partition.
40 pub column: String,
41 /// Sort direction (`true` = DESC).
42 pub descending: bool,
43}
44
45/// The `OVER` clause defining the window specification.
46#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
47pub struct OverClause {
48 /// `PARTITION BY` columns (empty = entire result set is one partition).
49 #[serde(default)]
50 pub partition_by: Vec<String>,
51 /// `ORDER BY` within each partition.
52 #[serde(default)]
53 pub order_by: Vec<WindowOrderBy>,
54}
55
56/// A window function expression in the SELECT list.
57///
58/// Example: `ROW_NUMBER() OVER (PARTITION BY source ORDER BY score DESC) AS rn`
59#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
60pub struct WindowFunction {
61 /// Type of window function.
62 pub function_type: WindowFunctionType,
63 /// `OVER` clause specification.
64 pub over_clause: OverClause,
65 /// Optional alias (`AS` clause). Defaults to function name.
66 pub alias: Option<String>,
67}