lora-analyzer 0.11.1

Semantic analysis for parsed Cypher queries in LoraDB.
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
use super::*;
use crate::errors::SemanticError;
use crate::resolved::{LiteralValue, ResolvedClause, ResolvedExpr};
use lora_parser::parse_query;
use lora_store::{GraphStorageMut, InMemoryGraph, Properties};

#[test]
fn create_allows_new_relationship_type_when_graph_is_not_empty() {
    let mut graph = InMemoryGraph::new();
    let alice = graph.create_node(vec!["User".into()], Properties::new());
    let bob = graph.create_node(vec!["User".into()], Properties::new());
    let _carol = graph.create_node(vec!["User".into()], Properties::new());

    graph
        .create_relationship(alice.id, bob.id, "FOLLOWS", Properties::new())
        .unwrap();

    let doc = parse_query(
        "MATCH (a:User {id: 2}), (b:User {id: 3}) CREATE (a)-[:KNOWS]->(b) RETURN a, b",
    )
    .unwrap();

    let mut analyzer = Analyzer::new(&graph);
    assert!(analyzer.analyze(&doc).is_ok());

    let match_doc = parse_query("MATCH (a)-[:KNOWS]->(b) RETURN a, b").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    assert!(matches!(
        analyzer.analyze(&match_doc),
        Err(SemanticError::UnknownRelationshipType(rel_type)) if rel_type == "KNOWS"
    ));
}

// --- Vector function analyzer tests ----------------------------------

#[test]
fn vector_type_cast_lowers_to_type_literal() {
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN [1, 2, 3]::VECTOR<INTEGER>(3) AS v").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    let resolved = analyzer
        .analyze(&doc)
        .expect("VECTOR type should be formatted as a type literal");
    let Some(ResolvedClause::Return(ret)) = resolved.clauses.last() else {
        panic!("expected RETURN clause");
    };
    let ResolvedExpr::Function { args, .. } = &ret.items[0].expr else {
        panic!("expected function call");
    };
    assert!(matches!(
        args.get(1),
        Some(ResolvedExpr::Literal(LiteralValue::TypeName(s))) if s == "VECTOR<INTEGER>(3)"
    ));
}

#[test]
fn vector_distance_rewrites_bare_metric_identifier() {
    let graph = InMemoryGraph::new();
    let doc = parse_query(
        "RETURN vector.distance([1,2]::VECTOR<INTEGER>(2), [3,4]::VECTOR<INTEGER>(2), EUCLIDEAN) AS d",
    )
    .unwrap();
    let mut analyzer = Analyzer::new(&graph);
    let resolved = analyzer
        .analyze(&doc)
        .expect("EUCLIDEAN should be rewritten as a string literal");
    let Some(ResolvedClause::Return(ret)) = resolved.clauses.last() else {
        panic!("expected RETURN clause");
    };
    let ResolvedExpr::Function { args, .. } = &ret.items[0].expr else {
        panic!("expected function call");
    };
    assert!(matches!(
        args.get(2),
        Some(ResolvedExpr::Literal(LiteralValue::String(s))) if s == "EUCLIDEAN"
    ));
}

#[test]
fn cast_rewrites_bare_target_to_type_literal() {
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN cast.to('42', INTEGER) AS n").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    let resolved = analyzer
        .analyze(&doc)
        .expect("INTEGER should be rewritten as a type literal");
    let Some(ResolvedClause::Return(ret)) = resolved.clauses.last() else {
        panic!("expected RETURN clause");
    };
    let ResolvedExpr::Function { args, .. } = &ret.items[0].expr else {
        panic!("expected function call");
    };
    assert!(matches!(
        args.get(1),
        Some(ResolvedExpr::Literal(LiteralValue::TypeName(s))) if s == "INTEGER"
    ));
}

#[test]
fn parenthesized_scalar_type_cast_lowers_to_strict_cast() {
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN ('42' AS INTEGER) AS n").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    let resolved = analyzer
        .analyze(&doc)
        .expect("parenthesized scalar cast should analyze");
    let ResolvedExpr::Function { function, args, .. } = return_expr(&resolved.clauses) else {
        panic!("expected function");
    };

    assert_eq!(function.name(), "cast.to");
    assert_eq!(args.len(), 2);
    assert!(matches!(
        args.get(1),
        Some(ResolvedExpr::Literal(LiteralValue::TypeName(s))) if s == "INTEGER"
    ));
}

#[test]
fn duckdb_cast_call_lowers_to_strict_cast() {
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN CAST('42' AS INTEGER) AS n").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    let resolved = analyzer.analyze(&doc).expect("CAST should analyze");
    let ResolvedExpr::Function { function, args, .. } = return_expr(&resolved.clauses) else {
        panic!("expected function");
    };

    assert_eq!(function.name(), "cast.to");
    assert!(matches!(
        args.get(1),
        Some(ResolvedExpr::Literal(LiteralValue::TypeName(s))) if s == "INTEGER"
    ));
}

#[test]
fn duckdb_try_cast_call_lowers_to_nullable_cast() {
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN TRY_CAST('bad' AS DATE) AS d").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    let resolved = analyzer.analyze(&doc).expect("TRY_CAST should analyze");
    let ResolvedExpr::Function { function, args, .. } = return_expr(&resolved.clauses) else {
        panic!("expected function");
    };

    assert_eq!(function.name(), "cast.try");
    assert!(matches!(
        args.get(1),
        Some(ResolvedExpr::Literal(LiteralValue::TypeName(s))) if s == "DATE"
    ));
}

#[test]
fn duckdb_postfix_cast_lowers_to_strict_cast() {
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN '42'::INTEGER AS n").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    let resolved = analyzer.analyze(&doc).expect("postfix cast should analyze");
    let ResolvedExpr::Function { function, args, .. } = return_expr(&resolved.clauses) else {
        panic!("expected function");
    };

    assert_eq!(function.name(), "cast.to");
    assert!(matches!(
        args.get(1),
        Some(ResolvedExpr::Literal(LiteralValue::TypeName(s))) if s == "INTEGER"
    ));
}

#[test]
fn parenthesized_temporal_type_cast_lowers_to_strict_cast() {
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN ('2024-01-15' AS DATE) AS d").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    let resolved = analyzer
        .analyze(&doc)
        .expect("DATE cast should analyze as strict cast");
    let ResolvedExpr::Function { function, args, .. } = return_expr(&resolved.clauses) else {
        panic!("expected function");
    };

    assert_eq!(function.name(), "cast.to");
    assert_eq!(args.len(), 2);
    assert!(matches!(
        args.get(1),
        Some(ResolvedExpr::Literal(LiteralValue::TypeName(s))) if s == "DATE"
    ));
}

#[test]
fn parenthesized_local_datetime_type_cast_lowers_to_strict_cast() {
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN ('2024-01-15T10:30:00' AS LOCAL DATETIME) AS dt").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    let resolved = analyzer
        .analyze(&doc)
        .expect("LOCAL DATETIME cast should analyze as strict cast");
    let ResolvedExpr::Function { function, args, .. } = return_expr(&resolved.clauses) else {
        panic!("expected function");
    };

    assert_eq!(function.name(), "cast.to");
    assert_eq!(args.len(), 2);
    assert!(matches!(
        args.get(1),
        Some(ResolvedExpr::Literal(LiteralValue::TypeName(s))) if s == "LOCAL_DATETIME"
    ));
}

#[test]
fn parenthesized_vector_type_cast_lowers_to_strict_cast() {
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN ([1, 2] AS VECTOR<FLOAT>(2)) AS v").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    let resolved = analyzer
        .analyze(&doc)
        .expect("VECTOR cast should analyze as strict cast");
    let ResolvedExpr::Function { function, args, .. } = return_expr(&resolved.clauses) else {
        panic!("expected function");
    };

    assert_eq!(function.name(), "cast.to");
    assert_eq!(args.len(), 2);
    assert!(matches!(
        args.get(1),
        Some(ResolvedExpr::Literal(LiteralValue::TypeName(s))) if s == "VECTOR<FLOAT64>(2)"
    ));
}

#[test]
fn parenthesized_list_type_cast_lowers_to_formatted_type_literal() {
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN ([[1], [2]] AS LIST<LIST<INTEGER>>) AS xs").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    let resolved = analyzer
        .analyze(&doc)
        .expect("LIST cast should analyze as a formatted type literal");
    let ResolvedExpr::Function { function, args, .. } = return_expr(&resolved.clauses) else {
        panic!("expected function");
    };

    assert_eq!(function.name(), "cast.to");
    assert!(matches!(
        args.get(1),
        Some(ResolvedExpr::Literal(LiteralValue::TypeName(s))) if s == "LIST<LIST<INTEGER>>"
    ));
}

#[test]
fn vector_norm_rewrites_bare_metric_identifier() {
    let graph = InMemoryGraph::new();
    let doc =
        parse_query("RETURN vector.norm([1,2,3]::VECTOR<FLOAT32>(3), MANHATTAN) AS n").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    assert!(analyzer.analyze(&doc).is_ok());
}

#[test]
fn bare_identifier_outside_enum_slot_still_resolves_as_variable() {
    // Outside the enum slot, INTEGER should still behave like a
    // variable reference — this guards against the rewrite leaking.
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN INTEGER AS v").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    assert!(matches!(
        analyzer.analyze(&doc),
        Err(SemanticError::UnknownVariable(name)) if name == "INTEGER"
    ));
}

#[test]
fn unknown_vector_function_is_rejected() {
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN vector.bogus([1,2,3], 3, INTEGER) AS v").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    assert!(matches!(
        analyzer.analyze(&doc),
        Err(SemanticError::UnknownFunction(name, _, _)) if name == "vector.bogus"
    ));
}

// --- Enum-literal rewrite scope --------------------------------------

/// Walk a ResolvedClause list, return the last RETURN's first item.
fn return_expr(clauses: &[ResolvedClause]) -> &ResolvedExpr {
    let Some(ResolvedClause::Return(ret)) = clauses.last() else {
        panic!("expected RETURN clause");
    };
    &ret.items[0].expr
}

#[test]
fn vector_does_not_rewrite_first_or_second_argument() {
    // INTEGER in the value slot should still attempt to resolve as a
    // variable; the target type itself is parser syntax.
    let graph = InMemoryGraph::new();

    let bad_first = parse_query("RETURN INTEGER::VECTOR<INTEGER>(3) AS v").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    assert!(matches!(
        analyzer.analyze(&bad_first),
        Err(SemanticError::UnknownVariable(name)) if name == "INTEGER"
    ));
}

#[test]
fn vector_distance_does_not_rewrite_first_or_second_argument() {
    let graph = InMemoryGraph::new();
    let doc =
        parse_query("RETURN vector.distance(EUCLIDEAN, [1,2]::VECTOR<INTEGER>(2), EUCLIDEAN) AS d")
            .unwrap();
    let mut analyzer = Analyzer::new(&graph);
    assert!(matches!(
        analyzer.analyze(&doc),
        Err(SemanticError::UnknownVariable(name)) if name == "EUCLIDEAN"
    ));
}

#[test]
fn vector_norm_does_not_rewrite_first_argument() {
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN vector.norm(MANHATTAN, EUCLIDEAN) AS n").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    assert!(matches!(
        analyzer.analyze(&doc),
        Err(SemanticError::UnknownVariable(name)) if name == "MANHATTAN"
    ));
}

#[test]
fn parameter_in_type_slot_is_preserved_as_parameter() {
    // A $param in a type slot must NOT be rewritten — callers need
    // to pass target types dynamically.
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN cast.to('42', $type) AS v").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    let resolved = analyzer.analyze(&doc).expect("parameter should be kept");
    let ResolvedExpr::Function { args, .. } = return_expr(&resolved.clauses) else {
        panic!("expected function");
    };
    assert!(matches!(args.get(1), Some(ResolvedExpr::Parameter(p)) if p == "type"));
}

#[test]
fn parameter_in_vector_norm_metric_slot_is_preserved() {
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN vector.norm([1,2,3]::VECTOR<FLOAT32>(3), $metric) AS n").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    let resolved = analyzer.analyze(&doc).expect("parameter should be kept");
    let ResolvedExpr::Function { args, .. } = return_expr(&resolved.clauses) else {
        panic!("expected function");
    };
    assert!(matches!(args.get(1), Some(ResolvedExpr::Parameter(p)) if p == "metric"));
}

#[test]
fn variable_named_like_metric_outside_enum_slot_is_not_rewritten() {
    // UNWIND exposes a variable literally called COSINE — which is a
    // metric name. Using it outside the enum slot must bind normally.
    let graph = InMemoryGraph::new();
    let doc = parse_query("UNWIND [1.0, 2.0, 3.0] AS COSINE RETURN COSINE AS val").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    assert!(analyzer.analyze(&doc).is_ok());
}

#[test]
fn string_literal_in_enum_slot_remains_string_literal() {
    let graph = InMemoryGraph::new();
    let doc = parse_query(
        "RETURN vector.distance([1]::VECTOR<INTEGER>(1), [2]::VECTOR<INTEGER>(1), 'euclidean') AS d",
    )
    .unwrap();
    let mut analyzer = Analyzer::new(&graph);
    let resolved = analyzer
        .analyze(&doc)
        .expect("string literal must be passed through");
    let ResolvedExpr::Function { args, .. } = return_expr(&resolved.clauses) else {
        panic!("expected function");
    };
    assert!(matches!(
        args.get(2),
        Some(ResolvedExpr::Literal(LiteralValue::String(s))) if s == "euclidean"
    ));
}

// --- Arity coverage for every vector function ------------------------

#[test]
fn every_vector_function_has_arity_guard() {
    // (function name, offending argument count, min–max hint)
    let cases = &[
        ("RETURN vector.similarity([1]) AS s", 1, "vector.similarity"),
        (
            "RETURN vector.similarity([1],[2],[3],[4]) AS s",
            4,
            "vector.similarity",
        ),
        (
            "RETURN vector.distance([1]::VECTOR<INTEGER>(1)) AS d",
            1,
            "vector.distance",
        ),
        (
            "RETURN vector.norm([1]::VECTOR<INTEGER>(1)) AS n",
            1,
            "vector.norm",
        ),
        ("RETURN vector.dimension() AS n", 0, "vector.dimension"),
        ("RETURN vector.coordinates() AS l", 0, "vector.coordinates"),
    ];
    for (query, expected_args, name) in cases {
        let graph = InMemoryGraph::new();
        let doc = parse_query(query).unwrap();
        let mut analyzer = Analyzer::new(&graph);
        let result = analyzer.analyze(&doc);
        match result {
            Err(SemanticError::WrongArity(got_name, _, got_args)) => {
                assert_eq!(got_name.to_ascii_lowercase(), name.to_ascii_lowercase());
                assert_eq!(got_args, *expected_args, "query {query:?}");
            }
            other => panic!("query {query:?} expected WrongArity, got {other:?}"),
        }
    }
}

#[test]
fn dotted_similarity_typo_is_rejected() {
    let graph = InMemoryGraph::new();
    let doc = parse_query("RETURN vector.similarity.manhattan([1,2],[3,4]) AS s").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    assert!(matches!(
        analyzer.analyze(&doc),
        Err(SemanticError::UnknownFunction(name, _, _))
            if name == "vector.similarity.manhattan"
    ));
}

#[test]
fn list_expression_scope_is_popped_after_analysis_error() {
    let graph = InMemoryGraph::new();
    let bad = parse_query("RETURN any(x IN [1] WHERE missing) AS ok").unwrap();
    let mut analyzer = Analyzer::new(&graph);
    assert!(matches!(
        analyzer.analyze(&bad),
        Err(SemanticError::UnknownVariable(name)) if name == "missing"
    ));

    let leaked = parse_query("RETURN x AS leaked").unwrap();
    assert!(matches!(
        analyzer.analyze(&leaked),
        Err(SemanticError::UnknownVariable(name)) if name == "x"
    ));
}