use super::match_order::{self, StartCosting};
use super::match_predicates;
use super::*;
use crate::{OptionalOptimizerRule, RuleEvent};
use hawdb_plan_cypher::{
membership_conjunct, predicate_cannot_fail, GraphMatchHashJoin, GraphMatchProgram,
};
pub(super) fn lower_graph_match_program(
program: &GraphMatchProgram,
input: Option<&PhysicalPlan>,
catalog: &OptimizerCatalog,
optimizer_context: &OptimizerContext,
decisions: &mut Vec<String>,
) -> GraphMatchProgram {
let labels = input.map(bound_node_labels).unwrap_or_default();
let options = optimizer_context.qualification_options();
let costing = StartCosting {
catalog,
options,
labels: Some(&labels),
};
let joined = input.and_then(|input| hash_join_program(program, input, costing, decisions));
let binder = program;
let program = joined.unwrap_or_else(|| {
let mut program = placed_graph_match_program(
anchored_graph_match_program(program, costing, decisions),
decisions,
);
program.hash_join = None;
program
});
let mut program = plan_id_sets(program, options, decisions);
if input.is_none() {
program.seeded_union =
super::seeded_union::seeded_union_branches(binder, &program, costing, decisions);
}
program
}
fn hash_join_program(
program: &GraphMatchProgram,
input: &PhysicalPlan,
costing: StartCosting<'_>,
decisions: &mut Vec<String>,
) -> Option<GraphMatchProgram> {
let estimate = match_order::meeting_join_estimate(program, costing)?;
let key = program.imports[0].variable.clone();
if program
.hash_join
.as_ref()
.is_some_and(|join| join.key == key)
{
decisions.push(format!(
"GraphMatchExec runs as the hash join chosen when its Expand chain was split at {key}"
));
return Some(build_hash_join(program, estimate, key, costing, decisions));
}
let input_rows = estimate_physical_plan_cost(input, costing.catalog)
.estimated_rows
.max(1) as f64;
let rule = OptionalOptimizerRule::GraphMatchHashJoin;
let detail = format!(
"run GraphMatchExec once from {} and hash its matches on {key}: estimated work {:.1} + {:.1} build matches + {input_rows:.0} probes instead of {input_rows:.0} input rows x {:.1} per row",
estimate.build_start,
estimate.estimate.build,
estimate.estimate.build_rows,
estimate.estimate.per_row
);
if !estimate.estimate.hash_join_is_cheaper(input_rows) {
decisions.push(
RuleEvent::estimated(
rule.as_str(),
format!("keep the per-row GraphMatchExec; would {detail}"),
)
.into_decision(),
);
return None;
}
if !costing.options.rule_enabled(rule) {
decisions.push(
RuleEvent::skipped(
rule.as_str(),
format!("disabled by statement qualification: {detail}"),
)
.into_decision(),
);
return None;
}
decisions.push(RuleEvent::applied(rule.as_str(), detail).into_decision());
Some(build_hash_join(program, estimate, key, costing, decisions))
}
fn build_hash_join(
program: &GraphMatchProgram,
estimate: match_order::MeetingJoinEstimate,
key: String,
costing: StartCosting<'_>,
decisions: &mut Vec<String>,
) -> GraphMatchProgram {
let mut joined = program.clone();
if let Some(predicate) = joined.predicate.take() {
let mut conjuncts = Vec::new();
flatten(predicate, &mut conjuncts);
joined.predicate = Some(if conjuncts.len() == 1 {
conjuncts.pop().expect("one conjunct")
} else {
Predicate::And(conjuncts)
});
}
let mut per_row = anchored_graph_match_program(&joined, costing, &mut Vec::new());
match_predicates::place_graph_match_predicates(&mut per_row);
let per_row_end = per_row.steps.len();
let per_row_step = |index: usize| {
per_row
.predicate_steps
.get(index)
.copied()
.unwrap_or(per_row_end)
};
joined.steps = estimate.steps;
let mut build = joined.clone();
build.imports.clear();
build.introduced.push(key.clone());
if let Some(decision) = match_predicates::place_graph_match_predicates(&mut build) {
decisions.push(format!("hash join build: {decision}"));
}
joined.predicate_steps = build.predicate_steps;
joined.edge_predicates = build.edge_predicates;
let variables = joined
.introduced
.iter()
.map(String::as_str)
.chain([key.as_str()])
.collect::<BTreeSet<_>>();
let conjuncts = joined.predicate_conjuncts();
let prefix = conjuncts
.iter()
.take_while(|conjunct| {
predicate_cannot_fail(conjunct)
&& match_predicates::predicate_variables(conjunct)
.iter()
.all(|name| variables.contains(name.as_str()))
})
.count();
let mut residual_order = (prefix..conjuncts.len()).collect::<Vec<_>>();
residual_order.sort_by_key(|index| {
(
per_row_step(*index),
!per_row.edge_predicates.contains(index),
*index,
)
});
let residual_placed = residual_order
.iter()
.filter(|index| per_row_step(**index) < per_row_end)
.count();
let probe_conjuncts = residual_order
.iter()
.take_while(|index| {
per_row_step(**index) == 0
&& !match_predicates::predicate_variables(&conjuncts[**index])
.iter()
.any(|name| variables.contains(name.as_str()))
})
.count();
let end = joined.steps.len();
if !joined.predicate_steps.is_empty() {
for step in &mut joined.predicate_steps[prefix..] {
*step = end;
}
joined.edge_predicates.retain(|index| *index < prefix);
}
joined.hash_join = Some(GraphMatchHashJoin {
key,
build_conjuncts: (0..prefix).collect(),
residual_order,
residual_placed,
probe_conjuncts,
});
joined
}
fn flatten(predicate: Predicate, output: &mut Vec<Predicate>) {
match predicate {
Predicate::And(children) => {
for child in children {
flatten(child, output);
}
}
predicate => output.push(predicate),
}
}
fn plan_id_sets(
mut program: GraphMatchProgram,
options: crate::OptimizerQualificationOptions,
decisions: &mut Vec<String>,
) -> GraphMatchProgram {
let candidates = program
.predicate_conjuncts()
.iter()
.enumerate()
.filter_map(|(index, conjunct)| {
let (variable, column) = membership_conjunct(conjunct)?;
let binds = |name: &str| {
program.steps.iter().any(|step| match step {
hawdb_plan_cypher::GraphMatchStep::Node(node) => node.variable == name,
hawdb_plan_cypher::GraphMatchStep::Expand { target, .. } => {
target.variable == name
}
hawdb_plan_cypher::GraphMatchStep::ShortestPath(_) => false,
})
};
let own = |name: &str| {
program.introduced.iter().any(|own| own == name)
|| program.imports.iter().any(|import| import.variable == name)
};
(binds(variable)
&& program.introduced.iter().any(|own| own == variable)
&& !own(column))
.then(|| (index, format!("{variable} IN {column}")))
})
.collect::<Vec<_>>();
if candidates.is_empty() {
return program;
}
let rule = OptionalOptimizerRule::GraphMatchIdSetSemiJoin;
let detail = format!(
"check [{}] against each input row's list id set before reading the node",
candidates
.iter()
.map(|(_, text)| text.as_str())
.collect::<Vec<_>>()
.join(", ")
);
if options.rule_enabled(rule) {
decisions.push(RuleEvent::applied(rule.as_str(), detail).into_decision());
program.id_set_conjuncts = candidates.into_iter().map(|(index, _)| index).collect();
} else {
decisions.push(
RuleEvent::skipped(
rule.as_str(),
format!("disabled by statement qualification: {detail}"),
)
.into_decision(),
);
}
program
}
pub(super) fn bound_node_labels(plan: &PhysicalPlan) -> BTreeMap<String, String> {
let mut labels = BTreeMap::new();
collect_labels(plan, &mut labels);
labels.retain(|_, label| !label.is_empty() && !label.contains(':'));
labels
}
fn collect_labels(plan: &PhysicalPlan, labels: &mut BTreeMap<String, String>) {
match plan {
PhysicalPlan::SeqNodeScan { variable, label }
| PhysicalPlan::IndexNodeSeek {
variable, label, ..
}
| PhysicalPlan::IndexNodeMultiSeek {
variable, label, ..
}
| PhysicalPlan::NodeProjectionScanExec {
variable, label, ..
} => {
labels.insert(variable.clone(), label.clone());
}
PhysicalPlan::AdjacencyExpandExec {
target_variable,
target_label,
input,
..
} => {
collect_labels(input, labels);
labels.insert(target_variable.clone(), target_label.clone());
}
PhysicalPlan::FilterExec { input, .. }
| PhysicalPlan::DistinctExec { input }
| PhysicalPlan::LimitExec { input, .. }
| PhysicalPlan::SortExec { input, .. } => collect_labels(input, labels),
PhysicalPlan::ProjectExec { items, input } => {
let mut inner = BTreeMap::new();
collect_labels(input, &mut inner);
for item in items {
if let hawdb_plan_cypher::ProjectionExpression::Variable { variable } =
&item.expression
&& let Some(label) = inner.get(variable)
{
labels.insert(item.name.clone(), label.clone());
}
}
}
_ => {}
}
}