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
//! Write-side parser coverage for BRIEF-18.

use selene_gql::{
    DdlStatement, DeleteMode, DropBehavior, GqlStatus, GqlType, MutationStatement,
    MutationTerminator, ParserError, PipelineStatement, Statement, TypePropertyConstraint,
    ValidationMode, YieldColumn, parse,
};

fn parse_mutation(source: &str) -> selene_gql::MutationPipeline {
    let Statement::Mutate(pipeline) = parse(source).expect("mutation parses") else {
        panic!("expected mutation pipeline");
    };
    pipeline
}

fn parse_ddl(source: &str) -> DdlStatement {
    let Statement::Ddl(statement) = parse(source).expect("DDL parses") else {
        panic!("expected DDL statement");
    };
    statement
}

#[test]
fn parse_insert_single_node() {
    let pipeline = parse_mutation("INSERT (n:Person {name: 'Alice'})");
    let [MutationStatement::Insert(insert)] = pipeline.statements.as_slice() else {
        panic!("expected single INSERT");
    };
    assert_eq!(insert.patterns.len(), 1);
    assert_eq!(insert.patterns[0].elements.len(), 1);
}

#[test]
fn parse_insert_path_and_multiple_patterns() {
    let pipeline = parse_mutation("INSERT (a:Person), (b:Company), (a)-[:WORKS_AT]->(b)");
    let [MutationStatement::Insert(insert)] = pipeline.statements.as_slice() else {
        panic!("expected single INSERT");
    };
    assert_eq!(insert.patterns.len(), 3);
    assert_eq!(insert.patterns[2].elements.len(), 3);
}

#[test]
fn parse_set_variants() {
    let pipeline = parse_mutation(
        "MATCH (n:Person) SET n.age = 30, n.active = true, n = {score: 5}, n :Engineer",
    );
    let [MutationStatement::Match(_), MutationStatement::Set(items)] =
        pipeline.statements.as_slice()
    else {
        panic!("expected MATCH then SET");
    };
    assert_eq!(items.len(), 4);
}

#[test]
fn parse_remove_variants() {
    let pipeline = parse_mutation("MATCH (n) REMOVE n.age, n :Engineer");
    let [
        MutationStatement::Match(_),
        MutationStatement::Remove(items),
    ] = pipeline.statements.as_slice()
    else {
        panic!("expected MATCH then REMOVE");
    };
    assert_eq!(items.len(), 2);
}

#[test]
fn parse_delete_modes() {
    for (source, mode) in [
        ("MATCH (n) DELETE n", DeleteMode::Bare),
        ("MATCH (n) DETACH DELETE n", DeleteMode::Detach),
        ("MATCH (n) NODETACH DELETE n", DeleteMode::NoDetach),
    ] {
        let pipeline = parse_mutation(source);
        let [
            MutationStatement::Match(_),
            MutationStatement::Delete(delete),
        ] = pipeline.statements.as_slice()
        else {
            panic!("expected MATCH then DELETE");
        };
        assert_eq!(delete.mode, mode);
        assert_eq!(delete.items.len(), 1);
    }
}

#[test]
fn parse_finish_terminator() {
    let pipeline = parse_mutation("INSERT (n:Person {name: 'Y'}) FINISH");
    assert!(matches!(
        pipeline.terminator,
        Some(MutationTerminator::Finish(_))
    ));
}

#[test]
fn parse_graph_ddl() {
    // CREATE GRAPH stays parse-rejected under D1 single-graph (GC04 unsupported
    // — cannot create a second graph).
    for source in [
        "CREATE GRAPH foo",
        "CREATE GRAPH IF NOT EXISTS foo",
        "CREATE GRAPH foo ANY",
        "CREATE GRAPH foo TYPED fooType",
        "CREATE GRAPH foo ::fooType",
        "CREATE GRAPH /foo LIKE /bar",
        "CREATE GRAPH foo ANY AS COPY OF bar",
        "CREATE GRAPH foo {(Person :Person {name STRING})}",
    ] {
        let error = parse(source).expect_err(source);
        assert_eq!(error.gqlstatus(), GqlStatus::FEATURE_NOT_SUPPORTED);
    }
    // DROP GRAPH now parses (BRIEF-152): it is the IM_DROP_GRAPH factory-reset
    // extension, reaching the executor instead of dying in the flagger. Under
    // D1 the parsed name is informational and IF EXISTS is trivially satisfied.
    for source in ["DROP GRAPH foo", "DROP GRAPH IF EXISTS foo"] {
        let DdlStatement::DropGraph { if_exists, .. } = parse_ddl(source) else {
            panic!("expected DROP GRAPH DDL for {source}");
        };
        assert_eq!(if_exists, source.contains("IF EXISTS"), "{source}");
    }
}

#[test]
fn parse_type_ddl() {
    let DdlStatement::CreateNodeType {
        label, properties, ..
    } = parse_ddl("CREATE NODE TYPE :Person (name :: STRING, age :: INT NOT NULL)")
    else {
        panic!("expected CREATE NODE TYPE");
    };
    assert_eq!(label.as_str(), "Person");
    assert_eq!(properties.len(), 2);
    assert!(matches!(
        &properties[1].gql_type,
        GqlType::NotNull(inner) if **inner == GqlType::Integer
    ));
    assert!(properties[1].constraints.is_empty());

    let DdlStatement::CreateNodeType {
        extends,
        validation_mode,
        ..
    } = parse_ddl("CREATE NODE TYPE IF NOT EXISTS :Person EXTENDS :Entity (id :: STRING) STRICT")
    else {
        panic!("expected CREATE NODE TYPE");
    };
    assert_eq!(extends.expect("parent").as_str(), "Entity");
    assert_eq!(validation_mode, Some(ValidationMode::Strict));
}

#[test]
fn parse_edge_type_and_show_ddl() {
    let DdlStatement::CreateEdgeType {
        label,
        extends,
        endpoints,
        properties,
        ..
    } = parse_ddl(
        "CREATE EDGE TYPE :KNOWS EXTENDS :RELATIONSHIP (FROM :Person TO :Person, since :: DATE)",
    )
    else {
        panic!("expected CREATE EDGE TYPE");
    };
    assert_eq!(label.as_str(), "KNOWS");
    assert_eq!(extends.expect("parent").as_str(), "RELATIONSHIP");
    let endpoints = endpoints.expect("endpoint spec");
    assert_eq!(endpoints.from_labels[0].as_str(), "Person");
    assert_eq!(properties[0].gql_type, GqlType::Date);

    // Default (no behavior keyword) lowers to RESTRICT.
    assert!(matches!(
        parse_ddl("DROP NODE TYPE IF EXISTS :Person"),
        DdlStatement::DropNodeType {
            if_exists: true,
            behavior: DropBehavior::Restrict,
            ..
        }
    ));
    assert!(matches!(
        parse_ddl("DROP EDGE TYPE :KNOWS"),
        DdlStatement::DropEdgeType {
            behavior: DropBehavior::Restrict,
            ..
        }
    ));
    // Explicit CASCADE / RESTRICT keywords are carried on the AST.
    assert!(matches!(
        parse_ddl("DROP NODE TYPE :Person CASCADE"),
        DdlStatement::DropNodeType {
            behavior: DropBehavior::Cascade,
            ..
        }
    ));
    assert!(matches!(
        parse_ddl("DROP EDGE TYPE :KNOWS RESTRICT"),
        DdlStatement::DropEdgeType {
            behavior: DropBehavior::Restrict,
            ..
        }
    ));
    assert!(matches!(
        parse_ddl("SHOW NODE TYPES"),
        DdlStatement::ShowNodeTypes(_)
    ));
    assert!(matches!(
        parse_ddl("SHOW EDGE TYPES"),
        DdlStatement::ShowEdgeTypes(_)
    ));
}

#[test]
fn parse_type_property_constraints_exhaustively() {
    // The ISO/IEC 39075:2024 §18 property constraints the engine accepts:
    // DEFAULT, IMMUTABLE, UNIQUE, INDEXED. Explicit value-type nullability is
    // carried on the GqlType and lowered to the required-property bit at the
    // catalog boundary. Donor full-text/time-series constraints (SEARCHABLE/
    // DICTIONARY/FILL/INTERVAL/ENCODING) were removed from the grammar — see
    // `donor_property_constraints_are_syntax_errors`.
    let DdlStatement::CreateNodeType { properties, .. } = parse_ddl(
        "CREATE NODE TYPE :Sensor (v :: STRING NOT NULL DEFAULT 'x' IMMUTABLE UNIQUE INDEXED)",
    ) else {
        panic!("expected CREATE NODE TYPE");
    };
    assert!(matches!(
        &properties[0].gql_type,
        GqlType::NotNull(inner) if **inner == GqlType::String
    ));
    assert_eq!(properties[0].constraints.len(), 4);
}

#[test]
fn donor_property_constraints_are_syntax_errors() {
    // SEARCHABLE/DICTIONARY/FILL/INTERVAL/ENCODING are donor full-text/
    // time-series residue with no place in ISO/IEC 39075:2024; the grammar no
    // longer recognizes them, so they fail as 42001 syntax errors at parse time.
    for source in [
        "CREATE NODE TYPE :S (v :: STRING SEARCHABLE)",
        "CREATE NODE TYPE :S (v :: STRING DICTIONARY)",
        "CREATE NODE TYPE :S (v :: STRING FILL LOCF)",
        "CREATE NODE TYPE :S (v :: STRING INTERVAL '60s')",
        "CREATE NODE TYPE :S (v :: STRING ENCODING RLE)",
    ] {
        let err = selene_gql::parse(source).expect_err("donor constraint should be rejected");
        assert_eq!(
            err.gqlstatus(),
            selene_gql::GqlStatus::SYNTAX_ERROR,
            "expected 42001 for {source:?}"
        );
    }
}

#[test]
fn parse_indexed_constraint_with_explicit_name() {
    let DdlStatement::CreateNodeType { properties, .. } =
        parse_ddl("CREATE NODE TYPE :Sensor (v :: STRING INDEXED AS sensor_v_idx)")
    else {
        panic!("expected CREATE NODE TYPE");
    };
    let TypePropertyConstraint::Indexed {
        name: Some(ref name),
        ..
    } = properties[0].constraints[0]
    else {
        panic!("expected named INDEXED constraint");
    };
    assert_eq!(name.as_str(), "sensor_v_idx");
}

#[test]
fn parse_named_index_ddl() {
    let DdlStatement::CreateIndex {
        name,
        label,
        properties,
        if_not_exists,
        ..
    } = parse_ddl("CREATE INDEX IF NOT EXISTS sensor_ts_idx ON :Sensor(ts, value)")
    else {
        panic!("expected CREATE INDEX");
    };
    assert_eq!(name.as_str(), "sensor_ts_idx");
    assert_eq!(label.as_str(), "Sensor");
    assert_eq!(
        properties
            .iter()
            .map(|property| property.as_str())
            .collect::<Vec<_>>(),
        ["ts", "value"]
    );
    assert!(if_not_exists);

    let DdlStatement::DropIndex {
        name, if_exists, ..
    } = parse_ddl("DROP INDEX IF EXISTS sensor_ts_idx")
    else {
        panic!("expected DROP INDEX");
    };
    assert_eq!(name.as_str(), "sensor_ts_idx");
    assert!(if_exists);
}

#[test]
fn parse_top_level_call_variants() {
    let Statement::Call(call) = parse("CALL pkg.fn(1, 'hello') YIELD col1").expect("CALL parses")
    else {
        panic!("expected top-level CALL");
    };
    assert_eq!(
        call.name
            .iter()
            .map(|part| part.as_str())
            .collect::<Vec<_>>(),
        ["pkg", "fn"]
    );
    assert_eq!(call.args.len(), 2);
    assert_eq!(call.yield_items.len(), 1);

    let Statement::Call(call) = parse("CALL pkg.cleanup()").expect("CALL without YIELD parses")
    else {
        panic!("expected top-level CALL");
    };
    assert!(call.yield_items.is_empty());
}

#[test]
fn parse_call_yield_star_alias_and_quoted_segment() {
    let Statement::Call(call) =
        parse("CALL foo.\"bar.baz\"() YIELD *, result AS alias").expect("CALL parses")
    else {
        panic!("expected top-level CALL");
    };
    assert_eq!(call.name[1].as_str(), "bar.baz");
    assert!(matches!(call.yield_items[0].column, YieldColumn::Star));
    assert_eq!(
        call.yield_items[1].alias.clone().expect("alias").as_str(),
        "alias"
    );
}

#[test]
fn call_yield_where_is_not_iso_syntax() {
    for source in [
        "CALL pkg.rank() YIELD score WHERE score >= 0",
        "CALL pkg.rank() YIELD score AS s WHERE s >= 0",
    ] {
        let error = parse(source).expect_err(source);
        assert!(
            matches!(error, ParserError::SyntaxError { .. }),
            "{source} should be a syntax error, got {error:?}"
        );
    }
}

#[test]
fn parse_in_pipeline_call() {
    let Statement::Query(query) =
        parse("MATCH (n) CALL pkg.fn(n) YIELD col RETURN col").expect("query with CALL parses")
    else {
        panic!("expected query");
    };
    assert!(matches!(query.statements[1], PipelineStatement::Call(_)));
}

#[test]
fn parse_transaction_control() {
    assert!(matches!(
        parse("START TRANSACTION").expect("parses"),
        Statement::StartTransaction { .. }
    ));
    assert!(matches!(
        parse("COMMIT").expect("parses"),
        Statement::Commit { .. }
    ));
    assert!(matches!(
        parse("ROLLBACK").expect("parses"),
        Statement::Rollback { .. }
    ));
}

#[test]
fn deferred_surfaces_return_not_implemented() {
    // These remain ISO-legal but are deferred under D1, so they parse and the
    // builder rejects them with FEATURE_NOT_SUPPORTED (42N01).
    for source in ["MERGE (n:Person {name: 'X'})", "SELECT * FROM g"] {
        let error = parse(source).expect_err(source);
        assert_eq!(
            error.gqlstatus(),
            GqlStatus::FEATURE_NOT_SUPPORTED,
            "{source}"
        );
    }
}

#[test]
fn removed_non_iso_grammar_is_syntax_error() {
    // Triggers, materialized views, procedure DDL, and auth (users/roles/grants)
    // are out of spec entirely (auth = embedder concern D1; procedures are native
    // built-ins; triggers/views are not in the D1 claim list). They have no
    // ISO equivalent and were removed from the grammar, so they now fail to
    // parse with SYNTAX_ERROR (42601) rather than FEATURE_NOT_SUPPORTED.
    for source in [
        "CREATE TRIGGER trig AFTER INSERT ON :Person EXECUTE SET n.x = 1",
        "CREATE MATERIALIZED VIEW v AS MATCH (n) RETURN n",
        "CREATE PROCEDURE pkg.fn() { RETURN 1 }",
        "CREATE USER alice SET PASSWORD 'pw'",
        "CREATE ROLE admin",
        "GRANT ROLE admin TO alice",
        "REVOKE ROLE admin FROM alice",
        "DROP TRIGGER trig",
        "SHOW TRIGGERS",
        "MATCH VIEW v YIELD x",
    ] {
        let error = parse(source).expect_err(source);
        assert_eq!(error.gqlstatus(), GqlStatus::SYNTAX_ERROR, "{source}");
    }
}

#[test]
fn call_where_without_yield_is_syntax_error() {
    let error = parse("CALL f() WHERE x = 1").expect_err("CALL WHERE without YIELD");
    assert_eq!(error.gqlstatus(), GqlStatus::SYNTAX_ERROR);
}