use std::fmt;
use crate::LogicalPlan;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum QueryType {
Read,
Write,
ReadWrite,
Schema,
}
impl QueryType {
pub fn as_str(self) -> &'static str {
match self {
QueryType::Read => "r",
QueryType::Write => "w",
QueryType::ReadWrite => "rw",
QueryType::Schema => "s",
}
}
}
impl fmt::Display for QueryType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
pub fn query_type(
plan: &LogicalPlan,
is_write_procedure: &mut impl FnMut(&str) -> bool,
) -> QueryType {
let mut access = crate::access::plan_access(plan, is_write_procedure);
access.write |= crate::namespaces::plan_calls_write_procedure(plan, is_write_procedure);
if access.schema && !access.read && !access.write {
return QueryType::Schema;
}
let returns_columns = !crate::plan_output_names_expect(plan).is_empty();
match (returns_columns, access.is_write()) {
(_, false) => QueryType::Read,
(false, true) => QueryType::Write,
(true, true) => QueryType::ReadWrite,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{plan_of, rejection};
fn type_of(cypher: &str) -> QueryType {
query_type(&plan_of(cypher), &mut |_| false)
}
#[test]
fn query_type_strings_match_query_api_v2() {
assert_eq!(QueryType::Read.as_str(), "r");
assert_eq!(QueryType::Write.as_str(), "w");
assert_eq!(QueryType::ReadWrite.as_str(), "rw");
assert_eq!(QueryType::Schema.as_str(), "s");
assert_eq!(QueryType::ReadWrite.to_string(), "rw");
}
#[test]
fn reads_classify_as_r() {
for cypher in [
"RETURN 1 AS n",
"MATCH (n) RETURN n",
"MATCH (n) WITH n UNWIND [1, 2] AS x RETURN n, x",
"SHOW INDEXES",
"SHOW DATABASES",
"CALL db.labels()",
"USE neo4j MATCH (n) RETURN n",
"RETURN 1 AS x UNION RETURN 2 AS x",
"WHEN true THEN { RETURN 1 AS x } ELSE { RETURN 2 AS x }",
] {
assert_eq!(type_of(cypher), QueryType::Read, "{cypher}");
}
}
#[test]
fn writes_without_returned_columns_classify_as_w() {
for cypher in [
"CREATE (n)",
"MERGE (n:N {id: 1})",
"MATCH (n) SET n.x = 1",
"MATCH (n) DETACH DELETE n",
"MATCH (n) DELETE n",
"MATCH (n) REMOVE n.x",
"FOREACH (x IN [1] | CREATE (n))",
"LOAD CSV FROM 'file:///f.csv' AS row CREATE (n)",
"CALL { CREATE (n) }",
"WHEN true THEN { CREATE (n) }",
"RETURN 1 AS x NEXT CREATE (n)",
] {
assert_eq!(type_of(cypher), QueryType::Write, "{cypher}");
}
}
#[test]
fn writes_with_returned_columns_classify_as_rw() {
for cypher in [
"CREATE (n) RETURN n",
"MATCH (n) SET n.x = 1 RETURN n",
"MATCH (n) DETACH DELETE n RETURN n",
"MATCH (n) REMOVE n.x RETURN n",
"RETURN 1 AS x UNION CREATE (n) RETURN 1 AS x",
"LOAD CSV FROM 'file:///f.csv' AS row RETURN row",
"CALL { RETURN 1 AS x } RETURN x",
"MERGE (n:N {id: 1}) RETURN *",
] {
assert_eq!(type_of(cypher), QueryType::ReadWrite, "{cypher}");
}
}
#[test]
fn schema_commands_classify_as_s() {
for cypher in [
"CREATE INDEX i FOR (n:N) ON (n.p)",
"DROP INDEX i",
"CREATE CONSTRAINT c FOR (n:N) REQUIRE n.id IS UNIQUE",
"DROP CONSTRAINT c",
"CREATE GRAPH TYPE g { (:A => {id :: INTEGER IS KEY}) }",
"ALTER CURRENT GRAPH TYPE SET GRAPH TYPE g",
] {
assert_eq!(type_of(cypher), QueryType::Schema, "{cypher}");
}
}
#[test]
fn schema_mixed_with_data_counts_the_schema_clause_as_a_write() {
assert_eq!(
type_of("CREATE INDEX i FOR (n:N) ON (n.p) NEXT MATCH (n) RETURN n"),
QueryType::ReadWrite
);
assert_eq!(
type_of("CREATE INDEX i FOR (n:N) ON (n.p) NEXT CREATE (n)"),
QueryType::Write
);
}
#[test]
fn writes_flagged_procedures_classify_as_writes() {
let is_write_proc = &mut |name: &str| name == "db.createLabel";
for cypher in [
"CALL db.createLabel('X')",
"MATCH (n) RETURN n AS x NEXT CALL db.createLabel('X')",
] {
assert_eq!(
query_type(&plan_of(cypher), is_write_proc),
QueryType::Write,
"{cypher}"
);
}
for cypher in [
"WHEN true THEN { CALL db.createLabel('X') RETURN 1 AS x }",
"CALL { CALL db.createLabel('X') RETURN 1 AS x } RETURN x",
] {
assert_eq!(
query_type(&plan_of(cypher), is_write_proc),
QueryType::ReadWrite,
"{cypher}"
);
}
assert_eq!(
query_type(&plan_of("CALL db.labels()"), is_write_proc),
QueryType::Read
);
}
#[test]
fn optional_match_reads_classify_as_r() {
for cypher in [
"MATCH (a:Room) OPTIONAL MATCH (a)-[:NORTH]->(b) RETURN a.name, b.name",
"EXPLAIN MATCH (a:Room) OPTIONAL MATCH (a)-[:NORTH]->(b) RETURN a.name, b.name",
"MATCH (a:Room)-[:NORTH]->(b) RETURN a.name, b.name",
"OPTIONAL MATCH (n) RETURN n",
"OPTIONAL MATCH (a) RETURN *",
"MATCH (a) OPTIONAL MATCH (a)-[:R]->(b) FINISH",
"OPTIONAL MATCH (n) FINISH",
"MATCH (a) RETURN a AS x UNION MATCH (b) OPTIONAL MATCH (b)-[:R]->(c) RETURN b AS x",
"MATCH (a) RETURN a NEXT MATCH (b) OPTIONAL MATCH (b)-[:R]->(c) RETURN b, c",
"WHEN true THEN { MATCH (a) OPTIONAL MATCH (a)-[:R]->(b) RETURN a, b }",
] {
assert_eq!(type_of(cypher), QueryType::Read, "{cypher}");
}
assert_eq!(
rejection("MATCH (a) OPTIONAL MATCH (a)-[:R]->(b)"),
rejection("MATCH (n)")
);
}
#[test]
fn optional_match_neighbors_classify_as_writes() {
for cypher in [
"MATCH (a) OPTIONAL MATCH (a)-[:R]->(b) SET a.x = 1",
"MATCH (a) OPTIONAL MATCH (a)-[:R]->(b) CREATE (c)",
"MATCH (a) OPTIONAL MATCH (a)-[:R]->(b) DELETE b",
"MATCH (a) OPTIONAL MATCH (a)-[:R]->(b) DETACH DELETE b",
] {
assert_eq!(type_of(cypher), QueryType::Write, "{cypher}");
}
for cypher in [
"MATCH (a) OPTIONAL MATCH (a)-[:R]->(b) SET a.x = 1 RETURN a",
"MATCH (a) OPTIONAL MATCH (a)-[:R]->(b) CREATE (c) RETURN c",
] {
assert_eq!(type_of(cypher), QueryType::ReadWrite, "{cypher}");
}
}
#[test]
fn the_adr0332_rule_table() {
assert_eq!(type_of("MATCH (n) RETURN n"), QueryType::Read);
assert_eq!(type_of("CREATE (n)"), QueryType::Write);
assert_eq!(type_of("CREATE (n) RETURN n"), QueryType::ReadWrite);
assert_eq!(type_of("MATCH (n) SET n.x = 1"), QueryType::Write);
assert_eq!(
type_of("MATCH (n) SET n.x = 1 RETURN n"),
QueryType::ReadWrite
);
assert_eq!(type_of("MATCH (n) DETACH DELETE n"), QueryType::Write);
assert_eq!(
type_of("MATCH (n) DETACH DELETE n RETURN n"),
QueryType::ReadWrite
);
assert_eq!(
type_of("CREATE INDEX i FOR (n:N) ON (n.p)"),
QueryType::Schema
);
assert_eq!(type_of("EXPLAIN CREATE (n)"), QueryType::Write);
}
}