use super::count_stats::{count_only_stats_result, stats_view_serves};
use super::test_support::{
clauses_of, commit_scratch, exec_autocommit, exec_cypher, exec_cypher_on, exec_write,
write_scratch,
};
use crate::graph::Graph;
use crate::overlay::ExecGraph;
use crate::row::Row;
use relate_core::Value;
use relate_ir::Clause;
use std::collections::BTreeMap;
fn declines_the_shortcircuit(g: &Graph, cypher: &str) {
let clauses = clauses_of(cypher);
let view = ExecGraph::read(g);
assert!(
count_only_stats_result(&view, &clauses).is_none(),
"must decline to the scan: {cypher}"
);
}
fn count_graph() -> Graph {
let g = Graph::default();
g.begin_write().unwrap();
let a = g.create_node(vec!["A".into()], BTreeMap::new()).unwrap();
let b = g
.create_node(vec!["B".into(), "C".into()], BTreeMap::new())
.unwrap();
g.create_rel(a, b, "R".into(), BTreeMap::new()).unwrap();
g.create_rel(a, a, "SELF".into(), BTreeMap::new()).unwrap();
g.commit_write();
g
}
#[test]
fn count_only_shapes_answer_from_the_stats() {
let g = count_graph();
let r = exec_cypher(&g, "MATCH (a:A) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(1));
let r = exec_cypher(&g, "MATCH (a:A)-[:R]->(b:B) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(1));
let r = exec_cypher(&g, "MATCH (b:B)<-[:R]-(a:A) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(1));
let r = exec_cypher(&g, "MATCH (a:A)-[r:R]->(b:B) RETURN count(r) AS n");
assert_eq!(r.rows[0][0], Value::Int(1));
let r = exec_cypher(&g, "MATCH (a:A)-[:NOPE]->(b:B) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(0));
let r = exec_cypher(&g, "MATCH (a:A) RETURN count(*)");
assert_eq!(r.columns[0], "count(*)");
}
#[test]
fn count_only_unlabeled_population_shapes_answer_from_the_stats() {
let g = count_graph();
g.begin_write().unwrap();
g.create_node(vec![], BTreeMap::new()).unwrap();
g.commit_write();
let r = exec_cypher(&g, "MATCH (n) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(3));
let r = exec_cypher(&g, "MATCH (n) RETURN count(n) AS n");
assert_eq!(r.rows[0][0], Value::Int(3));
let r = exec_cypher(&g, "MATCH ()-[r:R]->() RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(1));
let r = exec_cypher(&g, "MATCH ()<-[r:R]-() RETURN count(r) AS n");
assert_eq!(r.rows[0][0], Value::Int(1));
let r = exec_cypher(&g, "MATCH ()-[r:NOPE]->() RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(0));
let r = exec_cypher(&g, "MATCH ()-->() RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(2));
let r = exec_cypher(&g, "MATCH ()-[r]->() RETURN count(r) AS n");
assert_eq!(r.rows[0][0], Value::Int(2));
}
#[test]
fn count_only_unlabeled_population_counts_follow_deletes() {
let g = count_graph();
g.begin_write().unwrap();
g.create_node(vec![], BTreeMap::new()).unwrap();
g.commit_write();
exec_autocommit(&g, "MATCH ()-[r:SELF]->() DELETE r").unwrap();
let r = exec_cypher(&g, "MATCH ()-[r:SELF]->() RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(0));
let r = exec_cypher(&g, "MATCH ()-->() RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(1));
exec_autocommit(&g, "MATCH (a:A) DETACH DELETE a").unwrap();
let r = exec_cypher(&g, "MATCH (n) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(2));
let r = exec_cypher(&g, "MATCH ()-->() RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(0));
exec_autocommit(&g, "MATCH (b:B) SET b:D").unwrap();
let r = exec_cypher(&g, "MATCH (n) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(2));
}
#[test]
fn count_only_declines_the_shapes_between_the_population_arms() {
let g = Graph::default();
g.begin_write().unwrap();
let a = g.create_node(vec!["A".into()], BTreeMap::new()).unwrap();
let b = g.create_node(vec!["B".into()], BTreeMap::new()).unwrap();
g.create_rel(a, b, "R".into(), BTreeMap::new()).unwrap();
g.commit_write();
declines_the_shortcircuit(&g, "MATCH ()--() RETURN count(*) AS n");
assert_eq!(
exec_cypher(&g, "MATCH ()--() RETURN count(*) AS n").rows[0][0],
Value::Int(2)
);
declines_the_shortcircuit(&g, "MATCH (a:A)-[:R]->() RETURN count(*) AS n");
assert_eq!(
exec_cypher(&g, "MATCH (a:A)-[:R]->() RETURN count(*) AS n").rows[0][0],
Value::Int(1)
);
declines_the_shortcircuit(&g, "MATCH ()-[:R]->(b:B) RETURN count(*) AS n");
declines_the_shortcircuit(&g, "MATCH ()-[r:R|SELF]->() RETURN count(*) AS n");
declines_the_shortcircuit(&g, "MATCH ()-[*]->() RETURN count(*) AS n");
}
#[test]
fn count_only_declines_the_shapes_the_stats_cannot_answer() {
let g = count_graph();
g.begin_write().unwrap();
g.create_node(vec!["B".into()], BTreeMap::new()).unwrap();
g.create_node(vec!["C".into()], BTreeMap::new()).unwrap();
g.commit_write();
declines_the_shortcircuit(&g, "MATCH (b:B:C) RETURN count(*) AS n");
assert_eq!(
exec_cypher(&g, "MATCH (b:B:C) RETURN count(*) AS n").rows[0][0],
Value::Int(1)
);
declines_the_shortcircuit(&g, "MATCH (a:A) WHERE 1 = 1 RETURN count(*) AS n");
declines_the_shortcircuit(&g, "MATCH (a:A) RETURN count(DISTINCT a) AS n");
declines_the_shortcircuit(&g, "MATCH (a:A) RETURN count(*) AS n SKIP 1");
declines_the_shortcircuit(&g, "MATCH (a:A)-[:R]-(b:B) RETURN count(*) AS n");
}
#[test]
fn count_only_label_count_is_exact_after_deletes_and_label_removal() {
let g = Graph::default();
g.begin_write().unwrap();
let doomed = g.create_node(vec!["L".into()], BTreeMap::new()).unwrap();
g.create_node(vec!["L".into(), "M".into()], BTreeMap::new())
.unwrap();
g.create_node(vec!["L".into()], BTreeMap::new()).unwrap();
g.commit_write();
g.begin_write().unwrap();
g.delete_node(doomed, true).unwrap();
g.commit_write();
exec_autocommit(&g, "MATCH (n:L:M) REMOVE n:M").unwrap();
let r = exec_cypher(&g, "MATCH (n:L) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(2));
let r = exec_cypher(&g, "MATCH (n:M) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(0));
}
#[test]
fn count_only_declines_inside_a_transaction_and_reads_its_own_writes() {
let g = count_graph();
let scratch = write_scratch(&g);
exec_write(&g, &scratch, "CREATE (a:A)-[:R]->(b:B)").unwrap();
let view = ExecGraph::write(&g, &scratch);
let r = exec_cypher_on(&view, "MATCH (a:A)-[:R]->(b:B) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(2));
let r = exec_cypher_on(&view, "MATCH (a:A) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(2));
commit_scratch(&g, scratch);
let r = exec_cypher(&g, "MATCH (a:A)-[:R]->(b:B) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(2));
let r = exec_cypher(&g, "MATCH (a:A) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(2));
}
#[test]
fn count_only_declines_on_a_pinned_older_snapshot() {
let g = count_graph();
let stale = ExecGraph::read_at(&g, 0);
let r = exec_cypher_on(&stale, "MATCH (a:A) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(0));
let r = exec_cypher_on(&stale, "MATCH (a:A)-[:R]->(b:B) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(0));
}
#[test]
fn count_only_stats_follow_deletes_and_label_mutations() {
let g = count_graph();
let r = exec_cypher(&g, "MATCH (a:A)-[:R]->(b:B) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(1));
exec_autocommit(&g, "MATCH (a:A)-[r:R]->(b:B) DELETE r").unwrap();
let r = exec_cypher(&g, "MATCH (a:A)-[:R]->(b:B) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(0));
exec_autocommit(&g, "CREATE (a:A)-[:R]->(b:B)").unwrap();
let r = exec_cypher(&g, "MATCH (a:A)-[:R]->(b:B) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(1));
exec_autocommit(&g, "MATCH (b:B:C) REMOVE b:C").unwrap();
let r = exec_cypher(&g, "MATCH (a:A)-[:R]->(b:B) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(1));
}
#[test]
fn count_only_declines_seeded_rows_beyond_one_unbound_seed() {
let g = count_graph();
let q = relate_front::parse_and_lower("MATCH (a:A) RETURN count(*) AS n").unwrap();
let planned = relate_planner::plan(q, &mut |_| false).unwrap();
let clauses: Vec<Clause> = planned.parts.iter().flatten().cloned().collect();
let view = ExecGraph::read(&g);
assert!(stats_view_serves(&view, &[Row::new()]));
assert!(count_only_stats_result(&view, &clauses).is_some());
assert!(!stats_view_serves(&view, &[]));
let two = vec![Row::new(), Row::new()];
assert!(!stats_view_serves(&view, &two));
let bound = vec![Row::from([("x".to_string(), Value::Int(1))])];
assert!(!stats_view_serves(&view, &bound));
}
#[test]
fn count_only_declines_inline_pattern_predicates() {
let g = count_graph();
declines_the_shortcircuit(&g, "MATCH (a:A WHERE a.x > 1) RETURN count(*) AS n");
let r = exec_cypher(&g, "MATCH (a:A WHERE a.x > 1) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(0));
declines_the_shortcircuit(
&g,
"MATCH (a:A)-[r:R WHERE r.x > 1]->(b:B) RETURN count(*) AS n",
);
let r = exec_cypher(
&g,
"MATCH (a:A)-[r:R WHERE r.x > 1]->(b:B) RETURN count(*) AS n",
);
assert_eq!(r.rows[0][0], Value::Int(0));
}
#[test]
fn count_only_self_loop_label_mutation_charges_the_diagonal_once() {
let g = Graph::default();
g.begin_write().unwrap();
let a = g.create_node(vec!["A".into()], BTreeMap::new()).unwrap();
g.create_rel(a, a, "SELF".into(), BTreeMap::new()).unwrap();
g.commit_write();
exec_autocommit(&g, "MATCH (a:A) SET a:B").unwrap();
let r = exec_cypher(&g, "MATCH (x:B)-[:SELF]->(y:B) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(1));
let r = exec_cypher(&g, "MATCH (x:A)-[:SELF]->(y:B) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(1));
let r = exec_cypher(&g, "MATCH (x:B)-[:SELF]->(y:A) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(1));
exec_autocommit(&g, "MATCH (a:A:B) REMOVE a:B").unwrap();
let r = exec_cypher(&g, "MATCH (x:B)-[:SELF]->(y:B) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(0));
let r = exec_cypher(&g, "MATCH (x:B)-[:SELF]->(y:A) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(0));
}
#[test]
fn count_only_declines_same_variable_self_loop_shapes() {
let g = Graph::default();
g.begin_write().unwrap();
let a = g.create_node(vec!["A".into()], BTreeMap::new()).unwrap();
let a2 = g.create_node(vec!["A".into()], BTreeMap::new()).unwrap();
g.create_rel(a, a, "SELF".into(), BTreeMap::new()).unwrap();
g.create_rel(a, a2, "SELF".into(), BTreeMap::new()).unwrap();
g.commit_write();
declines_the_shortcircuit(&g, "MATCH (a:A)-[:SELF]->(a:A) RETURN count(*) AS n");
let r = exec_cypher(&g, "MATCH (a:A)-[:SELF]->(a:A) RETURN count(*) AS n");
assert_eq!(r.rows[0][0], Value::Int(1));
}