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
//! Preflight validation for read-side AST formatting.

use crate::ast::{
    EdgePattern, ExistsBody, GqlType, GraphPattern, InlineProcedureCall, IsCheckKind, MatchClause,
    NodePattern, PatternElement, ProcedureCall, QueryPipeline, RecordType, ReturnClause, Statement,
    ValueExpr, WithClause,
};

use super::FormatError;

/// Validate that a read-side AST can be rendered without semantic loss.
///
/// # Errors
///
/// Returns [`FormatError::Unsupported`] when the tree contains AST-only type
/// variants that the parser cannot read back from formatted source.
pub(crate) fn validate_formattable(stmt: &Statement) -> Result<(), FormatError> {
    match stmt {
        Statement::Query(pipeline) => validate_pipeline(pipeline),
        Statement::Composite { first, rest, .. } => {
            validate_pipeline(first)?;
            for (_, pipeline) in rest {
                validate_pipeline(pipeline)?;
            }
            Ok(())
        }
        Statement::Chained { blocks, .. } => {
            for block in blocks {
                validate_pipeline(block)?;
            }
            Ok(())
        }
        Statement::Mutate(_)
        | Statement::Ddl(_)
        | Statement::Call(_)
        | Statement::Explain { .. }
        | Statement::StartTransaction { .. }
        | Statement::Commit { .. }
        | Statement::Rollback { .. }
        | Statement::SessionSetValue { .. }
        | Statement::SessionSetTimeZone { .. }
        | Statement::SessionSetGraph { .. }
        | Statement::SessionReset { .. }
        | Statement::SessionClose { .. } => Ok(()),
    }
}

fn validate_pipeline(pipeline: &QueryPipeline) -> Result<(), FormatError> {
    for statement in &pipeline.statements {
        match statement {
            crate::PipelineStatement::Match(value) => validate_match(value)?,
            crate::PipelineStatement::Filter(value) => validate_expr(value)?,
            crate::PipelineStatement::Let(values) => {
                for value in values {
                    validate_expr(&value.value)?;
                }
            }
            crate::PipelineStatement::For(value) => validate_expr(&value.source)?,
            crate::PipelineStatement::Sorting(values) => {
                for value in values {
                    validate_expr(&value.expr)?;
                }
            }
            crate::PipelineStatement::Limit(_) | crate::PipelineStatement::Offset(_) => {}
            crate::PipelineStatement::Return(value) => validate_return(value)?,
            crate::PipelineStatement::With(value) => validate_with(value)?,
            crate::PipelineStatement::Call(value) => validate_procedure_call(value)?,
            crate::PipelineStatement::CallSubquery(value) => validate_inline_call(value)?,
        }
    }
    Ok(())
}

fn validate_match(clause: &MatchClause) -> Result<(), FormatError> {
    for pattern in &clause.patterns {
        validate_graph_pattern(pattern)?;
    }
    if let Some(where_clause) = &clause.where_clause {
        validate_expr(where_clause)?;
    }
    Ok(())
}

fn validate_graph_pattern(pattern: &GraphPattern) -> Result<(), FormatError> {
    for element in &pattern.elements {
        match element {
            PatternElement::Node(node) => validate_node_pattern(node)?,
            PatternElement::Edge(edge) => validate_edge_pattern(edge)?,
        }
    }
    Ok(())
}

fn validate_node_pattern(node: &NodePattern) -> Result<(), FormatError> {
    for (_, value) in &node.properties {
        validate_expr(value)?;
    }
    if let Some(where_clause) = &node.inline_where {
        validate_expr(where_clause)?;
    }
    Ok(())
}

fn validate_edge_pattern(edge: &EdgePattern) -> Result<(), FormatError> {
    for (_, value) in &edge.properties {
        validate_expr(value)?;
    }
    if let Some(where_clause) = &edge.inline_where {
        validate_expr(where_clause)?;
    }
    Ok(())
}

fn validate_return(clause: &ReturnClause) -> Result<(), FormatError> {
    if clause.star && clause.group_by.is_some() {
        return Err(FormatError::Invalid {
            reason: "RETURN * cannot specify GROUP BY",
        });
    }
    for item in &clause.items {
        validate_expr(&item.expr)?;
    }
    if let Some(group_by) = &clause.group_by {
        for item in group_by {
            validate_expr(item)?;
        }
    }
    if let Some(having) = &clause.having {
        validate_expr(having)?;
    }
    Ok(())
}

fn validate_with(clause: &WithClause) -> Result<(), FormatError> {
    for item in &clause.items {
        validate_expr(&item.expr)?;
    }
    if let Some(group_by) = &clause.group_by {
        for item in group_by {
            validate_expr(item)?;
        }
    }
    if let Some(having) = &clause.having {
        validate_expr(having)?;
    }
    if let Some(where_clause) = &clause.where_clause {
        validate_expr(where_clause)?;
    }
    Ok(())
}

pub(super) fn validate_procedure_call(call: &ProcedureCall) -> Result<(), FormatError> {
    for arg in &call.args {
        validate_expr(arg)?;
    }
    Ok(())
}

pub(super) fn validate_inline_call(call: &InlineProcedureCall) -> Result<(), FormatError> {
    validate_pipeline(&call.body)
}

fn validate_expr(expr: &ValueExpr) -> Result<(), FormatError> {
    match expr {
        ValueExpr::Literal(_) | ValueExpr::Variable { .. } => Ok(()),
        ValueExpr::Parameter { declared_type, .. } => {
            if let Some(ty) = declared_type {
                validate_type(ty)?;
            }
            Ok(())
        }
        ValueExpr::PropertyAccess { target, .. } => validate_expr(target),
        ValueExpr::ListLiteral { items, .. } => validate_exprs(items),
        ValueExpr::PathConstructor { elements, .. } => validate_exprs(elements),
        ValueExpr::RecordLiteral { fields, .. } => {
            for (_, value) in fields {
                validate_expr(value)?;
            }
            Ok(())
        }
        ValueExpr::BinaryOp { lhs, rhs, .. } => {
            validate_expr(lhs)?;
            validate_expr(rhs)
        }
        ValueExpr::UnaryOp { operand, .. } => validate_expr(operand),
        ValueExpr::FunctionCall { args, .. } => validate_exprs(args),
        ValueExpr::DurationBetween { start, end, .. } => {
            validate_expr(start)?;
            validate_expr(end)
        }
        ValueExpr::Normalize { source, .. } => validate_expr(source),
        ValueExpr::Trim {
            character, source, ..
        } => {
            if let Some(character) = character {
                validate_expr(character)?;
            }
            validate_expr(source)
        }
        ValueExpr::IsCheck { operand, kind, .. } => {
            validate_expr(operand)?;
            validate_is_check(kind)
        }
        ValueExpr::InList { operand, list, .. } => {
            validate_expr(operand)?;
            validate_exprs(list)
        }
        ValueExpr::InListExpression { operand, list, .. } => {
            validate_expr(operand)?;
            validate_expr(list)
        }
        ValueExpr::AllDifferent { items, .. } | ValueExpr::Same { items, .. } => {
            validate_exprs(items)
        }
        ValueExpr::PropertyExists { target, .. } => validate_expr(target),
        ValueExpr::Case {
            branches,
            else_branch,
            ..
        } => {
            for (condition, result) in branches {
                validate_expr(condition)?;
                validate_expr(result)?;
            }
            if let Some(value) = else_branch {
                validate_expr(value)?;
            }
            Ok(())
        }
        ValueExpr::Exists { body, .. } => match body {
            ExistsBody::Match(pattern) => validate_match(pattern),
            ExistsBody::Query(pipeline) => validate_pipeline(pipeline),
        },
        ValueExpr::ValueSubquery { body, .. } => validate_pipeline(body),
        ValueExpr::Cast {
            value, target_type, ..
        } => {
            validate_type(target_type)?;
            validate_expr(value)
        }
    }
}

fn validate_exprs(items: &[ValueExpr]) -> Result<(), FormatError> {
    for item in items {
        validate_expr(item)?;
    }
    Ok(())
}

fn validate_is_check(kind: &IsCheckKind) -> Result<(), FormatError> {
    match kind {
        IsCheckKind::Typed(ty) => validate_type(ty),
        IsCheckKind::SourceOf(value) | IsCheckKind::DestinationOf(value) => validate_expr(value),
        IsCheckKind::Null
        | IsCheckKind::Directed
        | IsCheckKind::Labeled(_)
        | IsCheckKind::TruthValue(_)
        | IsCheckKind::Normalized(_) => Ok(()),
    }
}

fn validate_type(ty: &GqlType) -> Result<(), FormatError> {
    if let Some(variant) = ast_only_type_variant(ty) {
        return Err(FormatError::Unsupported { variant });
    }
    match ty {
        GqlType::List(inner)
        | GqlType::BoundedList {
            element_type: inner,
            ..
        } => validate_type(inner)?,
        GqlType::NotNull(inner) => validate_type(inner)?,
        // Closed record types render their field structure, so each field's
        // value type must also be formattable (a nested reference type would
        // otherwise slip past the gate). The open form has no fields to check.
        GqlType::Record(RecordType::Closed(fields)) => {
            for (_name, field_ty) in fields {
                validate_type(field_ty)?;
            }
        }
        _ => {}
    }
    Ok(())
}

fn ast_only_type_variant(ty: &GqlType) -> Option<&'static str> {
    match ty {
        GqlType::NotNull(inner) => ast_only_type_variant(inner),
        GqlType::GraphRef => Some("GraphRef"),
        GqlType::TableRef(_) => Some("TableRef"),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::validate_formattable;
    use crate::{
        GqlType, IsCheckKind, Literal, PipelineStatement, QueryPipeline, RecordType, ReturnClause,
        ReturnItem, SourceSpan, Statement, ValueExpr, ast::FormatError,
    };

    #[test]
    fn preflight_accepts_open_and_closed_record_types() {
        // GV47 open and GV46 closed record TYPES round-trip through the
        // formatter, so the preflight gate must let them through.
        validate_formattable(&statement_with_type(GqlType::Record(RecordType::Open)))
            .expect("open record type is formattable");
        let closed = GqlType::Record(RecordType::Closed(vec![(
            selene_core::db_string("name").expect("db_string name"),
            GqlType::String,
        )]));
        validate_formattable(&statement_with_type(closed)).expect("closed record type formattable");
    }

    #[test]
    fn preflight_accepts_vector_types() {
        validate_formattable(&statement_with_type(GqlType::Vector))
            .expect("vector type is formattable");
        validate_formattable(&statement_with_type(GqlType::List(Box::new(
            GqlType::Vector,
        ))))
        .expect("list of vector type is formattable");
        validate_formattable(&statement_with_type(GqlType::BoundedList {
            element_type: Box::new(GqlType::Vector),
            max_len: 3,
        }))
        .expect("bounded list of vector type is formattable");
        validate_formattable(&parameter_statement_with_type(GqlType::Vector))
            .expect("typed vector parameter is formattable");
    }

    #[test]
    fn preflight_accepts_graph_element_reference_types() {
        validate_formattable(&statement_with_type(GqlType::NodeRef))
            .expect("NODE reference type is formattable");
        validate_formattable(&statement_with_type(GqlType::EdgeRef))
            .expect("EDGE reference type is formattable");
    }

    #[test]
    fn preflight_accepts_graph_element_reference_type_inside_closed_record_field() {
        let closed = GqlType::Record(RecordType::Closed(vec![(
            selene_core::db_string("ref").expect("db_string ref"),
            GqlType::NodeRef,
        )]));
        validate_formattable(&statement_with_type(closed))
            .expect("closed record with NODE reference field is formattable");
    }

    #[test]
    fn preflight_rejects_graph_ref_type() {
        assert_unsupported(GqlType::GraphRef, "GraphRef");
    }

    #[test]
    fn preflight_rejects_table_ref_type() {
        assert_unsupported(GqlType::TableRef(crate::BindingTableType::Any), "TableRef");
    }

    #[test]
    fn preflight_rejects_return_star_group_by() {
        let span = SourceSpan::default();
        let err = validate_formattable(&Statement::Query(QueryPipeline {
            statements: vec![PipelineStatement::Return(ReturnClause {
                distinct: false,
                star: true,
                items: Vec::new(),
                group_by: Some(vec![ValueExpr::Literal(Literal::Integer(1, span))]),
                having: None,
                span,
            })],
            span,
        }))
        .expect_err("RETURN * GROUP BY is invalid");
        match err {
            FormatError::Invalid { reason } => {
                assert_eq!(reason, "RETURN * cannot specify GROUP BY");
            }
            FormatError::Unsupported { .. } | FormatError::Fmt(_) => {
                panic!("expected invalid AST")
            }
        }
    }

    fn assert_unsupported(ty: GqlType, expected: &'static str) {
        let err = validate_formattable(&statement_with_type(ty)).expect_err("type is unsupported");
        match err {
            FormatError::Unsupported { variant } => assert_eq!(variant, expected),
            FormatError::Invalid { .. } | FormatError::Fmt(_) => {
                panic!("expected unsupported variant")
            }
        }
    }

    fn statement_with_type(ty: GqlType) -> Statement {
        let span = SourceSpan::default();
        Statement::Query(QueryPipeline {
            statements: vec![PipelineStatement::Return(ReturnClause {
                distinct: false,
                star: false,
                items: vec![ReturnItem {
                    expr: ValueExpr::IsCheck {
                        operand: Box::new(ValueExpr::Literal(Literal::Null(span))),
                        kind: IsCheckKind::Typed(ty),
                        negated: false,
                        span,
                    },
                    alias: None,
                    span,
                }],
                group_by: None,
                having: None,
                span,
            })],
            span,
        })
    }

    fn parameter_statement_with_type(ty: GqlType) -> Statement {
        let span = SourceSpan::default();
        Statement::Query(QueryPipeline {
            statements: vec![PipelineStatement::Return(ReturnClause {
                distinct: false,
                star: false,
                items: vec![ReturnItem {
                    expr: ValueExpr::Parameter {
                        name: selene_core::db_string("value").expect("db_string parameter name"),
                        declared_type: Some(ty),
                        span,
                    },
                    alias: None,
                    span,
                }],
                group_by: None,
                having: None,
                span,
            })],
            span,
        })
    }
}