Skip to main content

reifydb_sql/
ast.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4#[derive(Debug, Clone)]
5pub enum Statement {
6	Select(SelectStatement),
7	Insert(InsertStatement),
8	Update(UpdateStatement),
9	Delete(DeleteStatement),
10	CreateTable(CreateTableStatement),
11	CreateIndex(CreateIndexStatement),
12	DropTable(DropTableStatement),
13}
14
15#[derive(Debug, Clone)]
16pub struct CteDefinition {
17	pub name: String,
18	pub query: SelectStatement,
19}
20
21#[derive(Debug, Clone)]
22pub struct SelectStatement {
23	pub ctes: Vec<CteDefinition>,
24	pub distinct: bool,
25	pub columns: Vec<SelectColumn>,
26	pub from: Option<FromClause>,
27	pub joins: Vec<JoinClause>,
28	pub where_clause: Option<Expr>,
29	pub group_by: Vec<Expr>,
30	pub having: Option<Expr>,
31	pub order_by: Vec<OrderByItem>,
32	pub limit: Option<u64>,
33	pub offset: Option<u64>,
34	pub set_op: Option<(SetOp, Box<SelectStatement>)>,
35}
36
37#[derive(Debug, Clone)]
38pub enum SetOp {
39	Union,
40	UnionAll,
41	Intersect,
42	Except,
43}
44
45#[derive(Debug, Clone)]
46pub enum SelectColumn {
47	AllColumns,
48	Expr {
49		expr: Expr,
50		alias: Option<String>,
51	},
52}
53
54#[derive(Debug, Clone)]
55pub enum FromClause {
56	Table {
57		name: String,
58		schema: Option<String>,
59		alias: Option<String>,
60	},
61	Subquery(Box<SelectStatement>),
62}
63
64#[derive(Debug, Clone)]
65pub struct JoinClause {
66	pub join_type: JoinType,
67	pub table: FromClause,
68	pub table_alias: Option<String>,
69	pub on: Expr,
70}
71
72#[derive(Debug, Clone)]
73pub enum JoinType {
74	Inner,
75	Left,
76	Cross,
77}
78
79#[derive(Debug, Clone)]
80pub struct OrderByItem {
81	pub expr: Expr,
82	pub direction: OrderDirection,
83}
84
85#[derive(Debug, Clone)]
86pub enum OrderDirection {
87	Asc,
88	Desc,
89}
90
91#[derive(Debug, Clone)]
92pub struct InsertStatement {
93	pub table: String,
94	pub schema: Option<String>,
95	pub columns: Vec<String>,
96	pub source: InsertSource,
97}
98
99#[derive(Debug, Clone)]
100pub enum InsertSource {
101	Values(Vec<Vec<Expr>>),
102	Select(SelectStatement),
103}
104
105#[derive(Debug, Clone)]
106pub struct UpdateStatement {
107	pub table: String,
108	pub schema: Option<String>,
109	pub assignments: Vec<(String, Expr)>,
110	pub where_clause: Option<Expr>,
111}
112
113#[derive(Debug, Clone)]
114pub struct DeleteStatement {
115	pub table: String,
116	pub schema: Option<String>,
117	pub where_clause: Option<Expr>,
118}
119
120#[derive(Debug, Clone)]
121pub struct CreateTableStatement {
122	pub table: String,
123	pub schema: Option<String>,
124	pub columns: Vec<Column>,
125	pub primary_key: Vec<String>,
126	pub if_not_exists: bool,
127}
128
129#[derive(Debug, Clone)]
130pub struct CreateIndexStatement {
131	pub unique: bool,
132	pub index_name: String,
133	pub table: String,
134	pub schema: Option<String>,
135	pub columns: Vec<IndexColumn>,
136}
137
138#[derive(Debug, Clone)]
139pub struct IndexColumn {
140	pub name: String,
141	pub direction: Option<OrderDirection>,
142}
143
144#[derive(Debug, Clone)]
145pub struct DropTableStatement {
146	pub table: String,
147	pub schema: Option<String>,
148	pub if_exists: bool,
149}
150
151#[derive(Debug, Clone)]
152pub struct Column {
153	pub name: String,
154	pub data_type: SqlType,
155	pub nullable: bool,
156}
157
158#[derive(Debug, Clone)]
159pub enum SqlType {
160	Int,
161	Int2,
162	Int4,
163	Int8,
164	Smallint,
165	Integer,
166	Bigint,
167	Float4,
168	Float8,
169	Real,
170	Double,
171	Boolean,
172	Bool,
173	Varchar(Option<u64>),
174	Char(Option<u64>),
175	Text,
176	Utf8,
177	Blob,
178	FloatType,
179	Numeric,
180}
181
182#[derive(Debug, Clone)]
183pub enum Expr {
184	Identifier(String),
185	QualifiedIdentifier(String, String),
186	IntegerLiteral(i64),
187	FloatLiteral(f64),
188	StringLiteral(String),
189	BoolLiteral(bool),
190	Null,
191	BinaryOp {
192		left: Box<Expr>,
193		op: BinaryOp,
194		right: Box<Expr>,
195	},
196	UnaryOp {
197		op: UnaryOp,
198		expr: Box<Expr>,
199	},
200	FunctionCall {
201		name: String,
202		args: Vec<Expr>,
203	},
204	Between {
205		expr: Box<Expr>,
206		low: Box<Expr>,
207		high: Box<Expr>,
208		negated: bool,
209	},
210	InList {
211		expr: Box<Expr>,
212		list: Vec<Expr>,
213		negated: bool,
214	},
215	InSelect {
216		expr: Box<Expr>,
217		subquery: Box<SelectStatement>,
218		negated: bool,
219	},
220	IsNull {
221		expr: Box<Expr>,
222		negated: bool,
223	},
224	Cast {
225		expr: Box<Expr>,
226		data_type: SqlType,
227	},
228	Nested(Box<Expr>),
229	Case {
230		operand: Option<Box<Expr>>,
231		when_clauses: Vec<(Expr, Expr)>,
232		else_clause: Option<Box<Expr>>,
233	},
234	Exists(Box<SelectStatement>),
235	Subquery(Box<SelectStatement>),
236	Like {
237		expr: Box<Expr>,
238		pattern: Box<Expr>,
239		negated: bool,
240	},
241}
242
243#[derive(Debug, Clone)]
244pub enum BinaryOp {
245	Eq,
246	NotEq,
247	Lt,
248	Gt,
249	LtEq,
250	GtEq,
251	And,
252	Or,
253	Add,
254	Sub,
255	Mul,
256	Div,
257	Mod,
258	Concat,
259}
260
261#[derive(Debug, Clone)]
262pub enum UnaryOp {
263	Not,
264	Neg,
265}