Skip to main content

omgbase_surface/
planner.rs

1//! The tier-3 pushdown planner: reduces the row set a query scans by
2//! translating its pushable top-level `where` conjuncts into ONE SQL
3//! statement against the store, then handing the produced rows (plus the
4//! untranslatable residual) back to the in-memory engine to finish. Port of
5//! `packages/core/src/oqx-js/planner.ts` over the [`oqx`] seam
6//! ([`QueryPlanner`], [`Plan`], [`oqx::partition_pushable`],
7//! [`oqx::residual_query`], [`oqx::ROWS_ROOT`]).
8//!
9//! Correctness is guaranteed by the residual fallback — anything
10//! [`crate::translate`] declines stays in-memory — and verified by the
11//! differential gate (planned == in-memory over every corpus-backed query
12//! case and the conformance list). Deliberately conservative: only simple
13//! top-level target scans (no `follow`, no `from E` re-projection) whose
14//! source is a bare `docs|blocks|nodes|links` or `<target>`, with at
15//! least one pushable scalar conjunct, are planned; everything else declines
16//! to a full in-memory run.
17//!
18//! One shape difference from the reference, forced by ownership: the
19//! reference attaches a store context to its plan (`makeStoreContext(…,
20//! rowsRoot)`), but a [`crate::StoreContext`] borrows the store's connection
21//! and `Plan::context` must be `'static`, so the plan carries no context and
22//! the runner ([`mod@crate::query`]) builds the rows-root context itself
23//! ([`crate::StoreContext::with_rows_root`]) — the same six lines
24//! `oqx::PlannedEngine::run` would execute.
25
26use oqx::Consumer;
27use oqx::ast::{Expr, Follow, FollowDestination, OpNode, Query, SelectItem, Subquery, Where};
28use oqx::{Plan, QueryPlanner, Value, partition_pushable, residual_query};
29use rusqlite::Connection;
30use rusqlite::types::Value as SqlValue;
31
32use crate::context::{REVISION_COLS, REVISION_FROM, Target, fetch_rows, tag_rows};
33use crate::translate::{TranslateCtx, translate_predicate};
34
35/// The SQL aliases of the scanned row (`self`) and its owning doc (`doc`).
36fn aliases(t: Target) -> (&'static str, &'static str) {
37    match t {
38        Target::Docs => ("d", "d"),
39        Target::Blocks => ("b", "d"),
40        Target::Nodes => ("n", "d"),
41        Target::Links => ("e", "d"),
42        Target::Revisions => ("r", "d"),
43    }
44}
45
46/// The scan's `FROM`. The `revisions` arms here and below (4.7) are the
47/// context's own scan spelled as clauses, so a caller keyed by target
48/// (`names::present`, a probe) can speak of the kind; the planner itself
49/// never plans a revisions scan ([`compile`]).
50pub(crate) fn from_clause(t: Target) -> &'static str {
51    match t {
52        Target::Docs => "docs d",
53        Target::Blocks => "blocks b JOIN docs d ON d.doc_id = b.doc_id",
54        Target::Nodes => "nodes n JOIN docs d ON d.doc_id = n.doc_id",
55        Target::Links => "edges e JOIN docs d ON d.doc_id = e.src_doc",
56        Target::Revisions => REVISION_FROM,
57    }
58}
59
60/// Row columns + the owning-doc path as `__path` (matches the context's
61/// roots so produced rows are indistinguishable from a full scan's).
62pub(crate) fn columns(t: Target) -> &'static str {
63    match t {
64        Target::Docs => "d.*",
65        Target::Blocks => "b.*, d.path AS __path",
66        Target::Nodes => "n.*, d.path AS __path",
67        Target::Links => "e.*, d.path AS __path",
68        Target::Revisions => REVISION_COLS,
69    }
70}
71
72/// The root order (`spec/surface` §1.1).
73pub(crate) fn order_clause(t: Target) -> &'static str {
74    match t {
75        Target::Docs => "d.path, d.doc_id",
76        Target::Blocks => "d.path, b.block_id",
77        Target::Nodes => "d.path, n.node_id",
78        Target::Links => "d.path, e.edge_id",
79        Target::Revisions => "r.path, r.rev_id",
80    }
81}
82
83/// The liveness guards of the root scan, with the repo id bound. The history
84/// has no liveness: every revision of every document of the repository,
85/// tombstoned ones included (§1.1, 4.7) — the repository guard through the
86/// owning `docs` row, since `revisions` carries no `repo_id`.
87pub(crate) fn guards(t: Target) -> &'static str {
88    match t {
89        Target::Docs => "d.repo_id = ? AND d.deleted_commit IS NULL",
90        Target::Blocks => "b.repo_id = ? AND b.deleted_commit IS NULL AND d.deleted_commit IS NULL",
91        Target::Nodes => "n.repo_id = ? AND d.deleted_commit IS NULL",
92        Target::Links => "e.repo_id = ? AND e.to_commit IS NULL AND d.deleted_commit IS NULL",
93        Target::Revisions => "d.repo_id = ?",
94    }
95}
96
97/// The joined targets driven FROM the document: the same rows and columns as
98/// [`from_clause`], with the loop order fixed by `CROSS JOIN` so a predicate on
99/// the document (`d.path = ?`) is the outer search and the target's rows are
100/// reached through their `doc_id` / `src_doc` index (the store indexes'
101/// `path` probes). [`guards_by_doc`] goes with it; `Docs` is `from_clause`.
102pub(crate) fn from_by_doc(t: Target) -> &'static str {
103    match t {
104        Target::Docs | Target::Revisions => from_clause(t),
105        Target::Blocks => "docs d CROSS JOIN blocks b ON b.doc_id = d.doc_id",
106        Target::Nodes => "docs d CROSS JOIN nodes n ON n.doc_id = d.doc_id",
107        Target::Links => "docs d CROSS JOIN edges e ON e.src_doc = d.doc_id",
108    }
109}
110
111/// [`guards`] for a [`from_by_doc`] statement: the same tests, with the
112/// document's repo first and the target's repo term behind SQLite's unary `+`
113/// so it stays a filter and never selects a `(repo_id, …)` index over the
114/// `doc_id` one. Two repo-id binds (`Docs`: one, as `guards`).
115pub(crate) fn guards_by_doc(t: Target) -> &'static str {
116    match t {
117        Target::Docs | Target::Revisions => guards(t),
118        Target::Blocks => {
119            "d.repo_id = ? AND +b.repo_id = ? AND b.deleted_commit IS NULL AND d.deleted_commit IS NULL"
120        }
121        Target::Nodes => "d.repo_id = ? AND +n.repo_id = ? AND d.deleted_commit IS NULL",
122        Target::Links => {
123            "d.repo_id = ? AND +e.repo_id = ? AND e.to_commit IS NULL AND d.deleted_commit IS NULL"
124        }
125    }
126}
127
128/// The root collection a query scans, if it is a bare
129/// `docs|blocks|nodes|links|revisions` source at the root scope (else `None` —
130/// a nested `^docs` is a block's receiver, never the query's source). The
131/// runner asks too: a bare root scan's rows are store rows by construction.
132/// Five-wide (the reference's `ROOTS` test); [`compile`] plans four — a
133/// `revisions` scan (4.7) always runs in memory.
134pub(crate) fn root_target(source: &Expr) -> Option<Target> {
135    match source {
136        Expr::Ident { name, .. } => Target::parse(name),
137        _ => None,
138    }
139}
140
141/// Whether `name` is a relation of `target`'s rows that is spelled like a
142/// target (§1.2: `docs.nodes`, `docs.blocks`, `docs.revisions` (4.7),
143/// `blocks.nodes`, `section.blocks`).
144fn is_target_relation_of(target: Target, name: &str) -> bool {
145    matches!(
146        (target, name),
147        (Target::Docs, "nodes" | "blocks" | "revisions")
148            | (Target::Blocks, "nodes")
149            | (Target::Nodes, "blocks")
150    )
151}
152
153/// The row a reach-through head reaches (`doc.x`, `block.x`, `section.x`,
154/// and a link's `dst.x`).
155fn reach_through(head: &str) -> Option<Target> {
156    match head {
157        "doc" | "dst" => Some(Target::Docs),
158        "block" => Some(Target::Blocks),
159        "section" => Some(Target::Nodes),
160        _ => None,
161    }
162}
163
164// ---- the residual-error decline (surface 1.1 patch, §1) ---------------------------------
165
166/// Whether a residual `where` could raise an OQX eval error the pushed
167/// conjuncts might hide by emptying the scan. In memory every conjunct of the
168/// original `&&` is evaluated for the first row and a throwing one aborts the
169/// run; planned, a pushed conjunct that matches no row means the residual
170/// never runs and the error vanishes. So a residual containing any function
171/// or method call (an unknown function, a bad regex, a wrong arity…), a
172/// nested block with the `single` consumer (more than one row raises), a
173/// `^`-escaped name (no enclosing scope at the top level) sends the whole
174/// query to the in-memory engine unplanned (before this batch of 3.0 a bare
175/// reserved docs basename did too; the attributes are bare now and never
176/// raise). Only comparisons, logical
177/// operators, `in`, `!`, literals, bindings and plain reads keep the push.
178///
179/// The walk is generic over the whole `oqx` AST — every `Where` node, every
180/// nested block (its `from`, `where`, `select`, `order by`, `follow`,
181/// `limit` / `offset`) and every `Expr`. A `^name:` lift in a nested select
182/// counts as a `^`-escaped name. Same decisions as the reference's
183/// `residualMayRaise`.
184fn residual_may_raise(w: &Where, target: Target) -> bool {
185    where_may_raise(w, target, true)
186}
187
188fn where_may_raise(w: &Where, target: Target, root: bool) -> bool {
189    match w {
190        Where::And { parts, .. } | Where::Or { parts, .. } => {
191            parts.iter().any(|p| where_may_raise(p, target, root))
192        }
193        Where::Not { expr, .. } => where_may_raise(expr, target, root),
194        Where::Scalar { expr, .. } => expr_may_raise(expr, target, root),
195        Where::Op(op) => op_may_raise(op, target, root),
196    }
197}
198
199fn op_may_raise(op: &OpNode, target: Target, root: bool) -> bool {
200    op.op == Consumer::Single
201        || expr_may_raise(&op.receiver, target, root)
202        || subquery_may_raise(&op.sub, target)
203}
204
205fn subquery_may_raise(sub: &Subquery, target: Target) -> bool {
206    let inner = |e: &Expr| expr_may_raise(e, target, false);
207    sub.from.iter().any(inner)
208        || sub
209            .r#where
210            .as_ref()
211            .is_some_and(|w| where_may_raise(w, target, false))
212        || sub.select.iter().any(|item| match item {
213            SelectItem::Field { expr, lift, .. } => *lift > 0 || inner(expr),
214            SelectItem::Collect { op, .. } => op_may_raise(op, target, false),
215        })
216        || sub.order_by.iter().flatten().any(|o| inner(&o.expr))
217        || sub
218            .follow
219            .as_ref()
220            .is_some_and(|f| follow_may_raise(f, target))
221        || sub.frontier.as_ref().is_some_and(inner)
222        // A grouped block (oqx 0.24) evaluates its keys, `having` and its
223        // aggregates (`sum` over a string raises) per group.
224        || sub
225            .group_by
226            .as_ref()
227            .is_some_and(|g| g.keys.iter().any(|k| inner(&k.expr)))
228        || sub
229            .having
230            .as_ref()
231            .is_some_and(|h| where_may_raise(h, target, false))
232        || sub.limit.as_ref().is_some_and(inner)
233        || sub.offset.as_ref().is_some_and(inner)
234}
235
236// A destination is a relation read in the frontier row's scope, or a destination
237// block (whose receiver, body, `frontier` and `depth` are its own — oqx 0.20).
238fn follow_may_raise(f: &Follow, target: Target) -> bool {
239    let inner = |e: &Expr| expr_may_raise(e, target, false);
240    f.destinations.iter().any(|d| match d {
241        FollowDestination::Relation(e) => inner(e),
242        FollowDestination::Block(op) => op_may_raise(op, target, false),
243    })
244}
245
246fn expr_may_raise(e: &Expr, target: Target, root: bool) -> bool {
247    let again = |e: &Expr| expr_may_raise(e, target, root);
248    match e {
249        Expr::Lit { .. } | Expr::Binding { .. } => false,
250        // A bare target name read as a property raises (the root-row rule,
251        // `crate::query`) unless it is a relation of the rows at hand: known at
252        // the root scope, unknown below.
253        Expr::Ident { name, .. } if Target::parse(name).is_some() => {
254            !root || !is_target_relation_of(target, name)
255        }
256        // A bare attribute never raises (3.0: there is no loud-error set).
257        Expr::Ident { .. } => false,
258        Expr::Outer { .. } | Expr::Call { .. } => true,
259        // `<head>.<target>` raises unless the head is a reach-through whose row
260        // has that relation.
261        Expr::Member { recv, name, .. } => {
262            (Target::parse(name).is_some()
263                && !matches!(&**recv, Expr::Ident { name: head, .. }
264                        if reach_through(head).is_some_and(|t| is_target_relation_of(t, name))))
265                || again(recv)
266        }
267        Expr::Unary { expr, .. } => again(expr),
268        // `x!` raises when absent; a value-position directive may `single`-fail
269        // or read anything its block reads.
270        Expr::Required { .. } => true,
271        Expr::Op(op) => op_may_raise(op, target, root),
272        // A regex operator (oqx 0.22) is a `matches` call: an invalid pattern
273        // raises, exactly as the call would.
274        Expr::Binary {
275            op, left, right, ..
276        } => op.is_regex() || again(left) || again(right),
277        Expr::Logical { left, right, .. } | Expr::In { left, right, .. } => {
278            again(left) || again(right)
279        }
280        Expr::Range { lo, hi, .. } => {
281            lo.as_deref().is_some_and(again) || hi.as_deref().is_some_and(again)
282        }
283        // A tuple (oqx 0.20) is never pushed down; it raises iff an element does.
284        Expr::Tuple { items, .. } => items.iter().any(again),
285        // A template literal (oqx 0.21) is never pushed down; interpolating a
286        // list or a record is an eval error only the data decides, so with any
287        // interpolation it may raise.
288        Expr::Template { parts, .. } => oqx::walk::template_exprs(parts).next().is_some(),
289        // An aggregate (oqx 0.24) raises on a non-number under `sum`/`avg`,
290        // which only the data decides; a grouped query is never planned anyway
291        // (`compile`).
292        Expr::Aggregate { .. } => true,
293    }
294}
295
296/// A compiled plan before execution: the statement, its params and the
297/// residual query. Pure — what the planner would run, for inspection.
298#[derive(Clone, Debug, PartialEq)]
299pub struct Compiled {
300    pub target: Target,
301    pub sql: String,
302    pub params: Vec<SqlValue>,
303    pub residual: Query,
304}
305
306/// Compile `query` against `repo_id`, or `None` when the shape declines: a
307/// `follow`, a `from E` re-projection, a `group by` (oqx 0.24 — declined
308/// whole: its `where` conjuncts could be pushed, grouping happens after them,
309/// but a pushed conjunct that empties the scan would hide an eval error an
310/// aggregate or `having` raises in memory, so decline (c) extends to the
311/// post-group clauses; SQL `GROUP BY` pushdown is a later round), a source
312/// that is not a root scan, or no pushable conjunct at all (the engine then
313/// does everything).
314#[must_use]
315pub fn compile(query: &Query, params: &[Value], repo_id: &str) -> Option<Compiled> {
316    if query.follow.is_some() || !query.from.is_empty() || query.group_by.is_some() {
317        return None;
318    }
319    let target = root_target(&query.source)?;
320    // The history (4.7) is never planned: no store index, no pushdown — the
321    // scan runs in memory, as the reference's four-wide `rootTarget` decides.
322    if target == Target::Revisions {
323        return None;
324    }
325    let (self_alias, doc_alias) = aliases(target);
326    let ctx = TranslateCtx {
327        target,
328        self_alias,
329        doc_alias,
330        params,
331    };
332    let (pushed, residual) = partition_pushable(query.r#where.as_ref(), |e| {
333        translate_predicate(e, &ctx).is_some()
334    });
335    if pushed.is_empty() {
336        return None;
337    }
338    // Decline (c): a residual that could raise must not be hidden behind an
339    // emptied scan — the whole query runs in memory.
340    if residual
341        .as_ref()
342        .is_some_and(|w| residual_may_raise(w, target))
343    {
344        return None;
345    }
346    let mut where_sql = guards(target).to_owned();
347    let mut sql_params = vec![SqlValue::Text(repo_id.to_owned())];
348    for e in &pushed {
349        let frag = translate_predicate(e, &ctx).expect("accepted by partition_pushable");
350        where_sql.push_str(" AND (");
351        where_sql.push_str(&frag.sql);
352        where_sql.push(')');
353        sql_params.extend(frag.params);
354    }
355    let sql = format!(
356        "SELECT {} FROM {} WHERE {where_sql} ORDER BY {}",
357        columns(target),
358        from_clause(target),
359        order_clause(target)
360    );
361    Some(Compiled {
362        target,
363        sql,
364        params: sql_params,
365        residual: residual_query(query, residual),
366    })
367}
368
369/// The SQLite planner over one repo of the store.
370pub struct SqlitePlanner<'a> {
371    conn: &'a Connection,
372    repo_id: String,
373}
374
375impl<'a> SqlitePlanner<'a> {
376    #[must_use]
377    pub fn new(conn: &'a Connection, repo_id: &str) -> Self {
378        Self {
379            conn,
380            repo_id: repo_id.to_owned(),
381        }
382    }
383
384    /// Plan `query`: `Ok(None)` when the shape declines, `Ok(Some(plan))`
385    /// with the produced rows (tagged like the context's root rows) and the
386    /// residual, `Err` when the statement itself failed (a store error, not
387    /// a decline — the runner reports it rather than silently rescanning).
388    pub fn try_plan(&self, query: &Query, params: &[Value]) -> rusqlite::Result<Option<Plan>> {
389        let Some(compiled) = compile(query, params, &self.repo_id) else {
390            return Ok(None);
391        };
392        let rows = fetch_rows(self.conn, &compiled.sql, &compiled.params)?;
393        Ok(Some(Plan::new(
394            tag_rows(rows, compiled.target),
395            compiled.residual,
396        )))
397    }
398}
399
400impl QueryPlanner for SqlitePlanner<'_> {
401    /// The seam's shape: a failed statement declines (the engine falls back
402    /// to a full scan). The runner uses [`Self::try_plan`] to surface it.
403    fn plan(&self, query: &Query, params: &[Value]) -> Option<Plan> {
404        self.try_plan(query, params).ok().flatten()
405    }
406}
407
408#[cfg(test)]
409mod tests {
410    use super::*;
411    use oqx::ROWS_ROOT;
412    use oqx::ast::Where;
413
414    fn parse(src: &str) -> Query {
415        oqx::parse_string(src).expect("parses")
416    }
417
418    fn text(s: &str) -> SqlValue {
419        SqlValue::Text(s.to_owned())
420    }
421
422    #[test]
423    fn a_pushable_scan_compiles_to_one_statement_in_root_order() {
424        let c =
425            compile(&parse("from docs where path == \"index.md\""), &[], "r_1").expect("planned");
426        assert_eq!(c.target, Target::Docs);
427        assert_eq!(
428            c.sql,
429            "SELECT d.* FROM docs d WHERE d.repo_id = ? AND d.deleted_commit IS NULL AND ((('/' || d.path) IS ?)) ORDER BY d.path, d.doc_id"
430        );
431        assert_eq!(c.params, vec![text("r_1"), text("index.md")]);
432        assert_eq!(
433            c.residual.source,
434            Expr::Ident {
435                name: ROWS_ROOT.to_owned(),
436                span: oqx::Span::EMPTY,
437            }
438        );
439        assert_eq!(c.residual.r#where, None);
440    }
441
442    #[test]
443    fn every_target_has_its_join_columns_guards_and_order() {
444        let b = compile(
445            &parse("from blocks where path.startsWith(\"lab/\")"),
446            &[],
447            "r",
448        )
449        .unwrap();
450        assert_eq!(
451            b.sql,
452            "SELECT b.*, d.path AS __path FROM blocks b JOIN docs d ON d.doc_id = b.doc_id \
453             WHERE b.repo_id = ? AND b.deleted_commit IS NULL AND d.deleted_commit IS NULL \
454             AND ((substr(('/' || d.path), 1, length(?)) = ?)) ORDER BY d.path, b.block_id"
455        );
456        assert_eq!(b.params, vec![text("r"), text("lab/"), text("lab/")]);
457        let n = compile(
458            &parse("nodes count { where kind == \"md:task\" }"),
459            &[],
460            "r",
461        )
462        .unwrap();
463        assert_eq!(n.target, Target::Nodes);
464        assert!(n.sql.starts_with(
465            "SELECT n.*, d.path AS __path FROM nodes n JOIN docs d ON d.doc_id = n.doc_id WHERE n.repo_id = ? AND d.deleted_commit IS NULL AND ((n.kind IS ?))"
466        ));
467        assert!(n.sql.ends_with("ORDER BY d.path, n.node_id"));
468        let e = compile(
469            &parse("from links where predicate == \"references\""),
470            &[],
471            "r",
472        )
473        .unwrap();
474        assert!(e.sql.starts_with(
475            "SELECT e.*, d.path AS __path FROM edges e JOIN docs d ON d.doc_id = e.src_doc WHERE e.repo_id = ? AND e.to_commit IS NULL AND d.deleted_commit IS NULL AND ((e.predicate IS ?))"
476        ));
477        assert!(e.sql.ends_with("ORDER BY d.path, e.edge_id"));
478    }
479
480    #[test]
481    fn mixed_conjunctions_push_the_translatable_parts_and_keep_the_rest() {
482        let q = parse(
483            "from docs where path.startsWith(\"processes/\") && nodes exists { where kind == \"md:task\" } && layer == \"canon\"",
484        );
485        let c = compile(&q, &[], "r").expect("planned");
486        assert!(
487            c.sql
488                .contains("(substr(('/' || d.path), 1, length(?)) = ?)"),
489            "{}",
490            c.sql
491        );
492        assert!(c.sql.contains("p.key = 'layer'"), "{}", c.sql);
493        // Guards first, then the fragments' params in statement order.
494        assert_eq!(
495            c.params,
496            vec![
497                text("r"),
498                text("processes/"),
499                text("processes/"),
500                text("canon")
501            ]
502        );
503        // One conjunct left → it stands alone, not wrapped in an `and`.
504        assert!(
505            matches!(c.residual.r#where, Some(Where::Op(_))),
506            "{:?}",
507            c.residual.r#where
508        );
509        assert!(c.residual.from.is_empty());
510        assert_eq!(c.residual.select, q.select);
511        assert_eq!(c.residual.consumer, q.consumer);
512    }
513
514    #[test]
515    fn declined_shapes_return_none() {
516        // nothing pushable → let the engine do it all
517        assert!(compile(&parse("from docs"), &[], "r").is_none());
518        assert!(compile(&parse("from docs where era in 800..1680"), &[], "r").is_none());
519        assert!(compile(&parse("from docs where !verified"), &[], "r").is_none());
520        assert!(
521            compile(
522                &parse("from docs where path == \"a\" || path == \"b\""),
523                &[],
524                "r"
525            )
526            .is_none()
527        );
528        assert!(
529            compile(
530                &parse("from docs where nodes exists { where kind == \"md:task\" }"),
531                &[],
532                "r"
533            )
534            .is_none()
535        );
536        // follow
537        assert!(
538            compile(
539                &parse("from docs where path == \"a.md\" follow distinct out_links collect { from dst }"),
540                &[],
541                "r"
542            )
543            .is_none()
544        );
545        // follow with several destinations: relations and a destination block
546        // decline exactly like a single relation (0.14)
547        assert!(
548            compile(
549                &parse("from docs where path == \"a.md\" follow out_links collect { from dst }, in_links collect { from doc }"),
550                &[],
551                "r"
552            )
553            .is_none()
554        );
555        assert!(
556            compile(
557                &parse(
558                    "from docs where path == \"a.md\" follow ^docs collect { where after.contains(^path) }"
559                ),
560                &[],
561                "r"
562            )
563            .is_none()
564        );
565        // a body-level `frontier` (oqx 0.20) plans like the `follow` it needs
566        assert!(
567            compile(
568                &parse("from docs where path == \"a.md\" frontier type == \"hub\" follow distinct out_links collect { from dst frontier type == \"practitioner\" }"),
569                &[],
570                "r"
571            )
572            .is_none()
573        );
574        // a grouped query is declined whole (oqx 0.24), pushable `where` or not
575        assert!(
576            compile(
577                &parse("select layer, n: count() from docs where path == \"a.md\" group by layer"),
578                &[],
579                "r"
580            )
581            .is_none()
582        );
583        // not a root scan
584        assert!(compile(&parse("from things where path == \"a.md\""), &[], "r").is_none());
585        assert!(compile(&parse("from nope where path == \"a.md\""), &[], "r").is_none());
586        assert!(compile(&parse("from docs.nodes where kind == \"x\""), &[], "r").is_none());
587        // a `from E` re-projection (an AST-level shape)
588        let mut q = parse("from docs where path == \"a.md\"");
589        q.from.push(Expr::Ident {
590            name: "nodes".to_owned(),
591            span: oqx::Span::EMPTY,
592        });
593        assert!(compile(&q, &[], "r").is_none());
594    }
595
596    // -- decline (c): a residual that could raise sends the whole query in memory --
597
598    #[test]
599    fn a_residual_that_could_raise_declines_the_whole_query() {
600        let declined = |src: &str| {
601            assert!(
602                compile(&parse(src), &[], "r").is_none(),
603                "should decline: {src}"
604            );
605        };
606        let planned = |src: &str| {
607            assert!(
608                compile(&parse(src), &[], "r").is_some(),
609                "should plan: {src}"
610            );
611        };
612        // a bare attribute is never a raise (3.0: no loud-error set) — `path`
613        // pushes, `raw`/`updated_at`/`tags` are plain residuals
614        planned("from docs where path == \"x\" && path == \"nope.md\"");
615        planned("from docs where path == \"nope.md\" && !raw");
616        planned("from docs where path == \"nope.md\" && updated_at");
617        planned("from docs where path == \"nope.md\" && tags");
618        // `doc.<attribute>` on any target, at any depth, is a plain read too
619        planned("from blocks where path == \"x\" && doc.path == \"y\"");
620        planned("from blocks where path == \"x\" && nodes exists { where doc.path == \"y\" }");
621        // a function or method call
622        declined("from docs where path.matches(\"[\") && path == \"nope.md\"");
623        declined("from docs where nope(\"x\") && path == \"nope.md\"");
624        declined("from docs where path == \"x\" && size(tags) > 1");
625        declined("from docs where path == \"x\" && nodes exists { where name.lower() == \"a\" }");
626        declined(
627            "from docs where path == \"x\" && nodes count { where kind == \"a\" order by size(name) } > 1",
628        );
629        // a `^`-escaped name: an outer reference or a lift
630        declined("from docs where path == \"x\" && ^slug == \"y\"");
631        declined("from docs where path == \"x\" && nodes exists { where name == ^title }");
632        declined("from docs where path == \"x\" && nodes collect { ^first_task: name }");
633        // a nested block with the `single` consumer
634        declined(
635            "from docs where path == \"x\" && blocks exists { select t: nodes single { where kind == \"md:task\" } }",
636        );
637        // the pushed side raising is impossible; only the residual matters
638        planned("from docs where path.startsWith(\"lab/\") && path == \"x\"");
639        // plain reads, comparisons, `in`, `!`, literals and bindings keep the push
640        planned("from docs where path == \"x\" && era in 800..1680");
641        planned("from docs where path == \"x\" && !verified");
642        planned("from docs where path == \"x\" && (layer == \"a\" || layer == \"b\")");
643        planned("from docs where path == \"x\" && verified == true");
644        planned("from docs where path == \"x\" && nodes exists { where kind == \"md:task\" }");
645        planned(
646            "from docs where path == \"x\" && nodes count { where kind == \"md:task\" limit 5 } > 1",
647        );
648        // on blocks a bare `path` is an attribute, and inside a block the rows
649        // are another scope — an ordinary read, not the guard
650        planned("from blocks where path == \"x\" && !path");
651        planned("from docs where path == \"x\" && nodes exists { where path == \"y\" }");
652        planned("from docs where path == \"x\" && frontmatter.path == \"y\"");
653        // a key read through a source handle is pushed (3.0), scoped to its
654        // source; a WHOLE handle reference and `entries(…)` stay residual (and
655        // the function call declines the query)
656        let fm = compile(
657            &parse("from docs where frontmatter.layer == \"canon\""),
658            &[],
659            "r",
660        )
661        .expect("planned");
662        assert!(fm.sql.contains("p.source = 'frontmatter'"), "{}", fm.sql);
663        let data =
664            compile(&parse("from docs where data.layer == \"canon\""), &[], "r").expect("planned");
665        assert!(!data.sql.contains("p.source"), "{}", data.sql);
666        planned("from blocks where doc.inline.element == \"fire\"");
667        planned("from docs where path == \"x\" && frontmatter == null");
668        declined("from docs where path == \"x\" && entries(frontmatter) exists { }");
669    }
670
671    #[test]
672    fn a_top_level_not_or_or_is_the_whole_residual_and_declines() {
673        // `!(…)` and `||` at the top are `Where::Not` / `Where::Or`, never scalar
674        // parts, so nothing is pushed even when their leaves would be.
675        assert!(compile(&parse("from docs where !(path == \"a\")"), &[], "r").is_none());
676        assert!(
677            compile(
678                &parse("from docs where (path == \"a\" || path == \"b\") && layer == \"canon\""),
679                &[],
680                "r"
681            )
682            .is_some()
683        );
684    }
685}