use std::collections::HashSet;
use std::ops::ControlFlow;
use relate_names::SmallName;
use super::{each_label_expr_child, reads, walk_scope_children, ChildScope};
use crate::walk_query::{
clause_patterns, path_pattern_mentions, pattern_anchor_variables, walk_query_with_roles,
ExprRole,
};
use crate::{Bearing, CallScope, Clause, Expr, LabelExpr, MapProjectionItem, PathPattern, Query};
pub fn map_projection_shorthand_names(
items: &[MapProjectionItem],
) -> impl Iterator<Item = &SmallName> {
items.iter().filter_map(|item| match item {
MapProjectionItem::Variable { name } => Some(name),
MapProjectionItem::KeyValue { .. }
| MapProjectionItem::Property { .. }
| MapProjectionItem::AllProperties => None,
})
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum ElementBodies {
Enter,
Skip,
}
fn own_scope_reads(
expr: &Expr,
bound: &mut Vec<SmallName>,
elements: ElementBodies,
read: &mut impl FnMut(&SmallName) -> ControlFlow<()>,
) -> ControlFlow<()> {
match expr {
Expr::Variable(name) => {
if reads(name, bound) {
read(name)?;
}
ControlFlow::Continue(())
}
other if other.is_scope_bearing() => ControlFlow::Continue(()),
other => {
if let Expr::MapProjection { items, .. } = other {
for name in map_projection_shorthand_names(items) {
if reads(name, bound) {
read(name)?;
}
}
}
let mut flow = ControlFlow::Continue(());
walk_scope_children(other, &mut |child, scope| {
if flow.is_break() {
return;
}
flow = match scope {
ChildScope::Enclosing => own_scope_reads(child, bound, elements, read),
ChildScope::Element(_) if elements == ElementBodies::Skip => {
ControlFlow::Continue(())
}
ChildScope::Element(bound_names) => {
let mark = bound.len();
bound.extend(bound_names);
let flow = own_scope_reads(child, bound, elements, read);
bound.truncate(mark);
flow
}
};
});
flow
}
}
}
pub fn references_any(expr: &Expr, names: &HashSet<String>) -> bool {
own_scope_reads(expr, &mut Vec::new(), ElementBodies::Enter, &mut |name| {
if name.with_str(|text| names.contains(text)) {
ControlFlow::Break(())
} else {
ControlFlow::Continue(())
}
})
.is_break()
}
pub fn all_vars_satisfy(expr: &Expr, ok: impl Fn(&str) -> bool) -> bool {
each_read(expr, &mut Vec::new(), &mut |name| ok(name))
}
fn feed_read(name: &SmallName, bound: &[SmallName], sink: &mut impl FnMut(&str) -> bool) -> bool {
!reads(name, bound) || name.with_str(sink)
}
fn each_read(expr: &Expr, bound: &mut Vec<SmallName>, sink: &mut impl FnMut(&str) -> bool) -> bool {
fn feed(names: HashSet<String>, sink: &mut impl FnMut(&str) -> bool) -> bool {
names.iter().all(|name| sink(name))
}
match expr.bearing() {
Some(Bearing::Subquery(query)) => {
let mut names = HashSet::new();
exists_outer_vars(query, &mut names);
return feed(names, sink);
}
Some(Bearing::Pattern(path)) => {
let mut names = HashSet::new();
path_pattern_mentions(path, &mut names);
return feed(names, sink);
}
None => {}
}
if let Expr::MapProjection { items, .. } = expr {
if !map_projection_shorthand_names(items).all(|name| feed_read(name, bound, sink)) {
return false;
}
}
match expr {
Expr::Variable(v) => feed_read(v, bound, sink),
other => {
let mut going = true;
walk_scope_children(other, &mut |child, scope| {
if !going {
return;
}
match scope {
ChildScope::Enclosing => going = each_read(child, bound, sink),
ChildScope::Element(names) => {
let mark = bound.len();
bound.extend(names);
going = each_read(child, bound, sink);
bound.truncate(mark);
}
}
});
going
}
}
}
pub fn expr_vars(expr: &Expr, out: &mut HashSet<String>) {
each_read(expr, &mut Vec::new(), &mut |name| {
out.insert(name.to_string());
true
});
}
pub fn expr_row_vars(expr: &Expr, out: &mut HashSet<String>) {
let _ = own_scope_reads(expr, &mut Vec::new(), ElementBodies::Skip, &mut |name| {
out.insert(name.to_string());
ControlFlow::Continue(())
});
}
pub fn label_expr_vars(expr: &LabelExpr, out: &mut HashSet<String>) {
each_label_expr_child(expr, &mut |child| expr_vars(child, out));
}
pub fn path_pattern_vars(path: &PathPattern) -> impl Iterator<Item = &SmallName> {
path.nodes
.iter()
.filter_map(|n| n.variable.as_ref())
.chain(path.rels.iter().filter_map(|r| r.variable.as_ref()))
}
pub fn exists_outer_vars(query: &Query, out: &mut HashSet<String>) {
let mut pattern_names = HashSet::new();
walk_query_with_roles(
query,
&mut |clause| {
for path in clause_patterns(clause) {
path_pattern_mentions(path, &mut pattern_names);
}
if let Clause::CallSubquery {
scope: CallScope::Vars(vars),
..
} = clause
{
pattern_names.extend(vars.iter().map(SmallName::to_string));
}
},
&mut |expr, role| {
if matches!(role, ExprRole::Read) {
expr_vars(expr, out);
}
},
);
out.extend(pattern_names);
}
pub fn correlated_reads(query: &Query, out: &mut HashSet<String>) {
let mut reads = HashSet::new();
exists_outer_vars(query, &mut reads);
let mut bound = HashSet::new();
walk_query_with_roles(
query,
&mut |clause| {
for path in clause_patterns(clause) {
bound.extend(
pattern_anchor_variables(path)
.into_iter()
.map(|n| n.to_string()),
);
}
},
&mut |_, _| {},
);
out.extend(reads.into_iter().filter(|name| !bound.contains(name)));
}