1use serde::{Deserialize, Serialize};
4
5use super::expressions::Expr;
6
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9pub enum WindowFunction {
10 Lag {
12 expr: Box<Expr>,
13 offset: usize,
14 default: Option<Box<Expr>>,
15 },
16 Lead {
18 expr: Box<Expr>,
19 offset: usize,
20 default: Option<Box<Expr>>,
21 },
22 RowNumber,
24 Rank,
26 DenseRank,
28 Ntile(usize),
30 FirstValue(Box<Expr>),
32 LastValue(Box<Expr>),
34 NthValue(Box<Expr>, usize),
36 Sum(Box<Expr>),
38 Avg(Box<Expr>),
39 Min(Box<Expr>),
40 Max(Box<Expr>),
41 Count(Option<Box<Expr>>),
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
46pub enum SortDirection {
47 Ascending,
48 Descending,
49}
50
51#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
53pub struct OrderByClause {
54 pub columns: Vec<(Expr, SortDirection)>,
56}
57
58#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
60pub struct WindowSpec {
61 pub partition_by: Vec<Expr>,
63 pub order_by: Option<OrderByClause>,
65 pub frame: Option<WindowFrame>,
67}
68
69#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
71pub struct WindowFrame {
72 pub frame_type: WindowFrameType,
74 pub start: WindowBound,
76 pub end: WindowBound,
78}
79
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
82pub enum WindowFrameType {
83 Rows,
84 Range,
85}
86
87#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
89pub enum WindowBound {
90 UnboundedPreceding,
92 UnboundedFollowing,
94 CurrentRow,
96 Preceding(usize),
98 Following(usize),
100}
101
102#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
104pub struct WindowExpr {
105 pub function: WindowFunction,
106 pub over: WindowSpec,
107}