khive-query 0.12.0

GQL and SPARQL parsers with SQL compiler for knowledge graph queries.
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
use super::*;

#[test]
fn simple_two_node_pattern() {
    let q = parse("MATCH (a:concept)-[e:introduced_by]->(b:paper) RETURN a, e, b").unwrap();
    assert_eq!(q.pattern.elements.len(), 3);
    assert_eq!(
        q.return_items,
        vec![
            ReturnItem::Variable("a".into()),
            ReturnItem::Variable("e".into()),
            ReturnItem::Variable("b".into()),
        ]
    );

    let nodes: Vec<_> = q.pattern.nodes().collect();
    assert_eq!(nodes[0].kind.as_deref(), Some("concept"));
    assert_eq!(nodes[1].kind.as_deref(), Some("paper"));

    let edges: Vec<_> = q.pattern.edges().collect();
    assert_eq!(edges[0].relations, vec!["introduced_by"]);
    assert_eq!(edges[0].direction, EdgeDirection::Out);
}

#[test]
fn variable_length_with_multiple_relations() {
    let q = parse("MATCH (a {name: 'LoRA'})-[:extends|variant_of*1..3]->(b) RETURN b LIMIT 20")
        .unwrap();
    let nodes: Vec<_> = q.pattern.nodes().collect();
    assert_eq!(
        nodes[0].properties.get("name").unwrap(),
        &ConditionValue::String("LoRA".into())
    );

    let edges: Vec<_> = q.pattern.edges().collect();
    assert_eq!(edges[0].relations, vec!["extends", "variant_of"]);
    assert_eq!(edges[0].min_hops, 1);
    assert_eq!(edges[0].max_hops, 3);
    assert_eq!(q.offset, 0);
    assert_eq!(q.limit, Some(20));
}

#[test]
fn skip_and_limit_parse_as_page_window() {
    let q = parse("MATCH (a:concept) RETURN a SKIP 500 LIMIT 200").unwrap();
    assert_eq!(q.offset, 500);
    assert_eq!(q.limit, Some(200));
}

#[test]
fn skip_without_limit_is_supported() {
    let q = parse("MATCH (a:concept) RETURN a SKIP 500").unwrap();
    assert_eq!(q.offset, 500);
    assert_eq!(q.limit, None);
}

#[test]
fn negative_skip_is_rejected() {
    let err = parse("MATCH (a:concept) RETURN a SKIP -1").unwrap_err();
    assert!(
        matches!(err, QueryError::Parse { .. }),
        "negative SKIP must be a parse error, got {err:?}"
    );
}

#[test]
fn skip_after_limit_is_rejected() {
    let err = parse("MATCH (a:concept) RETURN a LIMIT 20 SKIP 10").unwrap_err();
    assert!(
        matches!(err, QueryError::Parse { .. }),
        "SKIP must precede LIMIT, got {err:?}"
    );
}

#[test]
fn sql_offset_keyword_remains_outside_gql() {
    let err = parse("MATCH (a:concept) RETURN a OFFSET 10").unwrap_err();
    assert!(
        matches!(err, QueryError::Parse { .. }),
        "GQL paging uses SKIP rather than OFFSET, got {err:?}"
    );
}

#[test]
fn where_clause() {
    let q = parse(
            "MATCH (a)-[e:implements]->(b:project) WHERE b.name = 'lattice-inference' RETURN a LIMIT 10"
        ).unwrap();
    let conds: Vec<_> = q.where_clause.conditions().collect();
    assert_eq!(conds.len(), 1);
    assert_eq!(conds[0].variable, "b");
    assert_eq!(conds[0].property, PropertyRef::Field("name".into()));
}

#[test]
fn where_json_property_path_is_explicit_in_ast() {
    let q = parse("MATCH (n) WHERE n.properties.finding.severity = 'high' RETURN n").unwrap();
    let cond = q.where_clause.conditions().next().unwrap();
    assert_eq!(
        cond.property,
        PropertyRef::JsonPath(vec!["finding".into(), "severity".into()])
    );
}

#[test]
fn where_nested_path_rejects_non_properties_root() {
    let err = parse("MATCH (n) WHERE n.finding.severity = 'high' RETURN n").unwrap_err();
    assert!(
        err.to_string()
            .contains("invalid WHERE property path root 'finding'"),
        "got: {err}"
    );
}

#[test]
fn where_clause_and() {
    let q = parse(
            "MATCH (a:concept)-[e:extends]->(b) WHERE a.name = 'LoRA' AND b.kind = 'concept' RETURN a, b"
        ).unwrap();
    let conds: Vec<_> = q.where_clause.conditions().collect();
    assert_eq!(conds.len(), 2, "AND should produce two leaf conditions");
    assert!(
        matches!(&q.where_clause, WhereExpr::And(_, _)),
        "should be And node"
    );
}

#[test]
fn where_clause_or() {
    let q = parse(
        "MATCH (a:concept)-[e:extends]->(b) WHERE a.name = 'LoRA' OR a.name = 'QLoRA' RETURN a",
    )
    .unwrap();
    let conds: Vec<_> = q.where_clause.conditions().collect();
    assert_eq!(conds.len(), 2, "OR should produce two leaf conditions");
    assert!(
        matches!(&q.where_clause, WhereExpr::Or(_, _)),
        "should be Or node"
    );
}

#[test]
fn where_clause_and_or() {
    let q = parse(
            "MATCH (a:concept)-[e:extends]->(b) WHERE a.name = 'X' AND a.kind = 'concept' OR b.kind = 'project' RETURN a"
        ).unwrap();
    let conds: Vec<_> = q.where_clause.conditions().collect();
    assert_eq!(conds.len(), 3);
    assert!(
        matches!(&q.where_clause, WhereExpr::Or(_, _)),
        "top-level should be Or"
    );
}

#[test]
fn where_clause_extended_operators() {
    let q = parse(
        "MATCH (n:entity) WHERE n.name CONTAINS '%_' AND n.name STARTS WITH 'pre' \
             AND n.kind IN ['concept', 'document'] AND n.properties.domain IS NOT NULL RETURN n",
    )
    .unwrap();
    let conds: Vec<_> = q.where_clause.conditions().collect();
    assert_eq!(conds.len(), 4);
    assert_eq!(conds[0].op, CompareOp::Contains);
    assert_eq!(conds[0].value, ConditionValue::String("%_".into()));
    assert_eq!(conds[1].op, CompareOp::StartsWith);
    assert_eq!(conds[2].op, CompareOp::In);
    assert_eq!(
        conds[2].value,
        ConditionValue::List(vec![
            ConditionValue::String("concept".into()),
            ConditionValue::String("document".into()),
        ])
    );
    assert_eq!(conds[3].op, CompareOp::IsNotNull);
    assert_eq!(conds[3].value, ConditionValue::Null);
}

#[test]
fn where_clause_is_null() {
    let q = parse("MATCH (n:entity) WHERE n.properties.domain IS NULL RETURN n").unwrap();
    let conds: Vec<_> = q.where_clause.conditions().collect();
    assert_eq!(conds.len(), 1);
    assert_eq!(conds[0].op, CompareOp::IsNull);
    assert_eq!(conds[0].value, ConditionValue::Null);
}

#[test]
fn where_clause_is_null_and_is_not_null_together() {
    let q =
        parse("MATCH (n:entity) WHERE n.name IS NOT NULL AND n.properties.domain IS NULL RETURN n")
            .unwrap();
    let conds: Vec<_> = q.where_clause.conditions().collect();
    assert_eq!(conds.len(), 2);
    assert_eq!(conds[0].op, CompareOp::IsNotNull);
    assert_eq!(conds[1].op, CompareOp::IsNull);
}

#[test]
fn where_in_accepts_scalar_list_and_empty_list() {
    let q = parse(
        "MATCH (n) WHERE n.properties.value IN ['x', 1, 2.5, true] \
             OR n.properties.value IN [] RETURN n",
    )
    .unwrap();
    let conds: Vec<_> = q.where_clause.conditions().collect();
    assert_eq!(
        conds[0].value,
        ConditionValue::List(vec![
            ConditionValue::String("x".into()),
            ConditionValue::Integer(1),
            ConditionValue::Number(2.5),
            ConditionValue::Bool(true),
        ])
    );
    assert_eq!(conds[1].value, ConditionValue::List(vec![]));
}

#[test]
fn contains_and_starts_with_require_strings() {
    let contains = parse("MATCH (n) WHERE n.name CONTAINS 1 RETURN n").unwrap_err();
    assert!(contains.to_string().contains("require a string literal"));

    let starts = parse("MATCH (n) WHERE n.name STARTS WITH true RETURN n").unwrap_err();
    assert!(starts.to_string().contains("require a string literal"));
}

#[test]
fn inbound_edge() {
    let q = parse("MATCH (a:paper)<-[e:introduced_by]-(b:concept) RETURN a, b").unwrap();
    let edges: Vec<_> = q.pattern.edges().collect();
    assert_eq!(edges[0].direction, EdgeDirection::In);
}

#[test]
fn undirected_edge() {
    let q = parse("MATCH (a)-[e:competes_with]-(b) RETURN a, b").unwrap();
    let edges: Vec<_> = q.pattern.edges().collect();
    assert_eq!(edges[0].direction, EdgeDirection::Both);
}

#[test]
fn three_node_chain() {
    let q = parse(
        "MATCH (a:concept)-[:introduced_by]->(p:paper)-[:introduced_by]->(c:concept) RETURN a, c",
    )
    .unwrap();
    assert_eq!(q.pattern.elements.len(), 5);
    let nodes: Vec<_> = q.pattern.nodes().collect();
    assert_eq!(nodes.len(), 3);
}

#[test]
fn comma_separated_match_patterns_name_unsupported_construct() {
    let err = parse("MATCH (a)-[e1]->(b), (a)-[e2]->(b) RETURN a, b").unwrap_err();
    assert!(
        matches!(&err, QueryError::Unsupported(_)),
        "multiple MATCH patterns must return Unsupported; got {err:?}"
    );
    let msg = err.to_string();
    assert!(
        msg.contains("comma-separated MATCH patterns"),
        "error must name the unsupported construct; got: {msg}"
    );
    assert!(
        msg.contains("e.id") && msg.contains("e.relation"),
        "error must preserve edge identity in its alternative; got: {msg}"
    );
}

#[test]
fn return_aliases_name_unsupported_construct() {
    for query in [
        "MATCH (a)-[e]->(b) RETURN a.name AS src, b.name AS dst LIMIT 5",
        "MATCH (a) RETURN a as node",
        "MATCH (a)-[e]->(b) RETURN a.name, b.name aS dst",
    ] {
        let err = parse(query).unwrap_err();
        assert!(
            matches!(&err, QueryError::Unsupported(_)),
            "RETURN alias must return Unsupported for {query:?}; got {err:?}"
        );
        assert!(
            err.to_string().contains("aliases (`AS`)"),
            "error must name AS for {query:?}; got: {err}"
        );
    }
}

#[test]
fn documented_single_path_parallel_edge_audit_parses() {
    let q = parse("MATCH (a)-[e]->(b) RETURN a.id, e.id, e.relation, b.id").unwrap();
    assert_eq!(
        q.return_items,
        vec![
            ReturnItem::Property("a".into(), "id".into()),
            ReturnItem::Property("e".into(), "id".into()),
            ReturnItem::Property("e".into(), "relation".into()),
            ReturnItem::Property("b".into(), "id".into()),
        ]
    );
}

#[test]
fn gql_dialect_reference_matches_parser_contract() {
    const DOCS: &str = include_str!("../../docs/api/parsing.md");
    const SUPPORTED: &str = "MATCH (a)-[e]->(b) RETURN a.id, e.id, e.relation, b.id";
    const MULTI_PATTERN: &str = "MATCH (a)-[e1]->(b), (a)-[e2]->(b) RETURN a, b";
    const ALIAS: &str = "MATCH (a)-[e]->(b) RETURN a.name AS src, b.name AS dst LIMIT 5";

    for query in [SUPPORTED, MULTI_PATTERN, ALIAS] {
        assert!(
            DOCS.contains(query),
            "dialect reference must include contract example: {query}"
        );
    }

    parse(SUPPORTED).expect("documented single-path alternative must parse");
    for (query, feature) in [
        (MULTI_PATTERN, "comma-separated MATCH patterns"),
        (ALIAS, "aliases (`AS`)"),
    ] {
        let err = parse(query).unwrap_err();
        assert!(
            matches!(&err, QueryError::Unsupported(_)),
            "documented unsupported form must return Unsupported; got {err:?}"
        );
        assert!(
            err.to_string().contains(feature),
            "documented error must name {feature:?}; got: {err}"
        );
    }
}

#[test]
fn node_pattern_entity_type_lifted_from_properties() {
    let q = parse("MATCH (n:document {entity_type: 'paper'}) RETURN n").unwrap();
    let nodes: Vec<_> = q.pattern.nodes().collect();
    assert_eq!(
        nodes[0].entity_type.as_deref(),
        Some("paper"),
        "entity_type must be lifted into NodePattern.entity_type"
    );
    assert!(
        !nodes[0].properties.contains_key("entity_type"),
        "entity_type must be removed from the properties map after lifting"
    );
}

#[test]
fn gql_create_rejected_with_readonly_message() {
    let err = parse("CREATE (n:concept {name: 'X'}) RETURN n").unwrap_err();
    assert!(
        matches!(err, crate::error::QueryError::Unsupported(_)),
        "GQL CREATE must return Unsupported; got {err:?}"
    );
    let msg = err.to_string();
    assert!(
        msg.contains("read-only"),
        "error must mention 'read-only'; got: {msg}"
    );
    assert!(
        msg.contains("create") && msg.contains("update") && msg.contains("delete"),
        "error must name the mutation verbs; got: {msg}"
    );
}

#[test]
fn gql_delete_rejected_with_readonly_message() {
    let err = parse("DELETE (n) WHERE n.kind = 'concept'").unwrap_err();
    assert!(
        matches!(err, crate::error::QueryError::Unsupported(_)),
        "GQL DELETE must return Unsupported; got {err:?}"
    );
    let msg = err.to_string();
    assert!(msg.contains("read-only"), "got: {msg}");
}

#[test]
fn gql_set_rejected_with_readonly_message() {
    let err = parse("SET (n:concept) RETURN n").unwrap_err();
    assert!(
        matches!(err, crate::error::QueryError::Unsupported(_)),
        "GQL SET must return Unsupported; got {err:?}"
    );
}

#[test]
fn gql_merge_rejected_with_readonly_message() {
    let err = parse("MERGE (n:concept {name: 'X'}) RETURN n").unwrap_err();
    assert!(
        matches!(err, crate::error::QueryError::Unsupported(_)),
        "GQL MERGE must return Unsupported; got {err:?}"
    );
}

#[test]
fn gql_match_still_compiles_after_write_guard() {
    let q = parse("MATCH (a:concept)-[:extends]->(b) RETURN a").unwrap();
    assert!(!q.pattern.elements.is_empty(), "valid MATCH must parse");
}

#[test]
fn gql_detach_delete_rejected() {
    let err = parse("DETACH DELETE (n)").unwrap_err();
    assert!(
        matches!(err, crate::error::QueryError::Unsupported(_)),
        "DETACH DELETE must return Unsupported; got {err:?}"
    );
    let msg = err.to_string();
    assert!(
        msg.contains("read-only"),
        "error must mention 'read-only'; got: {msg}"
    );
    assert!(
        msg.contains("create") && msg.contains("update") && msg.contains("delete"),
        "error must name the mutation verbs; got: {msg}"
    );
}