relate-ir 1.0.0

Planner IR (lowered Cypher AST)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! OpenCypher AST (ADR-0007): the statement and clause model, with the pure
//! `Expr`-shape recognizers beside it. The expression model itself
//! ([`Expr`] and its two carried types) lives in [`expr`], the pattern
//! model ([`PathPattern`] and its element, label and type vocabularies) in
//! [`pattern`], and both are re-exported here, so the public path stays
//! `ast::*`.

use relate_names::SmallName;

use crate::search::SearchSpec;

mod call;
mod expr;
mod pattern;
pub use call::*;
pub use expr::*;
pub use pattern::*;

#[derive(Debug, Clone, PartialEq)]
pub struct Query {
    pub parts: Vec<SingleQuery>,
    /// `union_all[i]` applies between `parts[i]` and `parts[i+1]`.
    pub union_all: Vec<bool>,
    /// CIP-213 sequential statement composition.
    pub next: Option<Box<Query>>,
    /// CIP-150: the statement's `CYPHER <version>` preamble, lowered from
    /// `relate_parser::Statement::cypher_version`. Set on the top-level
    /// query (and inherited by `NEXT` stages); nested subquery bodies
    /// (`CALL { }`, EXISTS/COUNT/COLLECT expression subqueries) keep `None`
    /// here and inherit the enclosing statement's version at evaluation
    /// time: the executor threads it through `QueryCtx`/`ExecCtx`/`EvalCtx`
    /// into `subquery_qctx` (`Query::cypher_version.or(enclosing)`).
    pub cypher_version: Option<u32>,
}

impl Query {
    /// The end of this query's `NEXT` chain โ€” its true terminal stage
    /// (CIP-213), not its own first stage. UNION arms are `parts` *inside*
    /// one `Query`, so the chain walk needs no union guard. The one
    /// IR-level spelling of the cursor the planner's projection checks and
    /// the executor's stage handling mirror.
    pub fn final_stage(&self) -> &Query {
        let mut tail = self;
        while let Some(next) = tail.next.as_deref() {
            tail = next;
        }
        tail
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct SingleQuery {
    pub clauses: Vec<Clause>,
}

impl SingleQuery {
    /// The clause a CALL subquery body opens with, when it is an
    /// *importing WITH*: the first non-`USE` clause, and only if it is an
    /// explicit `WITH` (`LET`/`FILTER` desugar to `With` but are ordinary
    /// projections, never importing WITHs). One derivation for both the
    /// planner's 42I28 rule and the notification layer's import-set
    /// analysis; the typed payload saves every caller re-discriminating the
    /// clause variant.
    pub fn leading_importing_with(&self) -> Option<LeadingWith<'_>> {
        let clause = self
            .clauses
            .iter()
            .find(|clause| !matches!(clause, Clause::Use { .. }))?;
        let Clause::With {
            distinct,
            items,
            order,
            skip,
            limit,
            where_clause,
            origin: WithOrigin::Explicit,
            ..
        } = clause
        else {
            return None;
        };
        Some(LeadingWith {
            distinct: *distinct,
            items,
            order,
            skip: skip.as_ref(),
            limit: limit.as_ref(),
            where_clause: where_clause.as_ref(),
        })
    }

    /// The names this part's importing WITH pulls in from `outer`. The one
    /// home for the import policy: a bare item imports its variable
    /// ([`Projection::imported_name`]), a `*` imports the whole outer
    /// scope, and anything else (the aliased forms 42I28 rejects) imports
    /// nothing. Returns an empty set when the part has no importing WITH.
    pub fn leading_imports(
        &self,
        outer: &std::collections::BTreeSet<String>,
    ) -> std::collections::BTreeSet<String> {
        use std::collections::BTreeSet;
        let Some(with) = self.leading_importing_with() else {
            return BTreeSet::new();
        };
        let mut imports = BTreeSet::new();
        for item in with.items {
            if matches!(item.expr, Expr::Star) {
                imports.extend(outer.iter().cloned());
            } else if let Some(name) = item.imported_name() {
                // Map-bridge: the import sets stay String-keyed because
                // their consumer, the notification layer's import-set
                // analysis (`statement_notifications/shape.rs`), is a
                // String scope-domain sink; the typed sink switch is
                // tracked as LABS-936.
                imports.insert(name.to_string());
            }
        }
        imports
    }
}

/// The typed payload of [`SingleQuery::leading_importing_with`]: the
/// importing WITH's fields, borrowed from the clause.
pub struct LeadingWith<'a> {
    pub distinct: bool,
    pub items: &'a [Projection],
    pub order: &'a [OrderItem],
    pub skip: Option<&'a Expr>,
    pub limit: Option<&'a Expr>,
    pub where_clause: Option<&'a Expr>,
}

/// A `CALL โ€ฆ YIELD` tail: no `YIELD` at all, `YIELD *`, or an explicit
/// list of (procedure output name, optional alias) pairs. The typed shape
/// replaces the old `"*"`-string sentinel, so a real column named `*`
/// cannot be confused with the wildcard.
#[derive(Debug, Clone, PartialEq)]
pub enum YieldItems {
    /// No `YIELD` clause: the procedure's default yields apply.
    Default,
    /// `YIELD *`: every output the procedure declares.
    Star,
    /// An explicit `YIELD a [AS x], b ...` list, in source order.
    Named(Vec<(SmallName, Option<SmallName>)>),
}

impl YieldItems {
    /// The explicit items, or an empty slice for `Default`/`Star`.
    pub fn items(&self) -> &[(SmallName, Option<SmallName>)] {
        match self {
            YieldItems::Named(items) => items,
            YieldItems::Default | YieldItems::Star => &[],
        }
    }

    /// Raw output names of an explicit list, in order (empty otherwise).
    pub fn names(&self) -> impl Iterator<Item = &SmallName> {
        self.items().iter().map(|(name, _)| name)
    }

    /// Each explicit item's raw name and effective (alias-else-name) name.
    pub fn effective_pairs(&self) -> impl Iterator<Item = (&SmallName, &SmallName)> {
        self.items()
            .iter()
            .map(|(name, alias)| (name, alias.as_ref().unwrap_or(name)))
    }

    /// Effective (post-alias) output names of an explicit list.
    pub fn effective_names(&self) -> impl Iterator<Item = &SmallName> {
        self.effective_pairs().map(|(_, effective)| effective)
    }

    /// Whether any `YIELD` tail was written (`Star` or `Named`).
    pub fn has_yield(&self) -> bool {
        !matches!(self, YieldItems::Default)
    }

    /// The yield tail resolved against a procedure's `default` output
    /// columns: `YIELD *` takes every default (the same as no `YIELD`),
    /// an explicit list is itself. The one classification for callers that
    /// need the resolved pairs โ€” never a boolean collapsed to `items()`
    /// (Star's items are empty by definition).
    pub fn resolved_pairs(&self, defaults: &[&str]) -> Vec<(String, Option<String>)> {
        // Map-bridge: the executor's procedure-call column handling stays
        // String-keyed (catalog-facing output columns), so both arms render
        // text here โ€” the one feed the stage-2 scope-map change (ADR-0509)
        // does not touch.
        match self {
            YieldItems::Named(items) => items
                .iter()
                .map(|(name, alias)| {
                    (
                        name.to_string(),
                        // allocation-free read of the optional alias
                        alias.as_ref().map(SmallName::to_string),
                    )
                })
                .collect(),
            YieldItems::Star | YieldItems::Default => defaults
                .iter()
                .map(|column| (column.to_string(), None))
                .collect(),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum Clause {
    /// Relate's rich catalog/admin AST, retained losslessly by the
    /// Relate front-end and executed against Relate metadata.
    Catalog(Box<relate_parser::Clause>),
    Match {
        optional: bool,
        match_mode: MatchMode,
        patterns: Vec<PathPattern>,
        where_clause: Option<Expr>,
        /// CIP-79 `USING INDEX [SEEK]` hints, carried for the plan-time
        /// notification that reports a hint no catalog index can serve
        /// (`01N31`, ADR-0363); the planner itself steers nothing by them.
        using_index: Vec<relate_parser::IndexHint>,
        /// CIP-79 `USING JOIN ON` hints, carried for the same purpose: the
        /// planner never fulfils one, so the `01N30` notification reports
        /// each.
        using_join: Vec<relate_parser::JoinHint>,
        /// CIP-224/CIP-240 vector index search attached to this MATCH.
        search: Option<Box<SearchClause>>,
    },
    Create {
        patterns: Vec<PathPattern>,
    },
    Merge {
        pattern: PathPattern,
        on_create: Vec<SetItem>,
        on_match: Vec<SetItem>,
    },
    Call {
        name: SmallName,
        args: Vec<Expr>,
        explicit: bool,
        yield_items: YieldItems,
        optional: bool,
        where_clause: Option<Expr>,
    },
    Delete {
        detach: bool,
        expressions: Vec<Expr>,
    },
    Set {
        items: Vec<SetItem>,
    },
    Remove {
        items: Vec<RemoveItem>,
    },
    With {
        distinct: bool,
        /// CIP-236: explicit grouping keys (Some(vec![]) = `GROUP BY ()`
        /// empty set; None = infer from the non-aggregating items).
        group_by: Option<Vec<Expr>>,
        items: Vec<Projection>,
        order: Vec<OrderItem>,
        skip: Option<Expr>,
        limit: Option<Expr>,
        where_clause: Option<Expr>,
        origin: WithOrigin,
    },
    Unwind {
        expression: Expr,
        alias: SmallName,
    },
    Return {
        distinct: bool,
        /// CIP-236 explicit grouping keys (same encoding as `With`).
        group_by: Option<Vec<Expr>>,
        items: Vec<Projection>,
        order: Vec<OrderItem>,
        skip: Option<Expr>,
        limit: Option<Expr>,
    },
    /// Named `(label, property)` catalog entry (ADR-0040).
    CreateIndex {
        name: Option<String>,
        if_not_exists: bool,
        label: SmallName,
        property: SmallName,
    },
    DropIndex {
        if_exists: bool,
        target: DropIndexTarget,
    },
    ShowIndexes,
    /// CIP-140: terminal empty projection (0 columns).
    Finish,
    /// `LOAD CSV [WITH HEADERS] FROM โ€ฆ AS โ€ฆ`.
    LoadCsv {
        with_headers: bool,
        url: Expr,
        variable: SmallName,
        field_terminator: Option<String>,
    },
    /// `CALL { โ€ฆ }` / `OPTIONAL CALL { โ€ฆ }` (CIP-141 / CIP-32).
    CallSubquery {
        query: Box<Query>,
        optional: bool,
        scope: CallScope,
        in_transactions: Option<InTransactions>,
    },
    /// CIP-135 top-level WHEN/THEN/ELSE.
    Conditional {
        branches: Vec<(Expr, Query)>,
        else_query: Option<Box<Query>>,
    },
    /// Updating FOREACH loop.
    Foreach {
        variable: SmallName,
        list: Expr,
        clauses: Vec<Clause>,
    },
    /// Single-graph runtime: validates scope, then acts as a no-op.
    Use {
        graph: Expr,
    },
}

/// Native Relate representation of a SEARCH subclause (CIP-224, the
/// CIP-278 `<source specification clause>` extension).
#[derive(Debug, Clone, PartialEq)]
pub struct SearchClause {
    pub binding_var: SmallName,
    /// CIP-278 `<source specification clause>`: one index search, or a
    /// `COMBINE` of two or more weighted sources fused with WRRF.
    pub spec: SearchSpec,
    pub score_var: Option<SmallName>,
    /// The MATCH's pattern goes beyond CIP-225 Rule 0's plain shape (the
    /// `complex-search-patterns` family) -- classified once at lowering by
    /// the same shape test the preview gate rejects. The executor's top-K
    /// rule keys on it: a plain pattern's `LIMIT` is a hard candidate
    /// window, with the MATCH's outer `WHERE`, label or score predicate
    /// post-filtering, while a complex pattern's `LIMIT` counts the
    /// candidates the whole MATCH keeps.
    pub complex_pattern: bool,
}

/// `DROP INDEX name` or `DROP INDEX ON :Label(prop)` (ADR-0040).
#[derive(Debug, Clone, PartialEq)]
pub enum DropIndexTarget {
    Name(String),
    Schema {
        label: SmallName,
        property: SmallName,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub struct Projection {
    pub expr: Expr,
    pub alias: Option<SmallName>,
    /// Verbatim source text of the unaliased projection expression, as
    /// captured by the parser. Used by [`Projection::output_name`] as the
    /// column name for expression shapes without a dedicated label
    /// (`count{()}`, list/map literals, `CASE`, etc.). Synthetic
    /// projections built after parsing leave this `None`.
    pub source_text: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WithOrigin {
    #[default]
    Explicit,
    Let,
    Filter,
}

#[derive(Debug, Clone, PartialEq)]
pub struct OrderItem {
    pub expr: Expr,
    pub ascending: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub enum SetItem {
    /// `SET n.k = v` / `SET n[expr] = v` -- one property assignment; the
    /// target is always a property access (`Expr::Property` /
    /// `Expr::DynamicProperty`).
    Property { target: Expr, value: Expr },
    /// `SET n = <map>` -- full property replacement; the target is always
    /// a bare variable. The front mints this where the parser already
    /// keeps the fact (`SetItem.property` is `None`), so no consumer
    /// re-derives the split by sniffing the target expression's shape.
    ReplaceAll { target: Expr, value: Expr },
    /// `SET n += <map>` -- merge the map into the target's existing
    /// properties; the target is always a bare variable (the parser only
    /// sets `merge` on the bare-variable form).
    MergeMap { target: Expr, value: Expr },
    Labels {
        target: Expr,
        labels: Vec<LabelExpr>,
    },
    LabelsFunction {
        target: Expr,
        value: Expr,
        op: LabelsFunctionOp,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LabelsFunctionOp {
    Overwrite,
    Extend,
    Remove,
}

#[derive(Debug, Clone, PartialEq)]
pub enum RemoveItem {
    Property {
        target: Expr,
    },
    Labels {
        target: Expr,
        labels: Vec<LabelExpr>,
    },
}

/// The `<var>.<key>` property access shape: the variable's and the
/// property key's borrowed names, or `None` for any other expression.
/// The seek/fold/vector operand recognizers derive from here.
pub fn var_prop(expr: &Expr) -> Option<(&SmallName, &SmallName)> {
    let Expr::Property { expr, key, .. } = expr else {
        return None;
    };
    let Expr::Variable(v) = expr.as_ref() else {
        return None;
    };
    Some((v, key))
}

/// [`var_prop`] with the key as the shared [`relate_names::NamedToken`]:
/// the written name beside its plan-resolved token, for the executor reads
/// that skip the by-name interner lookup.
pub fn var_prop_named(expr: &Expr) -> Option<(&SmallName, relate_names::NamedToken)> {
    let Expr::Property { key_id, .. } = expr else {
        return None;
    };
    var_prop(expr).map(|(var, key)| (var, (key.clone(), *key_id)))
}

/// The owned spelling of the `<var>.<key>` pair (filters and plan
/// items own their strings). A cross-crate contract, not a local
/// convenience: the planner passes it as the recognizer fn to
/// `orient_side` and the executor's `unwind` builds owned plan items
/// from it, so the public owned spelling stays named rather than
/// mapped inline at each call site.
pub fn var_prop_owned(expr: &Expr) -> Option<(String, String)> {
    // Map-bridge: plan items and filters own their strings (the planner's
    // plan structures stay String-keyed until their own SmallName stage);
    // the text render happens once, here, not per lookup.
    var_prop(expr).map(|(var, key)| (var.to_string(), key.to_string()))
}

/// The four Cypher 25 chronological comparison operators, the one
/// vocabulary shared by the lowering and the executor's evaluation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChronoOp {
    Before,
    After,
    SameInstant,
    SameRepresentation,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOp {
    Not,
    Plus,
    Minus,
    IsNull,
    IsNotNull,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOp {
    Or,
    Xor,
    And,
    Eq,
    Neq,
    Lt,
    Gt,
    Lte,
    Gte,
    Add,
    Sub,
    Mul,
    Div,
    Mod,
    Pow,
    Concat,
    In,
    StartsWith,
    EndsWith,
    Contains,
    Regex,
}

impl Projection {
    /// Column name of this projection item.
    ///
    /// Resolution order (matches Neo4j):
    /// 1. explicit `AS alias`
    /// 2. [`canonical_name`] โ€” a bare variable (`RETURN n` โ†’ `n`), a
    ///    `v.key` property access (`RETURN n.name` โ†’ `n.name`), or a map
    ///    projection on a variable (`RETURN n {.name}` โ†’ `n`; the map is a
    ///    view of that variable, and a CALL subquery exports it under the
    ///    variable's name), the shapes Neo4j names regardless of source
    ///    formatting
    /// 3. the verbatim source text captured by the parser (`RETURN 1+2`
    ///    โ†’ `1+2`, `RETURN count{()}` โ†’ `count{()}`); covers every other
    ///    expression shape without a parser-synthesized name
    /// 4. `column_<idx>` for synthetic post-parse projections that have
    ///    no `source_text`
    pub fn output_name(&self, idx: usize) -> String {
        if let Some(a) = &self.alias {
            // Rendering (a user-visible column name), not a lookup: the
            // text arms resolve through Display (ADR-0509 ยง2).
            return a.to_string();
        }
        if let Some(name) = canonical_name(&self.expr) {
            return name;
        }
        if let Some(src) = &self.source_text {
            return src.clone();
        }
        format!("column_{idx}")
    }

    /// The name this item imports from the enclosing scope, when it is a
    /// legal *importing-WITH* item: a bare, unaliased variable reference.
    /// `None` for an aliased item โ€” a shape 42I28 rejects, never an import
    /// under a silently different name. The one home for that policy: the
    /// planner's 42I28 classifier and the notification layer's import-set
    /// analysis both read it.
    pub fn imported_name(&self) -> Option<&SmallName> {
        match (&self.expr, &self.alias) {
            (Expr::Variable(name), None) => Some(name),
            _ => None,
        }
    }
}

/// The canonical names Neo4j spells identically regardless of source
/// formatting: a bare variable (`n`), `*`, a `v.key` property access, and
/// a map projection on a variable (`n {.name, .age}` โ†’ `n`). `None` for
/// any other expression shape.
fn canonical_name(expr: &Expr) -> Option<String> {
    // Rendering (a user-visible column name): the text arms resolve
    // through Display (ADR-0509 ยง2), once per name, not per lookup.
    match expr {
        Expr::Star => Some("*".into()),
        Expr::Variable(v) => Some(v.to_string()),
        Expr::MapProjection { expr, .. } => match expr.as_ref() {
            Expr::Variable(v) => Some(v.to_string()),
            _ => None,
        },
        other => var_prop(other).map(|(v, key)| format!("{v}.{key}")),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn vector_metric_names_round_trip_through_from_name() {
        // `ALL` is the one table `from_name` parses and `name` renders, so
        // this fails when a variant is added to one side only.
        for (keyword, metric) in VectorMetric::ALL {
            assert_eq!(metric.name(), keyword);
            assert_eq!(VectorMetric::from_name(keyword), Some(metric));
        }
        let names: std::collections::BTreeSet<_> = VectorMetric::ALL
            .iter()
            .map(|(keyword, _)| *keyword)
            .collect();
        assert_eq!(names.len(), VectorMetric::ALL.len(), "names are unique");
        // Parsing is case-insensitive; an unknown identifier is not a metric.
        assert_eq!(
            VectorMetric::from_name("euclidean_squared"),
            Some(VectorMetric::EuclideanSquared)
        );
        assert_eq!(VectorMetric::from_name("not-a-metric"), None);
    }

    fn path(variable: Option<&str>, subpath_variable: Option<&str>) -> PathPattern {
        PathPattern {
            variable: variable.map(SmallName::from_name),
            subpath_variable: subpath_variable.map(SmallName::from_name),
            elements: vec![],
            nodes: vec![],
            rels: vec![],
            where_clause: None,
            mode: None,
            selector: None,
            is_shortest_path_function: false,
        }
    }

    #[test]
    fn path_names_lists_outer_then_subpath_variable() {
        let both = path(Some("p"), Some("q"));
        assert_eq!(
            both.path_names()
                .map(SmallName::to_string)
                .collect::<Vec<_>>(),
            ["p", "q"]
        );
        assert!(both.has_path_name());

        let subpath_only = path(None, Some("q"));
        assert_eq!(
            subpath_only
                .path_names()
                .map(SmallName::to_string)
                .collect::<Vec<_>>(),
            ["q"]
        );
        assert!(subpath_only.has_path_name());

        let outer_only = path(Some("p"), None);
        assert_eq!(
            outer_only
                .path_names()
                .map(SmallName::to_string)
                .collect::<Vec<_>>(),
            ["p"]
        );
        assert!(outer_only.has_path_name());

        let unnamed = path(None, None);
        assert!(unnamed.path_names().next().is_none());
        assert!(!unnamed.has_path_name());
    }
}