Skip to main content

relate_parser/ast/
schema.rs

1//! Index and constraint DDL clause nodes.
2
3use super::*;
4
5/// The DDL index kind (CIP-11 RANGE/POINT/TEXT, CIP-38 FULLTEXT, the
6/// pre-existing VECTOR, and the label/type-token LOOKUP index).
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum IndexKind {
9    Range,
10    Point,
11    Text,
12    Fulltext,
13    Lookup,
14    Vector,
15}
16
17impl IndexKind {
18    /// The canonical lowercase tag persisted in
19    /// `relate_storage::Index::index_type` -- also the tag the engine
20    /// switches on to decide which (if any) in-memory acceleration
21    /// structure to build for a given index (`Engine::rebuild_indexes`).
22    /// Unchanged for `Point`/`Vector` from before CIP-11 so existing
23    /// vector/point index acceleration keeps working untouched; `Range`
24    /// replaces the old ad hoc `"secondary"` tag (still accepted as a
25    /// legacy alias when reading it back).
26    pub fn storage_tag(&self) -> &'static str {
27        match self {
28            IndexKind::Range => "range",
29            IndexKind::Point => "point",
30            IndexKind::Text => "text",
31            IndexKind::Fulltext => "fulltext",
32            IndexKind::Lookup => "lookup",
33            IndexKind::Vector => "vector",
34        }
35    }
36
37    /// The upper-case display name Neo4j's `SHOW INDEXES` `type` column
38    /// uses.
39    pub fn display_name(&self) -> &'static str {
40        match self {
41            IndexKind::Range => "RANGE",
42            IndexKind::Point => "POINT",
43            IndexKind::Text => "TEXT",
44            IndexKind::Fulltext => "FULLTEXT",
45            IndexKind::Lookup => "LOOKUP",
46            IndexKind::Vector => "VECTOR",
47        }
48    }
49}
50
51/// Which kind of graph element a CREATE INDEX / CREATE CONSTRAINT `FOR`
52/// pattern targets (CIP-11's relationship-pattern generalization: `FOR
53/// (n:Label)` vs. `FOR ()-[r:TYPE]-()` in all four direction spellings).
54#[derive(Debug, Clone, Copy, PartialEq, Eq)]
55pub enum SchemaEntityType {
56    Node,
57    Relationship,
58}
59
60impl SchemaEntityType {
61    /// The upper-case display value Neo4j's `SHOW INDEXES`/`SHOW
62    /// CONSTRAINTS` `entityType` column uses.
63    pub fn as_str(&self) -> &'static str {
64        match self {
65            SchemaEntityType::Node => "NODE",
66            SchemaEntityType::Relationship => "RELATIONSHIP",
67        }
68    }
69}
70
71/// The name of an index or constraint in CREATE INDEX / CREATE CONSTRAINT /
72/// DROP INDEX / DROP CONSTRAINT: either a literal identifier or a `$param`
73/// reference resolved against the query's parameter map at execution time
74/// (CIP-134).
75#[derive(Debug, Clone, PartialEq)]
76pub enum SchemaName {
77    Literal(String),
78    Parameter(String),
79}
80
81/// The optional `OPTIONS` tail of a schema DDL command (ADR-0489):
82/// the three states the old `options` + `options_param` pair
83/// encoded in a comment only.
84#[derive(Debug, Clone, PartialEq)]
85pub enum OptionsForm {
86    /// No `OPTIONS` clause at all.
87    Absent,
88    /// `OPTIONS { key: <value expression>, ... }`: lowered and evaluated at
89    /// execution time.
90    Literal(Vec<(String, Expr)>),
91    /// `OPTIONS $param`: the parameter must evaluate to a MAP at execution
92    /// time, whose entries become the command's literal options.
93    Parameter(String),
94}
95
96impl OptionsForm {
97    /// The literal map's entries — `None` for the two forms that carry no
98    /// literal map, so no caller can mistake an absent clause or an
99    /// unresolved `$param` for an empty map.
100    pub fn literal(&self) -> Option<&[(String, Expr)]> {
101        match self {
102            Self::Literal(entries) => Some(entries),
103            Self::Absent | Self::Parameter(_) => None,
104        }
105    }
106
107    /// The unresolved parameter's name, if this is that form.
108    pub fn parameter(&self) -> Option<&str> {
109        match self {
110            Self::Parameter(name) => Some(name),
111            Self::Absent | Self::Literal(_) => None,
112        }
113    }
114
115    /// The literal map's entries for in-place rewriting — `None` for the two
116    /// forms that carry no literal map, the mutable twin of [`Self::literal`].
117    pub fn literal_mut(&mut self) -> Option<&mut [(String, Expr)]> {
118        match self {
119            Self::Literal(entries) => Some(entries),
120            Self::Absent | Self::Parameter(_) => None,
121        }
122    }
123}
124
125/// CREATE INDEX clause.
126#[derive(Debug, Clone, PartialEq)]
127pub struct CreateIndexClause {
128    pub name: Option<SchemaName>,
129    pub entity_type: SchemaEntityType,
130    /// Node labels, or (for a relationship index) relationship types, the
131    /// index applies to. More than one entry only for the `A|B` disjunctive
132    /// form (CIP-38/CIP-235); empty for a label-less/type-less LOOKUP index
133    /// (`FOR (n)` / `FOR ()-[r]-()`).
134    pub labels: Vec<SmallName>,
135    pub properties: Vec<SmallName>,
136    pub if_not_exists: bool,
137    pub kind: IndexKind,
138    /// True when the property list was written as `ON EACH [...]` (CIP-38
139    /// FULLTEXT) rather than a plain `ON (...)` / paren-less `ON ...` list.
140    pub on_each: bool,
141    /// `WITH [var.prop1, var.prop2, ...]` (CIP-235): additional properties
142    /// available for single-stage filtering on a VECTOR index, following
143    /// the `ON` vector property. Always empty for every other index kind
144    /// and for a vector index that doesn't use the clause.
145    pub with_properties: Vec<SmallName>,
146    /// The `OPTIONS` tail: absent, a literal map, or a whole-map parameter
147    /// (resolved at execution time).
148    pub options: OptionsForm,
149}
150
151/// DROP INDEX clause.
152#[derive(Debug, Clone, PartialEq)]
153pub struct DropIndexClause {
154    pub name: SchemaName,
155    pub if_exists: bool,
156    /// Legacy `DROP INDEX ON :Label(property)` target.
157    pub legacy_target: Option<(SmallName, SmallName)>,
158}
159
160/// CREATE CONSTRAINT clause.
161#[derive(Debug, Clone, PartialEq)]
162pub struct CreateConstraintClause {
163    pub name: Option<SchemaName>,
164    pub entity_type: SchemaEntityType,
165    pub label: SmallName,
166    pub variable: SmallName,
167    pub properties: Vec<SmallName>,
168    pub constraint_type: String, // "unique", "exists", "key", "propertyType"
169    pub if_not_exists: bool,
170    /// The parsed `<value type>` for a `REQUIRE (n.prop) IS :: <type>` /
171    /// `IS TYPED <type>` property type constraint (CIP-85), `None` for
172    /// every other `constraint_type`.
173    pub property_type: Option<TypeExpr>,
174    /// The `OPTIONS` tail: absent, a literal map, or a whole-map parameter
175    /// (resolved at execution time).
176    pub options: OptionsForm,
177    /// The target label for a `REQUIRE <alias>:<Label>` label
178    /// existence constraint (`constraint_type` one of `"labelExistence"`/
179    /// `"sourceLabelExistence"`/`"targetLabelExistence"`), `None` for
180    /// every other `constraint_type`.
181    pub enforced_label: Option<SmallName>,
182}
183
184/// DROP CONSTRAINT clause.
185#[derive(Debug, Clone, PartialEq)]
186pub struct DropConstraintClause {
187    pub name: SchemaName,
188    pub if_exists: bool,
189}