use relate_core::Value;
use relate_ir::{BinaryOp, Expr, PathPattern};
use relate_names::SmallName;
use crate::overlay::ExecGraph;
use crate::row::Row;
pub(crate) fn id_call_variable(expr: &Expr) -> Option<&SmallName> {
match expr {
Expr::Function {
name,
distinct: false,
args,
} if name.with_str(|text| text.eq_ignore_ascii_case("id")) => match args.as_slice() {
[Expr::Variable(var)] => Some(var),
_ => None,
},
_ => None,
}
}
fn literal_or_property_of(expr: &Expr, var: &SmallName) -> bool {
match expr {
Expr::Null | Expr::Bool(_) | Expr::Int(_) | Expr::Float(_) | Expr::String(_) => true,
Expr::Property { expr, .. } => {
matches!(expr.as_ref(), Expr::Variable(name) if name == var)
}
_ => false,
}
}
pub(crate) fn cannot_fail_over_node(expr: &Expr, var: &SmallName) -> bool {
literal_or_property_of(expr, var)
|| matches!(expr, Expr::Variable(name) if name == var)
|| id_call_variable(expr) == Some(var)
}
pub(crate) fn where_cannot_fail(predicate: &Expr, var: &SmallName) -> bool {
match predicate {
Expr::Binary {
op: BinaryOp::And,
left,
right,
} => where_cannot_fail(left, var) && where_cannot_fail(right, var),
Expr::Binary {
op:
BinaryOp::Eq
| BinaryOp::Neq
| BinaryOp::Lt
| BinaryOp::Gt
| BinaryOp::Lte
| BinaryOp::Gte,
left,
right,
} => literal_or_property_of(left, var) && literal_or_property_of(right, var),
_ => false,
}
}
pub(crate) fn is_bare_node_scan_of(
patterns: &[PathPattern],
where_clause: Option<&Expr>,
var: &SmallName,
) -> bool {
let [path] = patterns else {
return false;
};
let [node] = path.nodes.as_slice() else {
return false;
};
node.variable.as_ref() == Some(var)
&& path.rels.is_empty()
&& path.variable.is_none()
&& path.where_clause.is_none()
&& path.selector.is_none()
&& path.mode.is_none()
&& node.properties.is_none()
&& node.where_clause.is_none()
&& where_clause.is_none_or(|predicate| where_cannot_fail(predicate, var))
}
pub(crate) fn cannot_fail_on(expr: &Expr, row: &Row, graph: &ExecGraph<'_>) -> bool {
let readable = |value: Option<&Value>| match value {
Some(Value::Node(node)) => graph.node_exists(node.id),
Some(Value::Relationship(rel)) => graph.rel_exists(rel.id),
Some(Value::Map(_) | Value::Null) => true,
_ => false,
};
match expr {
Expr::Null | Expr::Bool(_) | Expr::Int(_) | Expr::Float(_) | Expr::String(_) => true,
Expr::Variable(name) => row.get_name(name).is_some(),
Expr::Property { expr, .. } => match expr.as_ref() {
Expr::Variable(name) => readable(row.get_name(name)),
_ => false,
},
Expr::Function { .. } => id_call_variable(expr).is_some_and(|arg| {
matches!(
row.get_name(arg),
Some(Value::Node(_) | Value::Relationship(_) | Value::Null)
)
}),
_ => false,
}
}
#[cfg(test)]
mod tests {
use relate_front::parse_and_lower;
use relate_ir::Clause;
use super::*;
fn stoppable(cypher: &str) -> bool {
let query = parse_and_lower(cypher).unwrap();
let Clause::Match {
patterns,
where_clause,
..
} = &query.parts[0].clauses[0]
else {
panic!("the first clause is a MATCH");
};
is_bare_node_scan_of(patterns, where_clause.as_ref(), &SmallName::from("n"))
}
#[test]
fn a_bare_scan_with_a_comparison_filter_may_stop() {
assert!(stoppable("MATCH (n:N) RETURN n"));
assert!(stoppable("MATCH (n:N) WHERE n.k > 3 AND n.k <> 9 RETURN n"));
}
#[test]
fn a_filter_that_can_raise_never_stops_the_scan() {
assert!(!stoppable("MATCH (n:N) WHERE 1 / (n.k - 1) > 0 RETURN n"));
assert!(!stoppable("MATCH (n:N) WHERE n.k + 1 > 3 RETURN n"));
}
#[test]
fn an_element_pattern_where_keeps_its_own_path() {
assert!(!stoppable("MATCH (n:N WHERE n.k > 3) RETURN n"));
assert!(!stoppable("MATCH (n:N WHERE 1 / (n.k - 1) > 0) RETURN n"));
}
#[test]
fn a_pattern_beyond_one_bare_node_never_stops_the_scan() {
assert!(!stoppable("MATCH (n:N {k: 1}) RETURN n"));
assert!(!stoppable("MATCH (n:N), (m:M) RETURN n"));
assert!(!stoppable("MATCH (n:N)-[]->(m) RETURN n"));
assert!(!stoppable("MATCH p = (n:N) RETURN n"));
}
}