#![allow(clippy::result_large_err)]
use std::fs;
use std::path::{Path, PathBuf};
use omgbase_reconcile::Config;
use omgbase_store::{BatchItem, SequentialMinter, Store};
use omgbase_surface::planner::compile;
use omgbase_surface::{OqxResult, QueryOptions, SurfaceError, query, rewrite_query};
const CORPUS_DIR: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../packages/core/corpus/oqx/fixtures/alchemy"
);
const CORPUS_TS: &str = "2026-09-27T00:00:00.000Z";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Push {
Planned,
Declined,
}
use Push::{Declined, Planned};
const QUERIES: &[(&str, Push)] = &[
("from docs where _path == \"index.md\"", Planned),
("from docs where _path.startsWith(\"substances/\")", Planned),
(
"from docs where _path.startsWith(\"substances/\") && _path.endsWith(\"mercury.md\")",
Planned,
),
("from docs where _path != \"index.md\"", Planned),
("from blocks where _path.startsWith(\"lab/\")", Planned),
("from nodes where _path.startsWith(\"processes/\")", Planned),
("from links where _path.startsWith(\"index\")", Planned),
(
"from docs where _path.startsWith(\"substances/\") && \"substance\" in list(_tags)",
Declined,
),
(
"from docs where _path.startsWith(\"processes/\") && _nodes exists { where _kind == \"md:task\" }",
Planned,
),
("from docs where type == \"substance\"", Planned),
("from docs where layer == \"canon\"", Planned),
("from docs where era < 1000", Planned),
("from docs where era in 800..1680", Declined),
("from docs where era in 800...1680", Declined),
("from docs where era in 1600..", Declined),
("from docs where era in ..300", Declined),
(
"from docs where era in 1600..1700 && type == \"practitioner\"",
Planned,
),
("from docs where \"2026-01-15\" in range(window)", Declined),
("from docs where 2 in range(stage_range)", Declined),
(
"from docs where window == \"2026-01-01..2026-01-31\"",
Planned,
),
("from docs where stage_range == \"1..4\"", Planned),
("from docs where !verified", Declined),
(
"from docs where _nodes exists { where _kind == \"md:task\" && !_attrs.checked }",
Declined,
),
(
"from blocks where _type == \"task\" && _doc.type == \"lab-note\"",
Planned,
),
("from nodes where _kind == \"md:section\"", Planned),
("from docs where _format == \"markdown\"", Planned),
(
"from nodes where _kind == \"md:task\" && _attrs.checked == true",
Planned,
),
(
"from nodes where _kind == \"md:task\" && _attrs.checked == false",
Planned,
),
(
"from nodes where _kind == \"md:task\" && checked == true",
Planned,
),
(
"from nodes where _kind == \"md:task\" && checked == false",
Planned,
),
("from nodes where _kind == \"md:task\" && !checked", Planned),
(
"from blocks where _type == \"task\" && checked == false",
Planned,
),
(
"from nodes where _kind == \"md:section\" && level == 1",
Planned,
),
(
"from docs where _nodes exists { where _kind == \"md:task\" && !checked }",
Declined,
),
(
"select _path, checked from nodes where _kind == \"md:task\" && checked == false",
Planned,
),
(
"docs count { where _path.startsWith(\"substances/\") }",
Planned,
),
("docs exists { where _path == \"index.md\" }", Planned),
(
"docs first { where type == \"practitioner\" order by era desc }",
Planned,
),
(
"select _path, layer from docs where type == \"substance\"",
Planned,
),
(
"from docs where type == \"practitioner\" order by era asc",
Planned,
),
("from docs order by _path", Declined),
("select distinct type from docs", Declined),
(
"select k: _nodes collect distinct { select _kind } from docs where _path == \"processes/magnum-opus.md\"",
Planned,
),
(
"from docs where _nodes count distinct { select _kind } == 3",
Declined,
),
(
"select era values from docs where type == \"practitioner\" order by era asc",
Planned,
),
("select distinct type values from docs", Declined),
(
"docs first { select _path values where type == \"practitioner\" order by era desc }",
Planned,
),
(
"select tags: _tags collect { $it values where $it != \"substance\" } from docs where type == \"substance\"",
Planned,
),
(
"from docs where _tags exists { where $it == \"tria-prima\" }",
Declined,
),
(
"from docs where type == \"substance\" && _nodes none { where _kind == \"md:task\" }",
Planned,
),
("docs none { where type == \"nope\" }", Planned),
(
"from docs where type == \"practitioner\" order by era desc limit 2",
Planned,
),
(
"from docs where type == \"practitioner\" order by era desc limit 2 offset 1",
Planned,
),
("select distinct type values from docs limit 2", Declined),
(
"from docs where _nodes exists { where _kind == \"md:task\" offset 3 }",
Declined,
),
(
"docs first { select _path values where type == \"practitioner\" order by era asc offset 1 }",
Planned,
),
(
"select fm: entries(_frontmatter) collect { k: $key, v: $it } from docs where _path == \"substances/salt.md\"",
Planned,
),
(
"from docs where entries(_frontmatter) exists { where $key == \"era\" && $it > 1600 }",
Declined,
),
(
"select ks: entries(_inline) collect { $key values } from docs where entries(_inline) exists { }",
Declined,
),
(
"select _path from nodes where _kind == \"md:task\" && entries(_attrs) exists { where $key == \"checked\" && $it }",
Declined,
),
(
"select h: _nodes collect { select _name values where _kind == \"md:section\" order by first_ordinal limit 2 } from docs where _path == \"processes/magnum-opus.md\"",
Planned,
),
(
"from docs where _path == \"substances/philosophers-stone.md\" follow distinct _out_links collect { from _dst }",
Declined,
),
(
"select n: _name, d: $depth from nodes where _kind == \"md:section\" && _name == \"The magnum opus\" follow $it._children",
Declined,
),
(
"select slug from docs where type == \"substance\" && ^nodes exists { where _kind == \"md:wikilink\" && _value == ^slug }",
Declined,
),
(
"select _path, open from docs where _nodes collect { ^open: _value where _kind == \"md:task\" && !_attrs.checked }",
Declined,
),
(
"select _doc_id, _dst_path from links where _predicate == \"references\"",
Planned,
),
("from blocks where text(\"mercury\")", Declined),
(
"from blocks where type == \"heading\" && within(\"lab/*\")",
Declined,
),
(
"from docs where _path == \"index.md\" || layer == \"canon\"",
Declined,
),
(
"from docs where _path.lower().startsWith(\"SUBSTANCES/\".lower())",
Planned,
),
("from docs where _path.matches(\"^lab/\")", Declined),
("from docs where _hash != null", Planned),
("from docs where _updated_at >= \"2026-01-01\"", Planned),
(
"select _path, cited: ^docs collect { _path where _out_links exists { where _dst_path == ^^_path } } from docs",
Declined,
),
(
"select name, orders: ^docs collect { _path where type == \"order\" && customer == ^_path } from docs where type == \"customer\"",
Planned,
),
(
"from docs where _path == \"index.md\" follow _out_links collect { from _dst }, ^docs collect { where after.contains(^_path) }",
Declined,
),
(
"select _path, same_type: ^docs collect { _path where type == ^type && _path != ^_path } from docs",
Declined,
),
(
"select _path, same_era: ^docs collect { _path where era == ^era } from docs where type == \"practitioner\"",
Planned,
),
(
"select _path, same_layer: ^docs collect { _path values where layer == ^layer } from docs",
Declined,
),
(
"from docs where ^docs count { where layer == ^layer } > 3",
Declined,
),
(
"from docs where ^docs exists { where _title == ^_title && _id != ^_id }",
Declined,
),
(
"select _path, by_title: ^docs collect { _path values where _title == ^_title } from docs where type == \"process\"",
Planned,
),
(
"select _path, self: ^docs first { _path values where _path == ^_path } from docs",
Declined,
),
(
"select _path, by_id: ^docs single { _path values where _id == ^_id } from docs where type == \"substance\"",
Planned,
),
(
"select _path, s: ^docs collect { _path where slug == ^era } from docs where type == \"practitioner\"",
Planned,
),
(
"select _path, e: ^docs collect { _path where era == ^slug } from docs where type == \"substance\"",
Planned,
),
(
"from docs where ^docs none { where era == ^type }",
Declined,
),
(
"select _path, no_era: ^docs collect { _path values where era == ^nope } from docs where _path == \"index.md\"",
Planned,
),
(
"select _path, no_slug: ^docs collect { _path where slug == null } from docs where type == \"text\"",
Planned,
),
(
"select _path, l: ^docs collect { _path values where _tags == ^_tags } from docs",
Declined,
),
(
"select _path, l: ^docs collect { _path values where layer == ^_tags } from docs",
Declined,
),
(
"select _path, inbound: ^links collect { _doc_id where _dst_id == ^_id } from docs",
Declined,
),
(
"select _path, outbound: ^links collect { _dst_path values where _path == ^_path } from docs",
Declined,
),
(
"from docs where ^links count { where _path == ^_path } > 2",
Declined,
),
(
"select _doc_id, same_dst: ^links collect { _doc_id values where _dst_path == ^_dst_path && _id != ^_id } from links where _predicate == \"references\"",
Planned,
),
(
"select _id, paragraphs: ^blocks collect { _ordinal values where _doc_id == ^_doc_id && _type == \"paragraph\" } from blocks where _type == \"heading\"",
Planned,
),
(
"from blocks where _type == \"heading\" && ^blocks count { where _doc_id == ^_doc_id && _type == \"paragraph\" } > 3",
Declined,
),
(
"from nodes where _kind == \"md:section\" && ^nodes count { where _kind == ^_kind } > 10",
Declined,
),
(
"select _name, same_kind: ^nodes collect { _name values where _kind == ^_kind && _doc_id == ^_doc_id } from nodes where _kind == \"md:section\"",
Planned,
),
(
"select _path, by_name: ^nodes collect { _id where _name == ^_name } from nodes where _kind == \"md:section\" && _name == \"Preparation\"",
Planned,
),
(
"select _path, nameless: ^nodes collect { _kind values where _name == ^nope } from nodes where _kind == \"md:section\" && _name == \"Preparation\"",
Planned,
),
];
fn walk(dir: &Path, base: &Path, out: &mut Vec<(String, String)>) {
for entry in fs::read_dir(dir).expect("corpus dir") {
let path = entry.expect("entry").path();
if path.is_dir() {
walk(&path, base, out);
} else if path.extension().is_some_and(|e| e == "md") {
let rel = path
.strip_prefix(base)
.expect("under base")
.components()
.map(|c| c.as_os_str().to_string_lossy().into_owned())
.collect::<Vec<_>>()
.join("/");
out.push((rel, fs::read_to_string(&path).expect("readable")));
}
}
}
fn corpus() -> Option<Vec<(String, String)>> {
let dir = PathBuf::from(CORPUS_DIR);
if !dir.is_dir() {
eprintln!("conformance: corpus not present at {CORPUS_DIR}; skipping");
return None;
}
let mut files = Vec::new();
walk(&dir, &dir, &mut files);
files.sort_by(|a, b| a.0.as_bytes().cmp(b.0.as_bytes()));
assert!(
files.len() >= 10,
"alchemy corpus has {} files",
files.len()
);
Some(files)
}
fn alchemy_store() -> Option<(Store, String)> {
let files = corpus()?;
let mut store =
Store::open_in_memory_with_minter(Box::new(SequentialMinter::new())).expect("store");
let repo = store.create_repo("alchemy").expect("repo");
let items: Vec<BatchItem> = files
.iter()
.map(|(p, s)| BatchItem::observed(p, s))
.collect();
store
.observe_batch(&repo, &items, CORPUS_TS, &Config::default())
.expect("corpus observed");
Some((store, repo))
}
fn run(
store: &Store,
repo: &str,
q: &str,
limit: usize,
cursor: Option<&str>,
in_memory: bool,
) -> Result<OqxResult, SurfaceError> {
query(
store,
repo,
q,
QueryOptions {
limit: Some(limit),
cursor,
provider: None,
in_memory,
bare: false,
unknown_names: None,
vocabulary: None,
now_ms: None,
request: None,
},
)
}
fn describe(r: &Result<OqxResult, SurfaceError>) -> String {
match r {
Ok(res) => res.to_json().to_string(),
Err(e) => format!("error {} {:?}", e.code, e.message),
}
}
#[test]
fn planned_equals_in_memory_over_the_alchemy_corpus() {
let Some((store, repo)) = alchemy_store() else {
return;
};
let mut failures = Vec::new();
for (q, push) in QUERIES {
let planned = run(&store, &repo, q, 100, None, false);
let memory = run(&store, &repo, q, 100, None, true);
let agree = match (&planned, &memory) {
(Ok(p), Ok(m)) => p == m,
(Err(p), Err(m)) => p.code == m.code && p.message == m.message,
_ => false,
};
if !agree {
failures.push(format!(
"{q}\n planned: {}\n in-memory: {}",
describe(&planned),
describe(&memory)
));
}
let parsed = rewrite_query(&oqx::parse_string(q).expect("parses"), 0);
let compiled = compile(&parsed, &[], &repo);
let verdict = if compiled.is_some() {
Planned
} else {
Declined
};
if verdict != *push {
failures.push(format!(
"{q}\n expected {push:?}, planner said {verdict:?}{}",
compiled.map_or(String::new(), |c| format!(": {}", c.sql))
));
}
}
assert!(
failures.is_empty(),
"{} conformance failure(s):\n{}",
failures.len(),
failures.join("\n")
);
}
#[test]
fn planned_queries_actually_reduce_the_scan() {
let Some((store, repo)) = alchemy_store() else {
return;
};
let parsed = rewrite_query(
&oqx::parse_string("from docs where _path == \"index.md\" && layer == \"canon\"").unwrap(),
0,
);
let planner = omgbase_surface::SqlitePlanner::new(store.conn(), &repo);
let plan = planner
.try_plan(&parsed, &[])
.expect("statement runs")
.expect("planned");
assert_eq!(plan.rows.len(), 1, "{:?}", plan.rows);
assert_eq!(plan.residual.r#where, None, "both conjuncts were pushed");
let all = run(&store, &repo, "from docs", 100, None, true).unwrap();
assert!(all.hits.len() > 1);
let res = run(
&store,
&repo,
"select _title, n: _nodes collect { select _kind } from docs where _path == \"index.md\"",
100,
None,
false,
)
.unwrap();
assert_eq!(res.hits.len(), 1);
assert!(res.hits[0]["_title"].is_string(), "{}", res.hits[0]);
assert!(
res.hits[0]["n"].as_array().is_some_and(|a| !a.is_empty()),
"{}",
res.hits[0]
);
}
#[test]
fn agrees_across_a_paginated_sweep() {
let Some((store, repo)) = alchemy_store() else {
return;
};
let (mut cur_p, mut cur_m): (Option<String>, Option<String>) = (None, None);
for _ in 0..6 {
let p = run(&store, &repo, "from docs", 5, cur_p.as_deref(), false).unwrap();
let m = run(&store, &repo, "from docs", 5, cur_m.as_deref(), true).unwrap();
assert_eq!(p, m);
cur_p = p.cursor.clone();
cur_m = m.cursor.clone();
if cur_p.is_none() {
break;
}
}
let (mut cur_p, mut cur_m): (Option<String>, Option<String>) = (None, None);
for _ in 0..6 {
let q = "from docs where _path != \"index.md\"";
let p = run(&store, &repo, q, 5, cur_p.as_deref(), false).unwrap();
let m = run(&store, &repo, q, 5, cur_m.as_deref(), true).unwrap();
assert_eq!(p, m);
cur_p = p.cursor.clone();
cur_m = m.cursor.clone();
if cur_p.is_none() {
break;
}
}
}