selene-db-gql 1.3.0

ISO/IEC 39075:2024 GQL parser, planner, optimizer, and executor for selene-db.
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
//! Executor tests for unbounded and questioned quantifiers.

mod exec_common;

use exec_common::{
    ExecFixture, db_string, edge_ids_for, execute_plan, node_ids_for, planned, props,
};
use selene_core::{DbString, GraphId, LabelSet, Value};
use selene_gql::{
    Binding, BindingTable, BindingTableSchema, EmptyProcedureRegistry, ExecutorError, TxContext,
    execute_pattern, execute_pipeline,
};
use selene_graph::SharedGraph;

fn edge_lists_for(table: &BindingTable, name: &str) -> Vec<Option<Vec<u64>>> {
    exec_common::column_values(table, name)
        .into_iter()
        .map(|value| match value {
            Value::List(items) => Some(
                items
                    .into_iter()
                    .map(|item| match item {
                        Value::EdgeRef(id) => id.get(),
                        other => panic!("expected edge ref in group list, got {other:?}"),
                    })
                    .collect(),
            ),
            Value::Null => None,
            other => panic!("expected edge list or null, got {other:?}"),
        })
        .collect()
}

fn execute_on_graph(
    graph: &SharedGraph,
    plan: &selene_gql::ExecutionPlan,
) -> Result<BindingTable, ExecutorError> {
    let mut ctx = TxContext::read_only(
        graph.read(),
        &plan.impl_defined_caps,
        &EmptyProcedureRegistry,
        graph.index_providers(),
    )
    .with_plan_metadata(&plan.expr_ids, &plan.subqueries);
    let input = if let Some(pattern) = &plan.pattern_plan {
        execute_pattern(pattern, &ctx)?
    } else {
        BindingTable::new(
            BindingTableSchema {
                columns: Vec::new(),
            },
            vec![Binding::empty()],
        )
    };
    execute_pipeline(&plan.pipeline, input, &mut ctx)
}

fn cycle_graph() -> SharedGraph {
    let node = db_string("N");
    let edge = db_string("K");
    let name = db_string("name");
    let graph = SharedGraph::new(GraphId::new(6401));
    {
        let mut txn = graph.begin_write();
        let mut mutator = txn.mutator();
        let a = mutator
            .create_node(
                LabelSet::single(node.clone()),
                props([(name.clone(), Value::String(db_string("A")))]),
            )
            .expect("A inserts");
        let b = mutator
            .create_node(
                LabelSet::single(node),
                props([(name, Value::String(db_string("B")))]),
            )
            .expect("B inserts");
        mutator
            .create_edge(edge.clone(), a, b, props([]))
            .expect("edge 1");
        mutator.create_edge(edge, b, a, props([])).expect("edge 2");
        txn.commit().expect("fixture commits");
    }
    graph
}

fn chain_graph() -> SharedGraph {
    let node = db_string("N");
    let edge = db_string("K");
    let name = db_string("name");
    let graph = SharedGraph::new(GraphId::new(6402));
    {
        let mut txn = graph.begin_write();
        let mut mutator = txn.mutator();
        let a = named_node(&mut mutator, node.clone(), name.clone(), "A");
        let b = named_node(&mut mutator, node.clone(), name.clone(), "B");
        let c = named_node(&mut mutator, node, name, "C");
        mutator
            .create_edge(edge.clone(), a, b, props([]))
            .expect("edge 1");
        mutator.create_edge(edge, b, c, props([])).expect("edge 2");
        txn.commit().expect("fixture commits");
    }
    graph
}

fn named_node(
    mutator: &mut selene_graph::Mutator<'_, '_>,
    label: DbString,
    name_key: DbString,
    name: &str,
) -> selene_core::NodeId {
    mutator
        .create_node(
            LabelSet::single(label),
            props([(name_key, Value::String(db_string(name)))]),
        )
        .expect("node inserts")
}

#[test]
fn questioned_edge_emits_skipped_and_taken_rows() {
    let fixture = ExecFixture::build();
    let plan = planned("MATCH (a:Person {name: 'Alice'})-[r:KNOWS?]->(b) RETURN r, b");

    let table = execute_plan(&fixture, &plan).expect("questioned edge executes");

    assert_eq!(edge_ids_for(&table, "r"), vec![None, Some(1)]);
    assert_eq!(node_ids_for(&table, "b"), vec![Some(1), Some(2)]);
}

#[test]
fn questioned_edge_null_propagates_properties() {
    let fixture = ExecFixture::build();
    let plan = planned("MATCH (a:Person {name: 'Alice'})-[r:KNOWS?]->(b) RETURN r.score AS score");

    let table = execute_plan(&fixture, &plan).expect("questioned edge executes");

    assert_eq!(
        exec_common::column_values(&table, "score"),
        vec![Value::Null, Value::Int(1)]
    );
}

#[test]
fn questioned_edge_zero_hop_composes_with_selectors_and_path_modes() {
    let fixture = ExecFixture::build();
    let shortest = planned(
        "MATCH ANY SHORTEST (a:Person {name: 'Alice'})-[r:KNOWS?]->(b:Person {name: 'Alice'}) RETURN r, b",
    );
    let acyclic = planned(
        "MATCH ACYCLIC (a:Person {name: 'Alice'})-[r:KNOWS?]->(b:Person {name: 'Alice'}) RETURN r, b",
    );

    let shortest_rows = execute_plan(&fixture, &shortest).expect("shortest executes");
    let acyclic_rows = execute_plan(&fixture, &acyclic).expect("acyclic executes");

    assert_eq!(edge_ids_for(&shortest_rows, "r"), vec![None]);
    assert_eq!(node_ids_for(&shortest_rows, "b"), vec![Some(1)]);
    assert_eq!(edge_ids_for(&acyclic_rows, "r"), vec![None]);
    assert_eq!(node_ids_for(&acyclic_rows, "b"), vec![Some(1)]);
}

#[test]
fn unbounded_trail_prunes_repeated_edges_in_loop() {
    let graph = cycle_graph();
    let plan = planned("MATCH TRAIL (a:N {name: 'A'})-[r:K+]->(b:N) RETURN r, b");

    let table = execute_on_graph(&graph, &plan).expect("unbounded trail executes");

    assert_eq!(
        edge_lists_for(&table, "r"),
        vec![Some(vec![1]), Some(vec![1, 2])]
    );
    assert_eq!(node_ids_for(&table, "b"), vec![Some(2), Some(1)]);
}

#[test]
fn unbounded_simple_allows_terminal_return_to_source() {
    let graph = cycle_graph();
    let plan = planned("MATCH SIMPLE (a:N {name: 'A'})-[r:K+]->(b:N {name: 'A'}) RETURN r, b");

    let table = execute_on_graph(&graph, &plan).expect("unbounded simple executes");

    assert_eq!(edge_lists_for(&table, "r"), vec![Some(vec![1, 2])]);
    assert_eq!(node_ids_for(&table, "b"), vec![Some(1)]);
}

#[test]
fn unbounded_cap_exceed_returns_program_limit() {
    let graph = chain_graph();
    let mut plan = planned("MATCH ANY (a:N {name: 'A'})-[:K+]->(b:N) RETURN b");
    plan.impl_defined_caps.max_quantifier = 1;

    let err = execute_on_graph(&graph, &plan).expect_err("cap exceeds");

    assert!(matches!(
        err,
        ExecutorError::ProgramLimitExceeded {
            detail: "max_quantifier",
            ..
        }
    ));
    assert_eq!(err.gqlstatus().as_str(), "5GQL1");
}

// FU-2: an UNBOUNDED minimum-length shortest selector (ANY/ALL SHORTEST) must
// downshift its repeat traversal from the default WALK to TRAIL so it terminates
// on a cyclic graph — and the TRAIL traversal is result-equivalent because every
// minimum-hop path is simple (hence a trail). See
// `plan::lowering::match_clause::repeat_path_mode_under_filter`.

/// `(b, edge-id-list)` rows, sorted, for order-independent comparison.
fn shortest_rows(table: &BindingTable) -> Vec<(Option<u64>, Option<Vec<u64>>)> {
    let bs = node_ids_for(table, "b");
    let rs = edge_lists_for(table, "r");
    let mut rows: Vec<_> = bs.into_iter().zip(rs).collect();
    rows.sort();
    rows
}

#[test]
fn unbounded_all_shortest_terminates_and_equals_trail_on_cycle() {
    let graph = cycle_graph();
    // Pre-fix this raised 5GQL1 (ProgramLimitExceeded "max_quantifier") because the
    // bare ALL SHORTEST selector kept the default WALK and the unbounded WALK
    // expanded to the max_quantifier cap before the selector could run.
    let plan = planned("MATCH ALL SHORTEST (a:N {name: 'A'})-[r:K+]->(b:N) RETURN r, b");

    let table = execute_on_graph(&graph, &plan).expect("unbounded ALL SHORTEST terminates");

    // Identical to the explicit TRAIL spelling: b=B path [1], b=A path [1, 2].
    assert_eq!(
        shortest_rows(&table),
        vec![(Some(1), Some(vec![1, 2])), (Some(2), Some(vec![1]))]
    );

    let trail = planned("MATCH TRAIL (a:N {name: 'A'})-[r:K+]->(b:N) RETURN r, b");
    let trail_table = execute_on_graph(&graph, &trail).expect("trail executes");
    assert_eq!(shortest_rows(&table), shortest_rows(&trail_table));
}

#[test]
fn unbounded_any_shortest_terminates_one_row_per_endpoint_on_cycle() {
    let graph = cycle_graph();
    let plan = planned("MATCH ANY SHORTEST (a:N {name: 'A'})-[r:K+]->(b:N) RETURN b");

    let table = execute_on_graph(&graph, &plan).expect("unbounded ANY SHORTEST terminates");

    // One row per distinct (source, target) endpoint pair: b=B and b=A.
    let mut bs = node_ids_for(&table, "b");
    bs.sort();
    assert_eq!(bs, vec![Some(1), Some(2)]);
}

#[test]
fn unbounded_all_shortest_keeps_equal_length_paths_and_equals_trail() {
    // Diamond with two equal-length shortest paths to D plus a longer one:
    //   A -e1-> B -e3-> D   (len 2, shortest)
    //   A -e2-> C -e4-> D   (len 2, shortest)
    //   A -e1-> B -e5-> E -e6-> D  (len 3, longer — must be pruned by the selector)
    let node = db_string("N");
    let edge = db_string("K");
    let name = db_string("name");
    let graph = SharedGraph::new(GraphId::new(6403));
    {
        let mut txn = graph.begin_write();
        let mut mutator = txn.mutator();
        let a = named_node(&mut mutator, node.clone(), name.clone(), "A");
        let b = named_node(&mut mutator, node.clone(), name.clone(), "B");
        let c = named_node(&mut mutator, node.clone(), name.clone(), "C");
        let d = named_node(&mut mutator, node.clone(), name.clone(), "D");
        let e = named_node(&mut mutator, node, name, "E");
        mutator
            .create_edge(edge.clone(), a, b, props([]))
            .expect("e1");
        mutator
            .create_edge(edge.clone(), a, c, props([]))
            .expect("e2");
        mutator
            .create_edge(edge.clone(), b, d, props([]))
            .expect("e3");
        mutator
            .create_edge(edge.clone(), c, d, props([]))
            .expect("e4");
        mutator
            .create_edge(edge.clone(), b, e, props([]))
            .expect("e5");
        mutator.create_edge(edge, e, d, props([])).expect("e6");
        txn.commit().expect("fixture commits");
    }

    let plan =
        planned("MATCH ALL SHORTEST (a:N {name: 'A'})-[r:K+]->(d:N {name: 'D'}) RETURN r, d");
    let table = execute_on_graph(&graph, &plan).expect("ALL SHORTEST terminates");

    // BOTH equal-length shortest paths to D are retained (the longer 3-hop one
    // is pruned by the selector): edge-lists [1, 3] and [2, 4].
    let mut edge_lists: Vec<_> = edge_lists_for(&table, "r")
        .into_iter()
        .map(|opt| opt.expect("edge list present"))
        .collect();
    edge_lists.sort();
    assert_eq!(edge_lists, vec![vec![1, 3], vec![2, 4]]);

    // Result-equivalent to the explicit TRAIL-mode spelling of the same query.
    let trail =
        planned("MATCH ALL SHORTEST TRAIL (a:N {name: 'A'})-[r:K+]->(d:N {name: 'D'}) RETURN r, d");
    let trail_table = execute_on_graph(&graph, &trail).expect("trail spelling executes");
    let mut trail_lists: Vec<_> = edge_lists_for(&trail_table, "r")
        .into_iter()
        .map(|opt| opt.expect("edge list present"))
        .collect();
    trail_lists.sort();
    assert_eq!(edge_lists, trail_lists);
}

#[test]
fn bounded_shortest_unchanged_on_cycle() {
    // Regression: a BOUNDED repeat is finite under WALK already; the downshift
    // is unbounded-only and must not alter bounded behavior.
    let graph = cycle_graph();
    let plan = planned("MATCH ALL SHORTEST (a:N {name: 'A'})-[r:K*1..3]->(b:N) RETURN r, b");

    let table = execute_on_graph(&graph, &plan).expect("bounded shortest executes");

    // Shortest hop-rank to each reachable endpoint: b=B [1], b=A [1, 2].
    assert_eq!(
        shortest_rows(&table),
        vec![(Some(1), Some(vec![1, 2])), (Some(2), Some(vec![1]))]
    );
}

#[test]
fn unbounded_counted_shortest_still_program_limit_on_cycle() {
    // SCOPE: counted shortest (G019 SHORTEST N) is claimed. It counts paths by
    // hop-rank INCLUDING non-simple paths (ISO §22.4), so it must stay WALK and
    // keep raising 5GQL1 on an unbounded cyclic graph (downshifting to TRAIL would
    // silently change its semantics to count trails, inconsistent with bounded
    // counted-shortest). Pins the implementation-defined cap behavior.
    let graph = cycle_graph();
    let plan = planned("MATCH SHORTEST 2 (a:N {name: 'A'})-[r:K+]->(b:N) RETURN r");

    let err = execute_on_graph(&graph, &plan).expect_err("counted shortest still capped");

    assert!(matches!(
        err,
        ExecutorError::ProgramLimitExceeded {
            detail: "max_quantifier",
            ..
        }
    ));
    assert_eq!(err.gqlstatus().as_str(), "5GQL1");
}

// FU-2 (Codex PR #245 r2, P2): the count-1 counted spellings are ISO §16.6 SR2c
// EQUIVALENT to the keyword forms — `SHORTEST 1 [PATH]` == `ANY SHORTEST`
// (`CountedShortest { paths: 1 }`) and `SHORTEST [1] GROUP[S]` == `ALL SHORTEST`
// (`CountedShortestGroup { groups: 1 }`). They are min-length shortest selectors,
// so on a cyclic graph they must downshift to TRAIL and TERMINATE identically to
// their keyword twins, not raise 5GQL1. The downshift predicate matches on the
// count-1 *semantics*, not the surface keyword, so equivalent forms agree.

#[test]
fn unbounded_counted_shortest_one_terminates_and_equals_any_shortest_on_cycle() {
    let graph = cycle_graph();
    let counted = planned("MATCH SHORTEST 1 (a:N {name: 'A'})-[r:K+]->(b:N) RETURN b");

    let table = execute_on_graph(&graph, &counted)
        .expect("unbounded SHORTEST 1 terminates (== ANY SHORTEST)");

    // One row per distinct (source, target) endpoint pair, exactly like ANY SHORTEST.
    let mut bs = node_ids_for(&table, "b");
    bs.sort();
    assert_eq!(bs, vec![Some(1), Some(2)]);

    let any = planned("MATCH ANY SHORTEST (a:N {name: 'A'})-[r:K+]->(b:N) RETURN b");
    let any_table = execute_on_graph(&graph, &any).expect("ANY SHORTEST executes");
    let mut any_bs = node_ids_for(&any_table, "b");
    any_bs.sort();
    assert_eq!(bs, any_bs);
}

#[test]
fn unbounded_shortest_one_group_terminates_and_equals_all_shortest_on_cycle() {
    let graph = cycle_graph();
    let counted = planned("MATCH SHORTEST 1 GROUP (a:N {name: 'A'})-[r:K+]->(b:N) RETURN r, b");

    let table = execute_on_graph(&graph, &counted)
        .expect("unbounded SHORTEST 1 GROUP terminates (== ALL SHORTEST)");

    // Identical to ALL SHORTEST: b=A path [1, 2], b=B path [1].
    assert_eq!(
        shortest_rows(&table),
        vec![(Some(1), Some(vec![1, 2])), (Some(2), Some(vec![1]))]
    );

    let all = planned("MATCH ALL SHORTEST (a:N {name: 'A'})-[r:K+]->(b:N) RETURN r, b");
    let all_table = execute_on_graph(&graph, &all).expect("ALL SHORTEST executes");
    assert_eq!(shortest_rows(&table), shortest_rows(&all_table));
}

#[test]
fn unbounded_shortest_bare_group_defaults_to_one_and_terminates_on_cycle() {
    // `SHORTEST GROUP` (no count) defaults groups -> 1 per ISO §16.6 SR2b, so it is
    // also the ALL SHORTEST min-length selector and must terminate on a cycle.
    let graph = cycle_graph();
    let plan = planned("MATCH SHORTEST GROUP (a:N {name: 'A'})-[r:K+]->(b:N) RETURN r, b");

    let table =
        execute_on_graph(&graph, &plan).expect("unbounded SHORTEST GROUP (groups=1) terminates");

    assert_eq!(
        shortest_rows(&table),
        vec![(Some(1), Some(vec![1, 2])), (Some(2), Some(vec![1]))]
    );
}

#[test]
fn unbounded_counted_shortest_group_two_still_program_limit_on_cycle() {
    // Boundary pin (complements the SHORTEST 2 PATH deferral above): a count-`>= 2`
    // GROUP form admits a strictly-longer second length-group, which can be
    // non-simple — so it is NOT downshiftable and stays DEFERRED (5GQL1) on an
    // unbounded cyclic graph. Only count-1 downshifts.
    let graph = cycle_graph();
    let plan = planned("MATCH SHORTEST 2 GROUPS (a:N {name: 'A'})-[r:K+]->(b:N) RETURN r");

    let err = execute_on_graph(&graph, &plan).expect_err("count>=2 group form still capped");

    assert!(matches!(
        err,
        ExecutorError::ProgramLimitExceeded {
            detail: "max_quantifier",
            ..
        }
    ));
    assert_eq!(err.gqlstatus().as_str(), "5GQL1");
}

#[test]
fn different_edges_makes_counted_shortest_finite_on_cycle() {
    // The counted-shortest DEFERRAL (5GQL1, pinned above) is specific to plain WALK,
    // whose candidate set over a cycle is infinite. With DIFFERENT EDGES (G002, ISO
    // §16.4 NOTE 222) the candidate set is constrained to edge-distinct paths (TRAIL),
    // which is finite — so `SHORTEST N DIFFERENT EDGES` over a cycle TERMINATES and
    // counts the N shortest edge-distinct paths. Correct AND complete: node-repeating-
    // but-edge-distinct trails are still counted (DIFFERENT EDGES forbids only edge
    // reuse, not node reuse), so it is NOT the deferred plain-WALK case. The
    // pre-existing different_edges -> TRAIL downshift handles this; the FU-2 shortest
    // downshift is OR-composed and does not change it.
    let graph = cycle_graph();
    let plan = planned("MATCH SHORTEST 2 DIFFERENT EDGES (a:N {name: 'A'})-[r:K+]->(b:N) RETURN r");

    let table = execute_on_graph(&graph, &plan)
        .expect("DIFFERENT EDGES bounds the candidate set to trails, so it terminates");

    // The 2 shortest edge-distinct paths from A: A->B [1] and A->B->A [1, 2].
    assert_eq!(
        edge_lists_for(&table, "r"),
        vec![Some(vec![1]), Some(vec![1, 2])]
    );
}

#[test]
fn lower_bounded_shortest_over_cycle_is_deferred_not_wrong() {
    // Codex (PR #245, P2): the WALK->TRAIL downshift is only result-equivalent when
    // the quantifier lower bound is <= 1. With `min >= 2`, removing a cycle would
    // drop below the bound, so the shortest WALK satisfying the bound can legitimately
    // REUSE an edge (e.g. on the A<->B cycle the shortest >=2-hop walk to B is the
    // edge-reusing A->B->A->B [1,2,1]). A TRAIL downshift would drop those rows and
    // return a WRONG (under-)result. So `min >= 2` shortest is NOT downshifted: it
    // stays WALK and, over a cyclic graph, is DEFERRED (5GQL1, ProgramLimitExceeded) —
    // the same posture as plain counted-shortest (both need ordered length-enumeration
    // over an infinite WALK candidate set). The point is that the engine never returns
    // a silently-truncated result here.
    let graph = cycle_graph();
    let plan = planned("MATCH ALL SHORTEST (a:N {name: 'A'})-[r:K*2..]->(b:N) RETURN r");

    let err = execute_on_graph(&graph, &plan)
        .expect_err("lower-bounded shortest over a cycle is deferred, not wrongly truncated");

    assert!(matches!(
        err,
        ExecutorError::ProgramLimitExceeded {
            detail: "max_quantifier",
            ..
        }
    ));
    assert_eq!(err.gqlstatus().as_str(), "5GQL1");
}