nodedb-sql 0.6.0

SQL parser, planner, and optimizer for NodeDB
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
// SPDX-License-Identifier: Apache-2.0

//! Graph DSL entry point.

use super::super::statement::{GraphStmt, NodedbStatement};
use super::{edge_predicate, tokenizer, variants};
use crate::error::SqlError;

/// Parse a graph DSL statement.
///
/// `None` means the input is not graph DSL and the caller should keep trying
/// other statement families. `Some(Err(..))` means it *is* graph DSL and is
/// malformed — the two must stay apart, because returning `None` for a
/// malformed statement sends it to the general SQL parser, which can only
/// report that `GRAPH` is not an SQL statement and says nothing about the
/// clause actually at fault.
pub fn try_parse(sql: &str) -> Option<Result<NodedbStatement, SqlError>> {
    let trimmed = sql.trim();
    let upper = trimmed.to_ascii_uppercase();

    if upper.starts_with("MATCH ") || upper.starts_with("OPTIONAL MATCH ") {
        return Some(Ok(NodedbStatement::Graph(GraphStmt::MatchQuery {
            body: trimmed.to_string(),
        })));
    }

    if !upper.starts_with("GRAPH ") {
        return None;
    }

    Some(parse_graph(trimmed))
}

/// Parse a statement known to start with `GRAPH `. Comments are blanked
/// first. A trailing `EDGE WHERE` predicate splits off next, so the clause
/// text before it tokenizes alone.
fn parse_graph(sql: &str) -> Result<NodedbStatement, SqlError> {
    let blanked = tokenizer::blank_comments(sql);
    let trimmed = blanked.trim();
    let upper = trimmed.to_ascii_uppercase();
    let upper = upper.as_str();
    let (head, predicate_text) = edge_predicate::split_edge_where(trimmed)?;
    let predicate = match predicate_text {
        Some(text) => edge_predicate::parse_edge_predicate(text)?,
        None => Vec::new(),
    };
    let toks = tokenizer::tokenize(head);

    if upper.starts_with("GRAPH TRAVERSE ") {
        return variants::parse_traverse(&toks, predicate);
    }
    if upper.starts_with("GRAPH PATH ") {
        return variants::parse_path(&toks, predicate);
    }
    if predicate_text.is_some() {
        return Err(SqlError::Parse {
            detail: format!(
                "{} does not accept EDGE WHERE: only GRAPH TRAVERSE and GRAPH PATH filter on \
                 edge properties",
                graph_command(upper)
            ),
        });
    }

    if upper.starts_with("GRAPH INSERT EDGE ") {
        variants::parse_insert_edge(&toks)
    } else if upper.starts_with("GRAPH DELETE EDGE ") {
        variants::parse_delete_edge(&toks)
    } else if upper.starts_with("GRAPH LABEL ") {
        variants::parse_set_labels(&toks, false)
    } else if upper.starts_with("GRAPH UNLABEL ") {
        variants::parse_set_labels(&toks, true)
    } else if upper.starts_with("GRAPH NEIGHBORS ") {
        variants::parse_neighbors(&toks)
    } else if upper.starts_with("GRAPH ALGO ") {
        variants::parse_algo(&toks)
    } else if upper.starts_with("GRAPH RAG FUSION ") {
        variants::parse_rag_fusion(&toks)
    } else {
        // Starts with `GRAPH ` but names no known command. Still graph DSL,
        // so report it here rather than letting the SQL parser guess.
        Err(SqlError::Parse {
            detail: "unrecognised GRAPH command".to_owned(),
        })
    }
}

/// The command words of a `GRAPH` statement, for an error message:
/// `GRAPH NEIGHBORS`, `GRAPH INSERT EDGE`, `GRAPH RAG FUSION`.
fn graph_command(upper: &str) -> String {
    const MULTI_WORD: [&str; 3] = ["GRAPH INSERT EDGE", "GRAPH DELETE EDGE", "GRAPH RAG FUSION"];
    if let Some(command) = MULTI_WORD.iter().find(|c| upper.starts_with(*c)) {
        return (*command).to_owned();
    }
    upper
        .split_whitespace()
        .take(2)
        .collect::<Vec<_>>()
        .join(" ")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ddl_ast::statement::{GraphDirection, GraphProperties};

    /// Parse a statement that must be both graph DSL and well-formed.
    fn parsed(sql: &str) -> NodedbStatement {
        try_parse(sql)
            .expect("input is graph DSL")
            .expect("statement is well-formed")
    }

    #[test]
    fn parse_graph_insert_edge_keyword_shaped_ids() {
        let stmt = parsed("GRAPH INSERT EDGE IN 'myedges' FROM 'TO' TO 'FROM' TYPE 'LABEL'");
        match stmt {
            NodedbStatement::Graph(GraphStmt::GraphInsertEdge {
                collection,
                src,
                dst,
                label,
                properties,
            }) => {
                assert_eq!(collection, "myedges");
                assert_eq!(src, "TO");
                assert_eq!(dst, "FROM");
                assert_eq!(label, "LABEL");
                assert_eq!(properties, GraphProperties::None);
            }
            other => panic!("expected GraphInsertEdge, got {other:?}"),
        }
    }

    #[test]
    fn parse_graph_delete_edge_with_collection() {
        let stmt = parsed("GRAPH DELETE EDGE IN 'myedges' FROM 'a' TO 'b' TYPE 'l'");
        match stmt {
            NodedbStatement::Graph(GraphStmt::GraphDeleteEdge {
                collection,
                src,
                dst,
                label,
            }) => {
                assert_eq!(collection, "myedges");
                assert_eq!(src, "a");
                assert_eq!(dst, "b");
                assert_eq!(label, "l");
            }
            other => panic!("expected GraphDeleteEdge, got {other:?}"),
        }
    }

    /// A missing required clause is a malformed graph statement, not a
    /// non-graph one. `None` here would send it to the SQL parser, which
    /// reports only that `GRAPH` is not SQL.
    #[test]
    fn parse_graph_insert_edge_missing_collection_names_the_clause() {
        let error = try_parse("GRAPH INSERT EDGE FROM 'a' TO 'b' TYPE 'l'")
            .expect("input is graph DSL")
            .expect_err("missing IN <collection> must not produce a statement");
        assert!(
            error.to_string().contains("IN <collection>"),
            "the error must name the missing clause: {error}"
        );
    }

    #[test]
    fn parse_graph_insert_edge_with_object_properties() {
        let stmt = parsed(
            "GRAPH INSERT EDGE IN 'edges' FROM 'a' TO 'b' TYPE 'l' PROPERTIES { note: '} DEPTH 999' }",
        );
        match stmt {
            NodedbStatement::Graph(GraphStmt::GraphInsertEdge {
                collection,
                properties,
                ..
            }) => {
                assert_eq!(collection, "edges");
                match properties {
                    GraphProperties::Object(s) => assert!(s.contains("} DEPTH 999")),
                    other => panic!("expected Object properties, got {other:?}"),
                }
            }
            other => panic!("expected GraphInsertEdge, got {other:?}"),
        }
    }

    #[test]
    fn parse_graph_traverse_keyword_substring_id() {
        let stmt =
            parsed("GRAPH TRAVERSE IN 'kw' FROM 'node_with_DEPTH_in_name' DEPTH 2 LABEL 'l'");
        match stmt {
            NodedbStatement::Graph(GraphStmt::GraphTraverse {
                collection,
                start,
                depth,
                ..
            }) => {
                assert_eq!(collection, "kw");
                assert_eq!(start, "node_with_DEPTH_in_name");
                assert_eq!(depth, 2);
            }
            other => panic!("expected GraphTraverse, got {other:?}"),
        }
    }

    #[test]
    fn parse_graph_path() {
        let stmt = parsed("GRAPH PATH IN 'docs' FROM 'a' TO 'b' MAX_DEPTH 5 LABEL 'l'");
        match stmt {
            NodedbStatement::Graph(GraphStmt::GraphPath {
                collection,
                src,
                dst,
                max_depth,
                edge_labels,
                edge_predicate,
            }) => {
                assert!(edge_predicate.is_empty());
                assert_eq!(collection, "docs");
                assert_eq!(src, "a");
                assert_eq!(dst, "b");
                assert_eq!(max_depth, 5);
                assert_eq!(edge_labels, vec!["l".to_string()]);
            }
            other => panic!("expected GraphPath, got {other:?}"),
        }
    }

    fn labels_of(sql: &str) -> Vec<String> {
        match parsed(sql) {
            NodedbStatement::Graph(
                GraphStmt::GraphTraverse { edge_labels, .. }
                | GraphStmt::GraphNeighbors { edge_labels, .. }
                | GraphStmt::GraphPath { edge_labels, .. },
            ) => edge_labels,
            other => panic!("expected a graph walk statement, got {other:?}"),
        }
    }

    fn ab() -> Vec<String> {
        vec!["a".to_string(), "b".to_string()]
    }

    #[test]
    fn label_list_parses_on_every_walk_statement() {
        assert_eq!(
            labels_of("GRAPH TRAVERSE IN 'c' FROM 'x' DEPTH 2 LABEL 'a', 'b' DIRECTION out"),
            ab()
        );
        assert_eq!(
            labels_of("GRAPH NEIGHBORS IN 'c' OF 'x' LABEL 'a', 'b' DIRECTION both"),
            ab()
        );
        assert_eq!(
            labels_of("GRAPH PATH IN 'c' FROM 'x' TO 'y' MAX_DEPTH 3 LABEL 'a', 'b'"),
            ab()
        );
    }

    #[test]
    fn parenthesised_label_list_parses() {
        assert_eq!(
            labels_of("GRAPH TRAVERSE IN 'c' FROM 'x' LABEL ('a','b')"),
            ab()
        );
        assert_eq!(
            labels_of("GRAPH NEIGHBORS IN 'c' OF 'x' LABEL ('a', 'b')"),
            ab()
        );
        assert_eq!(
            labels_of("GRAPH PATH IN 'c' FROM 'x' TO 'y' LABEL ('a','b')"),
            ab()
        );
    }

    #[test]
    fn omitted_label_clause_is_an_empty_set() {
        assert!(labels_of("GRAPH TRAVERSE IN 'c' FROM 'x' DEPTH 2").is_empty());
        assert!(labels_of("GRAPH NEIGHBORS IN 'c' OF 'x'").is_empty());
        assert!(labels_of("GRAPH PATH IN 'c' FROM 'x' TO 'y'").is_empty());
    }

    #[test]
    fn label_clause_without_quoted_labels_is_refused() {
        for sql in [
            "GRAPH TRAVERSE IN 'c' FROM 'x' LABEL DIRECTION out",
            "GRAPH TRAVERSE IN 'c' FROM 'x' LABEL knows",
            "GRAPH NEIGHBORS IN 'c' OF 'x' LABEL",
            "GRAPH PATH IN 'c' FROM 'x' TO 'y' LABEL knows",
        ] {
            let error = try_parse(sql)
                .expect("graph DSL")
                .expect_err("a LABEL clause with no quoted label must be refused");
            assert!(
                error.to_string().contains("LABEL"),
                "the error must name the LABEL clause: {error}"
            );
        }
    }

    #[test]
    fn quoted_keyword_label_does_not_shadow_the_keyword() {
        let stmt = parsed("GRAPH TRAVERSE IN 'c' FROM 'x' LABEL 'DIRECTION', 'b' DIRECTION in");
        match stmt {
            NodedbStatement::Graph(GraphStmt::GraphTraverse {
                edge_labels,
                direction,
                ..
            }) => {
                assert_eq!(edge_labels, vec!["DIRECTION".to_string(), "b".to_string()]);
                assert_eq!(direction, GraphDirection::In);
            }
            other => panic!("expected GraphTraverse, got {other:?}"),
        }
    }

    #[test]
    fn parse_graph_labels_list() {
        let stmt = parsed("GRAPH LABEL 'alice' AS 'Person', 'User'");
        match stmt {
            NodedbStatement::Graph(GraphStmt::GraphSetLabels {
                node_id,
                labels,
                remove,
            }) => {
                assert_eq!(node_id, "alice");
                assert_eq!(labels, vec!["Person".to_string(), "User".to_string()]);
                assert!(!remove);
            }
            other => panic!("expected GraphSetLabels, got {other:?}"),
        }
    }

    #[test]
    fn parse_graph_algo_pagerank() {
        let stmt = parsed("GRAPH ALGO PAGERANK ON users ITERATIONS 5 DAMPING 0.85");
        match stmt {
            NodedbStatement::Graph(GraphStmt::GraphAlgo {
                algorithm,
                collection,
                damping,
                max_iterations,
                ..
            }) => {
                assert_eq!(algorithm, "PAGERANK");
                assert_eq!(collection, "users");
                assert_eq!(damping, Some(0.85));
                assert_eq!(max_iterations, Some(5));
            }
            other => panic!("expected GraphAlgo, got {other:?}"),
        }
    }

    #[test]
    fn parse_graph_algo_personalization() {
        let stmt = parsed(
            r#"GRAPH ALGO PAGERANK ON 'users' DAMPING 0.9 PERSONALIZATION {"alice": 1.0, "bob": 0.5}"#,
        );
        match stmt {
            NodedbStatement::Graph(GraphStmt::GraphAlgo {
                algorithm,
                collection,
                damping,
                personalization,
                ..
            }) => {
                assert_eq!(algorithm, "PAGERANK");
                assert_eq!(collection, "users");
                assert_eq!(damping, Some(0.9));
                let raw = personalization.expect("personalization object present");
                assert!(raw.contains("alice"));
                assert!(raw.contains("bob"));
                // Round-trips as a JSON node→weight map.
                let map: std::collections::HashMap<String, f64> = sonic_rs::from_str(&raw).unwrap();
                assert_eq!(map.get("alice"), Some(&1.0));
                assert_eq!(map.get("bob"), Some(&0.5));
            }
            other => panic!("expected GraphAlgo, got {other:?}"),
        }
    }

    #[test]
    fn parse_match_query_captures_raw() {
        let stmt = parsed("MATCH (x)-[:l]->(y) RETURN x, y");
        match stmt {
            NodedbStatement::Graph(GraphStmt::MatchQuery { body }) => {
                assert!(body.starts_with("MATCH"));
            }
            other => panic!("expected MatchQuery, got {other:?}"),
        }
    }

    #[test]
    fn non_graph_returns_none() {
        assert!(try_parse("SELECT * FROM users").is_none());
        assert!(try_parse("CREATE COLLECTION users").is_none());
    }

    // ── GraphRagFusion parser tests ──────────────────────────────────────

    #[test]
    fn parse_rag_fusion_full_syntax() {
        let stmt = parsed(
            "GRAPH RAG FUSION ON entities \
             QUERY ARRAY[0.1, 0.2, 0.3] \
             VECTOR_TOP_K 50 \
             EXPANSION_DEPTH 2 \
             EDGE_LABEL 'related_to' \
             FINAL_TOP_K 10 \
             RRF_K (60.0, 35.0)",
        );
        match stmt {
            NodedbStatement::Graph(GraphStmt::GraphRagFusion { collection, params }) => {
                assert_eq!(collection, "entities");
                let v = params.query_vector.expect("QUERY ARRAY parsed");
                assert_eq!(v.len(), 3);
                assert!((v[0] - 0.1f32).abs() < 1e-5);
                assert_eq!(params.vector_top_k, Some(50));
                assert_eq!(params.expansion_depth, Some(2));
                assert_eq!(params.edge_label.as_deref(), Some("related_to"));
                assert_eq!(params.final_top_k, Some(10));
                let (k1, k2) = params.rrf_k.unwrap();
                assert!((k1 - 60.0).abs() < 1e-10);
                assert!((k2 - 35.0).abs() < 1e-10);
            }
            other => panic!("expected GraphRagFusion, got {other:?}"),
        }
    }

    #[test]
    fn parse_rag_fusion_minimal_defaults_to_none() {
        let stmt = parsed("GRAPH RAG FUSION ON mycol QUERY ARRAY[1.0, 0.0]");
        match stmt {
            NodedbStatement::Graph(GraphStmt::GraphRagFusion { collection, params }) => {
                assert_eq!(collection, "mycol");
                assert!(params.query_vector.is_some());
                assert_eq!(params.vector_top_k, None);
                assert_eq!(params.expansion_depth, None);
                assert_eq!(params.edge_label, None);
                assert_eq!(params.final_top_k, None);
                assert_eq!(params.rrf_k, None);
                assert_eq!(params.vector_field, None);
                assert_eq!(params.direction, None);
                assert_eq!(params.max_visited, None);
            }
            other => panic!("expected GraphRagFusion, got {other:?}"),
        }
    }

    #[test]
    fn parse_rag_fusion_direction_and_max_visited() {
        let stmt =
            parsed("GRAPH RAG FUSION ON col QUERY ARRAY[0.5] DIRECTION both MAX_VISITED 500");
        match stmt {
            NodedbStatement::Graph(GraphStmt::GraphRagFusion { params, .. }) => {
                assert_eq!(params.direction, Some(GraphDirection::Both));
                assert_eq!(params.max_visited, Some(500));
            }
            other => panic!("expected GraphRagFusion, got {other:?}"),
        }
    }

    #[test]
    fn parse_rag_fusion_vector_field_is_captured() {
        let stmt = parsed("GRAPH RAG FUSION ON col QUERY ARRAY[0.5] VECTOR_FIELD 'embedding'");
        match stmt {
            NodedbStatement::Graph(GraphStmt::GraphRagFusion { params, .. }) => {
                assert_eq!(params.vector_field.as_deref(), Some("embedding"));
            }
            other => panic!("expected GraphRagFusion, got {other:?}"),
        }
    }

    #[test]
    fn parse_rag_fusion_rrf_k_both_values_captured() {
        let stmt = parsed("GRAPH RAG FUSION ON col QUERY ARRAY[0.5] RRF_K (1.0, 99.5)");
        match stmt {
            NodedbStatement::Graph(GraphStmt::GraphRagFusion { params, .. }) => {
                let (k1, k2) = params.rrf_k.expect("RRF_K must be parsed");
                assert!((k1 - 1.0).abs() < 1e-10, "vector_k must be 1.0, got {k1}");
                assert!((k2 - 99.5).abs() < 1e-10, "graph_k must be 99.5, got {k2}");
            }
            other => panic!("expected GraphRagFusion, got {other:?}"),
        }
    }

    #[test]
    fn parse_rag_fusion_missing_collection_names_the_clause() {
        let error = try_parse("GRAPH RAG FUSION QUERY ARRAY[0.1] VECTOR_TOP_K 5")
            .expect("input is graph DSL")
            .expect_err("missing ON <collection> must not produce a statement");
        assert!(
            error.to_string().contains("ON <collection>"),
            "the error must name the missing clause: {error}"
        );
    }

    /// The two states `try_parse` must never confuse.
    #[test]
    fn malformed_graph_is_distinguishable_from_non_graph() {
        assert!(
            try_parse("SELECT * FROM users").is_none(),
            "non-graph input must stay None so other families get a turn"
        );
        assert!(
            matches!(try_parse("GRAPH TRAVERSE FROM 'b'"), Some(Err(_))),
            "malformed graph input must report its own error"
        );
    }

    /// An unrecognised direction is refused; an omitted one still defaults.
    #[test]
    fn direction_vocabulary_is_closed() {
        for word in ["IN", "OUT", "BOTH", "both"] {
            let sql = format!("GRAPH TRAVERSE IN 'c' FROM 'a' DIRECTION {word}");
            assert!(
                try_parse(&sql).expect("graph DSL").is_ok(),
                "'{word}' is documented vocabulary and must parse"
            );
        }
        for word in ["INBOUND", "BANANA"] {
            let sql = format!("GRAPH TRAVERSE IN 'c' FROM 'a' DIRECTION {word}");
            let error = try_parse(&sql)
                .expect("graph DSL")
                .expect_err("'{word}' is outside the vocabulary");
            assert!(
                error.to_string().contains(word),
                "the error must name the offending value: {error}"
            );
        }
        assert!(
            try_parse("GRAPH TRAVERSE IN 'c' FROM 'a'")
                .expect("graph DSL")
                .is_ok(),
            "an omitted DIRECTION must keep defaulting"
        );
    }

    // ── Unconsumed and misplaced tokens ───────────────────────────────────

    fn parse_error(sql: &str) -> String {
        try_parse(sql)
            .expect("input is graph DSL")
            .expect_err("statement must be refused")
            .to_string()
    }

    #[test]
    fn misspelled_clause_keyword_is_named() {
        let error = parse_error("GRAPH TRAVERSE FROM 1 DEPTS 3 IN g");
        assert!(
            error.contains("unexpected token 'DEPTS' in GRAPH TRAVERSE"),
            "{error}"
        );
        let error = parse_error("GRAPH NEIGHBORS OF 1 LABELS 'follows' IN g");
        assert!(
            error.contains("unexpected token 'LABELS' in GRAPH NEIGHBORS"),
            "{error}"
        );
    }

    #[test]
    fn stray_tokens_are_refused() {
        for (sql, token) in [
            ("GRAPH TRAVERSE FROM 1 SIDEWAYS IN g", "SIDEWAYS"),
            ("GRAPH TRAVERSE FROM 1 BANANA PHONE IN g", "BANANA"),
            ("GRAPH INSERT EDGE IN 'e' FROM 'a' TO 'b' TYP 'x'", "TYP"),
            (
                "GRAPH DELETE EDGE IN 'e' FROM 'a' TO 'b' TYPE 'x' EXTRA",
                "EXTRA",
            ),
            ("GRAPH PATH IN 'g' FROM 'a' TO 'b' MAXDEPTH 3", "MAXDEPTH"),
            ("GRAPH LABEL 'a' AS 'P' EXTRA", "EXTRA"),
            ("GRAPH ALGO PAGERANK ON g ITERATION 5", "ITERATION"),
            (
                "GRAPH RAG FUSION ON e QUERY ARRAY[0.1] VECTOR_TOPK 5",
                "VECTOR_TOPK",
            ),
            ("GRAPH TRAVERSE IN 'g' FROM 'a' ]", "]"),
        ] {
            let error = parse_error(sql);
            assert!(
                error.contains(&format!("unexpected token '{token}'")),
                "{sql}: {error}"
            );
        }
    }

    #[test]
    fn stray_string_at_clause_position_is_refused() {
        let error = parse_error("GRAPH TRAVERSE IN 'g' FROM 'a' 'b'");
        assert!(error.contains("unexpected string 'b'"), "{error}");
    }

    #[test]
    fn repeated_clause_is_refused() {
        let error = parse_error("GRAPH TRAVERSE IN 'g' FROM 'a' DEPTH 1 DEPTH 3");
        assert!(
            error.contains("DEPTH given more than once in GRAPH TRAVERSE"),
            "{error}"
        );
    }

    #[test]
    fn direction_value_in_does_not_shadow_in_clause() {
        match parsed("GRAPH TRAVERSE FROM 'b' DIRECTION in IN 'g'") {
            NodedbStatement::Graph(GraphStmt::GraphTraverse {
                collection,
                start,
                direction,
                ..
            }) => {
                assert_eq!(collection, "g");
                assert_eq!(start, "b");
                assert_eq!(direction, GraphDirection::In);
            }
            other => panic!("expected GraphTraverse, got {other:?}"),
        }
    }

    #[test]
    fn non_numeric_clause_values_are_refused() {
        let error = parse_error("GRAPH ALGO PAGERANK ON g DAMPING abc");
        assert!(error.contains("DAMPING must be a number"), "{error}");
        let error = parse_error("GRAPH TRAVERSE IN 'g' FROM 'a' DEPTH '3'");
        assert!(
            error.contains("DEPTH must be a non-negative integer"),
            "{error}"
        );
        let error = parse_error("GRAPH RAG FUSION ON e QUERY ARRAY[0.1, x]");
        assert!(error.contains("'x'"), "{error}");
    }

    #[test]
    fn comments_are_not_tokens() {
        match parsed("GRAPH TRAVERSE IN 'g' /* note */ FROM 'a' -- trailing note") {
            NodedbStatement::Graph(GraphStmt::GraphTraverse {
                collection, start, ..
            }) => {
                assert_eq!(collection, "g");
                assert_eq!(start, "a");
            }
            other => panic!("expected GraphTraverse, got {other:?}"),
        }
    }

    #[test]
    fn quoted_identifier_is_a_value() {
        match parsed(r#"GRAPH NEIGHBORS IN "My Graph" OF 'a'"#) {
            NodedbStatement::Graph(GraphStmt::GraphNeighbors { collection, .. }) => {
                assert_eq!(collection, "My Graph");
            }
            other => panic!("expected GraphNeighbors, got {other:?}"),
        }
    }

    #[test]
    fn rag_fusion_bm25_field_is_the_on_after_bm25() {
        let params_of = |sql: &str| match parsed(sql) {
            NodedbStatement::Graph(GraphStmt::GraphRagFusion { collection, params }) => {
                assert_eq!(collection, "entities");
                params
            }
            other => panic!("expected GraphRagFusion, got {other:?}"),
        };
        let params = params_of(
            "GRAPH RAG FUSION ON entities QUERY ARRAY[0.1] BM25 'q' ON 'body' \
             RRF_K (1.0, 2.0, 3.0)",
        );
        assert_eq!(params.bm25_query.as_deref(), Some("q"));
        assert_eq!(params.bm25_field.as_deref(), Some("body"));
        assert_eq!(params.rrf_k_triple, Some((1.0, 2.0, 3.0)));
        let params = params_of("GRAPH RAG FUSION ON entities QUERY ARRAY[0.1]");
        assert_eq!(params.bm25_field, None);
    }
}