use std::collections::HashSet;
use relate_core::Result;
use relate_ir::{expr_vars, Clause, Expr, Projection, Query};
use super::import_seed;
use crate::exec::scope::{nested_seed_row, terminal_return_items};
use crate::overlay::ExecGraph;
use crate::row::Row;
pub(super) struct SharedExecution {
pub(super) seed: Row,
}
impl SharedExecution {
pub(super) fn check(&self) -> HashSet<String> {
self.seed.keys().cloned().collect()
}
}
pub(super) fn shared_execution_plan(
graph: &ExecGraph<'_>,
query: &relate_ir::Query,
scope: &relate_ir::CallScope,
rows: &[Row],
) -> Result<Option<SharedExecution>> {
let Some(first) = rows.first() else {
return Ok(None);
};
Ok(match scope {
relate_ir::CallScope::Vars(vars) if !vars.is_empty() => Some(SharedExecution {
seed: import_seed(vars, first)?,
}),
relate_ir::CallScope::None => {
if rows
.iter()
.any(|row| row.len() != first.len() || row.keys().any(|k| !first.contains_key(k)))
{
return Ok(None);
}
if query.parts.len() != 1 {
return Ok(None);
}
let seed = super::naked_import_row(query.parts.iter(), &nested_seed_row(graph, first))?;
Some(SharedExecution { seed })
}
_ => None,
})
}
fn body_safe_expr(expr: &Expr, check: &HashSet<String>) -> bool {
let mut vars = HashSet::new();
expr_vars(expr, &mut vars);
vars.is_disjoint(check)
}
fn body_safe_item(item: &Projection, check: &HashSet<String>) -> bool {
match &item.expr {
Expr::Star => true,
Expr::Variable(name) if name.with_str(|text| check.contains(text)) => {
item.alias.as_ref().is_none_or(|a| a == name)
}
expr => body_safe_expr(expr, check),
}
}
fn body_safe_pattern(pattern: &relate_ir::PathPattern, check: &HashSet<String>) -> bool {
if pattern.needs_gpm() || pattern.is_shortest_path_function {
return false;
}
let mut mentioned = HashSet::new();
relate_ir::path_pattern_mentions(pattern, &mut mentioned);
mentioned.is_disjoint(check)
}
fn body_safe_projection(
items: &[Projection],
order: &[relate_ir::OrderItem],
skip: &Option<Expr>,
limit: &Option<Expr>,
group_by: &Option<Vec<Expr>>,
check: &HashSet<String>,
) -> bool {
items.iter().all(|item| body_safe_item(item, check))
&& order.iter().all(|o| body_safe_expr(&o.expr, check))
&& skip.as_ref().is_none_or(|e| body_safe_expr(e, check))
&& limit.as_ref().is_none_or(|e| body_safe_expr(e, check))
&& group_by
.as_ref()
.is_none_or(|keys| keys.iter().all(|k| body_safe_expr(k, check)))
}
fn body_safe_clause(clause: &Clause, check: &HashSet<String>) -> bool {
match clause {
Clause::Match {
patterns,
where_clause,
search,
..
} => {
search.is_none()
&& where_clause
.as_ref()
.is_none_or(|w| body_safe_expr(w, check))
&& patterns.iter().all(|p| body_safe_pattern(p, check))
}
Clause::Unwind { expression, .. } => body_safe_expr(expression, check),
Clause::With {
items,
order,
skip,
limit,
where_clause,
group_by,
..
} => {
body_safe_projection(items, order, skip, limit, group_by, check)
&& where_clause
.as_ref()
.is_none_or(|w| body_safe_expr(w, check))
}
Clause::Return {
items,
order,
skip,
limit,
group_by,
..
} => body_safe_projection(items, order, skip, limit, group_by, check),
_ => false,
}
}
pub(super) fn import_invariant_body(query: &Query, check: &HashSet<String>) -> bool {
if !query.is_single_flat_part() {
return false;
}
if terminal_return_items(&query.parts[0]).is_none() {
return false;
}
let mut clauses_clean = true;
let mut exprs_clean = true;
relate_ir::walk_query(
query,
&mut |clause| clauses_clean &= body_safe_clause(clause, check),
&mut |expr| {
exprs_clean &= !crate::eval::is_row_unstable_node(expr);
},
);
clauses_clean && exprs_clean
}