use super::*;
use hawdb_core::{RelationshipDirection, Value};
use hawdb_plan_cypher::{
ComparisonOp, GraphBindingImport, GraphEntityKind, GraphMatchNode, GraphMatchShortestPath,
ListComprehensionProjection, PathFunction, ProjectionExpression, ScalarBinaryOp,
ShortestPathSelection,
};
fn node(variable: &str) -> GraphMatchNode {
GraphMatchNode {
variable: variable.to_string(),
label: String::new(),
properties: BTreeMap::new(),
}
}
fn expand(
source: &str,
relationship: Option<&str>,
target: &str,
hops: (usize, usize),
) -> GraphMatchStep {
GraphMatchStep::Expand {
source: source.to_string(),
relationship: relationship.map(str::to_string),
rel_type: "R".to_string(),
rel_types: Vec::new(),
properties: BTreeMap::new(),
direction: RelationshipDirection::Outgoing,
min_hops: hops.0,
max_hops: hops.1,
target: node(target),
}
}
fn chain() -> GraphMatchProgram {
program(
&[],
&["a", "r", "b", "c"],
vec![
GraphMatchStep::Node(node("a")),
expand("a", Some("r"), "b", (1, 1)),
expand("b", None, "c", (1, 1)),
],
)
}
fn program(imports: &[&str], introduced: &[&str], steps: Vec<GraphMatchStep>) -> GraphMatchProgram {
GraphMatchProgram {
imports: imports
.iter()
.map(|variable| GraphBindingImport {
variable: variable.to_string(),
column: format!("__column_{variable}"),
kind: GraphEntityKind::Node,
})
.collect(),
introduced: introduced.iter().map(|name| name.to_string()).collect(),
projected_nodes: BTreeMap::new(),
projected_relationships: BTreeMap::new(),
steps,
predicate: None,
predicate_steps: Vec::new(),
edge_predicates: Vec::new(),
optional: false,
hash_join: None,
id_set_conjuncts: Vec::new(),
seeded_union: Vec::new(),
}
}
fn property_eq(variable: &str, value: i64) -> Predicate {
Predicate::PropertyEq {
variable: variable.to_string(),
property: "x".to_string(),
value: Value::Int(value),
}
}
fn property(variable: &str) -> ProjectionExpression {
ProjectionExpression::Property {
variable: variable.to_string(),
property: "x".to_string(),
}
}
fn place(
mut program: GraphMatchProgram,
predicate: Predicate,
) -> (GraphMatchProgram, Option<String>) {
program.predicate = Some(predicate);
let decision = place_graph_match_predicates(&mut program);
(program, decision)
}
#[test]
fn single_variable_conjuncts_follow_the_step_that_binds_them() {
let (program, decision) = place(
chain(),
Predicate::And(vec![
property_eq("c", 3),
property_eq("a", 1),
property_eq("b", 2),
]),
);
assert_eq!(program.predicate_steps, vec![3, 1, 2]);
assert_eq!(program.predicate_conjuncts().len(), 3);
assert_eq!(
decision.as_deref(),
Some("evaluate 2 of 3 GraphMatchExec WHERE conjunct(s) before the last step: after steps [3, 1, 2] of 3")
);
}
#[test]
fn relationship_variable_is_bound_by_its_expand_step() {
let (program, _) = place(
chain(),
Predicate::And(vec![property_eq("r", 1), property_eq("c", 1)]),
);
assert_eq!(program.predicate_steps, vec![2, 3]);
}
#[test]
fn multi_variable_conjunct_waits_for_its_last_variable() {
let (program, _) = place(
chain(),
Predicate::And(vec![
Predicate::ExpressionEq {
expression: property("a"),
value: property("b"),
},
property_eq("c", 1),
]),
);
assert_eq!(program.predicate_steps, vec![2, 3]);
}
#[test]
fn imported_and_outer_variables_are_bound_before_the_first_step() {
let imported = program(
&["p"],
&["m", "c"],
vec![
GraphMatchStep::Node(node("p")),
expand("p", None, "m", (1, 1)),
expand("m", None, "c", (1, 1)),
],
);
let (program, _) = place(
imported,
Predicate::And(vec![
property_eq("p", 1),
Predicate::ExpressionCompare {
expression: ProjectionExpression::Column("k".to_string()),
op: ComparisonOp::Gt,
value: property("m"),
},
property_eq("c", 1),
Predicate::ExpressionEq {
expression: ProjectionExpression::Column("k".to_string()),
value: ProjectionExpression::Literal(Value::Int(1)),
},
]),
);
assert_eq!(program.predicate_steps, vec![0, 2, 3, 0]);
}
#[test]
fn variable_length_target_waits_until_the_trail_completes() {
let bounded = program(
&[],
&["a", "b", "c"],
vec![
GraphMatchStep::Node(node("a")),
expand("a", None, "b", (1, 3)),
expand("b", None, "c", (1, 1)),
],
);
let (program, _) = place(
bounded,
Predicate::And(vec![property_eq("b", 1), property_eq("a", 1)]),
);
assert_eq!(program.predicate_steps, vec![2, 1]);
}
#[test]
fn path_conjunct_waits_for_its_shortest_path_step() {
let paths = program(
&[],
&["a", "b", "p"],
vec![
GraphMatchStep::Node(node("a")),
GraphMatchStep::Node(node("b")),
GraphMatchStep::ShortestPath(Box::new(GraphMatchShortestPath {
path: "p".to_string(),
source: "a".to_string(),
target: "b".to_string(),
rel_type: "R".to_string(),
direction: RelationshipDirection::Outgoing,
min_hops: 1,
max_hops: 4,
bounded: true,
selection: ShortestPathSelection::Any,
})),
GraphMatchStep::Node(node("c")),
],
);
let (program, _) = place(
paths,
Predicate::And(vec![
Predicate::ExpressionCompare {
expression: ProjectionExpression::Path {
function: PathFunction::Length,
column: "p".to_string(),
},
op: ComparisonOp::Lt,
value: ProjectionExpression::Literal(Value::Int(3)),
},
property_eq("a", 1),
]),
);
assert_eq!(program.predicate_steps, vec![3, 1]);
}
#[test]
fn disjunction_and_negation_are_placed_whole() {
let (program, _) = place(
chain(),
Predicate::And(vec![
Predicate::Or(vec![property_eq("a", 1), property_eq("b", 2)]),
Predicate::Not(Box::new(Predicate::And(vec![
property_eq("a", 1),
property_eq("r", 2),
]))),
Predicate::Not(Box::new(property_eq("a", 3))),
]),
);
assert_eq!(program.predicate_steps, vec![2, 2, 1]);
assert!(matches!(program.predicate_conjuncts()[0], Predicate::Or(_)));
}
#[test]
fn null_tests_and_constant_conjuncts_are_placed_by_their_variables() {
let (program, _) = place(
chain(),
Predicate::And(vec![
Predicate::PropertyIsNull {
variable: "b".to_string(),
property: "x".to_string(),
},
Predicate::ExpressionEq {
expression: ProjectionExpression::Literal(Value::Null),
value: ProjectionExpression::Literal(Value::Int(1)),
},
Predicate::ConstantBool(false),
]),
);
assert_eq!(program.predicate_steps, vec![2, 0, 0]);
}
#[test]
fn list_comprehension_reads_its_list_and_outer_variables_only() {
let comprehension =
ProjectionExpression::ListComprehension(Box::new(ListComprehensionProjection {
variable: "c".to_string(),
list: property("a"),
predicate: Some(ProjectionExpression::Binary {
left: Box::new(ProjectionExpression::LocalVariable {
name: "c".to_string(),
property: None,
}),
op: ScalarBinaryOp::Gt,
right: Box::new(property("r")),
}),
projection: None,
}));
let (program, _) = place(
chain(),
Predicate::And(vec![
Predicate::ExpressionNotEq {
expression: ProjectionExpression::ListFunction {
function: hawdb_core::ListFunction::Size,
arguments: vec![comprehension],
},
value: ProjectionExpression::Literal(Value::Int(0)),
},
property_eq("c", 1),
]),
);
assert_eq!(program.predicate_steps, vec![2, 3]);
}
#[test]
fn nested_conjunctions_are_flattened_in_order() {
let (program, _) = place(
chain(),
Predicate::And(vec![
Predicate::And(vec![property_eq("a", 1), property_eq("c", 1)]),
property_eq("b", 1),
]),
);
assert_eq!(program.predicate_steps, vec![1, 3, 2]);
assert_eq!(
program.predicate,
Some(Predicate::And(vec![
property_eq("a", 1),
property_eq("c", 1),
property_eq("b", 1),
]))
);
}
#[test]
fn a_single_early_predicate_is_its_own_conjunct() {
let (program, _) = place(chain(), property_eq("a", 1));
assert_eq!(program.predicate_steps, vec![1]);
assert_eq!(program.predicate, Some(property_eq("a", 1)));
}
#[test]
fn predicates_that_can_only_run_last_leave_the_program_unchanged() {
let predicate = Predicate::And(vec![Predicate::And(vec![
property_eq("c", 1),
Predicate::ExpressionEq {
expression: property("a"),
value: property("c"),
},
])]);
let (program, decision) = place(chain(), predicate.clone());
assert!(program.predicate_steps.is_empty());
assert_eq!(program.predicate, Some(predicate));
assert_eq!(decision, None);
}
#[test]
fn an_introduced_variable_no_step_binds_keeps_its_conjunct_last() {
let mut unbound = chain();
unbound.introduced.push("ghost".to_string());
let (program, _) = place(
unbound,
Predicate::And(vec![property_eq("ghost", 1), property_eq("a", 1)]),
);
assert_eq!(program.predicate_steps, vec![3, 1]);
}
#[test]
fn placement_is_recomputed_from_the_current_step_order() {
let (mut program, _) = place(
chain(),
Predicate::And(vec![property_eq("a", 1), property_eq("c", 1)]),
);
program.steps = vec![
GraphMatchStep::Node(node("c")),
GraphMatchStep::Node(node("a")),
expand("a", Some("r"), "b", (1, 1)),
];
place_graph_match_predicates(&mut program);
assert_eq!(program.predicate_steps, vec![2, 1]);
}
#[test]
fn temporal_and_map_conjuncts_wait_for_all_nested_variables() {
let temporal = ProjectionExpression::TemporalFunction {
function: hawdb_core::TemporalFunction::DurationBetween,
arguments: vec![property("a"), property("c")],
};
let component = ProjectionExpression::PropertyAccess {
expression: Box::new(temporal.clone()),
key: "days".into(),
};
let map = ProjectionExpression::MapLiteral(vec![
("first".into(), property("b")),
("second".into(), component.clone()),
]);
let conversions =
hawdb_core::ConversionFunction::ALL.map(|function| ProjectionExpression::Conversion {
function,
argument: Box::new(property("c")),
});
for expression in [temporal, component, map].into_iter().chain(conversions) {
let (program, _) = place(
chain(),
Predicate::And(vec![
Predicate::ExpressionEq {
expression,
value: ProjectionExpression::Literal(Value::Null),
},
property_eq("a", 1),
]),
);
assert_eq!(program.predicate_steps, vec![3, 1]);
}
}
#[test]
fn output_cardinality_is_independent_of_conjunct_placement() {
use crate::graph::costing::estimate_physical_plan_cost_breakdown;
use crate::graph::{OptimizerCatalog, OptimizerCatalogIndexes, OptimizerCatalogStatistics};
use hawdb_plan_cypher::PhysicalPlan;
let catalog = OptimizerCatalog::new(
OptimizerCatalogIndexes::default(),
OptimizerCatalogStatistics {
label_counts: BTreeMap::from([("N".into(), 100)]),
..OptimizerCatalogStatistics::default()
},
);
let mut program = chain();
for step in &mut program.steps {
match step {
GraphMatchStep::Node(node) | GraphMatchStep::Expand { target: node, .. } => {
node.label = "N".into();
}
GraphMatchStep::ShortestPath(_) => unreachable!(),
}
}
let estimate = |program| {
estimate_physical_plan_cost_breakdown(
&PhysicalPlan::GraphMatchExec {
program,
input: None,
},
&catalog,
)
};
let unfiltered = estimate(program.clone());
program.predicate = Some(Predicate::And(vec![
property_eq("a", 1),
property_eq("b", 1),
property_eq("c", 1),
]));
let late = estimate(program.clone());
place_graph_match_predicates(&mut program);
assert_eq!(program.predicate_steps, vec![1, 2, 3]);
let early = estimate(program);
assert_eq!(early.estimated_rows, late.estimated_rows);
assert_eq!(
early.estimated_rows,
unfiltered
.estimated_rows
.div_ceil(crate::cardinality_defaults::FILTER_SELECTIVITY_DIVISOR),
);
assert!(early.random_io < late.random_io);
}
fn single_hop() -> GraphMatchProgram {
program(
&[],
&["a", "r", "b"],
vec![
GraphMatchStep::Node(node("a")),
expand("a", Some("r"), "b", (1, 1)),
],
)
}
#[test]
fn relationship_only_conjuncts_run_on_the_edge_before_its_target() {
let r_compare = Predicate::PropertyCompare {
variable: "r".to_string(),
property: "x".to_string(),
op: ComparisonOp::Gt,
value: Value::Int(1),
};
let (program, decision) = place(
chain(),
Predicate::And(vec![
property_eq("r", 1),
Predicate::Or(vec![r_compare.clone(), property_eq("r", 5)]),
Predicate::Not(Box::new(r_compare.clone())),
Predicate::PropertyIsNull {
variable: "r".to_string(),
property: "y".to_string(),
},
Predicate::ExpressionEq {
expression: property("r"),
value: property("b"),
},
Predicate::Or(vec![property_eq("r", 1), property_eq("a", 1)]),
property_eq("b", 2),
property_eq("c", 3),
Predicate::ConstantBool(true),
]),
);
assert_eq!(program.predicate_steps, vec![2, 2, 2, 2, 2, 2, 2, 3, 0]);
assert_eq!(program.edge_predicates, vec![0, 1, 2, 3]);
assert!(
decision.as_deref().unwrap().ends_with(
"evaluate conjunct(s) [0, 1, 2, 3] on each relationship before reading its target node"
),
"{decision:?}"
);
}
#[test]
fn a_relationship_conjunct_on_the_last_step_still_runs_on_the_edge() {
let (program, decision) = place(
single_hop(),
Predicate::And(vec![property_eq("b", 1), property_eq("r", 1)]),
);
assert_eq!(program.predicate_steps, vec![2, 2]);
assert_eq!(program.edge_predicates, vec![1]);
assert!(decision.is_some());
let (program, _) = place(single_hop(), property_eq("r", 1));
assert_eq!(program.predicate_steps, vec![2]);
assert_eq!(program.edge_predicates, vec![0]);
assert_eq!(program.predicate, Some(property_eq("r", 1)));
}
#[test]
fn bound_imported_and_reused_relationships_are_not_pushed() {
let mut imported = single_hop();
imported.introduced = vec!["a".to_string(), "b".to_string()];
let (placed, _) = place(imported, property_eq("r", 1));
assert_eq!(placed.predicate_steps, vec![0]);
assert!(placed.edge_predicates.is_empty());
let reused = program(
&[],
&["a", "r", "b", "c"],
vec![
GraphMatchStep::Node(node("a")),
expand("a", Some("r"), "b", (1, 1)),
expand("b", Some("r"), "c", (1, 1)),
],
);
let (program, _) = place(reused, property_eq("r", 1));
assert_eq!(program.predicate_steps, vec![2]);
assert_eq!(program.edge_predicates, vec![0]);
}
#[test]
fn anonymous_and_variable_length_steps_bind_no_edge_conjunct() {
let anonymous = program(
&[],
&["a", "b", "c"],
vec![
GraphMatchStep::Node(node("a")),
expand("a", None, "b", (1, 3)),
expand("b", None, "c", (1, 1)),
],
);
let (program, _) = place(
anonymous,
Predicate::And(vec![property_eq("b", 1), property_eq("c", 1)]),
);
assert!(program.edge_predicates.is_empty());
}