Skip to main content

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