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