use std::collections::HashMap;
use omgbase_search::{EmbeddingProvider, f32_to_blob};
use omgbase_store::Store;
use oqx::ast::{BinaryOp, Expr, Query, SelectItem};
use oqx::walk::{Clause, Node, VisitContext, Visitor, transform, visit};
use oqx::{Consumer, Engine, InMemoryEngine, Value, build, resolve_aliases};
use serde_json::{Map, Value as Json};
use crate::context::{SemanticVec, StoreContext, Target, render_row_values, target_of};
use crate::cursor::{decode_path_cursor, encode_cursor};
use crate::error::{Result, SurfaceError};
use crate::paths::reference_path;
use crate::planner::{SqlitePlanner, root_target};
pub const DEFAULT_LIMIT: usize = 50;
const ROW_FNS: [&str; 12] = [
"text",
"semantic",
"under",
"under_heading",
"within",
"under_kind",
"yaml_path",
"json_pointer",
"has_edge",
"has_anchor",
"child_count",
"parent_type",
];
const ID_KEY: &str = "__oqx_id";
const PATH_KEY: &str = "__oqx_path";
const SELF_KEY: &str = "__oqx_self";
const VALUE_KEY: &str = "__oqx_value";
#[derive(Clone, Copy, Default)]
pub struct QueryOptions<'a> {
pub limit: Option<usize>,
pub cursor: Option<&'a str>,
pub provider: Option<&'a dyn EmbeddingProvider>,
pub in_memory: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub struct OqxResult {
pub hits: Vec<Json>,
pub truncated: bool,
pub cursor: Option<String>,
pub consumer: Consumer,
pub count: Option<f64>,
pub exists: Option<bool>,
pub none: Option<bool>,
pub values: Option<Vec<Json>>,
}
impl OqxResult {
fn scalar(consumer: Consumer) -> Self {
Self {
hits: Vec::new(),
truncated: false,
cursor: None,
consumer,
count: None,
exists: None,
none: None,
values: None,
}
}
#[must_use]
pub fn to_json(&self) -> Json {
let mut m = Map::new();
m.insert("hits".to_owned(), Json::Array(self.hits.clone()));
m.insert("truncated".to_owned(), Json::Bool(self.truncated));
m.insert(
"cursor".to_owned(),
self.cursor.clone().map_or(Json::Null, Json::String),
);
m.insert(
"consumer".to_owned(),
Json::String(self.consumer.as_str().to_owned()),
);
if let Some(n) = self.count {
m.insert("count".to_owned(), Value::Number(n).to_canonical_json());
}
if let Some(b) = self.exists {
m.insert("exists".to_owned(), Json::Bool(b));
}
if let Some(b) = self.none {
m.insert("none".to_owned(), Json::Bool(b));
}
if let Some(v) = &self.values {
m.insert("values".to_owned(), Json::Array(v.clone()));
}
Json::Object(m)
}
}
const PATH_INTRINSICS: [&str; 2] = ["$path", "$dst_path"];
fn is_path_read(e: &Expr) -> bool {
match e {
Expr::Ident { name, .. } | Expr::Outer { name, .. } | Expr::Member { name, .. } => {
PATH_INTRINSICS.contains(&name.as_str())
}
_ => false,
}
}
fn root_literal(e: Expr) -> Expr {
match e {
Expr::Lit {
value: Value::Str(s),
span,
} => Expr::Lit {
value: Value::Str(reference_path(&s)),
span,
},
other => other,
}
}
#[must_use]
pub fn root_path_literals(e: Expr) -> Expr {
match e {
Expr::Binary {
op: op @ (BinaryOp::Eq | BinaryOp::Ne),
left,
right,
span,
} => {
if is_path_read(&left) {
Expr::Binary {
op,
left,
right: Box::new(root_literal(*right)),
span,
}
} else if is_path_read(&right) {
Expr::Binary {
op,
left: Box::new(root_literal(*left)),
right,
span,
}
} else {
Expr::Binary {
op,
left,
right,
span,
}
}
}
Expr::Call {
recv: Some(recv),
name,
mut args,
span,
} if name == "startsWith" && args.len() == 1 && is_path_read(&recv) => {
let arg = root_literal(args.remove(0));
Expr::Call {
recv: Some(recv),
name,
args: vec![arg],
span,
}
}
other => other,
}
}
#[must_use]
pub fn rewrite_query(q: &Query) -> Query {
transform(q, &mut |e, _| match e {
Expr::Call {
recv: None,
name,
args,
span,
} if ROW_FNS.contains(&name.as_str()) => Expr::Call {
recv: Some(Box::new(build::ident("$self"))),
name,
args,
span,
},
other => root_path_literals(other),
})
}
pub const REPO_REMOVED_MESSAGE: &str = "`$repo` was removed in surface 2.0 — reach the repository's collections through the root row: `^docs` from a top-level row (one caret per enclosing block, or the absolute `0^docs`), and the repository id as `^$id` / `0^$id`";
#[must_use]
pub fn bare_target_message(t: Target, depth: Option<usize>) -> String {
let name = t.as_str();
let noun = match t {
Target::Docs => "documents",
Target::Blocks => "blocks",
Target::Nodes => "nodes",
Target::Edges => "edges",
};
let carets = "^".repeat(depth.unwrap_or(1));
let how = if depth.is_none() {
format!("; one caret per enclosing block, or the absolute `0^{name}`")
} else {
String::new()
};
format!(
"`{name}` inside a block reads a property of the current row, which has none — did you mean `{carets}{name}` (the repository's {noun}{how})?"
)
}
#[must_use]
pub fn past_root_message(t: Target, levels: usize) -> String {
let name = t.as_str();
let noun = match t {
Target::Docs => "documents",
Target::Blocks => "blocks",
Target::Nodes => "nodes",
Target::Edges => "edges",
};
let carets = "^".repeat(levels);
format!(
"`{carets}{name}` reaches past the root — there is no enclosing row at this depth; at the top level the repository's {noun} are the bare `{name}` (`{name} count {{ … }}`, `from {name}`, `entries({name})`)"
)
}
pub fn check_root_spellings(q: &Query) -> Result<()> {
struct Check(Option<SurfaceError>);
impl Visitor for Check {
fn enter(&mut self, node: Node<'_>, ctx: &VisitContext<'_>) -> bool {
if self.0.is_some() {
return false;
}
match node {
Node::Expr(Expr::Ident { name, .. } | Expr::Outer { name, .. })
if name == "$repo" =>
{
self.0 = Some(SurfaceError::filter_invalid(REPO_REMOVED_MESSAGE, "OQX"));
}
Node::Expr(Expr::Ident { name, .. })
if ctx.depth >= 1 && matches!(name.as_str(), "docs" | "edges") =>
{
let t = Target::parse(name).expect("a target name");
self.0 = Some(SurfaceError::filter_invalid(
bare_target_message(t, Some(ctx.depth)),
"OQX",
));
}
Node::Expr(Expr::Outer { name, levels, .. }) if *levels > ctx.depth => {
if let Some(t) = Target::parse(name) {
self.0 = Some(SurfaceError::filter_invalid(
past_root_message(t, *levels),
"OQX",
));
}
}
_ => {}
}
self.0.is_none()
}
}
let mut check = Check(None);
visit(Node::Query(q), &mut check);
check.0.map_or(Ok(()), Err)
}
struct Phrases(Vec<String>);
impl Visitor for Phrases {
fn enter(&mut self, node: Node<'_>, _ctx: &VisitContext<'_>) -> bool {
if let Node::Expr(Expr::Call {
recv: None,
name,
args,
..
}) = node
&& name == "semantic"
&& let Some(Expr::Lit {
value: Value::Str(s),
..
}) = args.first()
&& !self.0.contains(s)
{
self.0.push(s.clone());
}
true
}
}
#[must_use]
pub fn collect_semantic_phrases(source: &str) -> Vec<String> {
let Ok(q) = oqx::parse_string(source) else {
return Vec::new();
};
let mut phrases = Phrases(Vec::new());
visit(Node::Query(&q), &mut phrases);
phrases.0
}
fn mentions_outside_source(q: &Query, name: &str) -> bool {
struct Mentions<'n> {
name: &'n str,
found: bool,
}
impl Visitor for Mentions<'_> {
fn enter(&mut self, node: Node<'_>, ctx: &VisitContext<'_>) -> bool {
if self.found {
return false;
}
if ctx.clause == Some(Clause::Source) && ctx.path.len() == 1 {
return false;
}
if let Node::Expr(Expr::Ident { name, .. } | Expr::Outer { name, .. }) = node
&& name == self.name
{
self.found = true;
}
!self.found
}
}
let mut m = Mentions { name, found: false };
visit(Node::Query(q), &mut m);
m.found
}
fn may_reach_non_rows(q: &Query) -> bool {
q.follow.is_some() || !q.from.is_empty() || root_target(&q.source).is_none()
}
fn describe_value(v: &Value) -> String {
match v {
Value::Undefined | Value::Null => "an absent value".to_owned(),
Value::Str(s) => format!(
"a string ({})",
serde_json::to_string(s).unwrap_or_default()
),
Value::Number(_) => format!("a number ({v})"),
Value::Bool(b) => format!("a boolean ({b})"),
Value::Array(_) => "an array".to_owned(),
Value::Range(_) => "a range".to_owned(),
Value::Object(_) => "an object".to_owned(),
}
}
fn not_a_store_row(v: &Value) -> SurfaceError {
SurfaceError::filter_invalid(
format!(
"a hit must be a document, block, node or edge row — the query reached {}; to follow document references held in a property use refs(<field>)",
describe_value(v)
),
"OQX",
)
}
fn to_hit(row: Value) -> Result<Value> {
let Value::Object(mut o) = row else {
return Ok(Value::Object(oqx::Object::new()));
};
if let Some(me) = o.remove(SELF_KEY) {
if target_of(&me).is_none() {
return Err(not_a_store_row(&me));
}
}
let Value::Object(o) = render_row_values(Value::Object(o)) else {
return Ok(Value::Object(oqx::Object::new()));
};
let mut id = Value::Undefined;
let mut path = Value::Undefined;
let mut rest = Vec::new();
for (k, v) in o {
match k.as_str() {
ID_KEY => id = v,
PATH_KEY => path = v,
_ => rest.push((k, v)),
}
}
let mut hit = oqx::Object::with_capacity(rest.len() + 2);
hit.insert("id", Value::Str(id.to_string()));
hit.insert(
"path",
Value::Str(if path.is_absent() {
String::new()
} else {
path.to_string()
}),
);
for (k, v) in rest {
hit.insert(k, v);
}
Ok(Value::Object(hit))
}
fn hit_str(hit: &Value, key: &str) -> String {
hit.as_object()
.and_then(|o| o.get(key))
.map(|v| v.to_string())
.unwrap_or_default()
}
fn dedup_hits_by_projection(hits: Vec<Value>) -> Vec<Value> {
let mut seen: Vec<String> = Vec::new();
let mut out = Vec::new();
for h in hits {
let mut pairs: Vec<(String, Value)> = h
.as_object()
.map(|o| {
o.iter()
.filter(|(k, _)| *k != "id" && *k != "path")
.map(|(k, v)| (k.to_owned(), v.clone()))
.collect()
})
.unwrap_or_default();
pairs.sort_by(|a, b| a.0.cmp(&b.0));
let key = Value::Array(
pairs
.into_iter()
.map(|(k, v)| Value::Array(vec![Value::Str(k), v]))
.collect(),
)
.to_canonical_json()
.to_string();
if seen.contains(&key) {
continue;
}
seen.push(key);
out.push(h);
}
out
}
fn const_bound(e: Option<&Expr>, word: &str) -> Result<Option<usize>> {
match e {
None => Ok(None),
Some(Expr::Lit {
value: Value::Number(n),
..
}) if n.fract() == 0.0 && *n >= 0.0 && n.is_finite() => Ok(Some(*n as usize)),
Some(_) => Err(SurfaceError::filter_invalid(
format!("top-level {word} must be a non-negative integer literal"),
"OQX",
)),
}
}
fn value_of(hit: &Value) -> Value {
hit.as_object()
.and_then(|o| o.get(VALUE_KEY))
.cloned()
.unwrap_or(Value::Undefined)
}
fn without_value_key(hit: Value) -> Json {
hit.to_canonical_json()
}
pub fn query(
store: &Store,
repo_id: &str,
source: &str,
opts: QueryOptions<'_>,
) -> Result<OqxResult> {
let phrases = collect_semantic_phrases(source);
let mut semantic: HashMap<String, SemanticVec> = HashMap::new();
if let Some(provider) = opts.provider.filter(|_| !phrases.is_empty()) {
for phrase in phrases {
let vec = provider
.embed_query(&phrase)
.map_err(|e| SurfaceError::new(e.code(), e.to_string()))?;
semantic.insert(
phrase,
SemanticVec {
model: provider.model().to_owned(),
vec: f32_to_blob(&vec),
},
);
}
}
let runner = Runner {
store,
repo_id,
semantic,
planned: !opts.in_memory,
};
run_inner(&runner, source, opts)
}
struct Runner<'a> {
store: &'a Store,
repo_id: &'a str,
semantic: HashMap<String, SemanticVec>,
planned: bool,
}
impl Runner<'_> {
fn run(&self, q: &Query) -> Result<oqx::OqxResult> {
let conn = self.store.conn();
let ctx = StoreContext::new(conn, self.repo_id, self.semantic.clone());
let plan = if self.planned {
SqlitePlanner::new(conn, self.repo_id)
.try_plan(q, &[])
.map_err(|e| SurfaceError::other(format!("sqlite: {e}")))?
} else {
None
};
let (ctx, residual) = match plan {
Some(plan) => {
let once = !mentions_outside_source(&plan.residual, oqx::ROWS_ROOT);
let ctx = if once {
ctx.with_rows_root_once(plan.rows)
} else {
ctx.with_rows_root(plan.rows)
};
(ctx, Some(plan.residual))
}
None => (ctx, None),
};
let engine = InMemoryEngine::new(ctx);
let out = engine.run(residual.as_ref().unwrap_or(q), &[]);
if let Some(failed) = engine.context().take_root_failure() {
return Err(failed.into());
}
Ok(out?)
}
}
fn run_inner(engine: &Runner<'_>, source: &str, opts: QueryOptions<'_>) -> Result<OqxResult> {
let raw = oqx::parse_string(source)?;
check_root_spellings(&raw)?;
let parsed = rewrite_query(&resolve_aliases(&raw)?);
let consumer = parsed.consumer;
match consumer {
Consumer::Exists => {
let res = engine.run(&parsed)?;
let mut r = OqxResult::scalar(consumer);
r.exists = Some(matches!(res, oqx::OqxResult::Exists(true)));
return Ok(r);
}
Consumer::Count => {
let res = engine.run(&parsed)?;
let mut r = OqxResult::scalar(consumer);
r.count = Some(match res {
oqx::OqxResult::Count(n) => n,
_ => 0.0,
});
return Ok(r);
}
Consumer::None => {
let res = engine.run(&parsed)?;
let mut r = OqxResult::scalar(consumer);
r.none = Some(match res {
oqx::OqxResult::None(b) => b,
_ => true,
});
return Ok(r);
}
Consumer::Collect | Consumer::First | Consumer::Single => {}
}
let top_distinct = parsed.distinct;
let top_values = parsed.values;
let user_select: Vec<SelectItem> = if top_values {
parsed
.select
.first()
.map(|it| match it {
SelectItem::Field {
expr, lift, span, ..
} => SelectItem::Field {
name: VALUE_KEY.to_owned(),
expr: expr.clone(),
lift: *lift,
span: *span,
},
SelectItem::Collect { op, span, .. } => SelectItem::Collect {
name: VALUE_KEY.to_owned(),
op: op.clone(),
span: *span,
},
})
.into_iter()
.collect()
} else {
parsed.select.clone()
};
let id_item = build::field(ID_KEY, build::ident("$id"));
let path_item = build::field(PATH_KEY, build::ident("$path"));
let mut select = vec![id_item, path_item];
if !top_values && may_reach_non_rows(&parsed) {
select.push(build::field(SELF_KEY, build::ident("$it")));
}
select.extend(user_select);
let (top_limit, top_offset) = (parsed.limit.clone(), parsed.offset.clone());
let q = Query {
distinct: false,
values: false,
select,
limit: if consumer == Consumer::Collect {
None
} else {
parsed.limit.clone()
},
offset: if consumer == Consumer::Collect {
None
} else {
parsed.offset.clone()
},
..parsed.clone()
};
let res = engine.run(&q)?;
if matches!(consumer, Consumer::First | Consumer::Single) {
let row = match res {
oqx::OqxResult::First(r) | oqx::OqxResult::Single(r) => r,
_ => None,
};
let mut out = OqxResult::scalar(consumer);
match row {
None => {
if top_values {
out.values = Some(Vec::new());
}
}
Some(r) => {
let hit = to_hit(r)?;
if top_values {
out.values = Some(vec![value_of(&hit).to_canonical_json()]);
} else {
out.hits = vec![without_value_key(hit)];
}
}
}
return Ok(out);
}
let mut rows: Vec<Value> = match res {
oqx::OqxResult::Collect(rows) => rows,
_ => Vec::new(),
};
let custom = parsed.order_by.as_ref().is_some_and(|o| !o.is_empty());
let cursor = opts.cursor.filter(|c| !c.is_empty() && !custom);
let eager = top_distinct || cursor.is_some();
if eager {
rows = rows.into_iter().map(to_hit).collect::<Result<_>>()?;
}
if top_distinct {
rows = dedup_hits_by_projection(rows);
}
let offset = const_bound(top_offset.as_ref(), "offset")?.unwrap_or(0);
let limit = const_bound(top_limit.as_ref(), "limit")?;
if offset > 0 || limit.is_some() {
let end = limit.map_or(rows.len(), |l| (offset + l).min(rows.len()));
if offset >= rows.len() {
rows.clear();
} else {
rows.truncate(end);
rows.drain(..offset);
}
}
let cap = opts.limit.unwrap_or(DEFAULT_LIMIT);
let mut page = rows;
if let Some(cursor) = cursor {
let parts = decode_path_cursor(cursor, "query", 2)?;
let (path, id) = (&parts[0], &parts[1]);
page.retain(|h| {
let hp = hit_str(h, "path");
let hi = hit_str(h, "id");
hp > *path || (hp == *path && hi > *id)
});
}
let truncated = page.len() > cap;
page.truncate(cap);
if !eager {
page = page.into_iter().map(to_hit).collect::<Result<_>>()?;
}
let cursor = if truncated && !custom {
page.last()
.map(|last| encode_cursor(&[&hit_str(last, "path"), &hit_str(last, "id")]))
} else {
None
};
let mut out = OqxResult::scalar(Consumer::Collect);
out.truncated = truncated;
out.cursor = cursor;
if top_values {
out.values = Some(
page.iter()
.map(|h| value_of(h).to_canonical_json())
.collect(),
);
} else {
out.hits = page.into_iter().map(without_value_key).collect();
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
use oqx::ast::Where;
#[test]
fn row_functions_become_self_methods() {
let q = oqx::parse_string(
"from blocks where text(\"x\") && under_heading(\"h\") && size(attrs) > 0 && doc.$path.startsWith(\"a\")",
)
.unwrap();
let r = rewrite_query(&q);
let Some(Where::And { parts, .. }) = &r.r#where else {
panic!("and")
};
let Where::Scalar { expr, .. } = &parts[0] else {
panic!("scalar")
};
assert!(
matches!(expr, Expr::Call { recv: Some(r), name, .. } if name == "text" && **r == build::ident("$self"))
);
let Where::Scalar { expr, .. } = &parts[2] else {
panic!("scalar")
};
assert!(
matches!(expr, Expr::Binary { left, .. } if matches!(&**left, Expr::Call { recv: None, name, .. } if name == "size"))
);
}
#[test]
fn path_literals_are_rooted_only_against_path_reads() {
let q = oqx::parse_string(
"select n: ^docs collect { $path where $path == ^after || \"x.md\" != $dst_path } from docs where $path.startsWith(\"lab/\") && doc.$path == \"a.md\" && type == \"a.md\" && ^$path == \"b.md\"",
)
.unwrap();
let printed = oqx::print_query(&rewrite_query(&q)).unwrap();
assert!(printed.contains("$path.startsWith(\"/lab/\")"), "{printed}");
assert!(printed.contains("doc.$path == \"/a.md\""), "{printed}");
assert!(printed.contains("type == \"a.md\""), "{printed}");
assert!(printed.contains("^$path == \"/b.md\""), "{printed}");
assert!(printed.contains("\"/x.md\" != $dst_path"), "{printed}");
assert!(printed.contains("$path == ^after"), "{printed}");
let q = oqx::parse_string(
"from docs where $path.contains(\"a/\") && $path.endsWith(\".md\") && $path == \"\"",
)
.unwrap();
let printed = oqx::print_query(&rewrite_query(&q)).unwrap();
assert!(printed.contains("$path.contains(\"a/\")"), "{printed}");
assert!(printed.contains("$path.endsWith(\".md\")"), "{printed}");
assert!(printed.contains("$path == \"/\""), "{printed}");
}
#[test]
fn repo_and_bare_targets_are_refused_statically() {
let parse = |s: &str| oqx::parse_string(s).unwrap();
for q in [
"from docs where $repo",
"select r: size($repo.docs) from docs",
"select r: $repo.$id from docs",
"select r: nodes first { select x: ^$repo.$id values } from docs",
"select r: nodes first { select x: 0^$repo values } from docs",
"$repo.docs count { }",
] {
let e = check_root_spellings(&parse(q)).unwrap_err();
assert_eq!(e.code, "filter_invalid", "{q}");
assert_eq!(e.message, REPO_REMOVED_MESSAGE, "{q}");
}
let e = check_root_spellings(&parse("select x: docs collect { } from docs")).unwrap_err();
assert_eq!(e.message, bare_target_message(Target::Docs, Some(1)));
assert!(
e.message
.contains("did you mean `^docs` (the repository's documents)?")
);
let e = check_root_spellings(&parse(
"select x: nodes collect { select y: size(edges) } from docs",
))
.unwrap_err();
assert!(
e.message
.contains("did you mean `^^edges` (the repository's edges)?")
);
let e = check_root_spellings(&parse("docs collect { from docs }")).unwrap_err();
assert!(e.message.contains("`^docs`"));
for q in [
"docs count { }",
"from docs where nodes exists { }",
"entries(docs) first { }",
"select n: size(^docs), id: ^$id, k: size(0^edges) from docs",
"from blocks where blocks exists { }", ] {
check_root_spellings(&parse(q)).unwrap_or_else(|e| panic!("{q}: {e}"));
}
let e = check_root_spellings(&parse("^docs count { }")).unwrap_err();
assert_eq!(e.message, past_root_message(Target::Docs, 1));
assert_eq!(
e.message,
"`^docs` reaches past the root — there is no enclosing row at this depth; at the top level the repository's documents are the bare `docs` (`docs count { … }`, `from docs`, `entries(docs)`)"
);
let e = check_root_spellings(&parse("select $path from docs where ^^edges exists { }"))
.unwrap_err();
assert!(
e.message.contains("`^^edges` reaches past the root"),
"{}",
e.message
);
let m = bare_target_message(Target::Blocks, None);
assert!(m.contains("did you mean `^blocks` (the repository's blocks; one caret per enclosing block, or the absolute `0^blocks`)?"), "{m}");
}
#[test]
fn semantic_phrases_are_collected_distinct() {
let phrases = collect_semantic_phrases(
"select s: semantic(\"alpha\") from docs where semantic(\"alpha\") > 0.5 || nodes exists { where semantic(\"beta\") > 0 } order by semantic(\"gamma\") desc",
);
assert_eq!(phrases, ["alpha", "beta", "gamma"]);
assert!(collect_semantic_phrases("not a query {{").is_empty());
assert!(collect_semantic_phrases("from docs").is_empty());
}
#[test]
fn hits_peel_the_injected_columns() {
let mut o = oqx::Object::new();
o.insert(ID_KEY, Value::Str("d_1".into()));
o.insert(PATH_KEY, Value::Null);
o.insert("layer", Value::Str("canon".into()));
let hit = to_hit(Value::Object(o)).unwrap();
let ho = hit.as_object().unwrap();
assert_eq!(ho.keys().collect::<Vec<_>>(), ["id", "path", "layer"]);
assert_eq!(ho.get("path"), Some(&Value::Str(String::new())));
let mut o = oqx::Object::new();
o.insert(ID_KEY, Value::Str("d_1".into()));
o.insert(PATH_KEY, Value::Str("a.md".into()));
o.insert("id", Value::Number(7.0));
let hit = to_hit(Value::Object(o)).unwrap();
let ho = hit.as_object().unwrap();
assert_eq!(ho.keys().collect::<Vec<_>>(), ["id", "path"]);
assert_eq!(ho.get("id"), Some(&Value::Number(7.0)));
}
#[test]
fn a_hit_that_is_not_a_store_row_fails_the_query() {
let mut o = oqx::Object::new();
o.insert(ID_KEY, Value::Undefined);
o.insert(PATH_KEY, Value::Undefined);
o.insert(SELF_KEY, Value::Str("/timeline/kickoff.md".into()));
let e = to_hit(Value::Object(o)).unwrap_err();
assert_eq!(e.code, "filter_invalid");
assert_eq!(
e.message,
"a hit must be a document, block, node or edge row — the query reached a string (\"/timeline/kickoff.md\"); to follow document references held in a property use refs(<field>)"
);
let mut row = oqx::Object::new();
row.insert("doc_id", Value::Str("d_1".into()));
row.insert("path", Value::Str("a.md".into()));
let row = crate::context::tag_row(row, crate::context::Target::Docs);
let mut o = oqx::Object::new();
o.insert(ID_KEY, Value::Str("d_1".into()));
o.insert(PATH_KEY, Value::Str("a.md".into()));
o.insert(SELF_KEY, row);
let hit = to_hit(Value::Object(o)).unwrap();
assert_eq!(
hit.as_object().unwrap().keys().collect::<Vec<_>>(),
["id", "path"]
);
let parse = |s: &str| oqx::parse_string(s).unwrap();
assert!(!may_reach_non_rows(&parse(
"from docs where layer == \"canon\""
)));
assert!(may_reach_non_rows(&parse("from docs follow before")));
assert!(may_reach_non_rows(&parse("refs(\"/index.md\") first { }")));
}
#[test]
fn distinct_dedups_by_user_projection_first_wins() {
let mk = |id: &str, t: &str| {
let mut o = oqx::Object::new();
o.insert("id", Value::Str(id.into()));
o.insert("path", Value::Str("p".into()));
o.insert("type", Value::Str(t.into()));
Value::Object(o)
};
let out = dedup_hits_by_projection(vec![mk("1", "a"), mk("2", "b"), mk("3", "a")]);
assert_eq!(out.len(), 2);
assert_eq!(hit_str(&out[0], "id"), "1");
assert_eq!(hit_str(&out[1], "id"), "2");
}
#[test]
fn top_level_bounds_must_be_literals() {
assert_eq!(const_bound(None, "limit").unwrap(), None);
assert_eq!(
const_bound(
Some(&Expr::Lit {
value: Value::Number(3.0),
span: oqx::Span::EMPTY
}),
"limit"
)
.unwrap(),
Some(3)
);
let e = const_bound(
Some(&Expr::Lit {
value: Value::Number(-1.0),
span: oqx::Span::EMPTY,
}),
"offset",
)
.unwrap_err();
assert_eq!(e.code, "filter_invalid");
assert!(e.message.contains("top-level offset"));
}
}