#[path = "common/harness.rs"]
mod harness;
#[path = "common/plan_fixture.rs"]
mod plan_fixture;
use harness::TestHarness;
use plan_fixture::{
assert_has_statistics, counts, counts_of, plan_of, populated_and_analysed, Counts,
};
const PINNED: &[(&str, &str, Counts)] = &[
(
"the traversal CTE's recursive step",
"SELECT l.target_id FROM links_current l WHERE l.source_id = ?1 \
AND l.valid_from <= ?3 AND ?3 < l.valid_to AND l.weight >= ?4",
counts(1, 1, 0),
),
(
"the overlap guard and the single-open probe",
"SELECT valid_from, valid_to FROM links_current \
WHERE source_id = ?1 AND target_id = ?2 AND edge_type = ?3 \
AND valid_from <> ?4",
counts(1, 1, 0),
),
(
"the fold's recorded_at window",
"SELECT seq_id, table_name, entity_id, operation, payload \
FROM transaction_log WHERE recorded_at <= ?1",
counts(2, 0, 1),
),
(
"the archive's supersession test",
"SELECT seq_id FROM transaction_log WHERE recorded_at < ?1 AND EXISTS ( \
SELECT 1 FROM transaction_log newer \
WHERE newer.entity_id = transaction_log.entity_id \
AND newer.seq_id > transaction_log.seq_id)",
counts(3, 1, 1),
),
(
"the archive cutoff on the links ledger",
"SELECT source_id, target_id FROM links WHERE recorded_at < ?1 AND ( \
EXISTS ( \
SELECT 1 FROM links newer \
WHERE newer.source_id = links.source_id \
AND newer.target_id = links.target_id \
AND newer.edge_type = links.edge_type \
AND newer.valid_from = links.valid_from \
AND newer.recorded_at > links.recorded_at) \
OR (valid_to <> '9999-12-31T23:59:59.999999Z' AND valid_to <= ?1))",
counts(3, 1, 1),
),
(
"the concept-archival reverse-reachability arm",
"SELECT id FROM concepts WHERE retired = 1 AND recorded_at < ?1 \
AND valid_to < ?1 AND NOT EXISTS ( \
SELECT 1 FROM links WHERE links.source_id = concepts.id \
OR links.target_id = concepts.id)",
counts(2, 2, 1),
),
(
"CONTROL: a query with no index to use",
"SELECT id FROM concepts WHERE content LIKE ?1",
counts(1, 0, 1),
),
];
const CONTROL: &str = "CONTROL: a query with no index to use";
#[tokio::test]
async fn every_pinned_query_does_the_work_it_is_pinned_to() {
let harness = TestHarness::new();
let conn = populated_and_analysed(&harness.db_path).await;
assert_has_statistics(&conn).await;
for (label, sql, expected) in PINNED {
let got = counts_of(&conn, sql).await;
assert_eq!(
&got, expected,
"{label}: the work changed. Re-run `cargo run --example \
opcode_probe`, decide whether the new plan is the one you want, \
and change the number deliberately"
);
}
}
#[tokio::test]
async fn a_full_scan_and_an_index_range_scan_are_distinguishable() {
let harness = TestHarness::new();
let conn = populated_and_analysed(&harness.db_path).await;
let scan = PINNED.iter().find(|(l, _, _)| *l == CONTROL).unwrap();
let ranged = PINNED
.iter()
.find(|(l, _, _)| *l == "the fold's recorded_at window")
.unwrap();
let scan_counts = counts_of(&conn, scan.1).await;
let ranged_counts = counts_of(&conn, ranged.1).await;
assert_eq!(
(scan_counts.seeks, scan_counts.rewinds),
(ranged_counts.seeks, ranged_counts.rewinds),
"the premise of this test has stopped holding: the two shapes now \
differ in seeks or rewinds, so the module note about one number \
proving nothing needs rewriting"
);
assert_ne!(
scan_counts.opens, ranged_counts.opens,
"a full table scan and an index range scan are now indistinguishable \
to this gate, which means the gate has stopped gating"
);
}
#[tokio::test]
async fn losing_coverage_moves_the_count_and_not_the_registry_assertion() {
let harness = TestHarness::new();
let conn = populated_and_analysed(&harness.db_path).await;
let covered = "SELECT l.target_id FROM links_current l WHERE l.source_id = ?1 AND l.valid_from <= ?3 AND ?3 < l.valid_to AND l.weight >= ?4";
let uncovered = "SELECT l.target_id, l.properties FROM links_current l WHERE l.source_id = ?1 AND l.valid_from <= ?3 AND ?3 < l.valid_to AND l.weight >= ?4";
for sql in [covered, uncovered] {
assert!(
plan_of(&conn, sql).await.contains("idx_lc_traversal_cover"),
"the premise has stopped holding: this variant no longer uses the index at all, so it demonstrates nothing about coverage"
);
}
assert_eq!(counts_of(&conn, covered).await.opens, 1, "covered");
assert_eq!(
counts_of(&conn, uncovered).await.opens,
2,
"an uncovered query must open the table as well as the index, or the two shapes are indistinguishable here and this gate adds nothing to the plan gate"
);
}
#[test]
fn every_pinned_query_is_also_plan_pinned() {
const PLAN_TESTS: &str = include_str!("index_plan_tests.rs");
for (label, _, _) in PINNED {
if *label == CONTROL {
assert!(
!PLAN_TESTS.contains(label),
"the control has acquired a plan-pinning entry; it is supposed \
to be the query no index justifies"
);
continue;
}
assert!(
PLAN_TESTS.contains(label),
"{label} is pinned here and named nowhere in index_plan_tests: \
either the label drifted or the query lost the index that \
justifies it"
);
}
}