Skip to main content

shape_ast/ast/
windows.rs

1//! Window function types for Shape AST
2
3use serde::{Deserialize, Serialize};
4
5use super::expressions::Expr;
6
7/// SQL-style window function
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9pub enum WindowFunction {
10    /// LAG(expr, offset, default) - access previous row value
11    Lag {
12        expr: Box<Expr>,
13        offset: usize,
14        default: Option<Box<Expr>>,
15    },
16    /// LEAD(expr, offset, default) - access next row value
17    Lead {
18        expr: Box<Expr>,
19        offset: usize,
20        default: Option<Box<Expr>>,
21    },
22    /// ROW_NUMBER() - sequential row number in partition
23    RowNumber,
24    /// RANK() - rank with gaps for ties
25    Rank,
26    /// DENSE_RANK() - rank without gaps
27    DenseRank,
28    /// NTILE(n) - divide rows into n buckets
29    Ntile(usize),
30    /// FIRST_VALUE(expr) - first value in window
31    FirstValue(Box<Expr>),
32    /// LAST_VALUE(expr) - last value in window
33    LastValue(Box<Expr>),
34    /// NTH_VALUE(expr, n) - nth value in window
35    NthValue(Box<Expr>, usize),
36    /// Running aggregate functions
37    Sum(Box<Expr>),
38    Avg(Box<Expr>),
39    Min(Box<Expr>),
40    Max(Box<Expr>),
41    Count(Option<Box<Expr>>),
42}
43
44/// Sort direction for ORDER BY clause
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
46pub enum SortDirection {
47    Ascending,
48    Descending,
49}
50
51/// ORDER BY clause for query results
52#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
53pub struct OrderByClause {
54    /// List of (expression, direction) pairs
55    pub columns: Vec<(Expr, SortDirection)>,
56}
57
58/// Window specification for OVER clause
59#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
60pub struct WindowSpec {
61    /// PARTITION BY expressions
62    pub partition_by: Vec<Expr>,
63    /// ORDER BY clause
64    pub order_by: Option<OrderByClause>,
65    /// Window frame (ROWS/RANGE BETWEEN)
66    pub frame: Option<WindowFrame>,
67}
68
69/// Window frame definition
70#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
71pub struct WindowFrame {
72    /// Frame type (ROWS or RANGE)
73    pub frame_type: WindowFrameType,
74    /// Start boundary
75    pub start: WindowBound,
76    /// End boundary (defaults to CURRENT ROW if not specified)
77    pub end: WindowBound,
78}
79
80/// Window frame type
81#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
82pub enum WindowFrameType {
83    Rows,
84    Range,
85}
86
87/// Window frame boundary
88#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
89pub enum WindowBound {
90    /// UNBOUNDED PRECEDING
91    UnboundedPreceding,
92    /// UNBOUNDED FOLLOWING
93    UnboundedFollowing,
94    /// CURRENT ROW
95    CurrentRow,
96    /// n PRECEDING
97    Preceding(usize),
98    /// n FOLLOWING
99    Following(usize),
100}
101
102/// Window function expression: func() OVER (window_spec)
103#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
104pub struct WindowExpr {
105    pub function: WindowFunction,
106    pub over: WindowSpec,
107}