use std::collections::BTreeMap;
use relate_core::Value;
use crate::exec::test_support::exec_autocommit;
use crate::graph::Graph;
use super::fixtures::{parity, parity_params, InnerPlanKind};
#[test]
fn one_hop_qpp_interior_predicates_bytecode_and_fallback() {
let g = Graph::default();
exec_autocommit(
&g,
"CREATE (a:User {id: 1, age: 30, active: true, blocked: false, min_age: 18}),
(b:User {id: 2, age: 15, active: true, blocked: true, min_age: 20}),
(c:User {id: 3, age: 25, active: false, blocked: false, min_age: 18}),
(d:User {id: 4, age: 40, active: true, blocked: false, min_age: 20}),
(e:User {id: 5, age: 10, active: false, blocked: false, min_age: 5}),
(a)-[:FOLLOWS]->(b)-[:FOLLOWS]->(d),
(a)-[:FOLLOWS]->(c)-[:FOLLOWS]->(d),
(a)-[:FOLLOWS]->(e)-[:FOLLOWS]->(c)",
)
.unwrap();
let cases = [
(
"simple comparison",
"MATCH p = SHORTEST 1 (a:User {id: 1}) ((u:User)-[:FOLLOWS]->(m:User WHERE m.age > 20)){1,5} (b:User {id: 4}) \
RETURN [n IN nodes(p) | n.id]",
InnerPlanKind::Bytecode,
),
(
"boolean OR + NOT",
"MATCH p = SHORTEST 1 (a:User {id: 1}) \
((u:User)-[:FOLLOWS]->(m:User WHERE (m.age > 20 OR m.active = true) AND NOT (m.blocked = true))){1,5} (b:User {id: 4}) \
RETURN [n IN nodes(p) | n.id]",
InnerPlanKind::Bytecode,
),
(
"mirrored literal",
"MATCH p = SHORTEST 1 (a:User {id: 1}) ((u:User)-[:FOLLOWS]->(m:User WHERE 20 < m.age)){1,5} (b:User {id: 4}) \
RETURN [n IN nodes(p) | n.id]",
InnerPlanKind::Bytecode,
),
(
"two-property comparison",
"MATCH p = SHORTEST 1 (a:User {id: 1}) ((u:User)-[:FOLLOWS]->(m:User WHERE m.age > m.min_age)){1,5} (b:User {id: 4}) \
RETURN [n IN nodes(p) | n.id]",
InnerPlanKind::Bytecode,
),
(
"outer-row variable reference fallback",
"MATCH (a:User {id: 2}) \
MATCH p = SHORTEST 1 (start:User {id: 1}) ((u:User)-[:FOLLOWS]->(m:User WHERE m.age > a.age)){1,5} (b:User {id: 4}) \
RETURN [n IN nodes(p) | n.id]",
InnerPlanKind::Fallback,
),
];
for (label, q, expected_kind) in cases {
let counts = parity(&g, q);
assert!(counts.route_runs > 0, "{label} must engage route: {q}");
assert!(
counts.qpp_plans.contains(&expected_kind),
"{label} must prepare {expected_kind:?}, got {:?}",
counts.qpp_plans
);
let unexpected = match expected_kind {
InnerPlanKind::Bytecode => InnerPlanKind::Fallback,
InnerPlanKind::Fallback => InnerPlanKind::Bytecode,
InnerPlanKind::LabelsOnly => InnerPlanKind::Fallback,
};
assert!(
!counts.qpp_plans.contains(&unexpected),
"{label} must not prepare {unexpected:?}"
);
}
let q_param = "MATCH p = SHORTEST 1 (a:User {id: 1}) ((u:User)-[:FOLLOWS]->(m:User WHERE m.age > $min_age)){1,5} (b:User {id: 4}) \
RETURN [n IN nodes(p) | n.id]";
let mut params = BTreeMap::new();
params.insert("min_age".to_string(), Value::Int(20));
let counts = parity_params(&g, q_param, ¶ms);
assert!(
counts.route_runs > 0,
"parameterized constant must engage route: {q_param}"
);
assert!(
counts.qpp_plans.contains(&InnerPlanKind::Bytecode),
"parameterized query must prepare bytecode: {q_param}"
);
assert!(
!counts.qpp_plans.contains(&InnerPlanKind::Fallback),
"parameterized query must not prepare fallback: {q_param}"
);
}