graphos_adapters/query/gql/ast.rs
1//! GQL Abstract Syntax Tree.
2
3use graphos_common::utils::error::SourceSpan;
4
5/// A GQL statement.
6#[derive(Debug, Clone)]
7pub enum Statement {
8 /// A query statement (MATCH, RETURN, etc.)
9 Query(QueryStatement),
10 /// A data modification statement (INSERT, DELETE, etc.)
11 DataModification(DataModificationStatement),
12 /// A schema statement (CREATE NODE TYPE, etc.)
13 Schema(SchemaStatement),
14}
15
16/// A query statement.
17#[derive(Debug, Clone)]
18pub struct QueryStatement {
19 /// MATCH clauses (regular and optional).
20 pub match_clauses: Vec<MatchClause>,
21 /// Optional WHERE clause.
22 pub where_clause: Option<WhereClause>,
23 /// WITH clauses for query chaining.
24 pub with_clauses: Vec<WithClause>,
25 /// Required RETURN clause.
26 pub return_clause: ReturnClause,
27 /// Source span in the original query.
28 pub span: Option<SourceSpan>,
29}
30
31/// A MATCH clause.
32#[derive(Debug, Clone)]
33pub struct MatchClause {
34 /// Whether this is an OPTIONAL MATCH.
35 pub optional: bool,
36 /// Graph patterns to match.
37 pub patterns: Vec<Pattern>,
38 /// Source span.
39 pub span: Option<SourceSpan>,
40}
41
42/// A WITH clause for query chaining.
43#[derive(Debug, Clone)]
44pub struct WithClause {
45 /// Whether to use DISTINCT.
46 pub distinct: bool,
47 /// Items to pass to the next query part.
48 pub items: Vec<ReturnItem>,
49 /// Optional WHERE clause after WITH.
50 pub where_clause: Option<WhereClause>,
51 /// Source span.
52 pub span: Option<SourceSpan>,
53}
54
55/// A graph pattern.
56#[derive(Debug, Clone)]
57pub enum Pattern {
58 /// A node pattern.
59 Node(NodePattern),
60 /// An edge pattern connecting nodes.
61 Path(PathPattern),
62}
63
64/// A node pattern like (n:Person).
65#[derive(Debug, Clone)]
66pub struct NodePattern {
67 /// Variable name (optional).
68 pub variable: Option<String>,
69 /// Labels to match.
70 pub labels: Vec<String>,
71 /// Property filters.
72 pub properties: Vec<(String, Expression)>,
73 /// Source span.
74 pub span: Option<SourceSpan>,
75}
76
77/// A path pattern like `(a)-[:KNOWS]->(b)`.
78#[derive(Debug, Clone)]
79pub struct PathPattern {
80 /// Source node pattern.
81 pub source: NodePattern,
82 /// Edge patterns.
83 pub edges: Vec<EdgePattern>,
84 /// Source span.
85 pub span: Option<SourceSpan>,
86}
87
88/// An edge pattern like `-[:KNOWS]->`.
89#[derive(Debug, Clone)]
90pub struct EdgePattern {
91 /// Variable name (optional).
92 pub variable: Option<String>,
93 /// Edge types to match.
94 pub types: Vec<String>,
95 /// Direction of the edge.
96 pub direction: EdgeDirection,
97 /// Target node pattern.
98 pub target: NodePattern,
99 /// Source span.
100 pub span: Option<SourceSpan>,
101}
102
103/// Direction of an edge.
104#[derive(Debug, Clone, Copy, PartialEq, Eq)]
105pub enum EdgeDirection {
106 /// Outgoing edge: ->
107 Outgoing,
108 /// Incoming edge: <-
109 Incoming,
110 /// Undirected edge: -
111 Undirected,
112}
113
114/// A WHERE clause.
115#[derive(Debug, Clone)]
116pub struct WhereClause {
117 /// The filter expression.
118 pub expression: Expression,
119 /// Source span.
120 pub span: Option<SourceSpan>,
121}
122
123/// A RETURN clause.
124#[derive(Debug, Clone)]
125pub struct ReturnClause {
126 /// Whether to return DISTINCT results.
127 pub distinct: bool,
128 /// Items to return.
129 pub items: Vec<ReturnItem>,
130 /// Optional ORDER BY.
131 pub order_by: Option<OrderByClause>,
132 /// Optional SKIP.
133 pub skip: Option<Expression>,
134 /// Optional LIMIT.
135 pub limit: Option<Expression>,
136 /// Source span.
137 pub span: Option<SourceSpan>,
138}
139
140/// An item in a RETURN clause.
141#[derive(Debug, Clone)]
142pub struct ReturnItem {
143 /// The expression to return.
144 pub expression: Expression,
145 /// Optional alias (AS name).
146 pub alias: Option<String>,
147 /// Source span.
148 pub span: Option<SourceSpan>,
149}
150
151/// An ORDER BY clause.
152#[derive(Debug, Clone)]
153pub struct OrderByClause {
154 /// Sort items.
155 pub items: Vec<OrderByItem>,
156 /// Source span.
157 pub span: Option<SourceSpan>,
158}
159
160/// A sort item.
161#[derive(Debug, Clone)]
162pub struct OrderByItem {
163 /// The expression to sort by.
164 pub expression: Expression,
165 /// Sort order.
166 pub order: SortOrder,
167}
168
169/// Sort order.
170#[derive(Debug, Clone, Copy, PartialEq, Eq)]
171pub enum SortOrder {
172 /// Ascending order.
173 Asc,
174 /// Descending order.
175 Desc,
176}
177
178/// A data modification statement.
179#[derive(Debug, Clone)]
180pub enum DataModificationStatement {
181 /// INSERT statement.
182 Insert(InsertStatement),
183 /// DELETE statement.
184 Delete(DeleteStatement),
185 /// SET statement.
186 Set(SetStatement),
187}
188
189/// An INSERT statement.
190#[derive(Debug, Clone)]
191pub struct InsertStatement {
192 /// Patterns to insert.
193 pub patterns: Vec<Pattern>,
194 /// Source span.
195 pub span: Option<SourceSpan>,
196}
197
198/// A DELETE statement.
199#[derive(Debug, Clone)]
200pub struct DeleteStatement {
201 /// Variables to delete.
202 pub variables: Vec<String>,
203 /// Whether to use DETACH DELETE.
204 pub detach: bool,
205 /// Source span.
206 pub span: Option<SourceSpan>,
207}
208
209/// A SET statement.
210#[derive(Debug, Clone)]
211pub struct SetStatement {
212 /// Property assignments.
213 pub assignments: Vec<PropertyAssignment>,
214 /// Source span.
215 pub span: Option<SourceSpan>,
216}
217
218/// A property assignment.
219#[derive(Debug, Clone)]
220pub struct PropertyAssignment {
221 /// Variable name.
222 pub variable: String,
223 /// Property key.
224 pub property: String,
225 /// Value expression.
226 pub value: Expression,
227}
228
229/// A schema statement.
230#[derive(Debug, Clone)]
231pub enum SchemaStatement {
232 /// CREATE NODE TYPE.
233 CreateNodeType(CreateNodeTypeStatement),
234 /// CREATE EDGE TYPE.
235 CreateEdgeType(CreateEdgeTypeStatement),
236}
237
238/// A CREATE NODE TYPE statement.
239#[derive(Debug, Clone)]
240pub struct CreateNodeTypeStatement {
241 /// Type name.
242 pub name: String,
243 /// Property definitions.
244 pub properties: Vec<PropertyDefinition>,
245 /// Source span.
246 pub span: Option<SourceSpan>,
247}
248
249/// A CREATE EDGE TYPE statement.
250#[derive(Debug, Clone)]
251pub struct CreateEdgeTypeStatement {
252 /// Type name.
253 pub name: String,
254 /// Property definitions.
255 pub properties: Vec<PropertyDefinition>,
256 /// Source span.
257 pub span: Option<SourceSpan>,
258}
259
260/// A property definition in a schema.
261#[derive(Debug, Clone)]
262pub struct PropertyDefinition {
263 /// Property name.
264 pub name: String,
265 /// Property type.
266 pub data_type: String,
267 /// Whether the property is nullable.
268 pub nullable: bool,
269}
270
271/// An expression.
272#[derive(Debug, Clone)]
273pub enum Expression {
274 /// A literal value.
275 Literal(Literal),
276 /// A variable reference.
277 Variable(String),
278 /// A parameter reference ($name).
279 Parameter(String),
280 /// A property access (var.prop).
281 PropertyAccess {
282 /// The variable.
283 variable: String,
284 /// The property name.
285 property: String,
286 },
287 /// A binary operation.
288 Binary {
289 /// Left operand.
290 left: Box<Expression>,
291 /// Operator.
292 op: BinaryOp,
293 /// Right operand.
294 right: Box<Expression>,
295 },
296 /// A unary operation.
297 Unary {
298 /// Operator.
299 op: UnaryOp,
300 /// Operand.
301 operand: Box<Expression>,
302 },
303 /// A function call.
304 FunctionCall {
305 /// Function name.
306 name: String,
307 /// Arguments.
308 args: Vec<Expression>,
309 },
310 /// A list expression.
311 List(Vec<Expression>),
312 /// A CASE expression.
313 Case {
314 /// Optional input expression.
315 input: Option<Box<Expression>>,
316 /// When clauses.
317 whens: Vec<(Expression, Expression)>,
318 /// Else clause.
319 else_clause: Option<Box<Expression>>,
320 },
321 /// EXISTS subquery expression - checks if inner query returns results.
322 ExistsSubquery {
323 /// The inner query pattern to check for existence.
324 query: Box<QueryStatement>,
325 },
326}
327
328/// A literal value.
329#[derive(Debug, Clone)]
330pub enum Literal {
331 /// Null literal.
332 Null,
333 /// Boolean literal.
334 Bool(bool),
335 /// Integer literal.
336 Integer(i64),
337 /// Float literal.
338 Float(f64),
339 /// String literal.
340 String(String),
341}
342
343/// A binary operator.
344#[derive(Debug, Clone, Copy, PartialEq, Eq)]
345pub enum BinaryOp {
346 // Comparison
347 /// Equal.
348 Eq,
349 /// Not equal.
350 Ne,
351 /// Less than.
352 Lt,
353 /// Less than or equal.
354 Le,
355 /// Greater than.
356 Gt,
357 /// Greater than or equal.
358 Ge,
359
360 // Logical
361 /// Logical AND.
362 And,
363 /// Logical OR.
364 Or,
365
366 // Arithmetic
367 /// Addition.
368 Add,
369 /// Subtraction.
370 Sub,
371 /// Multiplication.
372 Mul,
373 /// Division.
374 Div,
375 /// Modulo.
376 Mod,
377
378 // String
379 /// String concatenation.
380 Concat,
381 /// LIKE pattern matching.
382 Like,
383 /// IN list membership.
384 In,
385}
386
387/// A unary operator.
388#[derive(Debug, Clone, Copy, PartialEq, Eq)]
389pub enum UnaryOp {
390 /// Logical NOT.
391 Not,
392 /// Unary minus.
393 Neg,
394 /// IS NULL.
395 IsNull,
396 /// IS NOT NULL.
397 IsNotNull,
398}