Skip to main content

core_query/cypher/
ast.rs

1//! Cypher subset AST. Types match the Plan 3 Task 6 interface block.
2
3use crate::filter::CmpOp;
4use core_storage::Value;
5
6/// A LIMIT or SKIP value: either an exact count or a named query parameter.
7///
8/// `$name` parameters are resolved at execution time from the params map and
9/// validated to be a non-negative integer.
10#[derive(Debug, Clone, PartialEq)]
11pub enum LimitSkip {
12    Exact(u64),
13    Param(String),
14}
15
16/// One `OPTIONAL MATCH pattern [WHERE expr]` clause.
17///
18/// If the pattern produces no rows for a given input row, the input row
19/// survives with the optional variables set to `null` (left-outer-join
20/// semantics, openCypher §10.1.3).
21///
22/// `where_expr`, when present, is applied INSIDE the optional scope:
23/// it filters candidate rows before the left-outer fallback fires.  This
24/// differs from a post-filter that would eliminate the null row entirely.
25#[derive(Debug, Clone, PartialEq)]
26pub struct OptionalClause {
27    pub patterns: Vec<Pattern>,
28    /// WHERE clause scoped to this optional match (applied before nullification).
29    pub where_expr: Option<Expr>,
30}
31
32#[derive(Debug, Clone, PartialEq)]
33pub struct Query {
34    pub matches: Vec<Pattern>,
35    /// `OPTIONAL MATCH` clauses that follow the required matches.
36    pub optional_clauses: Vec<OptionalClause>,
37    /// Top-level WHERE filter, evaluated before UNWIND expansion.
38    pub where_expr: Option<Expr>,
39    /// Top-level UNWIND clauses (after WHERE, before post_unwind_where/WITH/RETURN).
40    pub unwinds: Vec<UnwindClause>,
41    /// Optional WHERE evaluated after UNWIND expansion (references UNWIND aliases).
42    pub post_unwind_where: Option<Expr>,
43    /// WITH pipeline stages. Each stage carries a WITH clause and optional
44    /// MATCH / UNWIND / WHERE that follow it.
45    pub stages: Vec<WithStage>,
46    pub returns: Vec<RetItem>,
47    /// `RETURN DISTINCT …` — executor hashes projected rows after `Project`.
48    pub distinct: bool,
49    pub order_by: Vec<OrderItem>,
50    pub skip: Option<LimitSkip>,
51    pub limit: Option<LimitSkip>,
52}
53
54/// One `UNWIND <expr> AS <alias>` clause.
55#[derive(Debug, Clone, PartialEq)]
56pub struct UnwindClause {
57    pub list: UnwindExpr,
58    pub alias: String,
59}
60
61/// The expression whose value is iterated in UNWIND.
62#[derive(Debug, Clone, PartialEq)]
63pub enum UnwindExpr {
64    /// Inline list literal: `[1, 2, 3]`.
65    Lit(Vec<Value>),
66    /// Property on a bound node: `n.tags`.
67    Prop { var: String, field: String },
68    /// A previously bound alias (from a prior WITH): `alias`.
69    Var(String),
70}
71
72/// One WITH stage in a pipeline:
73/// ```text
74/// WITH <items> [WHERE <expr>] [ORDER BY …] [SKIP n] [LIMIT n]
75/// [MATCH …]* [OPTIONAL MATCH …]* [UNWIND …]* [WHERE <expr>]
76/// ```
77#[derive(Debug, Clone, PartialEq)]
78pub struct WithStage {
79    /// The projected items in the WITH clause.
80    pub items: Vec<RetItem>,
81    /// Optional WHERE / HAVING filter immediately after the WITH keyword.
82    pub where_expr: Option<Expr>,
83    pub order_by: Vec<OrderItem>,
84    pub skip: Option<LimitSkip>,
85    pub limit: Option<LimitSkip>,
86    /// MATCH clauses that follow this WITH.
87    pub matches: Vec<Pattern>,
88    /// OPTIONAL MATCH clauses that follow the required MATCHes in this stage.
89    pub optional_clauses: Vec<OptionalClause>,
90    /// UNWIND clauses that follow this WITH.
91    pub unwinds: Vec<UnwindClause>,
92    /// WHERE clause that follows those MATCHes (pre-next-WITH/RETURN filter).
93    pub post_where: Option<Expr>,
94}
95
96/// Aggregate function in a RETURN clause.
97#[derive(Debug, Clone, PartialEq)]
98pub enum AggFunc {
99    Count,
100    Sum,
101    Avg,
102    Min,
103    Max,
104    /// `collect(x)` — gather each row's value of `x` into a list, skipping
105    /// nulls. Per group when grouping keys are present.
106    Collect,
107}
108
109/// Argument to an aggregate function.
110#[derive(Debug, Clone, PartialEq)]
111pub enum AggArg {
112    /// `COUNT(*)` — every matched row counts regardless of binding.
113    Star,
114    /// `COUNT(var)` — counts rows where `var` is bound (non-null).
115    Var(String),
116    /// `SUM(var.field)`, `AVG(var.field)`, etc.
117    Prop { var: String, field: String },
118}
119
120/// Hop-count range for variable-length relationship patterns (`*min..max`).
121/// Both bounds are inclusive.  `min = max` is a fixed-hop pattern.
122#[derive(Debug, Clone, Copy, PartialEq, Eq)]
123pub struct HopRange {
124    pub min: u8,
125    pub max: u8,
126}
127
128#[derive(Debug, Clone, PartialEq)]
129pub struct Pattern {
130    pub start: NodePat,
131    pub chain: Vec<(RelPat, NodePat)>,
132    /// True when parsed as `MATCH shortestPath(...)`.
133    pub shortest: bool,
134}
135
136#[derive(Debug, Clone, PartialEq)]
137pub struct NodePat {
138    pub var: Option<String>,
139    pub label: Option<String>,
140    pub props: Vec<(String, Operand)>,
141}
142
143#[derive(Debug, Clone, Copy, PartialEq, Eq)]
144pub enum RelDir {
145    Right,
146    Left,
147    Undirected,
148}
149
150#[derive(Debug, Clone, PartialEq)]
151pub struct RelPat {
152    pub var: Option<String>,
153    /// Relationship-type alternatives. Empty = any type; one = single type;
154    /// many = `[:A|:B]` alternation (match an edge of any listed type).
155    pub etypes: Vec<String>,
156    pub dir: RelDir,
157    /// `None` = single-hop (normal `Expand`).  `Some(r)` = variable-length
158    /// (`VarExpand`) with the given min/max hop bounds.
159    pub hops: Option<HopRange>,
160}
161
162#[derive(Debug, Clone, PartialEq)]
163pub enum Expr {
164    And(Box<Expr>, Box<Expr>),
165    Or(Box<Expr>, Box<Expr>),
166    Not(Box<Expr>),
167    Cmp {
168        lhs: Operand,
169        op: CmpOp,
170        rhs: Operand,
171    },
172    /// Standalone operand used as a boolean predicate.
173    ///
174    /// Enables `WHERE textMatches(n.bio, 'query')` without requiring an
175    /// explicit comparison.  Truthiness: `Bool(true)` → true, `Bool(false)`
176    /// → false, null → false, any other non-null non-false value → true.
177    Truthy(Operand),
178    /// `operand IS NULL` — true iff the operand evaluates to null.
179    IsNull(Operand),
180    /// `operand IS NOT NULL` — true iff the operand is non-null.
181    IsNotNull(Operand),
182    /// `expr IN [a, b, $p]` or `expr IN $list` (`$list` is `Value::List`).
183    In {
184        expr: Operand,
185        list: Vec<Operand>,
186    },
187}
188
189#[derive(Debug, Clone, PartialEq)]
190pub enum Operand {
191    Prop {
192        var: String,
193        field: String,
194    },
195    Lit(Value),
196    Param(String),
197    /// Bare variable reference (used in `WITH … WHERE alias > 2`).
198    Var(String),
199    /// Scalar function call: `toLower(n.name)`, `size(n.tags)`, `type(r)`, etc.
200    ///
201    /// Supported functions (case-insensitive): `toLower`, `toUpper`, `size`,
202    /// `coalesce`, `type`, `abs`, `round`.  Unknown names → named error at
203    /// execution time listing the supported set.
204    FuncCall {
205        name: String,
206        args: Vec<Operand>,
207    },
208    /// Arithmetic expression inside a function argument: `abs(n.age - 27)`,
209    /// `round(n.score * 1.5)`.  Supports `+`, `-`, `*`, `/`.
210    BinArith {
211        op: ArithOp,
212        left: Box<Operand>,
213        right: Box<Operand>,
214    },
215    /// Generic `CASE WHEN <cond> THEN <value> [WHEN …] [ELSE <value>] END`.
216    /// Evaluates each branch's condition in order, returning the first matching
217    /// value; the `default` (ELSE) or null if none match.
218    Case {
219        branches: Vec<(Expr, Operand)>,
220        default: Option<Box<Operand>>,
221    },
222}
223
224/// Arithmetic operators for `Operand::BinArith`.
225#[derive(Debug, Clone, PartialEq)]
226pub enum ArithOp {
227    Add,
228    Sub,
229    Mul,
230    Div,
231}
232
233/// RETURN item value: bare variable, `var.field`, an aggregate call, a
234/// scalar function call, or an arbitrary scalar expression (arithmetic etc.).
235#[derive(Debug, Clone, PartialEq)]
236pub enum RetVal {
237    Var(String),
238    Prop {
239        var: String,
240        field: String,
241    },
242    /// Single aggregate function call.  When combined with non-aggregate items
243    /// in the same RETURN clause, the planner routes to `GroupAggregate`.
244    /// Multiple aggregate items are also supported via `GroupAggregate`.
245    Agg {
246        func: AggFunc,
247        arg: AggArg,
248    },
249    /// Scalar function call in a RETURN position, e.g. `toLower(n.name)`.
250    /// The same function set as `Operand::FuncCall`.
251    FuncCall {
252        name: String,
253        args: Vec<Operand>,
254    },
255    /// Arbitrary scalar expression in a RETURN/WITH position, e.g. `n.age + 1`.
256    /// Evaluated via `resolve_operand` at execution time; null propagates.
257    ScalarExpr(Operand),
258}
259
260#[derive(Debug, Clone, PartialEq)]
261pub struct RetItem {
262    pub value: RetVal,
263    pub alias: Option<String>,
264}
265
266/// ORDER BY target. Bare identifiers that match a RETURN alias become `Alias`;
267/// otherwise they stay `Var`. `var.field` is always `Prop`.
268#[derive(Debug, Clone, PartialEq)]
269pub enum OrderTarget {
270    Alias(String),
271    Var(String),
272    Prop { var: String, field: String },
273}
274
275#[derive(Debug, Clone, PartialEq)]
276pub struct OrderItem {
277    pub target: OrderTarget,
278    pub descending: bool,
279}
280
281// ─── Write statement AST ──────────────────────────────────────────────────────
282
283/// Top-level write statement (CREATE / MATCH…SET / MATCH…DELETE / MATCH…DETACH DELETE / MERGE).
284/// Produced by `parse_write`; executed by `GraphDb::query_write`.
285#[derive(Debug, Clone, PartialEq)]
286pub enum WriteStatement {
287    Create(CreateStmt),
288    MatchSet(MatchSetStmt),
289    MatchDelete(MatchDeleteStmt),
290    MatchDeleteNode(MatchDeleteNodeStmt),
291    Merge(MergeStmt),
292}
293
294/// `CREATE (a:L {id: 'x', ...})[-[:T]->(b:L2 {id: 'y', ...})] [RETURN …]`
295///
296/// `nodes` is in encounter order. `edges` reference node vars that appear in
297/// `nodes`. Each node must have a string-valued `id` property (used as key).
298///
299/// `returns`, when `Some`, projects the created bindings as a read result.
300/// The write and the projection are committed as a single WAL batch frame.
301#[derive(Debug, Clone, PartialEq)]
302pub struct CreateStmt {
303    pub nodes: Vec<CreateNode>,
304    pub edges: Vec<CreateEdge>,
305    /// Optional RETURN clause: project created bindings after commit.
306    pub returns: Option<Vec<RetItem>>,
307}
308
309/// One node in a CREATE pattern.
310#[derive(Debug, Clone, PartialEq)]
311pub struct CreateNode {
312    /// Optional binding variable (`a` in `(a:Label {…})`).
313    pub var: Option<String>,
314    pub label: String,
315    /// Literal property pairs.  Must include a string-valued `id` field.
316    pub props: Vec<(String, core_storage::Value)>,
317}
318
319/// One edge in a CREATE pattern, referencing vars from `CreateStmt.nodes`.
320#[derive(Debug, Clone, PartialEq)]
321pub struct CreateEdge {
322    pub src_var: String,
323    pub etype: String,
324    pub dst_var: String,
325}
326
327/// `MATCH patterns [WHERE expr] SET var.field = literal [, …] [RETURN …]`
328#[derive(Debug, Clone, PartialEq)]
329pub struct MatchSetStmt {
330    pub matches: Vec<Pattern>,
331    pub where_expr: Option<Expr>,
332    pub sets: Vec<SetClause>,
333    /// Optional RETURN clause: project matched bindings from post-write state.
334    pub returns: Option<Vec<RetItem>>,
335}
336
337/// One `var.field = literal_or_param` assignment in a SET clause.
338///
339/// `value` is an `Operand` rather than a bare `Value` so that `$param`
340/// references are legal on the RHS (resolved at execution time from the
341/// query's parameter map).  Only `Operand::Lit` and `Operand::Param` are
342/// accepted by the parser; other variants produce a named parse error.
343#[derive(Debug, Clone, PartialEq)]
344pub struct SetClause {
345    pub var: String,
346    pub field: String,
347    pub value: Operand,
348}
349
350/// `MATCH patterns [WHERE expr] DELETE rel_var [, …]`
351///
352/// Each `EdgeDelete` carries the etype, src node var, and dst node var resolved
353/// from the MATCH patterns at parse time so the executor can emit a read query
354/// returning node keys without extra pattern-scanning.
355#[derive(Debug, Clone, PartialEq)]
356pub struct MatchDeleteStmt {
357    pub matches: Vec<Pattern>,
358    pub where_expr: Option<Expr>,
359    pub deletes: Vec<EdgeDelete>,
360}
361
362/// One edge variable to delete, with resolved topology.
363#[derive(Debug, Clone, PartialEq)]
364pub struct EdgeDelete {
365    pub rel_var: String,
366    pub etype: String,
367    pub src_var: String,
368    pub dst_var: String,
369}
370
371/// `MATCH patterns [WHERE expr] [DETACH] DELETE node_var [, …]`
372///
373/// When `detach` is `true` (DETACH DELETE), all incident edges are removed
374/// before the node is tombstoned (openCypher semantics).  When `false`
375/// (bare DELETE), the node must have no incident edges; the executor returns
376/// an error if any remain.
377#[derive(Debug, Clone, PartialEq)]
378pub struct MatchDeleteNodeStmt {
379    pub matches: Vec<Pattern>,
380    pub where_expr: Option<Expr>,
381    /// Node variable names whose nodes are to be deleted (resolved from MATCH patterns).
382    pub node_vars: Vec<String>,
383    /// `true` → DETACH DELETE (allowed regardless of edges).
384    /// `false` → bare DELETE (error if any edges touch the node).
385    pub detach: bool,
386}
387
388/// `MERGE (n:Label {id: 'x'}) [ON CREATE SET …] [ON MATCH SET …] [RETURN …]`
389///
390/// Exactly one property is allowed (the key). More properties → named error.
391/// `ON CREATE SET` / `ON MATCH SET` apply inside the same write batch as the
392/// insert-or-skip. `returns`, when `Some`, projects the node after commit.
393#[derive(Debug, Clone, PartialEq)]
394pub struct MergeStmt {
395    pub label: String,
396    /// The single property that identifies the node. Value must be a string.
397    pub key_field: String,
398    pub key_value: core_storage::Value,
399    /// Optional bound variable for the MERGE node (for RETURN projection).
400    pub var: Option<String>,
401    /// `ON CREATE SET` assignments, applied only when the node is inserted.
402    pub on_create: Vec<SetClause>,
403    /// `ON MATCH SET` assignments, applied only when the node already exists.
404    pub on_match: Vec<SetClause>,
405    /// Optional RETURN clause: project the node after commit.
406    pub returns: Option<Vec<RetItem>>,
407}