sql_cli/sql/parser/
ast.rs

1//! Abstract Syntax Tree (AST) definitions for SQL queries
2//!
3//! This module contains all the data structures that represent
4//! the parsed SQL query structure.
5
6// ===== Expression Types =====
7
8#[derive(Debug, Clone)]
9pub enum SqlExpression {
10    Column(String),
11    StringLiteral(String),
12    NumberLiteral(String),
13    BooleanLiteral(bool),
14    Null, // NULL literal
15    DateTimeConstructor {
16        year: i32,
17        month: u32,
18        day: u32,
19        hour: Option<u32>,
20        minute: Option<u32>,
21        second: Option<u32>,
22    },
23    DateTimeToday {
24        hour: Option<u32>,
25        minute: Option<u32>,
26        second: Option<u32>,
27    },
28    MethodCall {
29        object: String,
30        method: String,
31        args: Vec<SqlExpression>,
32    },
33    ChainedMethodCall {
34        base: Box<SqlExpression>,
35        method: String,
36        args: Vec<SqlExpression>,
37    },
38    FunctionCall {
39        name: String,
40        args: Vec<SqlExpression>,
41        distinct: bool, // For COUNT(DISTINCT col), SUM(DISTINCT col), etc.
42    },
43    WindowFunction {
44        name: String,
45        args: Vec<SqlExpression>,
46        window_spec: WindowSpec,
47    },
48    BinaryOp {
49        left: Box<SqlExpression>,
50        op: String,
51        right: Box<SqlExpression>,
52    },
53    InList {
54        expr: Box<SqlExpression>,
55        values: Vec<SqlExpression>,
56    },
57    NotInList {
58        expr: Box<SqlExpression>,
59        values: Vec<SqlExpression>,
60    },
61    Between {
62        expr: Box<SqlExpression>,
63        lower: Box<SqlExpression>,
64        upper: Box<SqlExpression>,
65    },
66    Not {
67        expr: Box<SqlExpression>,
68    },
69    CaseExpression {
70        when_branches: Vec<WhenBranch>,
71        else_branch: Option<Box<SqlExpression>>,
72    },
73}
74
75#[derive(Debug, Clone)]
76pub struct WhenBranch {
77    pub condition: Box<SqlExpression>,
78    pub result: Box<SqlExpression>,
79}
80
81// ===== WHERE Clause Types =====
82
83#[derive(Debug, Clone)]
84pub struct WhereClause {
85    pub conditions: Vec<Condition>,
86}
87
88#[derive(Debug, Clone)]
89pub struct Condition {
90    pub expr: SqlExpression,
91    pub connector: Option<LogicalOp>, // AND/OR connecting to next condition
92}
93
94#[derive(Debug, Clone)]
95pub enum LogicalOp {
96    And,
97    Or,
98}
99
100// ===== ORDER BY Types =====
101
102#[derive(Debug, Clone, PartialEq)]
103pub enum SortDirection {
104    Asc,
105    Desc,
106}
107
108#[derive(Debug, Clone)]
109pub struct OrderByColumn {
110    pub column: String,
111    pub direction: SortDirection,
112}
113
114// ===== Window Function Types =====
115
116#[derive(Debug, Clone)]
117pub struct WindowSpec {
118    pub partition_by: Vec<String>,
119    pub order_by: Vec<OrderByColumn>,
120}
121
122// ===== SELECT Statement Types =====
123
124/// Represents a SELECT item - either a simple column or a computed expression with alias
125#[derive(Debug, Clone)]
126pub enum SelectItem {
127    /// Simple column reference: "`column_name`"
128    Column(String),
129    /// Computed expression with alias: "expr AS alias"
130    Expression { expr: SqlExpression, alias: String },
131    /// Star selector: "*"
132    Star,
133}
134
135#[derive(Debug, Clone)]
136pub struct SelectStatement {
137    pub distinct: bool,                // SELECT DISTINCT flag
138    pub columns: Vec<String>,          // Keep for backward compatibility, will be deprecated
139    pub select_items: Vec<SelectItem>, // New field for computed expressions
140    pub from_table: Option<String>,
141    pub from_subquery: Option<Box<SelectStatement>>, // Subquery in FROM clause
142    pub from_function: Option<TableFunction>,        // Table function like RANGE() in FROM clause
143    pub from_alias: Option<String>,                  // Alias for subquery (AS name)
144    pub joins: Vec<JoinClause>,                      // JOIN clauses
145    pub where_clause: Option<WhereClause>,
146    pub order_by: Option<Vec<OrderByColumn>>,
147    pub group_by: Option<Vec<String>>,
148    pub having: Option<SqlExpression>, // HAVING clause for post-aggregation filtering
149    pub limit: Option<usize>,
150    pub offset: Option<usize>,
151    pub ctes: Vec<CTE>, // Common Table Expressions (WITH clause)
152}
153
154// ===== Table and Join Types =====
155
156/// Table function that generates virtual tables
157#[derive(Debug, Clone)]
158pub enum TableFunction {
159    Range {
160        start: SqlExpression,
161        end: SqlExpression,
162        step: Option<SqlExpression>,
163    },
164}
165
166/// Common Table Expression (CTE) structure
167#[derive(Debug, Clone)]
168pub struct CTE {
169    pub name: String,
170    pub column_list: Option<Vec<String>>, // Optional column list: WITH t(col1, col2) AS ...
171    pub query: SelectStatement,
172}
173
174/// Table source - either a file/table name or a derived table (subquery/CTE)
175#[derive(Debug, Clone)]
176pub enum TableSource {
177    Table(String), // Regular table from CSV/JSON
178    DerivedTable {
179        // Both CTE and subquery
180        query: Box<SelectStatement>,
181        alias: String, // Required alias for subqueries
182    },
183}
184
185/// Join type enumeration
186#[derive(Debug, Clone, PartialEq)]
187pub enum JoinType {
188    Inner,
189    Left,
190    Right,
191    Full,
192    Cross,
193}
194
195/// Join operator for join conditions
196#[derive(Debug, Clone, PartialEq)]
197pub enum JoinOperator {
198    Equal,
199    NotEqual,
200    LessThan,
201    GreaterThan,
202    LessThanOrEqual,
203    GreaterThanOrEqual,
204}
205
206/// Join condition - initially just column equality
207#[derive(Debug, Clone)]
208pub struct JoinCondition {
209    pub left_column: String, // Column from left table (can include table prefix)
210    pub operator: JoinOperator, // Join operator (initially just Equal)
211    pub right_column: String, // Column from right table (can include table prefix)
212}
213
214/// Join clause structure
215#[derive(Debug, Clone)]
216pub struct JoinClause {
217    pub join_type: JoinType,
218    pub table: TableSource,       // The table being joined
219    pub alias: Option<String>,    // Optional alias for the joined table
220    pub condition: JoinCondition, // ON condition
221}