use std::collections::BTreeMap;
use super::index_aggregates::recognize;
use super::stats_short_circuit::stats_short_circuit;
use super::test_support::{
clauses_of, commit_scratch, exec_autocommit, exec_cypher_on, exec_write, sort_rows,
with_n_stage, write_scratch,
};
use crate::ctx::ExecCtx;
use crate::graph::Graph;
use crate::overlay::ExecGraph;
use crate::row::Row;
use relate_core::Value;
fn index_answer(view: &ExecGraph<'_>, cypher: &str) -> Option<Vec<Vec<Value>>> {
let params = BTreeMap::new();
let ctx = ExecCtx::new(view, ¶ms);
stats_short_circuit(ctx, &clauses_of(cypher), &[Row::new()], None)
.unwrap()
.map(|(_, result)| result.rows)
}
fn scan_answer(view: &ExecGraph<'_>, cypher: &str) -> Vec<Vec<Value>> {
let scan = with_n_stage(cypher);
assert!(
index_answer(view, &scan).is_none(),
"the oracle must be the scan: {scan}"
);
exec_cypher_on(view, &scan).rows
}
fn sorted(mut rows: Vec<Vec<Value>>) -> Vec<Vec<Value>> {
sort_rows(&mut rows);
rows
}
const GROUPED: &[&str] = &[
"MATCH (n:P) RETURN n.age AS age, count(*) AS c",
"MATCH (n:P) RETURN count(n) AS c, n.age",
];
const GROUPED_ORDERED: &[&str] = &[
"MATCH (n:P) RETURN n.age AS age, count(*) AS c ORDER BY c DESC, age",
"MATCH (n:P) RETURN n.age AS age, count(*) AS c ORDER BY age SKIP 1 LIMIT 2",
];
const FOLDS: &[&str] = &[
"MATCH (n:P) RETURN min(n.age) AS lo, max(n.age) AS hi",
"MATCH (n:P) RETURN count(n.age) AS c",
"MATCH (n:P) RETURN max(n.age), min(n.age), count(n.age)",
];
fn assert_matches_scan(g: &Graph, key: &str, when: &str) {
let view = ExecGraph::read(g);
for q in GROUPED.iter().chain(FOLDS) {
let q = q.replace("age", key);
let got = index_answer(&view, &q)
.unwrap_or_else(|| panic!("{when}: the postings must answer: {q}"));
assert_eq!(sorted(got), sorted(scan_answer(&view, &q)), "{when}: {q}");
}
for q in GROUPED_ORDERED {
let q = q.replace("age", key);
let got = index_answer(&view, &q)
.unwrap_or_else(|| panic!("{when}: the postings must answer: {q}"));
assert_eq!(got, scan_answer(&view, &q), "{when}: {q}");
}
}
fn setup_indexed() -> Graph {
let g = Graph::default();
exec_autocommit(&g, "CREATE RANGE INDEX p_age FOR (n:P) ON (n.age)").unwrap();
setup_data(&g);
g
}
fn setup_unindexed() -> Graph {
let g = Graph::default();
setup_data(&g);
g
}
fn setup_data(g: &Graph) {
exec_autocommit(
g,
"CREATE (:P {age: 3, name: 'a'}), (:P {age: 3}), (:P {age: 5}), (:P {age: 9}), \
(:P {name: 'null'}), (:Q {age: 1}), (:Q {age: 99})",
)
.unwrap();
}
#[test]
fn a_range_index_answers_every_shape_across_writes() {
let g = setup_indexed();
assert_matches_scan(&g, "age", "after create");
exec_autocommit(&g, "MATCH (n:P {age: 9}) SET n.age = 4").unwrap();
exec_autocommit(&g, "MATCH (n:P {age: 3, name: 'a'}) SET n.age = 100").unwrap();
assert_matches_scan(&g, "age", "after overwrites");
exec_autocommit(&g, "MATCH (n:P {age: 100}) DELETE n").unwrap();
assert_matches_scan(&g, "age", "after delete");
exec_autocommit(&g, "MATCH (n:P {age: 5}) REMOVE n:P").unwrap();
exec_autocommit(&g, "MATCH (n:Q {age: 1}) SET n:P").unwrap();
assert_matches_scan(&g, "age", "after label moves");
exec_autocommit(&g, "MATCH (n:P) SET n.age = null").unwrap();
let view = ExecGraph::read(&g);
assert_eq!(
index_answer(&view, FOLDS[0]).unwrap(),
vec![vec![Value::Null, Value::Null]]
);
}
#[test]
fn string_extremes_equal_the_scan() {
let g = Graph::default();
exec_autocommit(&g, "CREATE RANGE INDEX p_name FOR (n:P) ON (n.name)").unwrap();
exec_autocommit(
&g,
"CREATE (:P {name: 'b'}), (:P {name: 'a'}), (:P {name: 'c'})",
)
.unwrap();
exec_autocommit(&g, "MATCH (n:P {name: 'c'}) SET n.name = 'aa'").unwrap();
assert_matches_scan(&g, "name", "string keys");
}
#[test]
fn mixed_types_and_floats_decline_the_extremes() {
let g = setup_indexed();
exec_autocommit(&g, "CREATE (:P {age: 'old'})").unwrap();
let view = ExecGraph::read(&g);
assert!(
index_answer(&view, FOLDS[0]).is_none(),
"Int and String keys"
);
let g = setup_indexed();
exec_autocommit(&g, "MATCH (n:P {age: 9}) SET n.age = 9.5").unwrap();
let view = ExecGraph::read(&g);
assert!(
index_answer(&view, FOLDS[0]).is_none(),
"Int and Float keys"
);
let g = setup_indexed();
exec_autocommit(&g, "MATCH (n:P {age: 3, name: 'a'}) SET n.age = [1]").unwrap();
let view = ExecGraph::read(&g);
assert!(
index_answer(&view, FOLDS[0]).is_none(),
"a value off the order"
);
}
#[test]
fn a_nan_value_never_makes_a_shape_disagree_with_the_scan() {
let g = setup_indexed();
exec_autocommit(&g, "CREATE (:P {age: 0.0 / 0.0})").unwrap();
let view = ExecGraph::read(&g);
for q in GROUPED.iter().chain(FOLDS) {
if let Some(got) = index_answer(&view, q) {
assert_eq!(sorted(got), sorted(scan_answer(&view, q)), "{q}");
}
}
for q in GROUPED_ORDERED {
if let Some(got) = index_answer(&view, q) {
assert_eq!(got, scan_answer(&view, q), "{q}");
}
}
}
#[test]
fn a_nan_value_in_a_built_equality_pair_declines() {
for nan in ["0.0 / 0.0", "[1, 0.0 / 0.0]"] {
let g = setup_unindexed();
exec_autocommit(&g, &format!("CREATE (:P {{age: {nan}}})")).unwrap();
g.equality_postings("P", "age", &Value::Int(3));
let view = ExecGraph::read(&g);
for q in GROUPED.iter().chain(FOLDS) {
if let Some(got) = index_answer(&view, q) {
assert_eq!(sorted(got), sorted(scan_answer(&view, q)), "{nan}: {q}");
}
}
for q in GROUPED_ORDERED {
if let Some(got) = index_answer(&view, q) {
assert_eq!(got, scan_answer(&view, q), "{nan}: {q}");
}
}
}
}
#[test]
fn counts_use_only_a_pair_that_already_exists() {
let g = setup_unindexed();
let view = ExecGraph::read(&g);
assert!(index_answer(&view, GROUPED[0]).is_none());
assert!(index_answer(&view, GROUPED[0]).is_none());
assert!(
index_answer(&view, FOLDS[0]).is_none(),
"extremes need a range index"
);
drop(view);
g.equality_postings("P", "age", &Value::Int(3));
exec_autocommit(&g, "MATCH (n:P {age: 9}) SET n.age = 4").unwrap();
let view = ExecGraph::read(&g);
for q in [GROUPED[0], FOLDS[1]] {
let got = index_answer(&view, q);
assert_eq!(
sorted(got.expect("the built pair answers")),
sorted(scan_answer(&view, q))
);
}
}
#[test]
fn views_that_are_not_the_latest_state_decline() {
let g = setup_indexed();
let old = g.commit_ts();
exec_autocommit(&g, "CREATE (:P {age: 7})").unwrap();
let stale = ExecGraph::read_at(&g, old);
for q in GROUPED.iter().chain(GROUPED_ORDERED).chain(FOLDS) {
assert!(
index_answer(&stale, q).is_none(),
"pinned older snapshot: {q}"
);
}
assert_eq!(
exec_cypher_on(&stale, "MATCH (n:P) RETURN count(n.age) AS c").rows,
vec![vec![Value::Int(4)]]
);
let view = ExecGraph::read(&g);
let params = BTreeMap::new();
let ctx = ExecCtx::new(&view, ¶ms);
let two = [Row::new(), Row::new()];
assert!(stats_short_circuit(ctx, &clauses_of(FOLDS[0]), &two, None)
.unwrap()
.is_none());
}
#[test]
fn an_explicit_transaction_declines_and_reads_its_own_writes() {
let g = setup_indexed();
let scratch = write_scratch(&g);
exec_write(&g, &scratch, "MATCH (n:P {age: 9}) SET n.age = 4").unwrap();
exec_write(&g, &scratch, "CREATE (:P {age: 50})").unwrap();
{
let view = ExecGraph::write(&g, &scratch);
for q in GROUPED.iter().chain(GROUPED_ORDERED).chain(FOLDS) {
assert!(index_answer(&view, q).is_none(), "in a transaction: {q}");
}
assert_eq!(
exec_cypher_on(&view, FOLDS[0]).rows,
vec![vec![Value::Int(3), Value::Int(50)]]
);
}
commit_scratch(&g, scratch);
assert_matches_scan(&g, "age", "after the transaction commits");
}
#[test]
fn the_recognizer_admits_its_shapes_and_declines_near_misses() {
let admitted = [
"MATCH (n:P) RETURN n.age, count(*)",
"MATCH (n:P) RETURN count(*) AS c, n.age AS a",
"MATCH (n:P) RETURN n.age, count(n) ORDER BY n.age",
"MATCH (n:P) RETURN min(n.age)",
"MATCH (n:P) RETURN max(n.age)",
"MATCH (n:P) RETURN count(n.age)",
"MATCH (n:P) RETURN min(n.age), max(n.age), count(n.age)",
];
for q in admitted {
assert!(recognize(&clauses_of(q)).is_some(), "must admit: {q}");
}
let declined = [
"MATCH (n) RETURN n.age, count(*)",
"MATCH (n) RETURN min(n.age)",
"MATCH (n:P:Q) RETURN n.age, count(*)",
"MATCH (n:P) RETURN avg(n.age)",
"MATCH (n:P) RETURN sum(n.age)",
"MATCH (n:P) WHERE n.age > 1 RETURN n.age, count(*)",
"MATCH (n:P {name: 'a'}) RETURN min(n.age)",
"MATCH (n:P) RETURN DISTINCT n.age, count(*)",
"MATCH (n:P) RETURN count(DISTINCT n.age)",
"MATCH (n:P) RETURN min(n.age), max(n.score)",
"MATCH (n:P) RETURN min(n.age) ORDER BY min(n.age)",
"MATCH (n:P) RETURN min(n.age) LIMIT 1",
"MATCH (n:P) RETURN n.age, n.name, count(*)",
"MATCH (n:P) RETURN n.age",
"MATCH (n:P) RETURN n.age + 1, count(*)",
"MATCH (n:P)-[:R]->() RETURN n.age, count(*)",
"MATCH (n:P) RETURN n.age AS n, count(*)",
"OPTIONAL MATCH (n:P) RETURN n.age, count(*)",
];
for q in declined {
assert!(recognize(&clauses_of(q)).is_none(), "must decline: {q}");
}
}