sqry-db 11.0.1

Salsa-style incremental computation engine for sqry semantic code search
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
//! Integration tests for [`sqry_db::planner::parse`] (DB13).
//!
//! Coverage targets (mirroring the DB13 task brief):
//!
//! - Every step keyword produces the expected [`PlanNode`]
//! - `kind:` / `visibility:` / `name:` fold into a single leading `NodeScan`
//!   when possible, preserving the by-kind index hot path
//! - Every [`Direction`] spelling (`forward`/`outgoing`/`out`,
//!   `reverse`/`incoming`/`in`, `both`)
//! - Every edge-kind keyword supported by the text syntax
//! - `impl:` and `implements:` both produce [`Predicate::Implements`]
//!   (spec §M8)
//! - Subqueries via parenthesised value
//! - Regex literals with every flag combination
//! - Bare globs, quoted strings (including escapes)
//! - Error variants: `UnexpectedChar`, `UnexpectedEnd`, `UnknownIdent`,
//!   `InvalidInteger`, `Build`

use sqry_core::graph::unified::bind::scope::arena::ScopeKind;
use sqry_core::graph::unified::edge::kind::EdgeKind;
use sqry_core::graph::unified::node::kind::NodeKind;
use sqry_core::schema::Visibility;

use sqry_db::planner::{
    Direction, ParseError, PlanNode, Predicate, PredicateValue, QueryPlan, SetOperation,
    StringPattern, parse_query,
};

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn parse(src: &str) -> QueryPlan {
    parse_query(src).unwrap_or_else(|err| panic!("parse({src:?}) failed: {err}"))
}

fn chain_steps(plan: QueryPlan) -> Vec<PlanNode> {
    let PlanNode::Chain { steps } = plan.root else {
        panic!("expected Chain root");
    };
    steps
}

// ---------------------------------------------------------------------------
// kind / visibility / name folding
// ---------------------------------------------------------------------------

#[test]
fn kind_only_produces_single_nodescan() {
    let steps = chain_steps(parse("kind:function"));
    assert_eq!(steps.len(), 1);
    match &steps[0] {
        PlanNode::NodeScan {
            kind: Some(NodeKind::Function),
            visibility,
            name_pattern,
        } => {
            assert!(visibility.is_none());
            assert!(name_pattern.is_none());
        }
        other => panic!("unexpected root: {other:?}"),
    }
}

#[test]
fn visibility_folds_into_leading_scan() {
    for (vis_text, expected) in [
        ("public", Visibility::Public),
        ("private", Visibility::Private),
    ] {
        let src = format!("kind:function visibility:{vis_text}");
        let steps = chain_steps(parse(&src));
        assert_eq!(steps.len(), 1, "{src}");
        match &steps[0] {
            PlanNode::NodeScan {
                kind: Some(NodeKind::Function),
                visibility: Some(vis),
                ..
            } => assert_eq!(*vis, expected),
            other => panic!("unexpected root in {src:?}: {other:?}"),
        }
    }
}

#[test]
fn name_pattern_folds_into_leading_scan() {
    let steps = chain_steps(parse("kind:function name:parse_*"));
    assert_eq!(steps.len(), 1);
    match &steps[0] {
        PlanNode::NodeScan {
            kind: Some(NodeKind::Function),
            name_pattern: Some(pat),
            ..
        } => {
            assert_eq!(pat.raw, "parse_*");
            assert_eq!(pat.mode, sqry_db::planner::MatchMode::Glob);
        }
        other => panic!("unexpected root: {other:?}"),
    }
}

#[test]
fn exact_name_pattern_is_chosen_when_no_glob_meta() {
    let steps = chain_steps(parse("kind:struct name:Parser"));
    assert_eq!(steps.len(), 1);
    match &steps[0] {
        PlanNode::NodeScan {
            name_pattern: Some(pat),
            ..
        } => {
            assert_eq!(pat.raw, "Parser");
            assert_eq!(pat.mode, sqry_db::planner::MatchMode::Exact);
        }
        other => panic!("unexpected root: {other:?}"),
    }
}

#[test]
fn kind_visibility_and_name_fold_together_into_single_scan() {
    let steps = chain_steps(parse("kind:function visibility:public name:parse_*"));
    assert_eq!(steps.len(), 1);
    match &steps[0] {
        PlanNode::NodeScan {
            kind: Some(NodeKind::Function),
            visibility: Some(Visibility::Public),
            name_pattern: Some(pat),
        } => {
            assert_eq!(pat.raw, "parse_*");
        }
        other => panic!("unexpected root: {other:?}"),
    }
}

// ---------------------------------------------------------------------------
// Attribute-only filters
// ---------------------------------------------------------------------------

#[test]
fn has_caller_and_has_callee() {
    let steps = chain_steps(parse("kind:function has:caller"));
    assert!(matches!(
        steps[1],
        PlanNode::Filter {
            predicate: Predicate::HasCaller
        }
    ));

    let steps = chain_steps(parse("kind:function has:callee"));
    assert!(matches!(
        steps[1],
        PlanNode::Filter {
            predicate: Predicate::HasCallee
        }
    ));
}

#[test]
fn unused_is_isunused_filter() {
    let steps = chain_steps(parse("kind:function unused"));
    assert!(matches!(
        steps[1],
        PlanNode::Filter {
            predicate: Predicate::IsUnused
        }
    ));
}

#[test]
fn in_path_glob_filter() {
    let steps = chain_steps(parse("kind:function in:src/api/**"));
    match &steps[1] {
        PlanNode::Filter {
            predicate: Predicate::InFile(path),
        } => {
            assert_eq!(path.as_str(), "src/api/**");
        }
        other => panic!("expected InFile, got {other:?}"),
    }
}

#[test]
fn scope_kind_filter() {
    for (text, expected) in [
        ("module", ScopeKind::Module),
        ("function", ScopeKind::Function),
        ("class", ScopeKind::Class),
        ("namespace", ScopeKind::Namespace),
        ("trait", ScopeKind::Trait),
        ("impl", ScopeKind::Impl),
    ] {
        let src = format!("kind:function scope:{text}");
        let steps = chain_steps(parse(&src));
        match &steps[1] {
            PlanNode::Filter {
                predicate: Predicate::InScope(kind),
            } => assert_eq!(*kind, expected, "{src}"),
            other => panic!("{src:?}: expected InScope, got {other:?}"),
        }
    }
}

// ---------------------------------------------------------------------------
// Relation predicates
// ---------------------------------------------------------------------------

#[test]
fn relation_predicates_with_pattern_values() {
    type Check = fn(&Predicate) -> bool;
    let cases: &[(&str, Check)] = &[
        ("callers", |p| matches!(p, Predicate::Callers(_))),
        ("callees", |p| matches!(p, Predicate::Callees(_))),
        ("imports", |p| matches!(p, Predicate::Imports(_))),
        ("exports", |p| matches!(p, Predicate::Exports(_))),
        ("implements", |p| matches!(p, Predicate::Implements(_))),
        ("impl", |p| matches!(p, Predicate::Implements(_))),
        ("references", |p| matches!(p, Predicate::References(_))),
    ];
    for (keyword, check) in cases {
        let src = format!("kind:function {keyword}:parse_expr");
        let steps = chain_steps(parse(&src));
        match &steps[1] {
            PlanNode::Filter { predicate } => {
                assert!(
                    check(predicate),
                    "keyword {keyword}: unexpected predicate {predicate:?}"
                );
            }
            other => panic!("{src:?}: expected Filter, got {other:?}"),
        }
    }
}

#[test]
fn callers_with_subquery_produces_plannode_subquery() {
    let steps = chain_steps(parse("kind:function callers:(kind:method)"));
    match &steps[1] {
        PlanNode::Filter {
            predicate: Predicate::Callers(PredicateValue::Subquery(inner)),
        } => match inner.as_ref() {
            PlanNode::Chain { steps: sub_steps } => {
                assert_eq!(sub_steps.len(), 1);
                assert!(matches!(
                    sub_steps[0],
                    PlanNode::NodeScan {
                        kind: Some(NodeKind::Method),
                        ..
                    }
                ));
            }
            other => panic!("expected Chain subquery, got {other:?}"),
        },
        other => panic!("expected Callers(Subquery), got {other:?}"),
    }
}

#[test]
fn nested_subquery_inside_outer_subquery() {
    let src = "kind:function callers:(kind:method callees:(kind:function))";
    let steps = chain_steps(parse(src));
    // Outer: Callers(Subquery(Chain[NodeScan(Method), Filter(Callees(Subquery(Chain[NodeScan(Function)])))]))
    let PlanNode::Filter {
        predicate: Predicate::Callers(PredicateValue::Subquery(outer_sub)),
    } = &steps[1]
    else {
        panic!("expected outer Callers(Subquery)");
    };
    let PlanNode::Chain { steps: outer_steps } = outer_sub.as_ref() else {
        panic!("outer subquery should be a Chain");
    };
    let PlanNode::Filter {
        predicate: Predicate::Callees(PredicateValue::Subquery(inner_sub)),
    } = &outer_steps[1]
    else {
        panic!("expected inner Callees(Subquery)");
    };
    match inner_sub.as_ref() {
        PlanNode::Chain { steps: inner_steps } => {
            assert!(matches!(
                inner_steps[0],
                PlanNode::NodeScan {
                    kind: Some(NodeKind::Function),
                    ..
                }
            ));
        }
        other => panic!("inner subquery Chain expected, got {other:?}"),
    }
}

#[test]
fn references_with_regex_and_flags() {
    let plan = parse("kind:function references ~= /handle_.*/ims");
    let steps = chain_steps(plan);
    match &steps[1] {
        PlanNode::Filter {
            predicate: Predicate::References(PredicateValue::Regex(re)),
        } => {
            assert_eq!(re.pattern, "handle_.*");
            assert!(re.flags.case_insensitive);
            assert!(re.flags.multiline);
            assert!(re.flags.dot_all);
        }
        other => panic!("expected References(Regex), got {other:?}"),
    }
}

#[test]
fn references_colon_still_accepts_pattern_form() {
    let steps = chain_steps(parse("kind:function references:parse_expr"));
    match &steps[1] {
        PlanNode::Filter {
            predicate: Predicate::References(PredicateValue::Pattern(pat)),
        } => {
            assert_eq!(pat.raw, "parse_expr");
        }
        other => panic!("expected References(Pattern), got {other:?}"),
    }
}

#[test]
fn quoted_string_value_preserves_spaces_and_escapes() {
    let steps = chain_steps(parse(r#"kind:function callers:"has space""#));
    match &steps[1] {
        PlanNode::Filter {
            predicate: Predicate::Callers(PredicateValue::Pattern(pat)),
        } => assert_eq!(pat.raw, "has space"),
        other => panic!("unexpected: {other:?}"),
    }

    let steps = chain_steps(parse(r#"kind:function callers:"with\"quote""#));
    match &steps[1] {
        PlanNode::Filter {
            predicate: Predicate::Callers(PredicateValue::Pattern(pat)),
        } => assert_eq!(pat.raw, "with\"quote"),
        other => panic!("unexpected: {other:?}"),
    }
}

// ---------------------------------------------------------------------------
// Traverse
// ---------------------------------------------------------------------------

#[test]
fn traverse_with_every_direction_alias() {
    let cases = [
        ("forward", Direction::Forward),
        ("outgoing", Direction::Forward),
        ("out", Direction::Forward),
        ("reverse", Direction::Reverse),
        ("incoming", Direction::Reverse),
        ("in", Direction::Reverse),
        ("both", Direction::Both),
    ];
    for (text, expected) in cases {
        let src = format!("kind:function traverse:{text}(calls,2)");
        let steps = chain_steps(parse(&src));
        match &steps[1] {
            PlanNode::EdgeTraversal {
                direction,
                max_depth,
                ..
            } => {
                assert_eq!(*direction, expected, "{src}");
                assert_eq!(*max_depth, 2);
            }
            other => panic!("{src}: expected EdgeTraversal, got {other:?}"),
        }
    }
}

#[test]
fn traverse_accepts_every_supported_edge_kind() {
    // Each edge kind is compared via discriminant; we only need to check the
    // discriminant matches what the text parser should have built.
    let cases = [
        (
            "calls",
            std::mem::discriminant(&EdgeKind::Calls {
                argument_count: 0,
                is_async: false,
            }),
        ),
        ("references", std::mem::discriminant(&EdgeKind::References)),
        (
            "imports",
            std::mem::discriminant(&EdgeKind::Imports {
                alias: None,
                is_wildcard: false,
            }),
        ),
        ("implements", std::mem::discriminant(&EdgeKind::Implements)),
        ("inherits", std::mem::discriminant(&EdgeKind::Inherits)),
        ("defines", std::mem::discriminant(&EdgeKind::Defines)),
        ("contains", std::mem::discriminant(&EdgeKind::Contains)),
    ];
    for (text, expected_disc) in cases {
        let src = format!("kind:function traverse:forward({text},1)");
        let steps = chain_steps(parse(&src));
        match &steps[1] {
            PlanNode::EdgeTraversal {
                edge_kind: Some(kind),
                ..
            } => {
                assert_eq!(std::mem::discriminant(kind), expected_disc, "{src}");
            }
            other => panic!("{src}: expected EdgeTraversal with edge_kind, got {other:?}"),
        }
    }
}

// ---------------------------------------------------------------------------
// Full pipeline: the design-doc example round-trips through the parser.
// ---------------------------------------------------------------------------

#[test]
fn design_doc_example_roundtrips() {
    let plan = parse("kind:function has:caller traverse:reverse(calls,3) in:src/api/**");
    let steps = chain_steps(plan);
    assert_eq!(steps.len(), 4);
    assert!(matches!(
        steps[0],
        PlanNode::NodeScan {
            kind: Some(NodeKind::Function),
            ..
        }
    ));
    assert!(matches!(
        steps[1],
        PlanNode::Filter {
            predicate: Predicate::HasCaller
        }
    ));
    match &steps[2] {
        PlanNode::EdgeTraversal {
            direction: Direction::Reverse,
            max_depth: 3,
            edge_kind: Some(ek),
        } => {
            assert_eq!(
                std::mem::discriminant(ek),
                std::mem::discriminant(&EdgeKind::Calls {
                    argument_count: 0,
                    is_async: false,
                })
            );
        }
        other => panic!("unexpected step[2]: {other:?}"),
    }
    match &steps[3] {
        PlanNode::Filter {
            predicate: Predicate::InFile(path),
        } => assert_eq!(path.as_str(), "src/api/**"),
        other => panic!("unexpected step[3]: {other:?}"),
    }
}

// ---------------------------------------------------------------------------
// Error cases
// ---------------------------------------------------------------------------

#[test]
fn empty_query_fails_build() {
    let err = parse_query("").unwrap_err();
    assert!(matches!(err, ParseError::Build(_)));
}

#[test]
fn whitespace_only_query_fails_build() {
    let err = parse_query("   \n\t  ").unwrap_err();
    assert!(matches!(err, ParseError::Build(_)));
}

#[test]
fn unknown_step_keyword_reports_unknown_ident() {
    let err = parse_query("kind:function notakeyword:foo").unwrap_err();
    match err {
        ParseError::UnknownIdent { kind, value, .. } => {
            assert_eq!(kind, "step keyword");
            assert_eq!(value, "notakeyword");
        }
        other => panic!("unexpected: {other:?}"),
    }
}

#[test]
fn unknown_node_kind_reports_unknown_ident() {
    let err = parse_query("kind:gadget").unwrap_err();
    match err {
        ParseError::UnknownIdent { kind, value, .. } => {
            assert_eq!(kind, "node kind");
            assert_eq!(value, "gadget");
        }
        other => panic!("unexpected: {other:?}"),
    }
}

#[test]
fn unknown_scope_kind_reports_unknown_ident() {
    let err = parse_query("kind:function scope:submarine").unwrap_err();
    assert!(matches!(
        err,
        ParseError::UnknownIdent {
            kind: "scope kind",
            ..
        }
    ));
}

#[test]
fn unknown_edge_kind_reports_unknown_ident() {
    let err = parse_query("kind:function traverse:forward(rollercoaster,2)").unwrap_err();
    assert!(matches!(
        err,
        ParseError::UnknownIdent {
            kind: "edge kind",
            ..
        }
    ));
}

#[test]
fn unknown_direction_reports_unknown_ident() {
    let err = parse_query("kind:function traverse:sideways(calls,2)").unwrap_err();
    assert!(matches!(
        err,
        ParseError::UnknownIdent {
            kind: "traversal direction",
            ..
        }
    ));
}

#[test]
fn invalid_has_target_reports_unknown_ident() {
    let err = parse_query("kind:function has:parent").unwrap_err();
    assert!(matches!(err, ParseError::UnknownIdent { .. }));
}

#[test]
fn missing_colon_after_kind_errors() {
    let err = parse_query("kind function").unwrap_err();
    match err {
        ParseError::UnexpectedChar { expected, .. } => {
            assert_eq!(expected, "':' after 'kind'");
        }
        other => panic!("unexpected: {other:?}"),
    }
}

#[test]
fn unterminated_quoted_string_errors() {
    let err = parse_query(r#"kind:function callers:"unclosed"#).unwrap_err();
    assert!(matches!(err, ParseError::UnexpectedEnd { .. }));
}

#[test]
fn unterminated_regex_errors() {
    let err = parse_query("kind:function references ~= /unclosed").unwrap_err();
    assert!(matches!(err, ParseError::UnexpectedEnd { .. }));
}

#[test]
fn unterminated_subquery_errors() {
    let err = parse_query("kind:function callers:(kind:method").unwrap_err();
    assert!(matches!(
        err,
        ParseError::UnexpectedChar { .. } | ParseError::UnexpectedEnd { .. }
    ));
}

#[test]
fn non_digit_depth_reports_unexpectedchar() {
    let err = parse_query("kind:function traverse:forward(calls,notnumber)").unwrap_err();
    assert!(matches!(err, ParseError::UnexpectedChar { .. }));
}

#[test]
fn zero_depth_surfaces_build_error() {
    let err = parse_query("kind:function traverse:forward(calls,0)").unwrap_err();
    assert!(matches!(err, ParseError::Build(_)));
}

// ---------------------------------------------------------------------------
// Determinism: same input, same plan (hash stability).
// ---------------------------------------------------------------------------

#[test]
fn parsing_identical_queries_yields_equal_plans() {
    let a = parse("kind:function has:caller in:src/**");
    let b = parse("kind:function has:caller in:src/**");
    assert_eq!(a, b);
}

// ---------------------------------------------------------------------------
// SetOperation reachability guard — the text parser does not currently expose
// set operations directly; this test documents that by attempting a textual
// union and ensuring it parses as a single filter rather than silently
// producing a SetOp. When a future text-syntax extension adds `|` / `UNION`
// tokens, this test should be updated to reflect the new behaviour.
// ---------------------------------------------------------------------------

#[test]
fn bar_pipe_character_is_rejected_for_now() {
    let err = parse_query("kind:function | kind:method").unwrap_err();
    assert!(matches!(
        err,
        ParseError::UnexpectedChar { .. } | ParseError::UnknownIdent { .. }
    ));
    // Silence unused SetOperation import while this test is a negative guard.
    let _ = SetOperation::Union;
    let _ = StringPattern::exact("sentinel");
}