mushroomdb-query 0.6.9

Cypher query planner and executor for mushroomdb
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
//! Cypher subset lexer. Never panics on any `&str` input.

#[derive(Debug, Clone, PartialEq)]
pub enum Tok {
    // keywords (case-insensitive source, canonical here)
    Match,
    Where,
    Return,
    Order,
    By,
    Skip,
    Limit,
    As,
    And,
    Or,
    Not,
    Asc,
    Desc,
    // pipeline keywords
    With,
    Unwind,
    /// `OPTIONAL` — marks the start of an `OPTIONAL MATCH` clause.
    Optional,
    // write keywords
    Create,
    Set,
    Delete,
    Detach,
    Merge,
    Ident(String),
    Str(String),
    Int(i64),
    Float(f64),
    Param(String),
    LParen,
    RParen,
    LBracket,
    RBracket,
    LBrace,
    RBrace,
    Colon,
    /// `|` — relationship-type alternation in a pattern (`[:A|:B]`).
    Pipe,
    Comma,
    Dot,
    Eq,
    Ne,
    Lt,
    Le,
    Gt,
    Ge,
    Dash, // BINDING: the lexer emits `Dash`, `Lt`, `Gt` as separate tokens and the
    // PARSER assembles rel-arrow shapes (`-[..]->`, `<-[..]-`, `-[..]-`);
    // `<=`, `>=`, `<>` are single tokens (Le, Ge, Ne).
    /// `*` — used in `COUNT(*)` and multiplication.
    Star,
    /// `+` — addition operator.
    Plus,
    /// `/` — division operator.
    Slash,
}

pub fn lex(input: &str) -> Result<Vec<Tok>, String> {
    let mut toks = Vec::new();
    let mut chars = input.char_indices().peekable();
    while let Some((i, ch)) = chars.next() {
        match ch {
            c if c.is_whitespace() => {}
            '(' => toks.push(Tok::LParen),
            ')' => toks.push(Tok::RParen),
            '[' => toks.push(Tok::LBracket),
            ']' => toks.push(Tok::RBracket),
            '{' => toks.push(Tok::LBrace),
            '}' => toks.push(Tok::RBrace),
            ':' => toks.push(Tok::Colon),
            '|' => toks.push(Tok::Pipe),
            ',' => toks.push(Tok::Comma),
            '.' => toks.push(Tok::Dot),
            '=' => toks.push(Tok::Eq),
            '*' => toks.push(Tok::Star),
            '-' => toks.push(Tok::Dash),
            '+' => toks.push(Tok::Plus),
            '/' => toks.push(Tok::Slash),
            '<' => match chars.peek() {
                Some((_, '=')) => {
                    chars.next();
                    toks.push(Tok::Le);
                }
                Some((_, '>')) => {
                    chars.next();
                    toks.push(Tok::Ne);
                }
                _ => toks.push(Tok::Lt),
            },
            '>' => {
                if matches!(chars.peek(), Some((_, '='))) {
                    chars.next();
                    toks.push(Tok::Ge);
                } else {
                    toks.push(Tok::Gt);
                }
            }
            '\'' => toks.push(lex_string(i, &mut chars)?),
            '$' => toks.push(lex_param(input, i, &mut chars)?),
            '0'..='9' => toks.push(lex_number(input, i, ch, &mut chars)?),
            'A'..='Z' | 'a'..='z' | '_' => toks.push(lex_word(input, i, ch, &mut chars)),
            _ => return Err(format!("illegal character {ch:?} at position {i}")),
        }
    }
    Ok(toks)
}

fn lex_string(
    start: usize,
    chars: &mut std::iter::Peekable<std::str::CharIndices<'_>>,
) -> Result<Tok, String> {
    let mut out = String::new();
    loop {
        match chars.next() {
            None => return Err(format!("unterminated string at position {start}")),
            Some((_, '\'')) => return Ok(Tok::Str(out)),
            Some((_, '\\')) => match chars.next() {
                Some((_, '\'')) => out.push('\''),
                Some((pos, ch)) => {
                    return Err(format!("invalid escape '\\{ch}' at position {pos}"));
                }
                None => return Err(format!("unterminated string at position {start}")),
            },
            Some((_, ch)) => out.push(ch),
        }
    }
}

fn lex_param(
    input: &str,
    start: usize,
    chars: &mut std::iter::Peekable<std::str::CharIndices<'_>>,
) -> Result<Tok, String> {
    match chars.peek() {
        Some((_, ch)) if is_ident_start(*ch) => Ok(Tok::Param(take_ident(input, chars))),
        _ => Err(format!("invalid parameter at position {start}")),
    }
}

fn lex_number(
    input: &str,
    start: usize,
    first: char,
    chars: &mut std::iter::Peekable<std::str::CharIndices<'_>>,
) -> Result<Tok, String> {
    let mut end = start + first.len_utf8();
    while let Some(&(p, ch)) = chars.peek() {
        if ch.is_ascii_digit() {
            chars.next();
            end = p + ch.len_utf8();
        } else {
            break;
        }
    }
    if let Some(&(dot_pos, '.')) = chars.peek() {
        let after_dot = input[dot_pos + '.'.len_utf8()..].chars().next();
        if after_dot.is_some_and(|c| c.is_ascii_digit()) {
            chars.next();
            end = dot_pos + '.'.len_utf8();
            while let Some(&(p, ch)) = chars.peek() {
                if ch.is_ascii_digit() {
                    chars.next();
                    end = p + ch.len_utf8();
                } else {
                    break;
                }
            }
            let val: f64 = input[start..end]
                .parse()
                .map_err(|_| format!("invalid float at position {start}"))?;
            return Ok(Tok::Float(val));
        }
        // `..` (two consecutive dots) is the range separator for variable-length
        // path patterns (`*1..5`).  When after_dot is also '.', stop the number
        // here without consuming the first dot — the main lex loop will emit two
        // `Dot` tokens for the `..` separator.
        if after_dot == Some('.') {
            // fall through to return the integer below
        } else {
            return Err(format!(
                "invalid number at position {start}: expected digit after decimal point"
            ));
        }
    }
    let val: i64 = input[start..end]
        .parse()
        .map_err(|_| format!("invalid integer at position {start}"))?;
    Ok(Tok::Int(val))
}

fn lex_word(
    input: &str,
    start: usize,
    first: char,
    chars: &mut std::iter::Peekable<std::str::CharIndices<'_>>,
) -> Tok {
    let mut end = start + first.len_utf8();
    while let Some(&(p, ch)) = chars.peek() {
        if is_ident_cont(ch) {
            chars.next();
            end = p + ch.len_utf8();
        } else {
            break;
        }
    }
    keyword(&input[start..end]).unwrap_or_else(|| Tok::Ident(input[start..end].to_string()))
}

fn take_ident(input: &str, chars: &mut std::iter::Peekable<std::str::CharIndices<'_>>) -> String {
    let (start, first) = match chars.next() {
        Some(pair) => pair,
        None => return String::new(),
    };
    let mut end = start + first.len_utf8();
    while let Some(&(p, ch)) = chars.peek() {
        if is_ident_cont(ch) {
            chars.next();
            end = p + ch.len_utf8();
        } else {
            break;
        }
    }
    input[start..end].to_string()
}

fn is_ident_start(ch: char) -> bool {
    ch.is_ascii_alphabetic() || ch == '_'
}

fn is_ident_cont(ch: char) -> bool {
    ch.is_ascii_alphanumeric() || ch == '_'
}

fn keyword(word: &str) -> Option<Tok> {
    Some(match word.to_ascii_lowercase().as_str() {
        "match" => Tok::Match,
        "where" => Tok::Where,
        "return" => Tok::Return,
        "order" => Tok::Order,
        "by" => Tok::By,
        "skip" => Tok::Skip,
        "limit" => Tok::Limit,
        "as" => Tok::As,
        "and" => Tok::And,
        "or" => Tok::Or,
        "not" => Tok::Not,
        "asc" => Tok::Asc,
        "desc" => Tok::Desc,
        "with" => Tok::With,
        "unwind" => Tok::Unwind,
        "optional" => Tok::Optional,
        "create" => Tok::Create,
        "set" => Tok::Set,
        "delete" => Tok::Delete,
        "detach" => Tok::Detach,
        "merge" => Tok::Merge,
        _ => return None,
    })
}

#[cfg(test)]
mod tests {
    use super::{lex, Tok};

    #[test]
    fn keywords_are_case_insensitive() {
        let expected = vec![Tok::Match];
        assert_eq!(lex("MATCH").unwrap(), expected);
        assert_eq!(lex("match").unwrap(), expected);
        assert_eq!(lex("MaTcH").unwrap(), expected);
        assert_eq!(
            lex("WHERE RETURN ORDER BY SKIP LIMIT AS AND OR NOT ASC DESC").unwrap(),
            vec![
                Tok::Where,
                Tok::Return,
                Tok::Order,
                Tok::By,
                Tok::Skip,
                Tok::Limit,
                Tok::As,
                Tok::And,
                Tok::Or,
                Tok::Not,
                Tok::Asc,
                Tok::Desc,
            ]
        );
        assert_eq!(
            lex("where return order by skip limit as and or not asc desc").unwrap(),
            vec![
                Tok::Where,
                Tok::Return,
                Tok::Order,
                Tok::By,
                Tok::Skip,
                Tok::Limit,
                Tok::As,
                Tok::And,
                Tok::Or,
                Tok::Not,
                Tok::Asc,
                Tok::Desc,
            ]
        );
    }

    #[test]
    fn comparison_ops_disambiguate() {
        assert_eq!(lex("<").unwrap(), vec![Tok::Lt]);
        assert_eq!(lex("<=").unwrap(), vec![Tok::Le]);
        assert_eq!(lex("<>").unwrap(), vec![Tok::Ne]);
        assert_eq!(lex(">").unwrap(), vec![Tok::Gt]);
        assert_eq!(lex(">=").unwrap(), vec![Tok::Ge]);
        assert_eq!(lex("=").unwrap(), vec![Tok::Eq]);
        assert_eq!(
            lex("< <= <> > >=").unwrap(),
            vec![Tok::Lt, Tok::Le, Tok::Ne, Tok::Gt, Tok::Ge]
        );
    }

    #[test]
    fn string_escape_apostrophe() {
        assert_eq!(lex(r"'it\'s'").unwrap(), vec![Tok::Str("it's".into())]);
    }

    #[test]
    fn float_vs_int_vs_bare_dot_is_error() {
        assert_eq!(lex("42").unwrap(), vec![Tok::Int(42)]);
        assert_eq!(lex("2.5").unwrap(), vec![Tok::Float(2.5)]);
        let err = lex("1.").expect_err("digit-dot with no following digit is an error");
        assert!(
            err.contains("position"),
            "error must include position info, got: {err}"
        );
    }

    #[test]
    fn dollar_param() {
        assert_eq!(lex("$name").unwrap(), vec![Tok::Param("name".into())]);
        assert_eq!(lex("$tid").unwrap(), vec![Tok::Param("tid".into())]);
    }

    #[test]
    fn unterminated_string_is_err_with_position() {
        let err = lex("'abc").expect_err("unterminated string must be Err");
        assert!(
            err.contains("position"),
            "error must include position info, got: {err}"
        );
        let err = lex("MATCH 'oops").expect_err("unterminated after tokens");
        assert!(
            err.contains("position"),
            "error must include position info, got: {err}"
        );
    }

    #[test]
    fn composite_query_emits_every_token_variant() {
        // Every Tok variant appears at least once. Rel arrows stay as Dash/Lt/Gt.
        let src = r"MATCH (n:L {k: 'it\'s', i: 1, f: 2.5})-[r]->(m) WHERE NOT n.a = $p AND n.b <> 0 OR n.c < 1 AND n.d <= 2 AND n.e > 3 AND n.f >= 4.0 RETURN n, n.a AS x ORDER BY x ASC, n.b DESC SKIP 0 LIMIT 10";
        assert_eq!(
            lex(src).unwrap(),
            vec![
                Tok::Match,
                Tok::LParen,
                Tok::Ident("n".into()),
                Tok::Colon,
                Tok::Ident("L".into()),
                Tok::LBrace,
                Tok::Ident("k".into()),
                Tok::Colon,
                Tok::Str("it's".into()),
                Tok::Comma,
                Tok::Ident("i".into()),
                Tok::Colon,
                Tok::Int(1),
                Tok::Comma,
                Tok::Ident("f".into()),
                Tok::Colon,
                Tok::Float(2.5),
                Tok::RBrace,
                Tok::RParen,
                Tok::Dash,
                Tok::LBracket,
                Tok::Ident("r".into()),
                Tok::RBracket,
                Tok::Dash,
                Tok::Gt,
                Tok::LParen,
                Tok::Ident("m".into()),
                Tok::RParen,
                Tok::Where,
                Tok::Not,
                Tok::Ident("n".into()),
                Tok::Dot,
                Tok::Ident("a".into()),
                Tok::Eq,
                Tok::Param("p".into()),
                Tok::And,
                Tok::Ident("n".into()),
                Tok::Dot,
                Tok::Ident("b".into()),
                Tok::Ne,
                Tok::Int(0),
                Tok::Or,
                Tok::Ident("n".into()),
                Tok::Dot,
                Tok::Ident("c".into()),
                Tok::Lt,
                Tok::Int(1),
                Tok::And,
                Tok::Ident("n".into()),
                Tok::Dot,
                Tok::Ident("d".into()),
                Tok::Le,
                Tok::Int(2),
                Tok::And,
                Tok::Ident("n".into()),
                Tok::Dot,
                Tok::Ident("e".into()),
                Tok::Gt,
                Tok::Int(3),
                Tok::And,
                Tok::Ident("n".into()),
                Tok::Dot,
                Tok::Ident("f".into()),
                Tok::Ge,
                Tok::Float(4.0),
                Tok::Return,
                Tok::Ident("n".into()),
                Tok::Comma,
                Tok::Ident("n".into()),
                Tok::Dot,
                Tok::Ident("a".into()),
                Tok::As,
                Tok::Ident("x".into()),
                Tok::Order,
                Tok::By,
                Tok::Ident("x".into()),
                Tok::Asc,
                Tok::Comma,
                Tok::Ident("n".into()),
                Tok::Dot,
                Tok::Ident("b".into()),
                Tok::Desc,
                Tok::Skip,
                Tok::Int(0),
                Tok::Limit,
                Tok::Int(10),
            ]
        );
    }

    #[test]
    fn plus_and_slash_lex_as_tokens() {
        assert_eq!(lex("+").unwrap(), vec![Tok::Plus]);
        assert_eq!(lex("/").unwrap(), vec![Tok::Slash]);
        assert_eq!(
            lex("1 + 2 / 3").unwrap(),
            vec![Tok::Int(1), Tok::Plus, Tok::Int(2), Tok::Slash, Tok::Int(3)]
        );
    }

    #[test]
    fn garbage_bytes_are_err_not_panic() {
        let cases = ["@", "#", "MATCH @ n", "\"double\"", "\u{0}", "1.2.3@"];
        for src in cases {
            let result = std::panic::catch_unwind(|| lex(src));
            assert!(result.is_ok(), "lex({src:?}) panicked");
            let err = result
                .unwrap()
                .expect_err(&format!("lex({src:?}) must be Err"));
            assert!(
                err.contains("position"),
                "error must include position info, got: {err}"
            );
        }
    }
}