kglite 0.16.4

Pure-Rust embedded Cypher knowledge graph engine with in-memory, mmap, and disk storage, and agent-facing schema introspection
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
//! Cypher parser: MATCH / OPTIONAL MATCH clause + pattern extraction.

use super::super::ast::*;
use super::super::tokenizer::{keyword_name_token, reserved_literal_name_token, CypherToken};
use super::CypherParser;

impl CypherParser {
    // ========================================================================
    // MATCH Clause
    // ========================================================================

    pub(super) fn parse_match_clause(&mut self, optional: bool) -> Result<Clause, String> {
        self.expect(&CypherToken::Match)?;

        let mut path_assignments = Vec::new();

        // Check for path assignment: p = shortestPath(...)
        // Pattern: Identifier Equals [Identifier("shortestPath") LParen] pattern [RParen]
        let patterns = if self.is_path_assignment() {
            let path_var = self.consume_identifier()?;
            self.expect(&CypherToken::Equals)?;

            // Check for shortestPath( / allShortestPaths( wrapper
            let is_all_shortest = self.is_all_shortest_paths_call();
            let is_shortest = is_all_shortest || self.is_shortest_path_call();
            if is_shortest {
                self.advance(); // consume the wrapper identifier
                self.expect(&CypherToken::LParen)?;
            }

            let patterns = self.parse_match_patterns()?;

            if is_shortest {
                self.expect(&CypherToken::RParen)?;
            }

            path_assignments.push(PathAssignment {
                variable: path_var,
                pattern_index: 0,
                is_shortest_path: is_shortest,
                all_shortest: is_all_shortest,
            });
            patterns
        } else {
            self.parse_match_patterns()?
        };

        // `Match = ['OPTIONAL'] 'MATCH' Pattern [Where]` — a WHERE directly
        // after OPTIONAL MATCH is part of *this* clause, so it filters
        // candidates during matching and a row whose candidates all fail is
        // null-extended rather than deleted. A plain MATCH's WHERE keeps its
        // own `Clause::Where` (identical semantics there, and every
        // MATCH+WHERE planner pass matches on that adjacency) — see
        // `MatchClause::where_clause`.
        let where_clause = if optional && self.check(&CypherToken::Where) {
            self.advance(); // consume WHERE
            Some(WhereClause {
                predicate: self.parse_predicate()?,
            })
        } else {
            None
        };

        let clause = MatchClause {
            patterns,
            path_assignments,
            limit_hint: None,
            distinct_node_hint: None,
            where_clause,
            node_anchors: Vec::new(),
        };
        if optional {
            Ok(Clause::OptionalMatch(clause))
        } else {
            Ok(Clause::Match(clause))
        }
    }

    /// Check if current position looks like: Identifier = [shortestPath(] ...
    pub(super) fn is_path_assignment(&self) -> bool {
        matches!(self.peek(), Some(CypherToken::Identifier(_)))
            && self.peek_at(1) == Some(&CypherToken::Equals)
    }

    /// Check if current position is shortestPath( — called AFTER consuming "var ="
    pub(super) fn is_shortest_path_call(&self) -> bool {
        if let Some(CypherToken::Identifier(name)) = self.peek() {
            name.eq_ignore_ascii_case("shortestPath")
                && self.peek_at(1) == Some(&CypherToken::LParen)
        } else {
            false
        }
    }

    /// Check if current position is allShortestPaths( — called AFTER consuming "var ="
    pub(super) fn is_all_shortest_paths_call(&self) -> bool {
        if let Some(CypherToken::Identifier(name)) = self.peek() {
            name.eq_ignore_ascii_case("allShortestPaths")
                && self.peek_at(1) == Some(&CypherToken::LParen)
        } else {
            false
        }
    }

    /// Consume an identifier token and return the string
    pub(super) fn consume_identifier(&mut self) -> Result<String, String> {
        match self.advance() {
            Some(CypherToken::Identifier(s)) => Ok(s.clone()),
            other => Err(format!("Expected identifier, got {:?}", other)),
        }
    }

    /// Parse one or more comma-separated patterns in MATCH
    pub(super) fn parse_match_patterns(
        &mut self,
    ) -> Result<Vec<crate::graph::core::pattern_matching::Pattern>, String> {
        let mut patterns = Vec::new();

        loop {
            // Reconstruct the pattern string from tokens until we hit a comma (at top-level)
            // or a clause boundary
            let pattern_str = self.extract_pattern_string()?;
            if pattern_str.is_empty() {
                return Err("Expected a pattern in MATCH clause".to_string());
            }

            let pattern = crate::graph::core::pattern_matching::parse_pattern(&pattern_str)
                .map_err(|e| format!("Pattern parse error: {}", e))?;
            patterns.push(pattern);

            // Check for comma to continue with more patterns
            if self.check(&CypherToken::Comma) {
                self.advance();
            } else {
                break;
            }
        }

        Ok(patterns)
    }

    /// Parse patterns inside EXISTS { ... } — same as parse_match_patterns but uses
    /// extract_exists_pattern_string which stops at RBrace instead of clause boundaries.
    /// Returns the patterns plus their clause-group ids (comma-joined
    /// patterns share a group; each MATCH keyword starts a new one).
    pub(super) fn parse_exists_patterns(
        &mut self,
    ) -> Result<
        (
            Vec<crate::graph::core::pattern_matching::Pattern>,
            Vec<usize>,
        ),
        String,
    > {
        // Default delimiter for `EXISTS { ... }` / `count { ... }`: closing brace.
        self.parse_pattern_subquery_patterns(&CypherToken::RBrace)
    }

    /// Parse one or more comma/MATCH-separated patterns until a delimiter
    /// token at top level. Used by EXISTS/count (delimiter = `}`) and the
    /// 0.9.0 §6 `size((pattern))` form (delimiter = `)`).
    ///
    /// The second return value assigns each pattern a clause-group id:
    /// comma-separated patterns share the group of the pattern before them
    /// (they form ONE clause, subject to the relationship-uniqueness trail
    /// rule); a `MATCH` keyword separator starts a new group (a separate
    /// clause — relationships may be re-used across it, as across separate
    /// MATCH clauses).
    pub(super) fn parse_pattern_subquery_patterns(
        &mut self,
        end_token: &CypherToken,
    ) -> Result<
        (
            Vec<crate::graph::core::pattern_matching::Pattern>,
            Vec<usize>,
        ),
        String,
    > {
        let mut patterns = Vec::new();
        let mut groups: Vec<usize> = Vec::new();
        let mut group = 0usize;

        loop {
            let pattern_str = self.extract_pattern_subquery_string(end_token)?;
            if pattern_str.is_empty() {
                if patterns.is_empty() {
                    return Err("Expected a pattern inside EXISTS { }".to_string());
                }
                break;
            }

            let pattern = crate::graph::core::pattern_matching::parse_pattern(&pattern_str)
                .map_err(|e| format!("Pattern parse error in EXISTS: {}", e))?;
            patterns.push(pattern);
            groups.push(group);

            if self.check(&CypherToken::Comma) {
                self.advance();
            } else if self.check(&CypherToken::Match) {
                // Subquery form: EXISTS { MATCH (a)-[:R]->(b) MATCH (c)-[:R2]->(d) ... }
                // Don't advance — the next iteration's
                // extract_exists_pattern_string will skip the MATCH at its
                // start (same path as the optional first MATCH).
                group += 1;
            } else {
                break;
            }
        }

        Ok((patterns, groups))
    }

    /// Extract tokens forming a pattern inside EXISTS { ... }, stopping at RBrace or comma.
    /// Re-serialize an identifier, adding backticks if it contains spaces or
    /// special chars, or if the secondary pattern lexer would read the bare
    /// word back as something other than an identifier.
    ///
    /// Quoting always routes through [`backtick_quote`], so an identifier that
    /// itself contains a backtick survives the round trip through the secondary
    /// pattern lexer instead of terminating the quote early.
    ///
    /// The second condition is the one that closes the mint-but-never-query
    /// trap: a token that reached the parser as `` `TRUE` `` is a plain
    /// `Identifier` by then, and emitting it bare handed the secondary lexer a
    /// boolean. The word list lives with that lexer
    /// ([`crate::graph::core::pattern_matching::parser::bare_word_needs_quoting`])
    /// because it is a property of *its* grammar, not of this emitter.
    pub(super) fn quote_identifier(s: &str) -> String {
        if s.contains(' ')
            || s.contains('-')
            || s.contains('/')
            || s.contains('.')
            || s.contains('(')
            || s.contains(')')
            || s.contains('`')
            || crate::graph::core::pattern_matching::parser::bare_word_needs_quoting(s)
        {
            backtick_quote(s)
        } else {
            s.to_string()
        }
    }

    /// Re-serialize tokens forming a pattern, stopping at the supplied
    /// delimiter (RBrace for EXISTS/count, RParen for size).
    pub(super) fn extract_pattern_subquery_string(
        &mut self,
        end_token: &CypherToken,
    ) -> Result<String, String> {
        // Skip optional MATCH keyword — standard Cypher allows EXISTS { MATCH (pattern) }
        if self.check(&CypherToken::Match) {
            self.advance();
        }

        let mut parts = Vec::new();
        let mut paren_depth = 0i32;
        let mut bracket_depth = 0i32;
        let mut brace_depth = 0i32;
        let mut prev: Option<CypherToken> = None;

        while self.has_tokens() {
            // Stop at the caller-supplied end-token (RBrace for EXISTS,
            // RParen for size). Only at top level (depth 0).
            if paren_depth == 0 && bracket_depth == 0 && self.check(end_token) {
                break;
            }

            // Stop at comma at top level (pattern separator)
            if paren_depth == 0 && bracket_depth == 0 && self.check(&CypherToken::Comma) {
                break;
            }

            // Stop at WHERE keyword (EXISTS { MATCH ... WHERE ... } subquery)
            if paren_depth == 0 && bracket_depth == 0 && self.check(&CypherToken::Where) {
                break;
            }

            // Stop at MATCH keyword at top level — multi-MATCH subquery
            // form (`EXISTS { MATCH ... MATCH ... }`). The outer
            // parse_exists_patterns loop continues on this case.
            if paren_depth == 0 && bracket_depth == 0 && self.check(&CypherToken::Match) {
                break;
            }

            let token = self.advance().unwrap().clone();

            match &token {
                CypherToken::LParen => {
                    paren_depth += 1;
                    parts.push("(".to_string());
                }
                CypherToken::RParen => {
                    paren_depth -= 1;
                    parts.push(")".to_string());
                }
                CypherToken::LBracket => {
                    bracket_depth += 1;
                    parts.push("[".to_string());
                }
                CypherToken::RBracket => {
                    bracket_depth -= 1;
                    parts.push("]".to_string());
                }
                CypherToken::LBrace => {
                    brace_depth += 1;
                    parts.push("{".to_string());
                }
                CypherToken::RBrace => {
                    brace_depth -= 1;
                    parts.push("}".to_string());
                }
                CypherToken::Colon => parts.push(":".to_string()),
                CypherToken::Comma => parts.push(",".to_string()),
                CypherToken::Dash => parts.push("-".to_string()),
                CypherToken::GreaterThan => parts.push(">".to_string()),
                CypherToken::LessThan => parts.push("<".to_string()),
                CypherToken::Star => parts.push("*".to_string()),
                CypherToken::DotDot => parts.push("..".to_string()),
                CypherToken::Dot => parts.push(".".to_string()),
                CypherToken::Identifier(s) => parts.push(Self::quote_identifier(s)),
                CypherToken::StringLit(s) => {
                    // Re-escape quotes so the pattern parser can re-tokenize correctly
                    let escaped = s.replace('\\', "\\\\").replace('\'', "\\'");
                    parts.push(format!("'{}'", escaped));
                }
                CypherToken::IntLit(n) => parts.push(n.to_string()),
                CypherToken::FloatLit(f) => parts.push(f.to_string()),
                // A value-literal word in a NAME position — see
                // `at_name_position`. Backtick its verbatim lexeme so the
                // secondary lexer reads a name, not a boolean.
                tok @ (CypherToken::True | CypherToken::False | CypherToken::Null)
                    if at_name_position(prev.as_ref(), self.peek(), brace_depth) =>
                {
                    let name = self
                        .keyword_lexeme_at(self.pos - 1)
                        .unwrap_or_else(|| reserved_literal_name_token(tok).unwrap());
                    parts.push(backtick_quote(name));
                }
                CypherToken::True => parts.push("true".to_string()),
                CypherToken::False => parts.push("false".to_string()),
                // Re-serialize `$param` so the inner pattern parser sees it
                // and the executor substitutes it (e.g.
                // `EXISTS { MATCH (a)-[:R]->(:T {id:$id}) }`). The top-level
                // MATCH-pattern extractor already does this; without it,
                // params worked everywhere except inside an EXISTS pattern.
                CypherToken::Parameter(name) => parts.push(format!("${}", name)),
                // KG-2: a reserved keyword used as a name (label / rel-type /
                // property key) — backtick its verbatim source lexeme so the
                // secondary pattern parser reads it as an identifier
                // (`[:CONTAINS]`, `(:CONTAINS)`, `{contains: 1}` keeps key
                // `contains`).
                tok if keyword_name_token(tok).is_some() => {
                    let name = self
                        .keyword_lexeme_at(self.pos - 1)
                        .unwrap_or_else(|| keyword_name_token(tok).unwrap());
                    parts.push(backtick_quote(name));
                }
                _ => {
                    return Err(format!("Unexpected token in EXISTS pattern: {:?}", token));
                }
            }
            prev = Some(token);
        }

        Ok(parts.join(" "))
    }

    /// Extract tokens forming a single pattern and reconstruct as a string
    /// for the existing pattern_matching parser.
    /// Stops at commas (outside parens/brackets), clause keywords, or end of input.
    pub(super) fn extract_pattern_string(&mut self) -> Result<String, String> {
        let mut parts = Vec::new();
        let mut paren_depth = 0i32;
        let mut bracket_depth = 0i32;
        let mut brace_depth = 0i32;
        let mut prev: Option<CypherToken> = None;

        while self.has_tokens() {
            // Stop at clause boundaries (only at top level)
            if paren_depth == 0 && bracket_depth == 0 && self.at_clause_boundary() {
                break;
            }

            // Stop at comma at top level (pattern separator)
            if paren_depth == 0 && bracket_depth == 0 && self.check(&CypherToken::Comma) {
                break;
            }

            // Stop at tokens that legitimately *follow* a pattern expression
            // at top level: boolean operators (AND/OR/XOR in WHERE), `AS`
            // (RETURN/WITH alias), CASE keywords (THEN/ELSE/END), and the
            // comprehension/reduce separator `|`. Inside parens/brackets the
            // soft-keyword ones (AS, XOR) still round-trip as property keys
            // via the keyword_name_token arm below.
            if paren_depth == 0
                && bracket_depth == 0
                && matches!(
                    self.peek(),
                    Some(CypherToken::And)
                        | Some(CypherToken::Or)
                        | Some(CypherToken::Xor)
                        | Some(CypherToken::As)
                        | Some(CypherToken::Then)
                        | Some(CypherToken::Else)
                        | Some(CypherToken::End)
                        | Some(CypherToken::Pipe)
                )
            {
                break;
            }

            // Stop at RParen that would go negative (e.g. closing shortestPath(...))
            if paren_depth == 0 && self.check(&CypherToken::RParen) {
                break;
            }

            // Stop at a top-level RBrace — it can only be the `}` closing a
            // `CALL { ... }` subquery body (property maps live inside `()` or
            // `[]`, so any `}` belonging to a map is at paren/bracket depth
            // > 0). Without this, a MATCH that is the last clause of a
            // subquery body swallows the terminating `}` into the pattern
            // string and the body fails to close.
            if paren_depth == 0 && bracket_depth == 0 && self.check(&CypherToken::RBrace) {
                break;
            }

            let token = self.advance().unwrap().clone();

            match &token {
                CypherToken::LParen => {
                    paren_depth += 1;
                    parts.push("(".to_string());
                }
                CypherToken::RParen => {
                    paren_depth -= 1;
                    parts.push(")".to_string());
                }
                CypherToken::LBracket => {
                    bracket_depth += 1;
                    parts.push("[".to_string());
                }
                CypherToken::RBracket => {
                    bracket_depth -= 1;
                    parts.push("]".to_string());
                }
                CypherToken::LBrace => {
                    brace_depth += 1;
                    parts.push("{".to_string());
                }
                CypherToken::RBrace => {
                    brace_depth -= 1;
                    parts.push("}".to_string());
                }
                CypherToken::Colon => parts.push(":".to_string()),
                CypherToken::Comma => parts.push(",".to_string()),
                CypherToken::Dash => parts.push("-".to_string()),
                CypherToken::GreaterThan => parts.push(">".to_string()),
                CypherToken::LessThan => parts.push("<".to_string()),
                CypherToken::Star => parts.push("*".to_string()),
                CypherToken::DotDot => parts.push("..".to_string()),
                CypherToken::Dot => parts.push(".".to_string()),
                CypherToken::Pipe => parts.push("|".to_string()),
                CypherToken::Identifier(s) => parts.push(Self::quote_identifier(s)),
                CypherToken::StringLit(s) => {
                    let escaped = s.replace('\\', "\\\\").replace('\'', "\\'");
                    parts.push(format!("'{}'", escaped));
                }
                CypherToken::IntLit(n) => parts.push(n.to_string()),
                CypherToken::FloatLit(f) => parts.push(f.to_string()),
                // A value-literal word in a NAME position — see
                // `at_name_position`. Backtick its verbatim lexeme so the
                // secondary lexer reads a name, not a boolean.
                tok @ (CypherToken::True | CypherToken::False | CypherToken::Null)
                    if at_name_position(prev.as_ref(), self.peek(), brace_depth) =>
                {
                    let name = self
                        .keyword_lexeme_at(self.pos - 1)
                        .unwrap_or_else(|| reserved_literal_name_token(tok).unwrap());
                    parts.push(backtick_quote(name));
                }
                CypherToken::True => parts.push("true".to_string()),
                CypherToken::False => parts.push("false".to_string()),
                CypherToken::Parameter(name) => {
                    parts.push(format!("${}", name));
                }
                // KG-2: a reserved keyword used as a name (label / rel-type /
                // property key) — backtick its verbatim source lexeme so the
                // secondary pattern parser reads it as an identifier
                // (`[:CONTAINS]`, `(:CONTAINS)`, `{contains: 1}` keeps key
                // `contains`). Only reached at bracket/paren depth > 0
                // (depth-0 keywords break out earlier as clause boundaries).
                tok if keyword_name_token(tok).is_some() => {
                    let name = self
                        .keyword_lexeme_at(self.pos - 1)
                        .unwrap_or_else(|| keyword_name_token(tok).unwrap());
                    parts.push(backtick_quote(name));
                }
                _ => {
                    return Err(format!("Unexpected token in MATCH pattern: {:?}", token));
                }
            }
            prev = Some(token);
        }

        Ok(parts.join(""))
    }

    // ========================================================================
    // WHERE Clause
    // ========================================================================
}

/// Does the token just consumed by a pattern re-serializer occupy a **name**
/// position — a label, a relationship type, or a property key — rather than a
/// value position?
///
/// The re-serializer walks a flat token stream, so this is the whole of its
/// grammar knowledge, and it is deliberately small because a pattern's
/// structure is: names follow `:` (label / relationship type) or `|` (type
/// alternation) outside any property map, and a property map's keys are the
/// tokens immediately before their `:`. Everything else at map depth 0 is a
/// variable position, where openCypher's `Variable = SymbolicName` excludes
/// the reserved words — refusing them there is what keeps MATCH symmetric with
/// CREATE, whose variable slot takes an `Identifier` only.
///
/// `brace_depth` counts open `{` of inline property maps; `prev` and `next`
/// are the surrounding tokens (`None` at the ends of the pattern).
///
/// **Extension point (T10, dynamic labels).** This is the single place that
/// answers "is this token a name here?", so a second token class joins by
/// adding one guarded arm at each of the two call sites below and reusing this
/// predicate — `$(...)` parameter labels are name-position tokens by exactly
/// this rule, and must not disturb the `$param` *value* arm that already
/// exists a few lines above them.
fn at_name_position(
    prev: Option<&CypherToken>,
    next: Option<&CypherToken>,
    brace_depth: i32,
) -> bool {
    if brace_depth > 0 {
        matches!(next, Some(CypherToken::Colon))
    } else {
        matches!(prev, Some(CypherToken::Colon) | Some(CypherToken::Pipe))
    }
}

/// Wrap `name` in backticks, doubling any backtick it contains.
///
/// This is the *only* place a quoted identifier is written back into Cypher
/// text, and it is the emitter half of the tokenizer's doubling rule: without
/// it, a re-serialized identifier carrying a backtick would close its own quote
/// in the secondary pattern lexer and the remainder would be read as grammar.
/// Round-tripping `a`b` through quote-then-lex must return `a`b`.
pub(super) fn backtick_quote(name: &str) -> String {
    let mut out = String::with_capacity(name.len() + 2);
    out.push('`');
    for ch in name.chars() {
        if ch == '`' {
            out.push('`');
        }
        out.push(ch);
    }
    out.push('`');
    out
}