use std::collections::BTreeMap;
use std::str::FromStr;
use relate_catalog::ConstraintDef;
use relate_core::{Result, Value};
use relate_model::wire::{Notification, NotificationKind};
use relate_model::{ConstraintType, EntityKind};
use relate_names::SmallName;
use relate_parser::{OptionsForm, SchemaName};
use crate::exec::catalog_schema::{constraint_create_gate, constraint_def};
use crate::exec::ddl::{
default_index_name, drop_target_exists, index_conflict_gate, ExistingRule, Gate, IndexFacts,
SchemaTarget,
};
use crate::graph_type::standalone_verb;
use crate::overlay::ExecGraph;
use crate::redo::{IndexDetailSpec, IndexFacts as IndexRowFacts, IndexKindSpec};
use super::catalog::schema_name_value;
pub fn ddl_notifications(
graph: &ExecGraph<'_>,
clause: &relate_ir::Clause,
params: &BTreeMap<String, Value>,
) -> Result<Vec<Notification>> {
match clause {
relate_ir::Clause::Catalog(command) => catalog_clause_notifications(graph, command, params),
relate_ir::Clause::CreateIndex {
name,
if_not_exists,
label,
property,
} => {
let name_text = name.as_ref().map(|n| n.to_string());
legacy_create_index_notifications(
graph,
name_text.as_deref(),
*if_not_exists,
label,
property,
)
}
relate_ir::Clause::DropIndex { if_exists, target } => {
Ok(legacy_drop_index_notifications(graph, *if_exists, target))
}
_ => Ok(Vec::new()),
}
}
fn catalog_clause_notifications(
graph: &ExecGraph<'_>,
clause: &relate_parser::Clause,
params: &BTreeMap<String, Value>,
) -> Result<Vec<Notification>> {
match clause {
relate_parser::Clause::CreateIndex(c) => match clause_name(c.name.as_ref(), params) {
ClauseName::Unresolvable => Ok(Vec::new()),
ClauseName::Unnamed => create_index_notifications(graph, c, None),
ClauseName::Named(name) => create_index_notifications(graph, c, Some(&name)),
},
relate_parser::Clause::CreateConstraint(c) => match clause_name(c.name.as_ref(), params) {
ClauseName::Unresolvable => Ok(Vec::new()),
ClauseName::Unnamed => create_constraint_notifications(graph, c, None, params),
ClauseName::Named(name) => {
create_constraint_notifications(graph, c, Some(&name), params)
}
},
relate_parser::Clause::DropIndex(c) => match clause_name(Some(&c.name), params) {
ClauseName::Named(name) => Ok(drop_notifications(
graph,
DropNoun::Index,
&name,
c.if_exists,
)),
ClauseName::Unnamed | ClauseName::Unresolvable => Ok(Vec::new()),
},
relate_parser::Clause::DropConstraint(c) => match clause_name(Some(&c.name), params) {
ClauseName::Named(name) => Ok(drop_notifications(
graph,
DropNoun::Constraint,
&name,
c.if_exists,
)),
ClauseName::Unnamed | ClauseName::Unresolvable => Ok(Vec::new()),
},
_ => Ok(Vec::new()),
}
}
enum ClauseName {
Unnamed,
Named(String),
Unresolvable,
}
fn clause_name(name: Option<&SchemaName>, params: &BTreeMap<String, Value>) -> ClauseName {
match name {
None => ClauseName::Unnamed,
Some(name) => match schema_name_value(name, params) {
Ok(resolved) => ClauseName::Named(resolved),
Err(_) => ClauseName::Unresolvable,
},
}
}
fn legacy_create_index_notifications(
graph: &ExecGraph<'_>,
name: Option<&str>,
if_not_exists: bool,
label: &SmallName,
property: &SmallName,
) -> Result<Vec<Notification>> {
if !if_not_exists {
return Ok(Vec::new());
}
let resolved = name
.map(str::to_string)
.unwrap_or_else(|| default_index_name(&label.to_string(), &property.to_string()));
let facts = IndexFacts::legacy_range(
resolved,
label.to_string(),
property.to_string(),
if_not_exists,
);
already_exists_index_note(graph, &facts, name, None)
}
fn legacy_drop_index_notifications(
graph: &ExecGraph<'_>,
if_exists: bool,
target: &relate_ir::DropIndexTarget,
) -> Vec<Notification> {
if !if_exists {
return Vec::new();
}
let (exists, command, name) = match target {
relate_ir::DropIndexTarget::Name(name) => (
drop_target_exists(graph, SchemaTarget::IndexName(name)).is_some(),
format!("DROP INDEX {name} IF EXISTS"),
Some(name.as_str()),
),
relate_ir::DropIndexTarget::Schema { label, property } => {
let label = label.to_string();
let property = property.to_string();
(
drop_target_exists(
graph,
SchemaTarget::IndexSchema {
label: &label,
property: &property,
},
)
.is_some(),
format!("DROP INDEX ON :{label}({property}) IF EXISTS"),
None,
)
}
};
if exists {
return Vec::new();
}
vec![does_not_exist_note(&command, name)]
}
#[derive(Clone, Copy)]
enum DropNoun {
Index,
Constraint,
}
impl DropNoun {
fn noun(self) -> &'static str {
match self {
DropNoun::Index => "INDEX",
DropNoun::Constraint => "CONSTRAINT",
}
}
}
fn drop_notifications(
graph: &ExecGraph<'_>,
noun: DropNoun,
name: &str,
if_exists: bool,
) -> Vec<Notification> {
if !if_exists {
return Vec::new();
}
let target = match noun {
DropNoun::Index => SchemaTarget::IndexName(name),
DropNoun::Constraint => SchemaTarget::ConstraintName(name),
};
if drop_target_exists(graph, target).is_some() {
return Vec::new();
}
vec![does_not_exist_note(
&format!("DROP {} {name} IF EXISTS", noun.noun()),
Some(name),
)]
}
fn create_index_notifications(
graph: &ExecGraph<'_>,
c: &relate_parser::CreateIndexClause,
name: Option<&str>,
) -> Result<Vec<Notification>> {
let mut notifications = Vec::new();
if c.kind == relate_parser::IndexKind::Vector && !vector_dimensions_given(c) {
notifications.push(Notification::new(
NotificationKind::SchemaVectorIndexDimensionsNotSpecified,
"note: successful completion - vector index dimensions not specified. \
When creating a vector index, `vector.dimensions` should be specified. \
Omitting it is allowed, but specifying dimensions ensures that only \
vectors of that size are indexed and makes dimension mismatches fail \
clearly at query time. For example, set `OPTIONS { indexConfig: { \
`vector.dimensions`: 1536 } }` when creating the index.",
));
}
if !c.if_not_exists {
return Ok(notifications);
}
let facts = IndexFacts::from_clause(
c,
name.map(str::to_string)
.unwrap_or_else(|| IndexFacts::generated_name(c)),
);
notifications.extend(already_exists_index_note(
graph,
&facts,
name,
literal_options_text(c.options.literal()),
)?);
Ok(notifications)
}
fn already_exists_index_note(
graph: &ExecGraph<'_>,
facts: &IndexFacts,
declared_name: Option<&str>,
options: Option<String>,
) -> Result<Vec<Notification>> {
let gate = index_conflict_gate(graph, facts)?;
let Gate::Skip(ExistingRule::Index(rule)) = gate else {
return Ok(Vec::new());
};
let Some(descriptor) = index_descriptor_text(&rule.index, rule.detail.as_ref()) else {
return Ok(Vec::new());
};
let Some(tail) = index_descriptor_tail(
IndexKindSpec::from(facts.kind),
EntityKind::from_relationship_flag(facts.relationship),
&facts.labels,
&facts.properties,
&facts.with_properties,
) else {
return Ok(Vec::new());
};
let command = create_index_command(
IndexKindSpec::from(facts.kind),
declared_name,
facts.if_not_exists,
&tail,
options,
);
Ok(vec![already_exists_note(&command, &descriptor)])
}
fn vector_dimensions_given(c: &relate_parser::CreateIndexClause) -> bool {
match &c.options {
OptionsForm::Absent => false,
OptionsForm::Parameter(_) => true,
OptionsForm::Literal(entries) => entries
.iter()
.filter(|(key, _)| key == super::ddl_options::KEY_INDEX_CONFIG)
.any(|(_, value)| {
let relate_parser::Expr::Map(pairs) = value else {
return false;
};
pairs.iter().any(|pair| pair.key == "vector.dimensions")
}),
}
}
fn index_descriptor_text(
index: &relate_catalog::IndexDef,
detail: Option<&IndexDetailSpec>,
) -> Option<String> {
let facts = IndexRowFacts::resolve(index, detail, None);
let tail = index_descriptor_tail(
facts.kind,
EntityKind::from_relationship_flag(facts.relationship),
&facts.labels,
&facts.properties,
&facts.with_properties,
)?;
Some(format!(
"{} INDEX {} {tail}",
facts.kind.display_name(),
index.name
))
}
fn index_descriptor_tail(
kind: IndexKindSpec,
entity: EntityKind,
labels: &[String],
properties: &[String],
with_properties: &[String],
) -> Option<String> {
if kind.is_lookup() {
let (pattern, each) = match entity {
EntityKind::Node => ("(e)", "labels(e)"),
EntityKind::Relationship => ("()-[e]-()", "type(e)"),
};
return Some(format!("FOR {pattern} ON EACH {each}"));
}
if properties.is_empty() {
return None;
}
let tokens = labels.join("|");
let pattern = match entity {
EntityKind::Node if tokens.is_empty() => "(e)".to_string(),
EntityKind::Node => format!("(e:{tokens})"),
EntityKind::Relationship if tokens.is_empty() => "()-[e]-()".to_string(),
EntityKind::Relationship => format!("()-[e:{tokens}]-()"),
};
let refs = properties
.iter()
.map(|property| format!("e.{property}"))
.collect::<Vec<_>>();
let mut text = format!("FOR {pattern} ON ({})", refs.join(", "));
if !with_properties.is_empty() {
let with = with_properties
.iter()
.map(|property| format!("e.{property}"))
.collect::<Vec<_>>();
text.push_str(&format!(" WITH [{}]", with.join(", ")));
}
Some(text)
}
fn create_index_command(
kind: IndexKindSpec,
name: Option<&str>,
if_not_exists: bool,
tail: &str,
options: Option<String>,
) -> String {
let mut command = format!("CREATE {} INDEX ", kind.display_name());
if let Some(name) = name {
command.push_str(name);
command.push(' ');
}
if if_not_exists {
command.push_str("IF NOT EXISTS ");
}
command.push_str(tail);
if let Some(options) = options {
command.push_str(&options);
}
command
}
fn literal_options_text(options: Option<&[(String, relate_parser::Expr)]>) -> Option<String> {
let options = options?;
if options.is_empty() {
return None;
}
let mut entries = Vec::with_capacity(options.len());
for (key, value) in options {
let relate_parser::Expr::Map(pairs) = value else {
return None;
};
let inner = pairs
.iter()
.map(|pair| {
simple_expr_text(&pair.value).map(|text| format!("`{}`: {}", pair.key, text))
})
.collect::<Option<Vec<_>>>()?;
entries.push(format!("`{key}`: {{{}}}", inner.join(", ")));
}
Some(format!(" OPTIONS {{{}}}", entries.join(", ")))
}
fn simple_expr_text(expr: &relate_parser::Expr) -> Option<String> {
match expr {
relate_parser::Expr::Literal(relate_parser::LiteralValue::Integer(value)) => {
Some(value.to_string())
}
relate_parser::Expr::Literal(relate_parser::LiteralValue::Float(value)) => {
Some(relate_core::Value::Float(*value).to_cypher_literal())
}
relate_parser::Expr::Literal(relate_parser::LiteralValue::Bool(value)) => {
Some(value.to_string())
}
relate_parser::Expr::Literal(relate_parser::LiteralValue::String(value)) => {
Some(relate_core::Value::String(value.as_str().into()).to_cypher_literal())
}
_ => None,
}
}
fn create_constraint_notifications(
graph: &ExecGraph<'_>,
c: &relate_parser::CreateConstraintClause,
name: Option<&str>,
params: &BTreeMap<String, Value>,
) -> Result<Vec<Notification>> {
if !c.if_not_exists {
return Ok(Vec::new());
}
let (def, constraint_type) = constraint_def(c, name.map(str::to_string))?;
let gate = constraint_create_gate(graph, c, &def, constraint_type, params, None)?;
let Gate::Skip(rule) = gate else {
return Ok(Vec::new());
};
let ExistingRule::Constraint(existing) = rule else {
return Ok(Vec::new());
};
let Some(descriptor) = constraint_descriptor_text(&existing) else {
return Ok(Vec::new());
};
let Some(command) = constraint_command_text(&def, constraint_type, name) else {
return Ok(Vec::new());
};
Ok(vec![already_exists_note(&command, &descriptor)])
}
fn constraint_tail(entity: EntityKind, label: &str, properties: &[String]) -> String {
let pattern = match entity {
EntityKind::Node => format!("(e:{label})"),
EntityKind::Relationship => format!("()-[e:{label}]-()"),
};
if properties.is_empty() {
return format!("FOR {pattern}");
}
let refs = properties
.iter()
.map(|property| format!("e.{property}"))
.collect::<Vec<_>>()
.join(", ");
format!("FOR {pattern} REQUIRE ({refs})")
}
fn constraint_command_text(
def: &ConstraintDef,
kind: ConstraintType,
name: Option<&str>,
) -> Option<String> {
let verb = standalone_verb(kind, def.property_type.as_deref())?;
let entity = EntityKind::from_relationship_flag(def.relationship);
let mut command = String::from("CREATE CONSTRAINT ");
if let Some(name) = name {
command.push_str(name);
command.push(' ');
}
command.push_str("IF NOT EXISTS ");
command.push_str(&constraint_tail(entity, &def.label, &def.properties));
command.push(' ');
command.push_str(&verb);
Some(command)
}
fn constraint_descriptor_text(def: &relate_catalog::ConstraintDef) -> Option<String> {
let kind = ConstraintType::from_str(&def.kind).ok()?;
let verb = standalone_verb(kind, def.property_type.as_deref())?;
let entity = EntityKind::from_relationship_flag(def.relationship);
Some(format!(
"CONSTRAINT {} {} {verb}",
def.name,
constraint_tail(entity, &def.label, &def.properties)
))
}
fn already_exists_note(command: &str, descriptor: &str) -> Notification {
Notification::new(
NotificationKind::SchemaIndexOrConstraintAlreadyExists,
format!(
"note: successful completion - index or constraint already exists. \
The command '{command}' has no effect. The index or constraint specified by \
'{descriptor}' already exists."
),
)
}
fn does_not_exist_note(command: &str, name: Option<&str>) -> Notification {
let subject = match name {
Some(name) => format!("`{name}` "),
None => String::new(),
};
Notification::new(
NotificationKind::SchemaIndexOrConstraintDoesNotExist,
format!(
"note: successful completion - index or constraint does not exist. \
The command '{command}' has no effect. The specified index or constraint \
{subject}does not exist."
),
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::exec::test_support::exec_autocommit;
use crate::graph::Graph;
fn notes(graph: &Graph, query: &str) -> Vec<Notification> {
let text = query.strip_prefix("EXPLAIN ").unwrap_or(query);
let statement = relate_parser::parse(text).expect("parses");
let view = ExecGraph::read(graph);
statement
.clauses
.iter()
.flat_map(|clause| {
catalog_clause_notifications(&view, clause, &BTreeMap::new())
.expect("notification facts read")
})
.collect()
}
fn assert_single_note(
graph: &Graph,
query: &str,
kind: NotificationKind,
code: &str,
) -> Notification {
let found = notes(graph, query);
assert_eq!(found.len(), 1, "expected one note for `{query}`: {found:?}");
assert_eq!(found[0].kind(), kind);
assert_eq!(found[0].gql_status(), code);
found.into_iter().next().expect("checked non-empty")
}
#[test]
fn duplicate_if_not_exists_index_raises_the_already_exists_note() {
let g = Graph::default();
exec_autocommit(&g, "CREATE INDEX i1 FOR (n:L) ON (n.p)").unwrap();
assert_single_note(
&g,
"EXPLAIN CREATE INDEX i1 IF NOT EXISTS FOR (n:L) ON (n.p)",
NotificationKind::SchemaIndexOrConstraintAlreadyExists,
"00NA0",
);
}
#[test]
fn equivalent_schema_notifications_name_the_existing_rule() {
let g = Graph::default();
exec_autocommit(&g, "CREATE INDEX i1 FOR (n:L) ON (n.p)").unwrap();
let note = assert_single_note(
&g,
"EXPLAIN CREATE INDEX i2 IF NOT EXISTS FOR (n:L) ON (n.p)",
NotificationKind::SchemaIndexOrConstraintAlreadyExists,
"00NA0",
);
assert!(note.description().contains("RANGE INDEX i1"));
exec_autocommit(&g, "CREATE CONSTRAINT c1 FOR (n:M) REQUIRE (n.p) IS UNIQUE").unwrap();
let note = assert_single_note(
&g,
"EXPLAIN CREATE CONSTRAINT c2 IF NOT EXISTS FOR (n:M) REQUIRE (n.p) IS UNIQUE",
NotificationKind::SchemaIndexOrConstraintAlreadyExists,
"00NA0",
);
assert!(note.description().contains("CONSTRAINT c1"));
}
#[test]
fn vector_without_dimensions_raises_the_dimensions_note() {
let g = Graph::default();
assert_single_note(
&g,
"EXPLAIN CREATE VECTOR INDEX v1 FOR (n:L) ON (n.p)",
NotificationKind::SchemaVectorIndexDimensionsNotSpecified,
"00NA2",
);
}
#[test]
fn drop_if_exists_missing_raises_the_does_not_exist_note() {
let g = Graph::default();
assert_single_note(
&g,
"EXPLAIN DROP INDEX missing IF EXISTS",
NotificationKind::SchemaIndexOrConstraintDoesNotExist,
"00NA1",
);
}
#[test]
fn plain_create_raises_no_note() {
let g = Graph::default();
assert!(notes(&g, "EXPLAIN CREATE INDEX i1 FOR (n:L) ON (n.p)").is_empty());
}
#[test]
fn parameter_names_resolve_for_the_note() {
let g = Graph::default();
exec_autocommit(&g, "CREATE INDEX i1 FOR (n:L) ON (n.p)").unwrap();
let statement = relate_parser::parse("CREATE INDEX $name IF NOT EXISTS FOR (n:L) ON (n.p)")
.expect("parses");
let view = ExecGraph::read(&g);
let params = BTreeMap::from([("name".to_string(), Value::String("i1".into()))]);
let found = statement
.clauses
.iter()
.flat_map(|clause| {
catalog_clause_notifications(&view, clause, ¶ms)
.expect("notification facts read")
})
.collect::<Vec<_>>();
assert_eq!(found[0].gql_status(), "00NA0");
assert!(
found[0]
.description()
.contains("CREATE RANGE INDEX i1 IF NOT EXISTS"),
"{}",
found[0].description()
);
}
#[test]
fn unresolvable_parameter_name_raises_no_note() {
let g = Graph::default();
exec_autocommit(&g, "CREATE INDEX i1 FOR (n:L) ON (n.p)").unwrap();
assert!(notes(
&g,
"EXPLAIN CREATE INDEX $missing IF NOT EXISTS FOR (n:L) ON (n.p)"
)
.is_empty());
}
#[test]
fn name_collision_note_names_the_occupied_index() {
let g = Graph::default();
exec_autocommit(&g, "CREATE INDEX i1 FOR (n:L) ON (n.p)").unwrap();
let note = assert_single_note(
&g,
"EXPLAIN CREATE INDEX i1 IF NOT EXISTS FOR (n:Other) ON (n.q)",
NotificationKind::SchemaIndexOrConstraintAlreadyExists,
"00NA0",
);
assert!(
note.description().contains("RANGE INDEX i1"),
"{}",
note.description()
);
assert!(
note.description()
.contains("CREATE RANGE INDEX i1 IF NOT EXISTS FOR (e:Other) ON (e.q)"),
"{}",
note.description()
);
}
#[test]
fn constraint_without_a_standalone_verb_skips_the_note() {
let g = Graph::default();
exec_autocommit(&g, "CREATE CONSTRAINT q FOR (n:P) REQUIRE n:Q").unwrap();
assert!(notes(
&g,
"EXPLAIN CREATE CONSTRAINT q2 IF NOT EXISTS FOR (n:P) REQUIRE n:Q"
)
.is_empty());
}
#[test]
fn unnamed_if_not_exists_index_probes_the_generated_name() {
let g = Graph::default();
exec_autocommit(&g, "CREATE INDEX FOR (n:L) ON (n.p)").unwrap();
let name = g
.named_indexes()
.into_iter()
.next()
.expect("the unnamed create landed")
.name;
let note = assert_single_note(
&g,
"EXPLAIN CREATE INDEX IF NOT EXISTS FOR (n:L) ON (n.p)",
NotificationKind::SchemaIndexOrConstraintAlreadyExists,
"00NA0",
);
assert!(
note.description().contains(&format!("RANGE INDEX {name}")),
"{}",
note.description()
);
exec_autocommit(&g, "CREATE INDEX IF NOT EXISTS FOR (n:L) ON (n.p)").unwrap();
assert_eq!(g.named_indexes().len(), 1);
}
#[test]
fn graph_type_managed_constraint_explain_propagates_the_gate_error() {
let g = Graph::default();
exec_autocommit(&g, "ALTER CURRENT GRAPH TYPE SET { (:L => {p :: STRING}) }").unwrap();
exec_autocommit(
&g,
"CREATE CONSTRAINT x IF NOT EXISTS FOR (n:L) REQUIRE n.p IS :: STRING",
)
.expect("an equivalent derived constraint satisfies IF NOT EXISTS");
let error = match exec_autocommit(
&g,
"CREATE CONSTRAINT x IF NOT EXISTS FOR (n:L) REQUIRE n.q IS :: STRING",
) {
Ok(_) => panic!("the gate must reject a dependency-managed constraint"),
Err(error) => error,
};
assert!(
error
.to_string()
.contains("IncompatibleGraphTypeDependence"),
"{error}"
);
let statement = relate_parser::parse(
"CREATE CONSTRAINT x IF NOT EXISTS FOR (n:L) REQUIRE n.q IS :: STRING",
)
.expect("parses");
let clause = statement.clauses.first().expect("one clause");
let view = ExecGraph::read(&g);
let error = catalog_clause_notifications(&view, clause, &BTreeMap::new())
.expect_err("the gate's rejection propagates out of the note derivation");
assert!(
error
.to_string()
.contains("IncompatibleGraphTypeDependence"),
"{error}"
);
}
#[test]
fn property_less_non_lookup_index_has_no_descriptor() {
assert_eq!(
index_descriptor_tail(IndexKindSpec::Point, EntityKind::Node, &[], &[], &[]),
None
);
assert_eq!(
index_descriptor_tail(IndexKindSpec::Lookup, EntityKind::Node, &[], &[], &[]),
Some("FOR (e) ON EACH labels(e)".to_string())
);
}
}