use std::collections::BTreeMap;
use relate_core::Value;
use crate::graph::Graph;
use super::batch::{take_batch_engagements, with_batch_disabled};
use super::take_zone_skips;
use super::test_support::{
column, error_of, exec_autocommit, int_cell, string_cell, try_exec_cypher_params, Builder,
};
fn comments() -> Graph {
let g = Builder::new().done();
exec_autocommit(
&g,
"CREATE (:Comment {id: 'short', length: 10}),
(:Comment {id: 'long', length: 2000}),
(:Comment {id: 'longer', length: 4000}),
(:Comment {id: 'missing'}),
(:Comment {id: 'textual', length: 'many'}),
(:Comment {id: 'flagged', length: 3000, archived: true}),
(:Root {id: 'root'})",
)
.unwrap();
exec_autocommit(
&g,
"MATCH (c:Comment {id: 'long'}), (r:Root) CREATE (c)-[:T]->(r)",
)
.unwrap();
g
}
fn differential(g: &Graph, cypher: &str, engages: bool) -> relate_core::Result<super::QueryResult> {
let batched = super::test_support::try_exec_cypher(g, cypher)?;
let engagements = take_batch_engagements();
assert_eq!(
engagements,
u32::from(engages),
"engagement for {cypher}: expected {}, took {}",
u32::from(engages),
engagements
);
let per_row = with_batch_disabled(|| super::test_support::try_exec_cypher(g, cypher))?;
assert_eq!(
batched.columns, per_row.columns,
"columns diverge for {cypher}"
);
assert_eq!(batched.rows, per_row.rows, "rows diverge for {cypher}");
Ok(batched)
}
fn ids(result: &super::QueryResult, name: &str) -> Vec<String> {
column(result, name, string_cell)
}
#[test]
fn where_filtering_and_count_match_the_row_path() {
let g = comments();
let counted = differential(
&g,
"MATCH (c:Comment) WHERE c.length > 1000 RETURN count(c.id)",
true,
)
.unwrap();
assert_eq!(counted.rows, vec![vec![Value::Int(3)]]);
let filtered = differential(
&g,
"MATCH (c:Comment) WHERE c.length > 1000 RETURN c.id LIMIT 99",
true,
)
.unwrap();
let mut kept = ids(&filtered, "c.id");
kept.sort();
assert_eq!(kept, vec!["flagged", "long", "longer"]);
}
#[test]
fn scan_results_keep_the_scan_order_and_bind_the_variable() {
let g = comments();
let batched = differential(&g, "MATCH (c:Comment) RETURN c.id LIMIT 99", true).unwrap();
let mut kept = ids(&batched, "c.id");
assert_eq!(kept.len(), 6);
kept.sort();
assert_eq!(
kept,
vec!["flagged", "long", "longer", "missing", "short", "textual"]
);
assert_eq!(batched.columns, vec!["c.id".to_string()]);
}
#[test]
fn null_predicates_read_the_bitmap() {
let g = comments();
let is_null = differential(
&g,
"MATCH (c:Comment) WHERE c.length IS NULL RETURN c.id LIMIT 99",
true,
)
.unwrap();
assert_eq!(ids(&is_null, "c.id"), vec!["missing"]);
let not_null = differential(
&g,
"MATCH (c:Comment) WHERE c.length IS NOT NULL RETURN count(c.id)",
true,
)
.unwrap();
assert_eq!(not_null.rows, vec![vec![Value::Int(5)]]);
let null_bound = differential(
&g,
"MATCH (c:Comment) WHERE c.length > null RETURN c.id LIMIT 99",
true,
)
.unwrap();
assert!(null_bound.rows.is_empty());
}
#[test]
fn several_conjuncts_short_circuit_like_the_row_path() {
let g = comments();
let result = differential(
&g,
"MATCH (c:Comment) WHERE c.length > 1000 AND c.archived IS NULL RETURN c.id LIMIT 99",
true,
)
.unwrap();
assert_eq!(ids(&result, "c.id"), vec!["long", "longer"]);
let counted = differential(
&g,
"MATCH (c:Comment) WHERE c.length > 1000 AND c.length < 3000 RETURN count(c.id)",
true,
)
.unwrap();
assert_eq!(counted.rows, vec![vec![Value::Int(1)]]);
}
#[test]
fn string_list_and_parameter_bounds_match_the_row_path() {
let g = comments();
let prefixed = differential(
&g,
"MATCH (c:Comment) WHERE c.id STARTS WITH 'long' RETURN c.id LIMIT 99",
true,
)
.unwrap();
let mut kept = ids(&prefixed, "c.id");
kept.sort();
assert_eq!(kept, vec!["long", "longer"]);
differential(
&g,
"MATCH (c:Comment) WHERE c.id IN ['short', 'textual'] RETURN count(c.id)",
true,
)
.unwrap();
let params = BTreeMap::from([("min".to_string(), Value::Int(2000))]);
let batched = try_exec_cypher_params(
&g,
"MATCH (c:Comment) WHERE c.length >= $min RETURN count(c.id)",
¶ms,
)
.unwrap();
assert_eq!(take_batch_engagements(), 1);
assert_eq!(batched.rows, vec![vec![Value::Int(3)]]);
}
#[test]
fn error_parity_on_a_bad_bound_operand() {
let g = comments();
let batched = error_of(super::test_support::try_exec_cypher(
&g,
"MATCH (c:Comment) WHERE c.id =~ '[' RETURN c.id LIMIT 99",
));
let per_row = with_batch_disabled(|| {
error_of(super::test_support::try_exec_cypher(
&g,
"MATCH (c:Comment) WHERE c.id =~ '[' RETURN c.id LIMIT 99",
))
});
assert_eq!(batched, per_row);
let _ = take_batch_engagements();
differential(
&g,
"MATCH (c:Nope) WHERE c.id =~ '[' RETURN c.id LIMIT 99",
true,
)
.unwrap();
}
#[test]
fn the_gate_declines_every_unsupported_shape() {
let g = comments();
for cypher in [
"MATCH (c:Comment)-[:T]->(:Root) WHERE c.length > 1000 RETURN count(c.id)",
"OPTIONAL MATCH (c:Comment) WHERE c.length > 1000 RETURN count(c.id)",
"MATCH (c:Comment {length: 2000}) RETURN count(c.id)",
"MATCH (c:Comment) WHERE c.length > 1000 OR c.archived IS NULL RETURN count(c.id)",
"MATCH (c:Comment) WHERE NOT c.length > 1000 RETURN count(c.id)",
"MATCH (c:Comment) WHERE c.length > c.archived RETURN count(c.id)",
"MATCH (c:Comment) WHERE size(c.id) > 3 RETURN count(c.id)",
"UNWIND [1000, 3000] AS min MATCH (c:Comment) WHERE c.length > min RETURN count(c.id)",
"MATCH (c:Comment) WHERE c.length > 1000 RETURN c.id",
"MATCH (c:Comment) WHERE c.length > 1000 RETURN c.id ORDER BY c.id",
"MATCH (c:Comment) WHERE c.length > 1000 RETURN DISTINCT c.id LIMIT 2",
"MATCH (c:Comment), (r:Root) WHERE c.length > 1000 RETURN count(c.id)",
"MATCH p = (c:Comment) WHERE c.length > 1000 RETURN count(p)",
"MATCH (c:Comment) WHERE 1000 < c.length RETURN count(c.id)",
] {
differential(&g, cypher, false).unwrap();
}
}
#[test]
fn a_nondeterministic_bound_declines() {
let g = comments();
let _ = super::test_support::try_exec_cypher(
&g,
"MATCH (c:Comment) WHERE c.length > toInteger(rand() * 100000) RETURN count(c.id)",
)
.unwrap();
assert_eq!(take_batch_engagements(), 0);
}
#[test]
fn the_chain_handles_more_than_one_chunk() {
let g = Builder::new().done();
let mut create = String::from("CREATE ");
for i in 0..1200 {
if i > 0 {
create.push_str(", ");
}
create.push_str(&format!("(:Bulk {{n: {i}}})"));
}
exec_autocommit(&g, &create).unwrap();
let counted =
differential(&g, "MATCH (b:Bulk) WHERE b.n > 100 RETURN count(b.n)", true).unwrap();
assert_eq!(counted.rows, vec![vec![Value::Int(1099)]]);
let paged = differential(
&g,
"MATCH (b:Bulk) WHERE b.n > 1195 RETURN b.n LIMIT 99",
true,
)
.unwrap();
let values = column(&paged, "b.n", int_cell);
assert_eq!(values, (1196..1200).collect::<Vec<_>>());
}
#[test]
fn zone_skips_are_measured_and_answers_unchanged() {
let g = Builder::new().done();
let mut create = String::from("CREATE ");
for i in 0..1200 {
if i > 0 {
create.push_str(", ");
}
create.push_str(&format!("(:Bulk {{n: {i}}})"));
}
exec_autocommit(&g, &create).unwrap();
let paged = differential(
&g,
"MATCH (b:Bulk) WHERE b.n > 1195 RETURN b.n LIMIT 99",
true,
)
.unwrap();
let values = column(&paged, "b.n", int_cell);
assert_eq!(values, (1196..1200).collect::<Vec<_>>());
assert_eq!(take_zone_skips(), 2, "the two provable chunks skipped");
let nulled = differential(
&g,
"MATCH (b:Bulk) WHERE b.n IS NULL RETURN count(b.n)",
true,
)
.unwrap();
assert_eq!(nulled.rows, vec![vec![Value::Int(0)]]);
assert_eq!(take_zone_skips(), 3, "every chunk skipped");
let cross_kind =
differential(&g, "MATCH (b:Bulk) WHERE b.n > 'x' RETURN count(b.n)", true).unwrap();
assert_eq!(cross_kind.rows, vec![vec![Value::Int(0)]]);
assert_eq!(take_zone_skips(), 3, "every chunk skipped");
let comments = comments();
let past = differential(
&comments,
"MATCH (c:Comment) WHERE c.id > 'zzz' RETURN count(c.id)",
true,
)
.unwrap();
assert_eq!(past.rows, vec![vec![Value::Int(0)]]);
assert_eq!(take_zone_skips(), 1, "the one chunk skipped");
}
#[test]
fn fallback_columns_never_skip() {
let g = comments();
let counted = differential(
&g,
"MATCH (c:Comment) WHERE c.length > 1000 RETURN count(c.id)",
true,
)
.unwrap();
assert_eq!(counted.rows, vec![vec![Value::Int(3)]]);
assert_eq!(
take_zone_skips(),
0,
"the fallback column has no zone facts"
);
let past = differential(
&g,
"MATCH (c:Comment) WHERE c.length > 4000 RETURN count(c.id)",
true,
)
.unwrap();
assert_eq!(past.rows, vec![vec![Value::Int(0)]]);
assert_eq!(take_zone_skips(), 0, "no bounds, no skip");
}