qail_core/ast/
operators.rs

1use serde::{Deserialize, Serialize};
2
3/// The action type (SQL operation).
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
5pub enum Action {
6    /// SELECT query
7    Get,
8    /// UPDATE query  
9    Set,
10    /// DELETE query
11    Del,
12    /// INSERT query
13    Add,
14    /// Generate Rust struct from table schema
15    Gen,
16    /// Create Table (Make)
17    Make,
18    /// Drop Table (Drop)
19    Drop,
20    /// Modify Table (Mod)
21    Mod,
22    /// Window Function (Over)
23    Over,
24    /// CTE (With)
25    With,
26    /// Create Index
27    Index,
28    // Transactions
29    TxnStart,
30    TxnCommit,
31    TxnRollback,
32    Put,
33    DropCol,
34    RenameCol,
35    // Additional clauses
36    /// JSON_TABLE - convert JSON to relational rows
37    JsonTable,
38}
39
40impl std::fmt::Display for Action {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        match self {
43            Action::Get => write!(f, "GET"),
44            Action::Set => write!(f, "SET"),
45            Action::Del => write!(f, "DEL"),
46            Action::Add => write!(f, "ADD"),
47            Action::Gen => write!(f, "GEN"),
48            Action::Make => write!(f, "MAKE"),
49            Action::Drop => write!(f, "DROP"),
50            Action::Mod => write!(f, "MOD"),
51            Action::Over => write!(f, "OVER"),
52            Action::With => write!(f, "WITH"),
53            Action::Index => write!(f, "INDEX"),
54            Action::TxnStart => write!(f, "TXN_START"),
55            Action::TxnCommit => write!(f, "TXN_COMMIT"),
56            Action::TxnRollback => write!(f, "TXN_ROLLBACK"),
57            Action::Put => write!(f, "PUT"),
58            Action::DropCol => write!(f, "DROP_COL"),
59            Action::RenameCol => write!(f, "RENAME_COL"),
60            Action::JsonTable => write!(f, "JSON_TABLE"),
61        }
62    }
63}
64
65/// Logical operator between conditions.
66#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
67pub enum LogicalOp {
68    #[default]
69    And,
70    Or,
71}
72
73/// Sort order direction.
74#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
75pub enum SortOrder {
76    Asc,
77    Desc,
78    /// ASC NULLS FIRST (nulls at top)
79    AscNullsFirst,
80    /// ASC NULLS LAST (nulls at bottom)
81    AscNullsLast,
82    /// DESC NULLS FIRST (nulls at top)
83    DescNullsFirst,
84    /// DESC NULLS LAST (nulls at bottom)
85    DescNullsLast,
86}
87
88/// Comparison operators.
89#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
90pub enum Operator {
91    /// Equal (=)
92    Eq,
93    /// Not equal (!=, <>)
94    Ne,
95    /// Greater than (>)
96    Gt,
97    /// Greater than or equal (>=)
98    Gte,
99    /// Less than (<)
100    Lt,
101    /// Less than or equal (<=)  
102    Lte,
103    /// Fuzzy match (~) -> ILIKE
104    Fuzzy,
105    /// IN array
106    In,
107    /// NOT IN array
108    NotIn,
109    /// IS NULL
110    IsNull,
111    /// IS NOT NULL
112    IsNotNull,
113    /// JSON/Array Contains (@>)
114    Contains,
115    /// JSON Key Exists (?)
116    KeyExists,
117    /// JSON_EXISTS - check if path exists (Postgres 17+)
118    JsonExists,
119    /// JSON_QUERY - extract JSON object/array at path (Postgres 17+)
120    JsonQuery,
121    /// JSON_VALUE - extract scalar value at path (Postgres 17+)
122    JsonValue,
123}
124
125/// Aggregate functions.
126#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
127pub enum AggregateFunc {
128    Count,
129    Sum,
130    Avg,
131    Min,
132    Max,
133}
134
135impl std::fmt::Display for AggregateFunc {
136    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
137        match self {
138            AggregateFunc::Count => write!(f, "COUNT"),
139            AggregateFunc::Sum => write!(f, "SUM"),
140            AggregateFunc::Avg => write!(f, "AVG"),
141            AggregateFunc::Min => write!(f, "MIN"),
142            AggregateFunc::Max => write!(f, "MAX"),
143        }
144    }
145}
146
147/// Join Type
148#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
149pub enum JoinKind {
150    Inner,
151    Left,
152    Right,
153    /// LATERAL join (Postgres, MySQL 8+)
154    Lateral,
155    /// FULL OUTER JOIN
156    Full,
157    /// CROSS JOIN
158    Cross,
159}
160
161/// Set operation type for combining queries
162#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
163pub enum SetOp {
164    /// UNION (removes duplicates)
165    Union,
166    /// UNION ALL (keeps duplicates)
167    UnionAll,
168    /// INTERSECT (common rows)
169    Intersect,
170    /// EXCEPT (rows in first but not second)
171    Except,
172}
173
174/// Column modification type
175#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
176pub enum ModKind {
177    Add,
178    Drop,
179}
180
181/// GROUP BY mode for advanced aggregations
182#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
183pub enum GroupByMode {
184    /// Standard GROUP BY
185    #[default]
186    Simple,
187    /// ROLLUP - hierarchical subtotals
188    Rollup,
189    /// CUBE - all combinations of subtotals
190    Cube,
191}