Skip to main content

relate_parser/ast/
graph_type.rs

1//! Graph-type DDL clause nodes (`CREATE/ALTER/SHOW ... GRAPH TYPE`) and the graph-type body.
2
3use super::*;
4
5/// See `Clause::AlterCurrentGraphType`.
6#[derive(Debug, Clone, PartialEq)]
7pub struct AlterCurrentGraphTypeClause {
8    pub verb: AlterGraphTypeVerb,
9    pub target: AlterCurrentGraphTypeTarget,
10}
11
12/// What an `ALTER CURRENT GRAPH TYPE` verb applies: an inline `{ <body> }`,
13/// or — ADR-0211's `ALTER CURRENT GRAPH TYPE SET GRAPH TYPE <name>` — a
14/// reference to a `CREATE GRAPH TYPE`-declared named type, promoted to
15/// current. The invalid combinations (a verb body *and* a named reference)
16/// are unrepresentable; no parser placeholder and no consumer-side doc
17/// invariant.
18#[derive(Debug, Clone, PartialEq)]
19pub enum AlterCurrentGraphTypeTarget {
20    Body(GraphTypeBody),
21    Named(SmallName),
22}
23
24/// See `Clause::ShowCurrentGraphType`.
25#[derive(Debug, Clone, PartialEq, Default)]
26pub struct ShowCurrentGraphTypeClause {
27    pub yields: YieldList,
28    /// Paging stages following YIELD, preserved in source order.
29    pub stages: Vec<PageStage>,
30    /// CIP-184: an optional trailing `WHERE <expr>`, permitted with or
31    /// without an explicit `YIELD` (mirrors `ShowDatabasesClause`).
32    pub where_expr: Option<Expr>,
33    /// `AS GRAPH [SINGLE | SEPARATE]` -- renders the current graph
34    /// type as `nodes`/`relationships` columns instead of the plain
35    /// `specification` string column. `None` when `AS GRAPH` wasn't
36    /// written at all.
37    pub as_graph: Option<AnyNodeMode>,
38}
39
40/// CIP-257's "Future improvements" any-node keyword, controlling how many
41/// synthetic `(:AnyLabel {})` nodes an unconstrained relationship endpoint
42/// gets: `Separate` (the default, whether or not the keyword is written) --
43/// one any-node per relationship that needs one; `Single` -- every
44/// unconstrained endpoint across the whole graph type shares one any-node.
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
46pub enum AnyNodeMode {
47    #[default]
48    Separate,
49    Single,
50}
51
52/// The four `ALTER CURRENT GRAPH TYPE` verbs (CIP-176/CIP-182 overview
53/// scope): `SET` replaces the whole current graph type (and, as observed
54/// live, every constraint in the database); `ADD`
55/// extends it; `ALTER` replaces an existing element type (matched by
56/// identifying label) with a new declaration; `DROP` removes the element
57/// types named in its body.
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
59pub enum AlterGraphTypeVerb {
60    Set,
61    Add,
62    Alter,
63    Drop,
64}
65
66/// See `Clause::CreateGraphType`.
67#[derive(Debug, Clone, PartialEq)]
68pub struct CreateGraphTypeClause {
69    pub name: SmallName,
70    pub body: GraphTypeBody,
71    /// Whether the `AS` spelling was used (the scaffolding form,
72    /// `CREATE GRAPH TYPE <name> AS { <body> }`, vs. CIP-178's plain
73    /// `CREATE GRAPH TYPE <name> { <body> }`). The worked
74    /// example (`()-[:REL =>]->()`, no properties, both endpoints the any
75    /// type) is an ineffectual single-label edge
76    /// type -- the `AS` form exists to exercise the CIP-180 canonicalizer,
77    /// so the engine's `CreateGraphType` handler exempts it from the
78    /// ineffectual-type rejection and
79    /// the one-index-backing-per-subject clash (both shapes the canonicalizer's own
80    /// scenarios use), and validates every other body rule normally.
81    pub via_as: bool,
82}
83
84/// See `Clause::ShowGraphType`.
85#[derive(Debug, Clone, PartialEq)]
86pub struct ShowGraphTypeClause {
87    pub name: SmallName,
88    pub yields: YieldList,
89}
90
91/// A fully parsed `<graph type specification body>` (CIP-178): the `{
92/// ... }` contents shared by `CreateGraphTypeClause` and
93/// `AlterCurrentGraphTypeClause`. Parsing is grammar-only (always succeeds
94/// once the tokens match); the CIP-178 "shall" rules (alias/label
95/// uniqueness, reference resolution, ...) are enforced separately by
96/// `relate-executor`'s graph-type validator (`validate_graph_type_body`),
97/// which only `CREATE GRAPH
98/// TYPE` invokes -- `ALTER CURRENT GRAPH TYPE` bodies legitimately reference
99/// element types declared by an *earlier* statement (e.g. `ADD`), which a
100/// single-body validation pass cannot see.
101#[derive(Debug, Clone, PartialEq, Default)]
102pub struct GraphTypeBody {
103    pub node_types: Vec<GraphTypeNodeTypeSpec>,
104    pub edge_types: Vec<GraphTypeEdgeTypeSpec>,
105    pub constraints: Vec<GraphTypeConstraintSpec>,
106    /// `<constraint drop specification>` = `CONSTRAINT <name>` (bare,
107    /// no `FOR ...`) -- only allowed in an `ALTER CURRENT GRAPH TYPE DROP {
108    /// ... }` body. The parser reads it in any body, and every other verb
109    /// rejects a non-empty list as a parse error
110    /// (`Parser::reject_bare_constraint_drops`), so this is always empty for
111    /// `CREATE GRAPH TYPE`/`SET`/`ADD`/`ALTER` bodies.
112    pub drop_constraint_names: Vec<SmallName>,
113}
114
115/// `'(' [alias] (':'|IS) <identifying label> ('=>'|IMPLIES) [(':'|IS)
116/// <implied label> ('&' <implied label>)*] ['{' <property types> '}'] ')'`
117/// (`<node type specification>`).
118#[derive(Debug, Clone, PartialEq)]
119pub struct GraphTypeNodeTypeSpec {
120    pub alias: Option<SmallName>,
121    pub identifying_label: SmallName,
122    pub implied_labels: Vec<SmallName>,
123    pub properties: Vec<GraphTypePropertyType>,
124}
125
126/// `<source endpoint> '-' '[' [alias] (':'|IS) <identifying label>
127/// ('=>'|IMPLIES) ['{' <property types> '}'] ']' '->' <destination
128/// endpoint>` (`<edge type specification>`).
129#[derive(Debug, Clone, PartialEq)]
130pub struct GraphTypeEdgeTypeSpec {
131    pub alias: Option<SmallName>,
132    pub identifying_label: SmallName,
133    pub properties: Vec<GraphTypePropertyType>,
134    pub source: GraphTypeElementRef,
135    pub target: GraphTypeElementRef,
136}
137
138/// One `<property type>`: a name, its declared value type, an optional
139/// `DEFAULT ON COMMIT <value expression>` (CIP-262), and an optional
140/// inline `IS [NODE|REL|RELATIONSHIP] (KEY|UNIQUE)` constraint (CIP-178
141/// Rules 10-12 rewrite these into standalone `GraphTypeConstraintSpec`s
142/// during registration).
143///
144/// The default value is parsed as a full expression here (grammar-only,
145/// like the rest of the body): the CIP-262 shape restriction -- a literal
146/// or an allowed type constructor, never `null` -- is enforced by the
147/// graph-type semantic layer, and the construct itself sits behind the
148/// ADR-0371 `graph-type-default-values` preview gate at statement
149/// admission (the trio's decision: ADR-0388). `None` is the no-default
150/// case.
151#[derive(Debug, Clone, PartialEq)]
152pub struct GraphTypePropertyType {
153    pub name: SmallName,
154    pub value_type: TypeExpr,
155    pub default_value: Option<Expr>,
156    pub inline_constraint: Option<GraphTypeInlineConstraint>,
157}
158
159/// An inline property-type constraint (`{prop :: TYPE IS [qualifier]
160/// KEY|UNIQUE}`). `qualifier` records which (if any) `NODE`/`REL`/
161/// `RELATIONSHIP` keyword was written, so the graph-type validator can
162/// enforce that a node type must not contain `REL`/
163/// `RELATIONSHIP`; an edge type must not contain `NODE`.
164#[derive(Debug, Clone, Copy, PartialEq, Eq)]
165pub struct GraphTypeInlineConstraint {
166    pub verb: GraphTypeInlineVerb,
167    pub qualifier: Option<GraphTypeEntityQualifier>,
168}
169
170#[derive(Debug, Clone, Copy, PartialEq, Eq)]
171pub enum GraphTypeInlineVerb {
172    Key,
173    Unique,
174}
175
176#[derive(Debug, Clone, Copy, PartialEq, Eq)]
177pub enum GraphTypeEntityQualifier {
178    Node,
179    Rel,
180}
181
182/// A `<node type reference>` / `<edge type reference>` (CIP-178): how a
183/// node/edge type spec's endpoint, or a constraint specification's `FOR`
184/// target, names the element type it applies to.
185#[derive(Debug, Clone, PartialEq)]
186pub enum GraphTypeElementRef {
187    /// `()` -- the any node/edge type.
188    Any,
189    /// `(alias)` / `()-[alias]->()` -- resolves to a previously declared
190    /// node/edge type alias (it must already be declared).
191    AliasRef(SmallName),
192    /// `[alias] (':'|IS) <label> ['=>'|IMPLIES]` -- an in-situ reference.
193    /// `implies` (`=>`/`IMPLIES` present) requires `label` to name an
194    /// already-declared identifying label; without it, the
195    /// label need not be declared. `alias` is only legal in constraint
196    /// `FOR`-clause context (forbidden on an edge type's own
197    /// endpoint references).
198    InSitu {
199        alias: Option<SmallName>,
200        label: SmallName,
201        implies: bool,
202    },
203}
204
205/// `[CONSTRAINT [<name>]] FOR <node/edge type reference> REQUIRE
206/// <property name spec> <constraint verb> [OPTIONS <map>]` (CIP-178
207/// `<constraint specification>`) -- also the target of a node/edge type
208/// spec's trailing inline `REQUIRE ...` list and a property's own inline
209/// `IS KEY`/`IS UNIQUE` (both rewritten to this same shape at parse time,
210/// per Rules 10-12).
211#[derive(Debug, Clone, PartialEq)]
212pub struct GraphTypeConstraintSpec {
213    pub name: Option<SmallName>,
214    pub target: GraphTypeElementRef,
215    /// True when `target` is an edge type reference (`FOR ()-[...]->()`);
216    /// false for a node type reference (`FOR (...)`).
217    pub is_edge: bool,
218    pub properties: Vec<SmallName>,
219    pub kind: GraphTypeConstraintVerb,
220    /// The parsed `<value type>` for a `PropertyType`-kind constraint,
221    /// `None` for every other kind.
222    pub property_type: Option<TypeExpr>,
223    /// The `OPTIONS` tail: absent, a literal map, or a whole-map parameter
224    /// (resolved before the body is captured/persisted).
225    pub options: OptionsForm,
226}
227
228#[derive(Debug, Clone, PartialEq, Eq)]
229pub enum GraphTypeConstraintVerb {
230    Key,
231    Unique,
232    Exists,
233    PropertyType,
234}