Skip to main content

omgbase_surface/
query.rs

1//! The runner (`spec/surface/README.md` §1.4): parse the source, rewrite the
2//! row functions to `$self` methods, run it through the engine over the
3//! store context, and shape the engine's result into the surface's
4//! `OqxResult` (lean `{ id, path, … }` hits, keyset paging, the consumer
5//! scalars). Port of `packages/core/src/oqx-js/run.ts`.
6//!
7//! By default the tier-3 planner ([`crate::planner`]) pre-filters the scan in
8//! SQL and the in-memory engine finishes the residual over the produced rows;
9//! [`QueryOptions::in_memory`] forces the pure in-memory engine (the
10//! differential gate runs every case both ways — a planner must be invisible).
11
12use std::collections::HashMap;
13
14use omgbase_search::{EmbeddingProvider, f32_to_blob};
15use omgbase_store::Store;
16use oqx::ast::{BinaryOp, Expr, Query, SelectItem};
17use oqx::walk::{Clause, Node, VisitContext, Visitor, transform, visit};
18use oqx::{Consumer, Engine, InMemoryEngine, Value, build, resolve_aliases};
19use serde_json::{Map, Value as Json};
20
21use crate::context::{SemanticVec, StoreContext, Target, render_row_values, target_of};
22use crate::cursor::{decode_path_cursor, encode_cursor};
23use crate::dates;
24use crate::error::{Result, SurfaceError};
25use crate::names::{UnknownNames, VocabularyCache, check_names};
26use crate::paths::reference_path;
27use crate::planner::{SqlitePlanner, root_target};
28
29/// The default page size.
30pub const DEFAULT_LIMIT: usize = 50;
31
32/// Row-scoped domain functions: authored as free calls that implicitly
33/// reference the current row; rewritten to `$self.fn(…)`. `refs(x)` is not
34/// one: it reads no row, so it stays a free function of the context. The
35/// planner treats every call alike (declined by the translator, flagged by
36/// `expr_may_raise`), `has_edge` and `refs` included.
37const ROW_FNS: [&str; 12] = [
38    "text",
39    "semantic",
40    "under",
41    "under_heading",
42    "within",
43    "under_kind",
44    "yaml_path",
45    "json_pointer",
46    "has_edge",
47    "has_anchor",
48    "child_count",
49    "parent_type",
50];
51
52const ID_KEY: &str = "__oqx_id";
53const PATH_KEY: &str = "__oqx_path";
54/// The hit envelope's keys (§1.4, 3.0): the row's identity under the same
55/// bare names the expression language reads, so a key copied from a hit is
56/// valid in the next `where` with no translation; `select id, path` is simply
57/// the envelope and any other projection whose output key would be one of
58/// these must be aliased ([`envelope_collision`]).
59pub(crate) const HIT_ID: &str = "id";
60pub(crate) const HIT_PATH: &str = "path";
61/// A hit is a store row (§1.4, 1.5). A bare root scan's rows are store rows by
62/// construction; a `follow` destination (`follow before` over a frontmatter
63/// list of paths), a `from E` re-projection or any other source can reach a
64/// scalar, which the injected `id`/`path` reads would render as the junk hit
65/// `{ id: "undefined", path: "" }`. For those queries the row itself is
66/// projected too (`$it`) under this key and [`to_hit`] fails the query when it
67/// is not a row. Only then: the clone of every projected row is a real cost
68/// here, and a root scan cannot need it. A `values` projection returns no
69/// hits, so it is exempt. Same rule as the reference's `SELF_ITEM`.
70const SELF_KEY: &str = "__oqx_self";
71/// The reserved key a top-level `values` projection's single item is renamed
72/// to, so it rides through id/path injection, paging and distinct as an
73/// ordinary field and is peeled off at the end.
74const VALUE_KEY: &str = "__oqx_value";
75
76/// `query`'s options.
77#[derive(Clone, Copy, Default)]
78pub struct QueryOptions<'a> {
79    /// The page cap (default 50).
80    pub limit: Option<usize>,
81    /// Resume after a truncated page's cursor.
82    pub cursor: Option<&'a str>,
83    /// The provider behind `semantic(...)`; `None` → `semantic_unavailable`
84    /// when the query names a phrase.
85    pub provider: Option<&'a dyn EmbeddingProvider>,
86    /// Force the pure in-memory engine (skip the tier-3 pushdown planner).
87    /// The default plans; the differential gate runs both and compares.
88    pub in_memory: bool,
89    /// `bare: true` (§1.4, 4.2): every hit is its projection only — the
90    /// automatic `id`/`path` envelope members are stripped when the result is
91    /// shaped, AFTER paging and distinct (identity is kept internally: the
92    /// keyset cursor and the dedup key read the envelope). Needs a `select`
93    /// (without a projection a hit is only its envelope) and is refused with
94    /// `values` (already the bare-values form); a projected `id`/`path` is the
95    /// row's intrinsic, kept as any projected key is.
96    pub bare: bool,
97    /// What a tier-3 name on an open kind does (`spec/surface` §1.5, 4.6; a
98    /// tier-3 name on `links` is `filter_invalid` either way): `Hint` records
99    /// it in `hints` and runs the query — the default of the source entry
100    /// ([`query`], every read); `Refuse` fails with `filter_invalid` before
101    /// any row is read — the default of the AST entry ([`query_ast`]), whose
102    /// caller is the write selector compiler, where a name no row carries
103    /// would select every row. `None` takes the entry's default.
104    pub unknown_names: Option<UnknownNames>,
105    /// The vocabulary cache of the store (§1.5 "Caching"), kept by the
106    /// `Surface` beside its store; `None` judges with a cache of the run's own.
107    pub vocabulary: Option<&'a VocabularyCache>,
108    /// The instant the named points of `date()` resolve against
109    /// (`spec/surface` §1.3, 4.8): the host clock, UTC, in milliseconds —
110    /// the `Surface` passes the clock it stamps commits with (pinned by
111    /// `OMGBASE_SPEC_CLOCK`); `None` reads the wall clock. One instant per
112    /// run: the fold and the context share it.
113    pub now_ms: Option<i64>,
114}
115
116/// §1.4 `OqxResult`.
117#[derive(Clone, Debug, PartialEq)]
118pub struct OqxResult {
119    pub hits: Vec<Json>,
120    pub truncated: bool,
121    pub cursor: Option<String>,
122    pub consumer: Consumer,
123    pub count: Option<f64>,
124    pub exists: Option<bool>,
125    pub none: Option<bool>,
126    /// A top-level `values` projection's bare values, in place of `hits`.
127    pub values: Option<Vec<Json>>,
128    /// Unknown names (`spec/surface` §1.5, 4.6): one line per name the query
129    /// reads that is neither a field of its row kind nor a property any object
130    /// of that kind in the repository carries — the rows are still returned.
131    /// First-reference order, deduplicated; empty = absent on the wire (never
132    /// `[]`). The last member of the result.
133    pub hints: Vec<String>,
134}
135
136impl OqxResult {
137    fn scalar(consumer: Consumer) -> Self {
138        Self {
139            hits: Vec::new(),
140            truncated: false,
141            cursor: None,
142            consumer,
143            count: None,
144            exists: None,
145            none: None,
146            values: None,
147            hints: Vec::new(),
148        }
149    }
150
151    /// The wire shape: `{ hits, truncated, cursor, consumer, count?, exists?,
152    /// none?, values?, hints? }` — `hints` last, only when there is one.
153    #[must_use]
154    pub fn to_json(&self) -> Json {
155        let mut m = Map::new();
156        m.insert("hits".to_owned(), Json::Array(self.hits.clone()));
157        m.insert("truncated".to_owned(), Json::Bool(self.truncated));
158        m.insert(
159            "cursor".to_owned(),
160            self.cursor.clone().map_or(Json::Null, Json::String),
161        );
162        m.insert(
163            "consumer".to_owned(),
164            Json::String(self.consumer.as_str().to_owned()),
165        );
166        if let Some(n) = self.count {
167            m.insert("count".to_owned(), Value::Number(n).to_canonical_json());
168        }
169        if let Some(b) = self.exists {
170            m.insert("exists".to_owned(), Json::Bool(b));
171        }
172        if let Some(b) = self.none {
173            m.insert("none".to_owned(), Json::Bool(b));
174        }
175        if let Some(v) = &self.values {
176            m.insert("values".to_owned(), Json::Array(v.clone()));
177        }
178        if !self.hints.is_empty() {
179            m.insert(
180                "hints".to_owned(),
181                Json::Array(self.hints.iter().cloned().map(Json::String).collect()),
182            );
183        }
184        Json::Object(m)
185    }
186}
187
188// ---- the `$self` rewrite -----------------------------------------------------------
189
190/// Paths on the surface are the reference form (`spec/surface` §1 "Paths",
191/// 2.0): `path` and the edge field `dst_path` read `/a/b.md`. A string
192/// literal a query compares with one of them — `path == "a.md"`, `"a.md" !=
193/// dst_path`,
194/// `path.startsWith("lab/")` — is rooted first, so both spellings match; a
195/// property or binding compared with a path is never touched (it is the
196/// author's value). The planner sees the rewritten tree too, so the pushed
197/// SQL and the in-memory engine agree by construction.
198const PATH_ATTRIBUTES: [&str; 2] = ["path", "dst_path"];
199/// The handles through which a member read reaches a ROW's `path` (`doc.path`,
200/// `block.path`, a link's `dst.path`, `$it.path`); a member read through a
201/// source handle (`data.path`, `frontmatter.path`) is the author's value and
202/// is never rooted.
203const ROW_HANDLES: [&str; 4] = ["doc", "block", "dst", "$it"];
204
205fn is_path_read(e: &Expr) -> bool {
206    match e {
207        Expr::Ident { name, .. } | Expr::Outer { name, .. } => {
208            PATH_ATTRIBUTES.contains(&name.as_str())
209        }
210        Expr::Member { recv, name, .. } if PATH_ATTRIBUTES.contains(&name.as_str()) => {
211            matches!(&**recv, Expr::Ident { name, .. } | Expr::Outer { name, .. } if ROW_HANDLES.contains(&name.as_str()))
212        }
213        _ => false,
214    }
215}
216
217/// Result projections may not overwrite the envelope (§1.4, 3.0): a projection
218/// whose output key is `id` or `path` is allowed only when it IS the envelope's
219/// own selection — the bare attribute of the same name, which collapses onto
220/// the envelope key (`select id, path` is simply the envelope) — and is
221/// otherwise a loud error asking for an alias: `select data.path` must be
222/// `select p: data.path`, `select path: doc.layer` likewise. Checked over the
223/// resolved items (an alias is just a name here), before the engine runs, so
224/// the error never depends on the rows. A `values` projection returns no hits,
225/// so the envelope — and the rule — do not apply. Same rule as the reference's
226/// `envelopeCollision`.
227fn envelope_collision(items: &[SelectItem]) -> Option<SurfaceError> {
228    for item in items {
229        let name = match item {
230            SelectItem::Field { name, .. } | SelectItem::Collect { name, .. } => name.as_str(),
231        };
232        if name != HIT_ID && name != HIT_PATH {
233            continue;
234        }
235        if let SelectItem::Field { expr, .. } = item
236            && matches!(expr, Expr::Ident { name: n, .. } if n == name)
237        {
238            continue;
239        }
240        let hint = if name == HIT_PATH {
241            "p: data.path"
242        } else {
243            "i: data.id"
244        };
245        return Some(SurfaceError::filter_invalid(
246            format!("projection `{name}` collides with the hit envelope — alias it (`{hint}`)"),
247            "OQX",
248        ));
249    }
250    None
251}
252
253fn root_literal(e: Expr) -> Expr {
254    match e {
255        Expr::Lit {
256            value: Value::Str(s),
257            span,
258        } => Expr::Lit {
259            value: Value::Str(reference_path(&s)),
260            span,
261        },
262        other => other,
263    }
264}
265
266/// The literal-rooting rewrite of one expression (see [`rewrite_query`]).
267#[must_use]
268pub fn root_path_literals(e: Expr) -> Expr {
269    match e {
270        Expr::Binary {
271            op: op @ (BinaryOp::Eq | BinaryOp::Ne),
272            left,
273            right,
274            span,
275        } => {
276            if is_path_read(&left) {
277                Expr::Binary {
278                    op,
279                    left,
280                    right: Box::new(root_literal(*right)),
281                    span,
282                }
283            } else if is_path_read(&right) {
284                Expr::Binary {
285                    op,
286                    left: Box::new(root_literal(*left)),
287                    right,
288                    span,
289                }
290            } else {
291                Expr::Binary {
292                    op,
293                    left,
294                    right,
295                    span,
296                }
297            }
298        }
299        Expr::Call {
300            recv: Some(recv),
301            name,
302            mut args,
303            span,
304        } if name == "startsWith" && args.len() == 1 && is_path_read(&recv) => {
305            let arg = root_literal(args.remove(0));
306            Expr::Call {
307                recv: Some(recv),
308                name,
309                args: vec![arg],
310                span,
311            }
312        }
313        other => other,
314    }
315}
316
317/// Dates over literals fold to literals (`spec/surface` §1.3 "Dates and
318/// durations", 4.8): `date("today")`, `dur("30 days")`, and a date method
319/// whose receiver and arguments are literals (`date("today").minus(dur("30
320/// days"))`, folded inside-out since `transform` maps children first) become
321/// the string / number / null they evaluate to — by the SAME functions the
322/// context runs, so nothing changes in meaning — before the planner sees the
323/// tree. So `where updated_at < date("today")` is a text-vs-text comparison
324/// the translator pushes down (a call is otherwise an unconditional decline),
325/// and a named point is resolved ONCE per query, at `now_ms`. A call with the
326/// wrong arity is left in place: it raises when evaluated, and an empty source
327/// never evaluates it (`spec/oqx` §11).
328#[must_use]
329pub fn fold_dates(e: Expr, now_ms: i64) -> Expr {
330    let Expr::Call {
331        recv, name, args, ..
332    } = &e
333    else {
334        return e;
335    };
336    let mut vals = Vec::with_capacity(args.len());
337    for a in args {
338        let Expr::Lit { value, .. } = a else { return e };
339        vals.push(value.clone());
340    }
341    let folded = match recv {
342        None => {
343            if dates::date_function_arity(name) != Some(vals.len()) {
344                return e;
345            }
346            dates::date_function(name, &vals, now_ms)
347        }
348        Some(r) => {
349            let Expr::Lit { value, .. } = &**r else {
350                return e;
351            };
352            if dates::date_method_arity(name) != Some(vals.len()) {
353                return e;
354            }
355            dates::date_method(name, value, &vals)
356        }
357    };
358    match folded {
359        Some(Ok(v)) => build::lit(v),
360        _ => e,
361    }
362}
363
364/// Rewrite every row function in a parsed query to a `$self` method call,
365/// root every string literal compared with a path intrinsic, and fold the
366/// dates over literals ([`fold_dates`], at `now_ms`) — one `transform` over
367/// the AST (`spec/oqx/AST.md` §5), every block included.
368#[must_use]
369pub fn rewrite_query(q: &Query, now_ms: i64) -> Query {
370    transform(q, &mut |e, _| match e {
371        Expr::Call {
372            recv: None,
373            name,
374            args,
375            span,
376        } if ROW_FNS.contains(&name.as_str()) => Expr::Call {
377            recv: Some(Box::new(build::ident("$self"))),
378            name,
379            args,
380            span,
381        },
382        other => fold_dates(root_path_literals(other), now_ms),
383    })
384}
385
386// ---- the root row (spec/surface §1.1, 2.0) ----------------------------------------------
387
388/// The `$repo` error (removed in surface 2.0): the same text in both engines.
389pub 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`";
390
391/// The bare-target error. `depth` is the scope depth the name was read at when
392/// known statically (the carets in the hint count it); `None` when raised at
393/// read time by the context, where the hint spells out the rule instead.
394#[must_use]
395pub fn bare_target_message(t: Target, depth: Option<usize>) -> String {
396    let name = t.as_str();
397    let noun = t.noun();
398    let carets = "^".repeat(depth.unwrap_or(1));
399    let how = if depth.is_none() {
400        format!("; one caret per enclosing block, or the absolute `0^{name}`")
401    } else {
402        String::new()
403    };
404    format!(
405        "`{name}` inside a block reads a property of the current row, which has none — did you mean `{carets}{name}` (the repository's {noun}{how})?"
406    )
407}
408
409/// The unknown-target error (3.0 name pass): a top-level `from <name>` whose
410/// name is not one of the five collections would read an absent root and
411/// return an empty result — refused instead, naming the collections (in the
412/// root object's order, `revisions` the fifth since 4.7); `edges`, the links
413/// target's name before 3.0, gets its new name spelled out.
414#[must_use]
415pub fn unknown_target_message(name: &str) -> String {
416    let was = if name == "edges" {
417        " (`edges` was renamed `links` in surface 3.0)"
418    } else {
419        ""
420    };
421    let roots: Vec<String> = crate::context::ALL_TARGETS
422        .iter()
423        .map(|t| format!("`{}`", t.as_str()))
424        .collect();
425    format!(
426        "`{name}` is not a target — the repository's collections are {}{was}",
427        roots.join(", ")
428    )
429}
430
431/// The past-the-root error: `^docs` at the root scope (more carets than there
432/// are enclosing scopes) would read absent and count 0 — refused, naming the
433/// bare spelling. `levels` is the caret count written.
434#[must_use]
435pub fn past_root_message(t: Target, levels: usize) -> String {
436    let name = t.as_str();
437    let noun = t.noun();
438    let carets = "^".repeat(levels);
439    format!(
440        "`{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})`)"
441    )
442}
443
444/// The repository is OQX's root row (`spec/oqx` 0.18): from a top-level row
445/// `^docs` reaches the documents (one caret per enclosing block, or the
446/// absolute `0^docs`) and `^id` the repository id. Two spellings are refused
447/// before evaluation, by one walk over the parsed tree, so the error does not
448/// depend on which rows the data holds and the planned and in-memory paths
449/// agree trivially: `$repo` anywhere (bare, as a member head, `^$repo`,
450/// `0^$repo`), and a bare `docs`/`links` at scope depth ≥ 1 — a property read
451/// of a row that has none. `blocks`/`nodes` are relations of some rows
452/// (`docs.blocks`, `docs.nodes`, `blocks.nodes`, `section.blocks`) and are
453/// judged by the context at read time with the same message. A `^<target>`
454/// with more carets than enclosing scopes (`^docs count { }` at the top level)
455/// would read absent and count 0: refused too, naming the bare spelling. Port
456/// of the reference's `checkRootSpellings`.
457pub fn check_root_spellings(q: &Query) -> Result<()> {
458    // A top-level `from <name>` that is no collection (`from edges`, the links
459    // target's name before 3.0) would scan an absent root and come back empty:
460    // refused, naming the five collections.
461    if let Expr::Ident { name, .. } = &q.source {
462        if Target::parse(name).is_none() {
463            return Err(SurfaceError::filter_invalid(
464                unknown_target_message(name),
465                "OQX",
466            ));
467        }
468    }
469    struct Check(Option<SurfaceError>);
470    impl Visitor<'_> for Check {
471        fn enter(&mut self, node: Node<'_>, ctx: &VisitContext<'_>) -> bool {
472            if self.0.is_some() {
473                return false;
474            }
475            match node {
476                Node::Expr(Expr::Ident { name, .. } | Expr::Outer { name, .. })
477                    if name == "$repo" =>
478                {
479                    self.0 = Some(SurfaceError::filter_invalid(REPO_REMOVED_MESSAGE, "OQX"));
480                }
481                Node::Expr(Expr::Ident { name, .. })
482                    if ctx.depth >= 1 && matches!(name.as_str(), "docs" | "links") =>
483                {
484                    let t = Target::parse(name).expect("a target name");
485                    self.0 = Some(SurfaceError::filter_invalid(
486                        bare_target_message(t, Some(ctx.depth)),
487                        "OQX",
488                    ));
489                }
490                Node::Expr(Expr::Outer { name, levels, .. }) if *levels > ctx.depth => {
491                    if let Some(t) = Target::parse(name) {
492                        self.0 = Some(SurfaceError::filter_invalid(
493                            past_root_message(t, *levels),
494                            "OQX",
495                        ));
496                    }
497                }
498                _ => {}
499            }
500            self.0.is_none()
501        }
502    }
503    let mut check = Check(None);
504    visit(Node::Query(q), &mut check);
505    check.0.map_or(Ok(()), Err)
506}
507
508// ---- semantic phrases -----------------------------------------------------------------
509
510/// Collects the distinct `semantic("…")` phrases of a tree.
511struct Phrases(Vec<String>);
512
513impl Visitor<'_> for Phrases {
514    fn enter(&mut self, node: Node<'_>, _ctx: &VisitContext<'_>) -> bool {
515        if let Node::Expr(Expr::Call {
516            recv: None,
517            name,
518            args,
519            ..
520        }) = node
521            && name == "semantic"
522            && let Some(Expr::Lit {
523                value: Value::Str(s),
524                ..
525            }) = args.first()
526            && !self.0.contains(s)
527        {
528            self.0.push(s.clone());
529        }
530        true
531    }
532}
533
534/// The distinct phrases `semantic("…")` names (free calls in the raw parse);
535/// empty when the source does not parse.
536#[must_use]
537pub fn collect_semantic_phrases(source: &str) -> Vec<String> {
538    let Ok(q) = oqx::parse_string(source) else {
539        return Vec::new();
540    };
541    semantic_phrases_of(&q)
542}
543
544/// [`collect_semantic_phrases`] over a parsed tree.
545#[must_use]
546pub fn semantic_phrases_of(q: &Query) -> Vec<String> {
547    let mut phrases = Phrases(Vec::new());
548    visit(Node::Query(q), &mut phrases);
549    phrases.0
550}
551
552/// Embed the query's phrases through the provider: the vectors
553/// `semantic(...)` ranks by. Without a provider the phrases stay unembedded
554/// and the context reports `filter_invalid` ("needs an embedding provider")
555/// when one is reached — after its target check, so `semantic()` on nodes
556/// names the targets (§9; the `query` tool's pre-check is what reports
557/// `semantic_unavailable`).
558pub fn embed_phrases(
559    provider: Option<&dyn EmbeddingProvider>,
560    phrases: Vec<String>,
561) -> Result<HashMap<String, SemanticVec>> {
562    let mut semantic: HashMap<String, SemanticVec> = HashMap::new();
563    if let Some(provider) = provider.filter(|_| !phrases.is_empty()) {
564        for phrase in phrases {
565            let vec = provider
566                .embed_query(&phrase)
567                .map_err(|e| SurfaceError::new(e.code(), e.to_string()))?;
568            semantic.insert(
569                phrase,
570                SemanticVec {
571                    model: provider.model().to_owned(),
572                    vec: f32_to_blob(&vec),
573                },
574            );
575        }
576    }
577    Ok(semantic)
578}
579
580// ---- reserved projection names ----------------------------------------------------------
581
582/// The runner's own projection names (`__oqx_id`, `__oqx_path`,
583/// `__oqx_self`, `__oqx_value`) and the selector compiler's slots
584/// (`__omg_sel_<t>_<slot>`, [`crate::select`]) share two reserved prefixes.
585/// A user alias under either would shadow an injected item (`select
586/// __oqx_id: 1` overwrote the hit's `id` through 4.0) or collide with a
587/// slot, so it is refused before anything runs (4.1) — over the query's own
588/// top-level items, the only ones the runner injects beside. The reference's
589/// `reservedAlias`.
590pub const RESERVED_ALIAS_PREFIXES: [&str; 2] = ["__oqx_", "__omg_"];
591
592/// Refuse a top-level `select` alias under a reserved prefix.
593pub fn check_reserved_aliases(q: &Query) -> Result<()> {
594    for item in &q.select {
595        if let Some(prefix) = RESERVED_ALIAS_PREFIXES
596            .iter()
597            .find(|p| item.name().starts_with(*p))
598        {
599            return Err(SurfaceError::filter_invalid(
600                format!(
601                    "projection `{}` uses the reserved prefix `{prefix}` — choose another alias",
602                    item.name()
603                ),
604                "OQX",
605            ));
606        }
607    }
608    Ok(())
609}
610
611// ---- name mentions --------------------------------------------------------------------
612
613/// Whether `name` is read anywhere in `q` other than as its source: a bare
614/// identifier or a `^`-escaped one in the `from` steps, `where`, `select`,
615/// `order by`, `follow`, `limit`/`offset`, or any nested block.
616fn mentions_outside_source(q: &Query, name: &str) -> bool {
617    struct Mentions<'n> {
618        name: &'n str,
619        found: bool,
620    }
621    impl Visitor<'_> for Mentions<'_> {
622        fn enter(&mut self, node: Node<'_>, ctx: &VisitContext<'_>) -> bool {
623            if self.found {
624                return false;
625            }
626            // The source itself (the root's `source` slot) is not a mention.
627            if ctx.clause == Some(Clause::Source) && ctx.path.len() == 1 {
628                return false;
629            }
630            if let Node::Expr(Expr::Ident { name, .. } | Expr::Outer { name, .. }) = node
631                && name == self.name
632            {
633                self.found = true;
634            }
635            !self.found
636        }
637    }
638    let mut m = Mentions { name, found: false };
639    visit(Node::Query(q), &mut m);
640    m.found
641}
642
643// ---- hits ----------------------------------------------------------------------------
644
645/// Whether the engine's top-level rows can be anything but store rows: a
646/// `follow` (a destination may be a property's value), a `from E`
647/// re-projection, or a source that is not a bare root scan (one of the five
648/// collections, `revisions` included — the planner's `compile` is narrower:
649/// the four it can push down).
650fn may_reach_non_rows(q: &Query) -> bool {
651    q.follow.is_some() || !q.from.is_empty() || root_target(&q.source).is_none()
652}
653
654/// The reference's `describeValue`: what a non-row hit was, for the error.
655fn describe_value(v: &Value) -> String {
656    match v {
657        Value::Undefined | Value::Null => "an absent value".to_owned(),
658        Value::Str(s) => format!(
659            "a string ({})",
660            serde_json::to_string(s).unwrap_or_default()
661        ),
662        Value::Number(_) => format!("a number ({v})"),
663        Value::Bool(b) => format!("a boolean ({b})"),
664        Value::Array(_) => "an array".to_owned(),
665        Value::Range(_) => "a range".to_owned(),
666        Value::Object(_) => "an object".to_owned(),
667    }
668}
669
670fn not_a_store_row(v: &Value) -> SurfaceError {
671    SurfaceError::filter_invalid(
672        format!(
673            "a hit must be a document, block, node, link or revision row — the query reached {}; to follow document references held in a property use refs(<field>)",
674            describe_value(v)
675        ),
676        "OQX",
677    )
678}
679
680/// A shaped row (the reference's `Shaped`, 4.2): the identity the runner
681/// injected, kept APART from the user projection until the very end, so
682/// `distinct`, the keyset page and the cursor run on identity whether or not
683/// the hits carry it (`bare`). `id` is JavaScript's `String()` on the injected
684/// id, `path` `""` for an absent path; `proj` is the user projection with its
685/// values rendered per §1.4 "rows as values" (a store row nested in the result
686/// — an empty-projection `collect { }` and friends — becomes `{ id, path }`).
687/// When the row itself was projected under [`SELF_KEY`] and is not a store
688/// row, the query fails (§1.4: a hit is a store row).
689#[derive(Debug)]
690struct Shaped {
691    id: String,
692    path: String,
693    proj: oqx::Object,
694}
695
696fn path_string(path: &Value) -> String {
697    if path.is_absent() {
698        String::new()
699    } else {
700        path.to_string()
701    }
702}
703
704fn shape_row(row: Value) -> Result<Shaped> {
705    let mut shaped = Shaped {
706        id: Value::Undefined.to_string(),
707        path: String::new(),
708        proj: oqx::Object::new(),
709    };
710    let Value::Object(mut o) = row else {
711        return Ok(shaped);
712    };
713    if let Some(me) = o.remove(SELF_KEY)
714        && target_of(&me).is_none()
715    {
716        return Err(not_a_store_row(&me));
717    }
718    let Value::Object(o) = render_row_values(Value::Object(o)) else {
719        return Ok(shaped);
720    };
721    for (k, v) in o {
722        match k.as_str() {
723            ID_KEY => shaped.id = v.to_string(),
724            PATH_KEY => shaped.path = path_string(&v),
725            _ => {
726                shaped.proj.insert(k, v);
727            }
728        }
729    }
730    Ok(shaped)
731}
732
733/// The identity of a row the engine produced, read off the injected columns
734/// without shaping it — exactly the strings [`shape_row`] would carry — so an
735/// unshaped page can be cut by the keyset cursor before shaping (a scan that
736/// projects thousands of rows shapes fifty).
737fn raw_identity(row: &Value) -> (String, String) {
738    let get = |k: &str| {
739        row.as_object()
740            .and_then(|o| o.get(k))
741            .cloned()
742            .unwrap_or(Value::Undefined)
743    };
744    (path_string(&get(PATH_KEY)), get(ID_KEY).to_string())
745}
746
747/// A hit: `{ id, path, ...proj }` (§1.4, 3.0: the envelope keys are the
748/// attributes that read them). The envelope keys lead; a user projection of
749/// `id`/`path` collapses onto them in place (the same value, read by the
750/// same attribute).
751fn to_hit(s: Shaped) -> Value {
752    let mut hit = oqx::Object::with_capacity(s.proj.len() + 2);
753    hit.insert(HIT_ID, Value::Str(s.id));
754    hit.insert(HIT_PATH, Value::Str(s.path));
755    for (k, v) in s.proj {
756        hit.insert(k, v);
757    }
758    Value::Object(hit)
759}
760
761/// `bare` (§1.4, 4.2): the projection alone, in the query's own order. A
762/// projected `id`/`path` is an ordinary column here — the intrinsic the
763/// attribute reads, under the key the query gave it — and nothing is injected
764/// beside it.
765fn to_bare_hit(s: Shaped) -> Value {
766    Value::Object(s.proj)
767}
768
769/// The wire shape of one shaped row: the hit, or the bare hit.
770fn render_hit(s: Shaped, bare: bool) -> Json {
771    if bare {
772        to_bare_hit(s).to_canonical_json()
773    } else {
774        to_hit(s).to_canonical_json()
775    }
776}
777
778/// Top-level `select distinct`: dedup by the USER projection — every field but
779/// the envelope's `id`/`path`, which a projection of the attribute collapses
780/// onto (so `select distinct id` dedups on nothing but the empty projection,
781/// as it always has); under `bare` there is no envelope, so a projected
782/// `id`/`path` IS part of the projection and counts. Keeps the first (so the
783/// retained row carries a real identity), order-preserving. The key is the
784/// canonical JSON of the sorted `[key, value]` pairs (`JSON.stringify` in the
785/// reference).
786fn dedup_by_projection(rows: Vec<Shaped>, bare: bool) -> Vec<Shaped> {
787    let mut seen: Vec<String> = Vec::new();
788    let mut out = Vec::new();
789    for s in rows {
790        let mut pairs: Vec<(String, Value)> = s
791            .proj
792            .iter()
793            .filter(|(k, _)| bare || (*k != HIT_ID && *k != HIT_PATH))
794            .map(|(k, v)| (k.to_owned(), v.clone()))
795            .collect();
796        pairs.sort_by(|a, b| a.0.cmp(&b.0));
797        let key = Value::Array(
798            pairs
799                .into_iter()
800                .map(|(k, v)| Value::Array(vec![Value::Str(k), v]))
801                .collect(),
802        )
803        .to_canonical_json()
804        .to_string();
805        if seen.contains(&key) {
806            continue;
807        }
808        seen.push(key);
809        out.push(s);
810    }
811    out
812}
813
814/// A top-level `limit`/`offset` on the collect path is applied by the runner,
815/// so it must be a plain non-negative integer literal.
816fn const_bound(e: Option<&Expr>, word: &str) -> Result<Option<usize>> {
817    match e {
818        None => Ok(None),
819        Some(Expr::Lit {
820            value: Value::Number(n),
821            ..
822        }) if n.fract() == 0.0 && *n >= 0.0 && n.is_finite() => Ok(Some(*n as usize)),
823        Some(_) => Err(SurfaceError::filter_invalid(
824            format!("top-level {word} must be a non-negative integer literal"),
825            "OQX",
826        )),
827    }
828}
829
830/// The collect page over `rows`, whatever their representation — `ident`
831/// yields a row's `(path, id)`: the query's own `offset`/`limit` define the
832/// result SET, the keyset `cursor` (when given) cuts it, `cap` pages it.
833/// Returns the page, whether more rows followed, and the next cursor (none
834/// under a custom order, §1.4).
835fn page_rows<T>(
836    mut rows: Vec<T>,
837    ident: impl Fn(&T) -> (String, String),
838    offset: usize,
839    limit: Option<usize>,
840    cursor: Option<&str>,
841    cap: usize,
842    custom: bool,
843) -> Result<(Vec<T>, bool, Option<String>)> {
844    if offset > 0 || limit.is_some() {
845        let end = limit.map_or(rows.len(), |l| (offset + l).min(rows.len()));
846        if offset >= rows.len() {
847            rows.clear();
848        } else {
849            rows.truncate(end);
850            rows.drain(..offset);
851        }
852    }
853    if let Some(cursor) = cursor {
854        // The cursor carries the hit's (rooted) path; a 1.x cursor is refused (§1.4).
855        let parts = decode_path_cursor(cursor, "query", 2)?;
856        let (path, id) = (&parts[0], &parts[1]);
857        rows.retain(|r| {
858            let (rp, ri) = ident(r);
859            rp > *path || (rp == *path && ri > *id)
860        });
861    }
862    let truncated = rows.len() > cap;
863    rows.truncate(cap);
864    let next = if truncated && !custom {
865        rows.last().map(|last| {
866            let (p, i) = ident(last);
867            encode_cursor(&[&p, &i])
868        })
869    } else {
870        None
871    };
872    Ok((rows, truncated, next))
873}
874
875fn value_of(s: &Shaped) -> Json {
876    s.proj
877        .get(VALUE_KEY)
878        .cloned()
879        .unwrap_or(Value::Undefined)
880        .to_canonical_json()
881}
882
883/// `bare: true` needs a projection and is not the `values` form (§1.4, 4.2):
884/// without a projection a hit is only its envelope, and `values` is already
885/// the bare-values form. Checked over the resolved query where hits are built
886/// (the scalar consumers have no hits and ignore `bare`), before the engine
887/// runs, so the error never depends on the rows and the planned and in-memory
888/// paths agree trivially. The reference's `bareMisuse`.
889pub fn check_bare(q: &Query) -> Result<()> {
890    if q.values {
891        return Err(SurfaceError::filter_invalid(
892            BARE_WITH_VALUES_MESSAGE,
893            "OQX",
894        ));
895    }
896    if q.select.is_empty() {
897        return Err(SurfaceError::filter_invalid(
898            BARE_NEEDS_SELECT_MESSAGE,
899            "OQX",
900        ));
901    }
902    Ok(())
903}
904
905/// The `bare` refusals (4.2): the same text in both engines.
906pub const BARE_NEEDS_SELECT_MESSAGE: &str =
907    "`bare` needs a `select` — without a projection a hit is only its envelope";
908pub const BARE_WITH_VALUES_MESSAGE: &str =
909    "`bare` is redundant with `values` — `values` is already the bare-values form; drop one";
910
911// ---- the run --------------------------------------------------------------------------
912
913/// Run an OQX query against `repo_id` (§1.4).
914pub fn query(
915    store: &Store,
916    repo_id: &str,
917    source: &str,
918    opts: QueryOptions<'_>,
919) -> Result<OqxResult> {
920    let semantic = embed_phrases(opts.provider, collect_semantic_phrases(source))?;
921    let runner = Runner {
922        store,
923        repo_id,
924        semantic,
925        planned: !opts.in_memory,
926        now_ms: opts.now_ms.unwrap_or_else(dates::wall_clock_ms),
927    };
928    let raw = oqx::parse_string(source)?;
929    check_reserved_aliases(&raw)?;
930    // The source entry is every read: a tier-3 name on an open kind is a hint.
931    run_parsed(
932        &runner,
933        &raw,
934        opts,
935        opts.unknown_names.unwrap_or(UnknownNames::Hint),
936    )
937}
938
939/// Run an already-parsed query (§1.4) — the AST entry the selector compile
940/// uses (`spec/mutate` §11.8: the selector's tree with the templates' slot
941/// projections appended runs as any query does; the reserved-alias check is
942/// the source entry's, so the appended `__omg_sel_*` items pass). `semantic`
943/// holds the embedded phrases ([`embed_phrases`]); `opts.provider` is not
944/// read here. Unknown names (4.6) default to [`UnknownNames::Refuse`] here:
945/// this entry's caller is the write selector, where a name no row carries
946/// would select every row; the source entry passes `Hint`.
947pub fn query_ast(
948    store: &Store,
949    repo_id: &str,
950    raw: &Query,
951    opts: QueryOptions<'_>,
952    semantic: HashMap<String, SemanticVec>,
953) -> Result<OqxResult> {
954    let runner = Runner {
955        store,
956        repo_id,
957        semantic,
958        planned: !opts.in_memory,
959        now_ms: opts.now_ms.unwrap_or_else(dates::wall_clock_ms),
960    };
961    run_parsed(
962        &runner,
963        raw,
964        opts,
965        opts.unknown_names.unwrap_or(UnknownNames::Refuse),
966    )
967}
968
969/// One query's engine: a fresh store context per run (the planned path gives
970/// the residual a context serving the produced rows as its root). A failure
971/// inside a property read or row function is the engine's own error (the
972/// context's `get` / `call_method` return `Err`); only a failed root scan,
973/// which the `root` seam cannot raise, is read back after the run.
974struct Runner<'a> {
975    store: &'a Store,
976    repo_id: &'a str,
977    semantic: HashMap<String, SemanticVec>,
978    planned: bool,
979    /// The one instant of this run (§1.3): the fold and the context share it.
980    now_ms: i64,
981}
982
983impl Runner<'_> {
984    /// Tier-3 pushdown reduces the scan in SQL and the in-memory engine
985    /// finishes the residual over the produced rows (a declined plan, or
986    /// `in_memory`, runs the whole query in memory over a full scan), so
987    /// results match a pure scan. This is `oqx::PlannedEngine::run` inlined:
988    /// the store context borrows the connection, so it cannot be the
989    /// `'static` context a `Plan` carries.
990    fn run(&self, q: &Query) -> Result<oqx::OqxResult> {
991        let conn = self.store.conn();
992        let ctx =
993            StoreContext::new(conn, self.repo_id, self.semantic.clone()).with_now_ms(self.now_ms);
994        let plan = if self.planned {
995            SqlitePlanner::new(conn, self.repo_id)
996                .try_plan(q, &[])
997                .map_err(|e| SurfaceError::other(format!("sqlite: {e}")))?
998        } else {
999            None
1000        };
1001        let (ctx, residual) = match plan {
1002            // The residual's source is its one read of the rows root unless
1003            // the query text itself names `__oqx_rows__` somewhere else (a
1004            // `^`-reach, a `limit`, a nested block…): then every read must
1005            // see the rows, so they are cloned out instead of moved.
1006            Some(plan) => {
1007                let once = !mentions_outside_source(&plan.residual, oqx::ROWS_ROOT);
1008                let ctx = if once {
1009                    ctx.with_rows_root_once(plan.rows)
1010                } else {
1011                    ctx.with_rows_root(plan.rows)
1012                };
1013                (ctx, Some(plan.residual))
1014            }
1015            None => (ctx, None),
1016        };
1017        let engine = InMemoryEngine::new(ctx);
1018        let out = engine.run(residual.as_ref().unwrap_or(q), &[]);
1019        // `root` has no error channel: a store failure during a root scan was
1020        // served as an empty scan and wins over whatever the run made of it.
1021        if let Some(failed) = engine.context().take_root_failure() {
1022            return Err(failed.into());
1023        }
1024        // A root scan projected as a VALUE (`select all: ^docs`) was
1025        // materialized into its rows by the engine (`DataContext::materialize`).
1026        Ok(out?)
1027    }
1028}
1029
1030fn run_parsed(
1031    engine: &Runner<'_>,
1032    raw: &Query,
1033    opts: QueryOptions<'_>,
1034    mode: UnknownNames,
1035) -> Result<OqxResult> {
1036    check_root_spellings(raw)?;
1037    // Unknown names (spec/surface §1.5): judged over the RAW tree — before
1038    // alias resolution and the runner's own items — so an alias is never
1039    // mistaken for a row property and the verdict never depends on the rows.
1040    // The selector compiler's slot items (`__omg_sel_*`, the templates'
1041    // expressions) are not the selector: an expression reading an absent
1042    // property there is spec/mutate's `target_missing` / absent value, not an
1043    // unknown name.
1044    let own = Query {
1045        select: raw
1046            .select
1047            .iter()
1048            .filter(|it| {
1049                !RESERVED_ALIAS_PREFIXES
1050                    .iter()
1051                    .any(|p| it.name().starts_with(p))
1052            })
1053            .cloned()
1054            .collect(),
1055        ..raw.clone()
1056    };
1057    let local = VocabularyCache::default();
1058    let cache = opts.vocabulary.unwrap_or(&local);
1059    let hints = check_names(engine.store, engine.repo_id, &own, mode, cache)?;
1060    let mut result = run_resolved(engine, raw, opts)?;
1061    // `hints` is the last member, present only when there is something to say.
1062    result.hints = hints;
1063    Ok(result)
1064}
1065
1066fn run_resolved(engine: &Runner<'_>, raw: &Query, opts: QueryOptions<'_>) -> Result<OqxResult> {
1067    // The query's `select` aliases are resolved HERE, once, before the runner
1068    // renames/injects items (an engine evaluates the query it is given).
1069    let parsed = rewrite_query(&resolve_aliases(raw)?, engine.now_ms);
1070    let consumer = parsed.consumer;
1071
1072    match consumer {
1073        Consumer::Exists => {
1074            let res = engine.run(&parsed)?;
1075            let mut r = OqxResult::scalar(consumer);
1076            r.exists = Some(matches!(res, oqx::OqxResult::Exists(true)));
1077            return Ok(r);
1078        }
1079        Consumer::Count => {
1080            let res = engine.run(&parsed)?;
1081            let mut r = OqxResult::scalar(consumer);
1082            r.count = Some(match res {
1083                oqx::OqxResult::Count(n) => n,
1084                _ => 0.0,
1085            });
1086            return Ok(r);
1087        }
1088        Consumer::None => {
1089            let res = engine.run(&parsed)?;
1090            let mut r = OqxResult::scalar(consumer);
1091            r.none = Some(match res {
1092                oqx::OqxResult::None(b) => b,
1093                _ => true,
1094            });
1095            return Ok(r);
1096        }
1097        Consumer::Collect | Consumer::First | Consumer::Single => {}
1098    }
1099
1100    // A grouped query (oqx 0.24, SEMANTICS §24) yields GROUPS, not store rows:
1101    // the envelope does not apply — there is no `id`/`path` to inject and no
1102    // keyset to page by — so a hit is the group's projection as the query
1103    // wrote it (`{ layer, n }`), exactly what `bare` yields for a row query;
1104    // `bare` is accepted and changes nothing. Rows that surface as values
1105    // (`rows collect { }` with an empty projection, `collect(doc)`) render as
1106    // `{ id, path }` refs like every row-as-value. Paging is by position: the
1107    // query's own `limit`/`offset` define the set, the tool's `limit` takes
1108    // the first page, `truncated` says whether more groups follow, the cursor
1109    // is always null (as under a custom `order by`). `distinct` cannot occur
1110    // (a parse error).
1111    if parsed.group_by.is_some() {
1112        return run_grouped(engine, &parsed, consumer, opts);
1113    }
1114
1115    // collect / first / single: inject id + path so every hit carries them. A
1116    // top-level `select distinct` is applied HERE, not in the engine (the
1117    // injected id/path are unique per row and would defeat the engine's
1118    // projection dedup). A top-level `values` projection runs as a RECORD
1119    // projection whose single item is renamed to VALUE_KEY.
1120    let top_distinct = parsed.distinct;
1121    let top_values = parsed.values;
1122    let user_select: Vec<SelectItem> = if top_values {
1123        parsed
1124            .select
1125            .first()
1126            .map(|it| match it {
1127                SelectItem::Field {
1128                    expr, lift, span, ..
1129                } => SelectItem::Field {
1130                    name: VALUE_KEY.to_owned(),
1131                    expr: expr.clone(),
1132                    lift: *lift,
1133                    span: *span,
1134                },
1135                SelectItem::Collect { op, span, .. } => SelectItem::Collect {
1136                    name: VALUE_KEY.to_owned(),
1137                    op: op.clone(),
1138                    span: *span,
1139                },
1140            })
1141            .into_iter()
1142            .collect()
1143    } else {
1144        parsed.select.clone()
1145    };
1146    // `bare` (4.2): the hits are the projection alone — refused without one
1147    // and with `values`; with no envelope there is no collision to refuse
1148    // either, a projected `id`/`path` is simply a column.
1149    let bare = opts.bare;
1150    if bare {
1151        check_bare(&parsed)?;
1152    } else if !top_values && let Some(e) = envelope_collision(&user_select) {
1153        return Err(e);
1154    }
1155    let id_item = build::field(ID_KEY, build::ident("id"));
1156    let path_item = build::field(PATH_KEY, build::ident("path"));
1157    let mut select = vec![id_item, path_item];
1158    if !top_values && may_reach_non_rows(&parsed) {
1159        select.push(build::field(SELF_KEY, build::ident("$it")));
1160    }
1161    select.extend(user_select);
1162    // On the collect path the query's own limit/offset is taken out of the
1163    // engine query and applied after the runner's distinct; first/single keep
1164    // theirs (the engine's offset-aware cap is exactly right for them).
1165    let (top_limit, top_offset) = (parsed.limit.clone(), parsed.offset.clone());
1166    let q = Query {
1167        distinct: false,
1168        values: false,
1169        select,
1170        limit: if consumer == Consumer::Collect {
1171            None
1172        } else {
1173            parsed.limit.clone()
1174        },
1175        offset: if consumer == Consumer::Collect {
1176            None
1177        } else {
1178            parsed.offset.clone()
1179        },
1180        ..parsed.clone()
1181    };
1182    let res = engine.run(&q)?;
1183
1184    if matches!(consumer, Consumer::First | Consumer::Single) {
1185        let row = match res {
1186            oqx::OqxResult::First(r) | oqx::OqxResult::Single(r) => r,
1187            _ => None,
1188        };
1189        let mut out = OqxResult::scalar(consumer);
1190        match row {
1191            None => {
1192                if top_values {
1193                    out.values = Some(Vec::new());
1194                }
1195            }
1196            Some(r) => {
1197                let shaped = shape_row(r)?;
1198                if top_values {
1199                    out.values = Some(vec![value_of(&shaped)]);
1200                } else {
1201                    out.hits = vec![render_hit(shaped, bare)];
1202                }
1203            }
1204        }
1205        return Ok(out);
1206    }
1207
1208    // collect: keyset pagination on (path, id) when the order is the default.
1209    let rows: Vec<Value> = match res {
1210        oqx::OqxResult::Collect(rows) => rows,
1211        _ => Vec::new(),
1212    };
1213    let custom = parsed.order_by.as_ref().is_some_and(|o| !o.is_empty());
1214    let cursor = opts.cursor.filter(|c| !c.is_empty() && !custom);
1215    let offset = const_bound(top_offset.as_ref(), "offset")?.unwrap_or(0);
1216    let limit = const_bound(top_limit.as_ref(), "limit")?;
1217    let cap = opts.limit.unwrap_or(DEFAULT_LIMIT);
1218    // The page runs on IDENTITY, never on the hit: a `distinct` needs every
1219    // row shaped first (its key is the projection), else the plain rows are
1220    // paged by their injected columns and only the page is shaped. The
1221    // envelope is stripped LAST (`bare`), so a bare result pages exactly like
1222    // the enveloped one.
1223    let (page, truncated, next) = if top_distinct {
1224        let shaped = rows
1225            .into_iter()
1226            .map(shape_row)
1227            .collect::<Result<Vec<_>>>()?;
1228        let shaped = dedup_by_projection(shaped, bare);
1229        page_rows(
1230            shaped,
1231            |s| (s.path.clone(), s.id.clone()),
1232            offset,
1233            limit,
1234            cursor,
1235            cap,
1236            custom,
1237        )?
1238    } else {
1239        let (page, truncated, next) =
1240            page_rows(rows, raw_identity, offset, limit, cursor, cap, custom)?;
1241        let page = page
1242            .into_iter()
1243            .map(shape_row)
1244            .collect::<Result<Vec<_>>>()?;
1245        (page, truncated, next)
1246    };
1247    let mut out = OqxResult::scalar(Consumer::Collect);
1248    out.truncated = truncated;
1249    out.cursor = next;
1250    if top_values {
1251        out.values = Some(page.iter().map(value_of).collect());
1252    } else {
1253        out.hits = page.into_iter().map(|s| render_hit(s, bare)).collect();
1254    }
1255    Ok(out)
1256}
1257
1258/// The collect/first/single path of a grouped query (see [`run_resolved`]).
1259fn run_grouped(
1260    engine: &Runner<'_>,
1261    parsed: &Query,
1262    consumer: Consumer,
1263    opts: QueryOptions<'_>,
1264) -> Result<OqxResult> {
1265    let top_values = parsed.values;
1266    // A `values` projection rides as a record under VALUE_KEY and is peeled off
1267    // the page, as on the row path.
1268    let select: Vec<SelectItem> = if top_values {
1269        parsed
1270            .select
1271            .first()
1272            .map(|it| match it {
1273                SelectItem::Field {
1274                    expr, lift, span, ..
1275                } => SelectItem::Field {
1276                    name: VALUE_KEY.to_owned(),
1277                    expr: expr.clone(),
1278                    lift: *lift,
1279                    span: *span,
1280                },
1281                SelectItem::Collect { op, span, .. } => SelectItem::Collect {
1282                    name: VALUE_KEY.to_owned(),
1283                    op: op.clone(),
1284                    span: *span,
1285                },
1286            })
1287            .into_iter()
1288            .collect()
1289    } else {
1290        parsed.select.clone()
1291    };
1292    let (top_limit, top_offset) = (parsed.limit.clone(), parsed.offset.clone());
1293    let q = Query {
1294        values: false,
1295        select,
1296        limit: if consumer == Consumer::Collect {
1297            None
1298        } else {
1299            parsed.limit.clone()
1300        },
1301        offset: if consumer == Consumer::Collect {
1302            None
1303        } else {
1304            parsed.offset.clone()
1305        },
1306        ..parsed.clone()
1307    };
1308    let res = engine.run(&q)?;
1309    let project = |row: Value| -> oqx::Object {
1310        match render_row_values(row) {
1311            Value::Object(o) => o,
1312            _ => oqx::Object::new(),
1313        }
1314    };
1315    let value_of = |g: &oqx::Object| -> Json {
1316        g.get(VALUE_KEY)
1317            .cloned()
1318            .unwrap_or(Value::Undefined)
1319            .to_canonical_json()
1320    };
1321    if matches!(consumer, Consumer::First | Consumer::Single) {
1322        let row = match res {
1323            oqx::OqxResult::First(r) | oqx::OqxResult::Single(r) => r,
1324            _ => None,
1325        };
1326        let mut out = OqxResult::scalar(consumer);
1327        match row {
1328            None => {
1329                if top_values {
1330                    out.values = Some(Vec::new());
1331                }
1332            }
1333            Some(r) => {
1334                let g = project(r);
1335                if top_values {
1336                    out.values = Some(vec![value_of(&g)]);
1337                } else {
1338                    out.hits = vec![Value::Object(g).to_canonical_json()];
1339                }
1340            }
1341        }
1342        return Ok(out);
1343    }
1344    let mut groups: Vec<oqx::Object> = match res {
1345        oqx::OqxResult::Collect(rows) => rows.into_iter().map(project).collect(),
1346        _ => Vec::new(),
1347    };
1348    let offset = const_bound(top_offset.as_ref(), "offset")?.unwrap_or(0);
1349    let limit = const_bound(top_limit.as_ref(), "limit")?;
1350    if offset > 0 || limit.is_some() {
1351        let end = limit.map_or(groups.len(), |l| (offset + l).min(groups.len()));
1352        if offset >= groups.len() {
1353            groups.clear();
1354        } else {
1355            groups.truncate(end);
1356            groups.drain(..offset);
1357        }
1358    }
1359    let cap = opts.limit.unwrap_or(DEFAULT_LIMIT);
1360    let truncated = groups.len() > cap;
1361    groups.truncate(cap);
1362    let mut out = OqxResult::scalar(Consumer::Collect);
1363    out.truncated = truncated;
1364    if top_values {
1365        out.values = Some(groups.iter().map(value_of).collect());
1366    } else {
1367        out.hits = groups
1368            .into_iter()
1369            .map(|g| Value::Object(g).to_canonical_json())
1370            .collect();
1371    }
1372    Ok(out)
1373}
1374
1375#[cfg(test)]
1376mod tests {
1377    use super::*;
1378    use oqx::ast::Where;
1379
1380    #[test]
1381    fn row_functions_become_self_methods() {
1382        let q = oqx::parse_string(
1383            "from blocks where text(\"x\") && under_heading(\"h\") && size(attrs) > 0 && doc.path.startsWith(\"a\")",
1384        )
1385        .unwrap();
1386        let r = rewrite_query(&q, 0);
1387        let Some(Where::And { parts, .. }) = &r.r#where else {
1388            panic!("and")
1389        };
1390        let Where::Scalar { expr, .. } = &parts[0] else {
1391            panic!("scalar")
1392        };
1393        assert!(
1394            matches!(expr, Expr::Call { recv: Some(r), name, .. } if name == "text" && **r == build::ident("$self"))
1395        );
1396        let Where::Scalar { expr, .. } = &parts[2] else {
1397            panic!("scalar")
1398        };
1399        assert!(
1400            matches!(expr, Expr::Binary { left, .. } if matches!(&**left, Expr::Call { recv: None, name, .. } if name == "size"))
1401        );
1402    }
1403
1404    // ---- the constant fold (spec/surface §1.3, 4.8; the reference's dates.test.ts) ----
1405
1406    /// 2026-09-27T00:00:00.000Z — the corpus clock.
1407    const NOW: i64 = 1_790_467_200_000;
1408
1409    fn where_expr(src: &str) -> Expr {
1410        let q = oqx::parse_string(&format!("from docs where {src}")).unwrap();
1411        let Some(Where::Scalar { expr, .. }) = q.r#where else {
1412            panic!("not a scalar where")
1413        };
1414        expr
1415    }
1416
1417    /// Fold inside-out as `transform` does: children first.
1418    fn fold(src: &str) -> Expr {
1419        fn go(e: Expr) -> Expr {
1420            match e {
1421                Expr::Call {
1422                    recv,
1423                    name,
1424                    args,
1425                    span,
1426                } => fold_dates(
1427                    Expr::Call {
1428                        recv: recv.map(|r| Box::new(go(*r))),
1429                        name,
1430                        args: args.into_iter().map(go).collect(),
1431                        span,
1432                    },
1433                    NOW,
1434                ),
1435                other => other,
1436            }
1437        }
1438        go(where_expr(src))
1439    }
1440
1441    fn lit(e: &Expr) -> Option<&Value> {
1442        match e {
1443            Expr::Lit { value, .. } => Some(value),
1444            _ => None,
1445        }
1446    }
1447
1448    #[test]
1449    fn folds_date_dur_over_literals_and_date_methods_over_literal_receivers_and_arguments() {
1450        assert_eq!(
1451            lit(&fold("date(\"2026-01-05\")")),
1452            Some(&Value::from("2026-01-05"))
1453        );
1454        assert_eq!(
1455            lit(&fold("date(\"today\")")),
1456            Some(&Value::from("2026-09-27"))
1457        );
1458        assert_eq!(
1459            lit(&fold("dur(\"30 days\")")),
1460            Some(&Value::from(30.0 * 86400.0))
1461        );
1462        assert_eq!(
1463            lit(&fold("date(\"today\").minus(dur(\"30 days\"))")),
1464            Some(&Value::from("2026-08-28"))
1465        );
1466        assert_eq!(lit(&fold("date(\"x\")")), Some(&Value::Null));
1467        assert_eq!(lit(&fold("date(5)")), Some(&Value::Null));
1468        assert_eq!(lit(&fold("dur(60).dur_text()")), Some(&Value::from("1m")));
1469    }
1470
1471    #[test]
1472    fn leaves_a_non_literal_argument_an_unknown_name_and_a_wrong_arity_in_place() {
1473        for src in [
1474            "date(when)",
1475            "date(\"today\").until(when)",
1476            "size(\"x\")",
1477            "date()",
1478            "date(\"2026-01-01\").plus()",
1479            "\"x\".lower()",
1480        ] {
1481            assert!(matches!(fold(src), Expr::Call { .. }), "{src}");
1482        }
1483    }
1484
1485    #[test]
1486    fn the_runner_folds_at_its_clock_so_a_named_point_pushes_down() {
1487        let q = oqx::parse_string("from docs where updated_at < date(\"today\") && date(when) < date(\"today\").minus(dur(\"30 days\"))").unwrap();
1488        let printed = oqx::print_query(&rewrite_query(&q, NOW)).unwrap();
1489        assert!(printed.contains("updated_at < \"2026-09-27\""), "{printed}");
1490        assert!(printed.contains("date(when) < \"2026-08-28\""), "{printed}");
1491    }
1492
1493    #[test]
1494    fn path_literals_are_rooted_only_against_path_reads() {
1495        let q = oqx::parse_string(
1496            "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\"",
1497        )
1498        .unwrap();
1499        let printed = oqx::print_query(&rewrite_query(&q, 0)).unwrap();
1500        assert!(printed.contains("path.startsWith(\"/lab/\")"), "{printed}");
1501        assert!(printed.contains("doc.path == \"/a.md\""), "{printed}");
1502        assert!(printed.contains("type == \"a.md\""), "{printed}");
1503        assert!(printed.contains("^path == \"/b.md\""), "{printed}");
1504        assert!(printed.contains("\"/x.md\" != dst_path"), "{printed}");
1505        // a property compared with a path is the author's value: untouched
1506        assert!(printed.contains("path == ^after"), "{printed}");
1507        // `contains`/`endsWith`/`matches` and the empty string are literal
1508        let q = oqx::parse_string(
1509            "from docs where path.contains(\"a/\") && path.endsWith(\".md\") && path == \"\"",
1510        )
1511        .unwrap();
1512        let printed = oqx::print_query(&rewrite_query(&q, 0)).unwrap();
1513        assert!(printed.contains("path.contains(\"a/\")"), "{printed}");
1514        assert!(printed.contains("path.endsWith(\".md\")"), "{printed}");
1515        assert!(printed.contains("path == \"/\""), "{printed}");
1516    }
1517
1518    #[test]
1519    fn repo_and_bare_targets_are_refused_statically() {
1520        let parse = |s: &str| oqx::parse_string(s).unwrap();
1521        for q in [
1522            "from docs where $repo",
1523            "select r: size($repo.docs) from docs",
1524            "select r: $repo.id from docs",
1525            "select r: nodes first { select x: ^$repo.id values } from docs",
1526            "select r: nodes first { select x: 0^$repo values } from docs",
1527            "$repo.docs count { }",
1528        ] {
1529            let e = check_root_spellings(&parse(q)).unwrap_err();
1530            assert_eq!(e.code, "filter_invalid", "{q}");
1531            assert_eq!(e.message, REPO_REMOVED_MESSAGE, "{q}");
1532        }
1533        // a bare `docs`/`links` at depth ≥ 1; the hint counts the carets
1534        let e = check_root_spellings(&parse("select x: docs collect { } from docs")).unwrap_err();
1535        assert_eq!(e.message, bare_target_message(Target::Docs, Some(1)));
1536        assert!(
1537            e.message
1538                .contains("did you mean `^docs` (the repository's documents)?")
1539        );
1540        let e = check_root_spellings(&parse(
1541            "select x: nodes collect { select y: size(links) } from docs",
1542        ))
1543        .unwrap_err();
1544        assert!(
1545            e.message
1546                .contains("did you mean `^^links` (the repository's links)?")
1547        );
1548        let e = check_root_spellings(&parse("docs collect { from docs }")).unwrap_err();
1549        assert!(e.message.contains("`^docs`"));
1550        // the root scope is depth 0: a bare target there is the scan
1551        for q in [
1552            "docs count { }",
1553            "from docs where nodes exists { }",
1554            "entries(docs) first { }",
1555            "select n: size(^docs), id: ^id, k: size(0^links) from docs",
1556            "from blocks where blocks exists { }", // judged by the context at read time
1557        ] {
1558            check_root_spellings(&parse(q)).unwrap_or_else(|e| panic!("{q}: {e}"));
1559        }
1560        // a `^target` reaching past the root
1561        let e = check_root_spellings(&parse("^docs count { }")).unwrap_err();
1562        assert_eq!(e.message, past_root_message(Target::Docs, 1));
1563        assert_eq!(
1564            e.message,
1565            "`^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)`)"
1566        );
1567        let e = check_root_spellings(&parse("select path from docs where ^^links exists { }"))
1568            .unwrap_err();
1569        assert!(
1570            e.message.contains("`^^links` reaches past the root"),
1571            "{}",
1572            e.message
1573        );
1574        // the read-time message spells out the rule
1575        let m = bare_target_message(Target::Blocks, None);
1576        assert!(m.contains("did you mean `^blocks` (the repository's blocks; one caret per enclosing block, or the absolute `0^blocks`)?"), "{m}");
1577    }
1578
1579    #[test]
1580    fn reserved_projection_names_are_refused() {
1581        let parse = |s: &str| oqx::parse_string(s).unwrap();
1582        let e = check_reserved_aliases(&parse("select __oqx_id: 1 from docs")).unwrap_err();
1583        assert_eq!(e.code, "filter_invalid");
1584        assert_eq!(
1585            e.message,
1586            "projection `__oqx_id` uses the reserved prefix `__oqx_` — choose another alias"
1587        );
1588        assert_eq!(e.data.as_ref().unwrap()["hint"], "OQX");
1589        let e = check_reserved_aliases(&parse("select __omg_sel_0_x: 1 from docs")).unwrap_err();
1590        assert!(
1591            e.message.contains("reserved prefix `__omg_`"),
1592            "{}",
1593            e.message
1594        );
1595        // only the top level: the runner injects beside those items alone
1596        assert!(
1597            check_reserved_aliases(&parse(
1598                "select __oqx: 1, x: nodes collect { select __oqx_y: 1 } from docs"
1599            ))
1600            .is_ok()
1601        );
1602    }
1603
1604    #[test]
1605    fn semantic_phrases_are_collected_distinct() {
1606        let phrases = collect_semantic_phrases(
1607            "select s: semantic(\"alpha\") from docs where semantic(\"alpha\") > 0.5 || nodes exists { where semantic(\"beta\") > 0 } order by semantic(\"gamma\") desc",
1608        );
1609        assert_eq!(phrases, ["alpha", "beta", "gamma"]);
1610        assert!(collect_semantic_phrases("not a query {{").is_empty());
1611        assert!(collect_semantic_phrases("from docs").is_empty());
1612    }
1613
1614    #[test]
1615    fn hits_peel_the_injected_columns() {
1616        let mut o = oqx::Object::new();
1617        o.insert(ID_KEY, Value::Str("d_1".into()));
1618        o.insert(PATH_KEY, Value::Null);
1619        o.insert("layer", Value::Str("canon".into()));
1620        let shaped = shape_row(Value::Object(o)).unwrap();
1621        assert_eq!((shaped.id.as_str(), shaped.path.as_str()), ("d_1", ""));
1622        let hit = to_hit(shaped);
1623        let ho = hit.as_object().unwrap();
1624        assert_eq!(ho.keys().collect::<Vec<_>>(), ["id", "path", "layer"]);
1625        assert_eq!(ho.get("path"), Some(&Value::Str(String::new())));
1626        // A user projection of `id` collapses onto the envelope key in place
1627        // (3.0): same position, the projected value.
1628        let mut o = oqx::Object::new();
1629        o.insert(ID_KEY, Value::Str("d_1".into()));
1630        o.insert(PATH_KEY, Value::Str("a.md".into()));
1631        o.insert("id", Value::Number(7.0));
1632        let hit = to_hit(shape_row(Value::Object(o)).unwrap());
1633        let ho = hit.as_object().unwrap();
1634        assert_eq!(ho.keys().collect::<Vec<_>>(), ["id", "path"]);
1635        assert_eq!(ho.get("id"), Some(&Value::Number(7.0)));
1636        // The unshaped row's identity reads the same strings.
1637        let mut o = oqx::Object::new();
1638        o.insert(ID_KEY, Value::Str("d_1".into()));
1639        o.insert(PATH_KEY, Value::Undefined);
1640        let row = Value::Object(o);
1641        assert_eq!(raw_identity(&row), (String::new(), "d_1".to_owned()));
1642        let shaped = shape_row(row).unwrap();
1643        assert_eq!((shaped.path, shaped.id), (String::new(), "d_1".to_owned()));
1644    }
1645
1646    #[test]
1647    fn bare_hits_are_the_projection_in_its_own_order() {
1648        // `select title, id`: the projected intrinsic under the query's key, in
1649        // the query's order — nothing injected beside it.
1650        let mut o = oqx::Object::new();
1651        o.insert(ID_KEY, Value::Str("d_1".into()));
1652        o.insert(PATH_KEY, Value::Str("/a.md".into()));
1653        o.insert("title", Value::Str("T".into()));
1654        o.insert("id", Value::Str("d_1".into()));
1655        let shaped = shape_row(Value::Object(o)).unwrap();
1656        let bare = to_bare_hit(shaped);
1657        let bo = bare.as_object().unwrap();
1658        assert_eq!(bo.keys().collect::<Vec<_>>(), ["title", "id"]);
1659        // `select path: layer` is fine bare (no envelope to collide with) and
1660        // the identity the page runs on is still the row's own path.
1661        let mut o = oqx::Object::new();
1662        o.insert(ID_KEY, Value::Str("d_1".into()));
1663        o.insert(PATH_KEY, Value::Str("/a.md".into()));
1664        o.insert("path", Value::Str("canon".into()));
1665        let shaped = shape_row(Value::Object(o)).unwrap();
1666        assert_eq!(shaped.path, "/a.md");
1667        assert_eq!(
1668            render_hit(shaped, true),
1669            serde_json::json!({ "path": "canon" })
1670        );
1671        // the same row, enveloped: the user's value collapses onto the key
1672        let mut o = oqx::Object::new();
1673        o.insert(ID_KEY, Value::Str("d_1".into()));
1674        o.insert(PATH_KEY, Value::Str("/a.md".into()));
1675        o.insert("path", Value::Str("/a.md".into()));
1676        assert_eq!(
1677            render_hit(shape_row(Value::Object(o)).unwrap(), false),
1678            serde_json::json!({ "id": "d_1", "path": "/a.md" })
1679        );
1680    }
1681
1682    #[test]
1683    fn bare_needs_a_select_and_is_not_the_values_form() {
1684        let parse = |s: &str| oqx::parse_string(s).unwrap();
1685        for q in ["from docs", "from docs where layer == \"canon\""] {
1686            let e = check_bare(&parse(q)).unwrap_err();
1687            assert_eq!(e.code, "filter_invalid", "{q}");
1688            assert_eq!(e.message, BARE_NEEDS_SELECT_MESSAGE, "{q}");
1689            assert_eq!(e.data.as_ref().unwrap()["hint"], "OQX");
1690        }
1691        let e = check_bare(&parse("select path values from docs")).unwrap_err();
1692        assert_eq!(e.code, "filter_invalid");
1693        assert_eq!(e.message, BARE_WITH_VALUES_MESSAGE);
1694        for q in [
1695            "select path from docs",
1696            "select id, title from docs",
1697            "docs first { select t: title }",
1698        ] {
1699            check_bare(&parse(q)).unwrap_or_else(|e| panic!("{q}: {e}"));
1700        }
1701    }
1702
1703    #[test]
1704    fn a_projection_onto_the_envelope_must_be_aliased() {
1705        let items = |src: &str| oqx::parse_string(src).unwrap().select;
1706        // the attribute itself collapses (allowed)
1707        assert!(envelope_collision(&items("select id, path from docs")).is_none());
1708        assert!(
1709            envelope_collision(&items("select id: id, path: path, p: path from docs")).is_none()
1710        );
1711        // anything else landing on `id`/`path` is the error, first offender first
1712        let e = envelope_collision(&items("select data.path from docs")).unwrap();
1713        assert_eq!(e.code, "filter_invalid");
1714        assert_eq!(
1715            e.message,
1716            "projection `path` collides with the hit envelope — alias it (`p: data.path`)"
1717        );
1718        let e = envelope_collision(&items("select id: layer from docs")).unwrap();
1719        assert_eq!(
1720            e.message,
1721            "projection `id` collides with the hit envelope — alias it (`i: data.id`)"
1722        );
1723        assert!(envelope_collision(&items("select path: doc.path from blocks")).is_some());
1724        assert!(envelope_collision(&items("select path: nodes collect { } from docs")).is_some());
1725    }
1726
1727    #[test]
1728    fn a_path_read_is_the_attribute_or_a_row_handle_never_a_source_handle() {
1729        let pred = |src: &str| match oqx::parse_string(src).unwrap().r#where.unwrap() {
1730            oqx::ast::Where::Scalar { expr, .. } => expr,
1731            other => panic!("{other:?}"),
1732        };
1733        let left = |e: Expr| match e {
1734            Expr::Binary { left, .. } => *left,
1735            other => panic!("{other:?}"),
1736        };
1737        assert!(is_path_read(&left(pred("from docs where path == \"x\""))));
1738        assert!(is_path_read(&left(pred(
1739            "from blocks where doc.path == \"x\""
1740        ))));
1741        assert!(is_path_read(&left(pred("from docs where ^path == \"x\""))));
1742        assert!(is_path_read(&left(pred(
1743            "from links where dst_path == \"x\""
1744        ))));
1745        assert!(!is_path_read(&left(pred(
1746            "from docs where data.path == \"x\""
1747        ))));
1748        assert!(!is_path_read(&left(pred(
1749            "from docs where frontmatter.path == \"x\""
1750        ))));
1751    }
1752
1753    #[test]
1754    fn a_hit_that_is_not_a_store_row_fails_the_query() {
1755        // The row itself rides under SELF_KEY when the query can reach a
1756        // non-row; a string there is the `follow before` shape (§1.4, 1.5).
1757        let mut o = oqx::Object::new();
1758        o.insert(ID_KEY, Value::Undefined);
1759        o.insert(PATH_KEY, Value::Undefined);
1760        o.insert(SELF_KEY, Value::Str("/timeline/kickoff.md".into()));
1761        let e = shape_row(Value::Object(o)).unwrap_err();
1762        assert_eq!(e.code, "filter_invalid");
1763        assert_eq!(
1764            e.message,
1765            "a hit must be a document, block, node, link or revision row — the query reached a string (\"/timeline/kickoff.md\"); to follow document references held in a property use refs(<field>)"
1766        );
1767        // A tagged row under SELF_KEY passes and the key is peeled off.
1768        let mut row = oqx::Object::new();
1769        row.insert("doc_id", Value::Str("d_1".into()));
1770        row.insert("path", Value::Str("a.md".into()));
1771        let row = crate::context::tag_row(row, crate::context::Target::Docs);
1772        let mut o = oqx::Object::new();
1773        o.insert(ID_KEY, Value::Str("d_1".into()));
1774        o.insert(PATH_KEY, Value::Str("a.md".into()));
1775        o.insert(SELF_KEY, row);
1776        let hit = to_hit(shape_row(Value::Object(o)).unwrap());
1777        assert_eq!(
1778            hit.as_object().unwrap().keys().collect::<Vec<_>>(),
1779            ["id", "path"]
1780        );
1781        // Only queries that can reach a non-row project the row itself.
1782        let parse = |s: &str| oqx::parse_string(s).unwrap();
1783        assert!(!may_reach_non_rows(&parse(
1784            "from docs where layer == \"canon\""
1785        )));
1786        assert!(may_reach_non_rows(&parse("from docs follow before")));
1787        assert!(may_reach_non_rows(&parse("refs(\"/index.md\") first { }")));
1788    }
1789
1790    #[test]
1791    fn distinct_dedups_by_user_projection_first_wins() {
1792        let mk = |id: &str, t: &str| {
1793            let mut proj = oqx::Object::new();
1794            proj.insert("type", Value::Str(t.into()));
1795            Shaped {
1796                id: id.to_owned(),
1797                path: "p".to_owned(),
1798                proj,
1799            }
1800        };
1801        let out = dedup_by_projection(vec![mk("1", "a"), mk("2", "b"), mk("3", "a")], false);
1802        assert_eq!(out.len(), 2);
1803        assert_eq!(out[0].id, "1");
1804        assert_eq!(out[1].id, "2");
1805        // A projected `id` is the envelope's own selection: not part of the key
1806        // enveloped (`select distinct id` dedups on the empty projection), part
1807        // of it bare (there is no envelope — it is the projection).
1808        let with_id = |id: &str| {
1809            let mut proj = oqx::Object::new();
1810            proj.insert("id", Value::Str(id.into()));
1811            Shaped {
1812                id: id.to_owned(),
1813                path: "p".to_owned(),
1814                proj,
1815            }
1816        };
1817        assert_eq!(
1818            dedup_by_projection(vec![with_id("1"), with_id("2")], false).len(),
1819            1
1820        );
1821        assert_eq!(
1822            dedup_by_projection(vec![with_id("1"), with_id("2")], true).len(),
1823            2
1824        );
1825    }
1826
1827    #[test]
1828    fn the_page_runs_on_identity() {
1829        let rows: Vec<(String, String)> = [("/a.md", "d_1"), ("/a.md", "d_2"), ("/b.md", "d_3")]
1830            .iter()
1831            .map(|(p, i)| ((*p).to_owned(), (*i).to_owned()))
1832            .collect();
1833        let ident = |r: &(String, String)| (r.0.clone(), r.1.clone());
1834        // cap 2: truncated, the cursor is the last row's (path, id)
1835        let (page, truncated, next) =
1836            page_rows(rows.clone(), ident, 0, None, None, 2, false).unwrap();
1837        assert_eq!(page.len(), 2);
1838        assert!(truncated);
1839        let next = next.expect("a cursor");
1840        assert_eq!(next, encode_cursor(&["/a.md", "d_2"]));
1841        // the cursor resumes after it
1842        let (page, truncated, next) =
1843            page_rows(rows.clone(), ident, 0, None, Some(&next), 2, false).unwrap();
1844        assert_eq!(page, vec![("/b.md".to_owned(), "d_3".to_owned())]);
1845        assert!(!truncated && next.is_none());
1846        // the query's own offset/limit define the set; a custom order has no cursor
1847        let (page, truncated, next) =
1848            page_rows(rows.clone(), ident, 1, Some(1), None, 50, true).unwrap();
1849        assert_eq!(page, vec![("/a.md".to_owned(), "d_2".to_owned())]);
1850        assert!(!truncated && next.is_none());
1851        let (page, truncated, next) = page_rows(rows, ident, 0, None, None, 1, true).unwrap();
1852        assert_eq!(page.len(), 1);
1853        assert!(truncated && next.is_none());
1854    }
1855
1856    #[test]
1857    fn top_level_bounds_must_be_literals() {
1858        assert_eq!(const_bound(None, "limit").unwrap(), None);
1859        assert_eq!(
1860            const_bound(
1861                Some(&Expr::Lit {
1862                    value: Value::Number(3.0),
1863                    span: oqx::Span::EMPTY
1864                }),
1865                "limit"
1866            )
1867            .unwrap(),
1868            Some(3)
1869        );
1870        let e = const_bound(
1871            Some(&Expr::Lit {
1872                value: Value::Number(-1.0),
1873                span: oqx::Span::EMPTY,
1874            }),
1875            "offset",
1876        )
1877        .unwrap_err();
1878        assert_eq!(e.code, "filter_invalid");
1879        assert!(e.message.contains("top-level offset"));
1880    }
1881}