Skip to main content

omgbase_surface/
context.rs

1//! The query binding (`spec/surface/README.md` §1): a [`DataContext`] over
2//! the store, so the `oqx` in-memory engine reproduces the whole OQX surface
3//! — roots, intrinsics, reach-through, structural relations, the edge graph,
4//! the row functions — without a bespoke compiler. Port of
5//! `packages/core/src/oqx-js/context.ts`.
6//!
7//! Rows are the store's raw column objects (blobs as hex) carrying a hidden
8//! tag column ([`TAG_KEY`]) naming their target; `get` routes field /
9//! intrinsic / relation resolution per target, lazily querying the store.
10//! Row functions (`text`, `under`, …) arrive as methods on the `$self`
11//! receiver (see the runner's AST rewrite), since a free function sees no row.
12//!
13//! The tier-3 planner ([`crate::planner`]) hands the rows its SQL produced
14//! back through [`StoreContext::with_rows_root`]: the context then serves them
15//! as the residual query's [`oqx::ROWS_ROOT`] scan, while every other root,
16//! relation, intrinsic and row function still reaches the store — the
17//! reference's `rowsRoot` context option.
18//!
19//! Errors travel the engine's channel: a failure inside a property read or a
20//! row function — the reserved-basename guard, a store failure — is the
21//! `Err` of `get` / `call_method` (an eval-stage [`OqxError`], since `oqx`
22//! 0.13), which aborts the run exactly like a throw from the reference's
23//! `get`; the runner maps it to `filter_invalid` with the same message. The
24//! one seam still without a channel is `root` (the engine reads a named root
25//! for the top-level source and for a caret that reaches the root scope), so
26//! a store failure during a root scan is kept in [`StoreContext::take_root_failure`]
27//! and the runner reports it after the run.
28//!
29//! One seam differs from the reference and is bridged here:
30//!
31//! * the Rust engine expands `entries(x)` in row position itself (never via
32//!   `call_function`), so the `frontmatter` / `inline` source handles are
33//!   materialized eagerly as plain objects — one key per top-level property
34//!   in key order, valued by the scalar-vs-list rule — instead of the
35//!   reference's lazy handle. `frontmatter.<k>` and `entries(frontmatter)`
36//!   read the same values either way.
37
38use std::cell::RefCell;
39use std::collections::HashMap;
40
41use omgbase_properties::Bound;
42use omgbase_search::{cosine_bytes, sanitize_fts_query};
43use oqx::semantics::{builtin_function, builtin_method_with, make_range};
44use oqx::{DataContext, Object, OqxError, RegexDialect, Value};
45use rusqlite::types::{Value as SqlValue, ValueRef};
46use rusqlite::{Connection, OptionalExtension, params_from_iter};
47
48/// The hidden column tagging a store row with its target.
49pub const TAG_KEY: &str = "__oqx_target";
50const REPO_TAG: &str = "$repo";
51
52/// The four scan targets.
53#[derive(Clone, Copy, Debug, PartialEq, Eq)]
54pub enum Target {
55    Docs,
56    Blocks,
57    Nodes,
58    Edges,
59}
60
61impl Target {
62    /// The root name: `docs` | `blocks` | `nodes` | `edges`.
63    #[must_use]
64    pub fn as_str(self) -> &'static str {
65        match self {
66            Target::Docs => "docs",
67            Target::Blocks => "blocks",
68            Target::Nodes => "nodes",
69            Target::Edges => "edges",
70        }
71    }
72
73    /// The target a root name denotes, if any.
74    #[must_use]
75    pub fn parse(s: &str) -> Option<Self> {
76        Some(match s {
77            "docs" => Target::Docs,
78            "blocks" => Target::Blocks,
79            "nodes" => Target::Nodes,
80            "edges" => Target::Edges,
81            _ => return None,
82        })
83    }
84}
85
86/// docs intrinsics whose BARE form is almost always a typo: a loud error
87/// ("did you mean the intrinsic").
88const RESERVED_DOC_BASENAMES: [&str; 5] = ["id", "path", "updated_at", "content_hash", "body"];
89
90/// A query phrase's embedding: the model whose cache to read and the vector
91/// as a float32 little-endian blob.
92#[derive(Clone, Debug, PartialEq)]
93pub struct SemanticVec {
94    pub model: String,
95    pub vec: Vec<u8>,
96}
97
98/// The store-backed context for one repo.
99pub struct StoreContext<'a> {
100    conn: &'a Connection,
101    repo_id: String,
102    semantic: HashMap<String, SemanticVec>,
103    /// A store failure inside [`DataContext::root`], the one read the
104    /// engine's seam cannot fail through (see the module doc).
105    root_failure: RefCell<Option<OqxError>>,
106    /// The rows a tier-3 plan produced, served as [`oqx::ROWS_ROOT`].
107    rows_root: Option<Vec<Value>>,
108}
109
110fn sql_value(v: ValueRef<'_>) -> Value {
111    match v {
112        ValueRef::Null => Value::Null,
113        ValueRef::Integer(i) => Value::Number(i as f64),
114        ValueRef::Real(f) => Value::Number(f),
115        ValueRef::Text(t) => Value::Str(String::from_utf8_lossy(t).into_owned()),
116        ValueRef::Blob(b) => Value::Str(omgbase_format::hash::hex(b)),
117    }
118}
119
120/// A [`Value`] as a SQL parameter (`has_edge`'s destination, the planner's
121/// bound operands): booleans as 1/0 — how `json_extract` surfaces JSON
122/// booleans, so `attrs.b == true` compares against `1` — numbers as REAL
123/// (a JavaScript number binds as a double), absent as NULL.
124pub(crate) fn to_sql(v: &Value) -> SqlValue {
125    match v {
126        Value::Undefined | Value::Null | Value::Range(_) => SqlValue::Null,
127        Value::Bool(b) => SqlValue::Integer(i64::from(*b)),
128        Value::Number(n) => SqlValue::Real(*n),
129        Value::Str(s) => SqlValue::Text(s.clone()),
130        Value::Array(_) | Value::Object(_) => SqlValue::Text(v.to_string()),
131    }
132}
133
134/// JavaScript `String(v)` of an argument.
135fn js_string(v: &Value) -> String {
136    v.to_string()
137}
138
139/// `String(args[0] ?? "")`.
140fn arg_or_empty(args: &[Value], i: usize) -> String {
141    match args.get(i) {
142        None | Some(Value::Undefined) | Some(Value::Null) => String::new(),
143        Some(v) => js_string(v),
144    }
145}
146
147/// `JSON.parse` when a string, else the value (`null` → absent).
148fn parse_json(v: &Value) -> Value {
149    match v {
150        Value::Str(s) => {
151            serde_json::from_str::<serde_json::Value>(s).map_or_else(|_| v.clone(), Value::from)
152        }
153        Value::Null | Value::Undefined => Value::Undefined,
154        other => other.clone(),
155    }
156}
157
158/// A store failure as the engine's eval error (the message the reference's
159/// raw exception would carry).
160fn sql_err(e: rusqlite::Error) -> OqxError {
161    OqxError::eval(format!("sqlite: {e}"))
162}
163
164/// The tag of a store row, if it is one.
165pub fn target_of(row: &Value) -> Option<Target> {
166    row.as_object()
167        .and_then(|o| o.get(TAG_KEY))
168        .and_then(Value::as_str)
169        .and_then(Target::parse)
170}
171
172fn is_repo_root(row: &Value) -> bool {
173    row.as_object()
174        .and_then(|o| o.get(TAG_KEY))
175        .and_then(Value::as_str)
176        == Some(REPO_TAG)
177}
178
179fn col<'v>(row: &'v Value, key: &str) -> &'v Value {
180    row.as_object()
181        .and_then(|o| o.get(key))
182        .unwrap_or(&Value::Undefined)
183}
184
185fn col_str(row: &Value, key: &str) -> String {
186    match col(row, key) {
187        Value::Undefined | Value::Null => String::new(),
188        v => js_string(v),
189    }
190}
191
192/// Strip the hidden tag from a value tree (the wire form never carries it).
193#[must_use]
194pub fn strip_tags(v: Value) -> Value {
195    match v {
196        Value::Object(o) => Value::Object(
197            o.into_iter()
198                .filter(|(k, _)| k != TAG_KEY)
199                .map(|(k, x)| (k, strip_tags(x)))
200                .collect(),
201        ),
202        Value::Array(a) => Value::Array(a.into_iter().map(strip_tags).collect()),
203        other => other,
204    }
205}
206
207impl<'a> StoreContext<'a> {
208    /// A context over `conn` scoped to `repo_id`, with the query phrases'
209    /// vectors for `semantic(...)` (empty when no provider ran).
210    #[must_use]
211    pub fn new(
212        conn: &'a Connection,
213        repo_id: &str,
214        semantic: HashMap<String, SemanticVec>,
215    ) -> Self {
216        Self {
217            conn,
218            repo_id: repo_id.to_owned(),
219            semantic,
220            root_failure: RefCell::new(None),
221            rows_root: None,
222        }
223    }
224
225    /// Serve `rows` — target-tagged store rows a plan produced — as the
226    /// [`oqx::ROWS_ROOT`] scan (the residual query's source). Every other
227    /// root and every relation, intrinsic and row function still hits the
228    /// store, so the residual sees exactly what a full scan would.
229    #[must_use]
230    pub fn with_rows_root(mut self, rows: Vec<Value>) -> Self {
231        self.rows_root = Some(rows);
232        self
233    }
234
235    /// The store failure a root scan hit during the run, if any. Every other
236    /// read fails through the engine's channel (`get` / `call_method` return
237    /// `Err`); `root` has none, so it serves an empty scan and leaves the
238    /// failure here for the runner to report.
239    pub fn take_root_failure(&self) -> Option<OqxError> {
240        self.root_failure.borrow_mut().take()
241    }
242
243    // ---- SQL helpers ------------------------------------------------------------------
244
245    fn all(&self, sql: &str, params: &[SqlValue]) -> oqx::Result<Vec<Object>> {
246        fetch_rows(self.conn, sql, params).map_err(sql_err)
247    }
248
249    fn one(&self, sql: &str, params: &[SqlValue]) -> oqx::Result<Option<Object>> {
250        Ok(self.all(sql, params)?.into_iter().next())
251    }
252
253    fn scalar(&self, sql: &str, params: &[SqlValue]) -> oqx::Result<Value> {
254        Ok(self
255            .one(sql, params)?
256            .and_then(|o| o.values().next().cloned())
257            .unwrap_or(Value::Undefined))
258    }
259
260    fn exists(&self, sql: &str, params: &[SqlValue]) -> oqx::Result<bool> {
261        let mut stmt = self.conn.prepare_cached(sql).map_err(sql_err)?;
262        stmt.exists(params_from_iter(params.iter()))
263            .map_err(sql_err)
264    }
265
266    fn tag_all(rows: Vec<Object>, t: Target) -> Value {
267        Value::Array(tag_rows(rows, t))
268    }
269
270    fn tag(row: Object, t: Target) -> Value {
271        tag_row(row, t)
272    }
273
274    fn repo_root(&self) -> Value {
275        let mut o = Object::with_capacity(1);
276        o.insert(TAG_KEY, Value::Str(REPO_TAG.to_owned()));
277        Value::Object(o)
278    }
279
280    // ---- roots (ordered for a stable (path, id) default) ------------------------------
281
282    fn root_scan(&self, t: Target) -> oqx::Result<Value> {
283        let repo = [SqlValue::Text(self.repo_id.clone())];
284        let sql = match t {
285            Target::Docs => {
286                "SELECT * FROM docs WHERE repo_id = ?1 AND deleted_commit IS NULL ORDER BY path, doc_id"
287            }
288            Target::Blocks => {
289                "SELECT b.*, d.path AS __path FROM blocks b JOIN docs d ON d.doc_id = b.doc_id
290                 WHERE b.repo_id = ?1 AND b.deleted_commit IS NULL AND d.deleted_commit IS NULL
291                 ORDER BY d.path, b.block_id"
292            }
293            Target::Nodes => {
294                "SELECT n.*, d.path AS __path FROM nodes n JOIN docs d ON d.doc_id = n.doc_id
295                 WHERE n.repo_id = ?1 AND d.deleted_commit IS NULL ORDER BY d.path, n.node_id"
296            }
297            Target::Edges => {
298                "SELECT e.*, d.path AS __path FROM edges e JOIN docs d ON d.doc_id = e.src_doc
299                 WHERE e.repo_id = ?1 AND e.to_commit IS NULL AND d.deleted_commit IS NULL
300                 ORDER BY d.path, e.edge_id"
301            }
302        };
303        Ok(Self::tag_all(self.all(sql, &repo)?, t))
304    }
305
306    // ---- properties ---------------------------------------------------------------------
307
308    /// A property row decoded to a plain scalar; a range-shaped string stays a
309    /// string (`range(prop)` is the opt-in).
310    fn decode_prop(r: &Object) -> Value {
311        let get = |k: &str| r.get(k).cloned().unwrap_or(Value::Undefined);
312        match get("type").as_str().unwrap_or("") {
313            "number" => get("val_num"),
314            "bool" => Value::Bool(get("val_bool").truthy()),
315            "null" => Value::Null,
316            "json" => parse_json(&get("val_json")),
317            _ => get("val_text"),
318        }
319    }
320
321    /// The scalar-vs-list rule: exactly one `card = scalar` row → the scalar;
322    /// otherwise the array; no row → the nested object under `key.`.
323    fn doc_prop(&self, doc_id: &str, key: &str, source: Option<&str>) -> oqx::Result<Value> {
324        let rows = match source {
325            Some(s) => self.all(
326                "SELECT * FROM properties WHERE doc_id = ?1 AND key = ?2 AND source = ?3 AND deleted_commit IS NULL ORDER BY ord",
327                &[SqlValue::Text(doc_id.to_owned()), SqlValue::Text(key.to_owned()), SqlValue::Text(s.to_owned())],
328            )?,
329            None => self.all(
330                "SELECT * FROM properties WHERE doc_id = ?1 AND key = ?2 AND deleted_commit IS NULL ORDER BY ord",
331                &[SqlValue::Text(doc_id.to_owned()), SqlValue::Text(key.to_owned())],
332            )?,
333        };
334        if rows.is_empty() {
335            return self.doc_prop_object(doc_id, key, source);
336        }
337        if rows.len() == 1 && rows[0].get("card").and_then(Value::as_str) == Some("scalar") {
338            return Ok(Self::decode_prop(&rows[0]));
339        }
340        Ok(Value::Array(rows.iter().map(Self::decode_prop).collect()))
341    }
342
343    /// The nested object rebuilt from flattened dotted keys under `prefix.`
344    /// (`Undefined` when none); leaves decoded.
345    fn doc_prop_object(
346        &self,
347        doc_id: &str,
348        prefix: &str,
349        source: Option<&str>,
350    ) -> oqx::Result<Value> {
351        let like = SqlValue::Text(format!("{prefix}.%"));
352        let rows = match source {
353            Some(s) => self.all(
354                "SELECT * FROM properties WHERE doc_id = ?1 AND key LIKE ?2 AND source = ?3 AND deleted_commit IS NULL ORDER BY ord",
355                &[SqlValue::Text(doc_id.to_owned()), like, SqlValue::Text(s.to_owned())],
356            )?,
357            None => self.all(
358                "SELECT * FROM properties WHERE doc_id = ?1 AND key LIKE ?2 AND deleted_commit IS NULL ORDER BY ord",
359                &[SqlValue::Text(doc_id.to_owned()), like],
360            )?,
361        };
362        if rows.is_empty() {
363            return Ok(Value::Undefined);
364        }
365        let mut out = Object::new();
366        for r in &rows {
367            let key = r.get("key").and_then(Value::as_str).unwrap_or("");
368            let rest: Vec<&str> = key[(prefix.len() + 1).min(key.len())..]
369                .split('.')
370                .collect();
371            set_nested(&mut out, &rest, Self::decode_prop(r));
372        }
373        Ok(Value::Object(out))
374    }
375
376    /// The `frontmatter` / `inline` bag as a plain object: one entry per
377    /// top-level key in key order, each valued by [`Self::doc_prop`].
378    fn doc_prop_bag(&self, doc_id: &str, source: &str) -> oqx::Result<Value> {
379        let keys = self.all(
380            "SELECT DISTINCT key FROM properties WHERE doc_id = ?1 AND source = ?2 AND deleted_commit IS NULL ORDER BY key",
381            &[SqlValue::Text(doc_id.to_owned()), SqlValue::Text(source.to_owned())],
382        )?;
383        let mut out = Object::new();
384        for k in keys {
385            let key = k.get("key").and_then(Value::as_str).unwrap_or("");
386            let top = key.split('.').next().unwrap_or("");
387            if !out.contains_key(top) {
388                let v = self.doc_prop(doc_id, top, Some(source))?;
389                out.insert(top, v);
390            }
391        }
392        Ok(Value::Object(out))
393    }
394
395    // ---- structure ----------------------------------------------------------------------
396
397    /// The ordinal of a block's top-level ancestor (section ranges are in
398    /// top-level ordinals).
399    fn top_ordinal(&self, block: &Value) -> oqx::Result<Value> {
400        let ordinal = col(block, "ordinal").clone();
401        if col(block, "parent_block").is_absent() {
402            return Ok(ordinal);
403        }
404        let ap = col_str(block, "ancestor_path");
405        let Some(first) = ap.split('/').find(|s| !s.is_empty()) else {
406            return Ok(ordinal);
407        };
408        let r = self.scalar(
409            "SELECT ordinal FROM blocks WHERE doc_id = ?1 AND block_id = ?2",
410            &[
411                SqlValue::Text(col_str(block, "doc_id")),
412                SqlValue::Text(first.to_owned()),
413            ],
414        )?;
415        Ok(if r.is_absent() { ordinal } else { r })
416    }
417
418    /// A document's live blocks in document order — pre-order over the
419    /// containment tree (children by `ordinal` under their parent; a row whose
420    /// parent is not live is a root) — tagged, with `__path` = `path`.
421    fn doc_blocks_preorder(&self, doc_id: &str, path: &str) -> oqx::Result<Vec<Value>> {
422        let rows = self.all(
423            "SELECT b.*, ?1 AS __path FROM blocks b WHERE b.doc_id = ?2 AND b.deleted_commit IS NULL ORDER BY b.ordinal, b.block_id",
424            &[SqlValue::Text(path.to_owned()), SqlValue::Text(doc_id.to_owned())],
425        )?;
426        let ids: Vec<String> = rows
427            .iter()
428            .map(|r| {
429                r.get("block_id")
430                    .and_then(Value::as_str)
431                    .unwrap_or("")
432                    .to_owned()
433            })
434            .collect();
435        let parent_index: Vec<Option<usize>> = rows
436            .iter()
437            .map(|r| {
438                r.get("parent_block")
439                    .and_then(Value::as_str)
440                    .and_then(|p| ids.iter().position(|id| id == p))
441            })
442            .collect();
443        let mut children: Vec<Vec<usize>> = vec![Vec::new(); rows.len()];
444        let mut roots = Vec::new();
445        for (i, p) in parent_index.iter().enumerate() {
446            match p {
447                Some(p) => children[*p].push(i),
448                None => roots.push(i),
449            }
450        }
451        fn walk(i: usize, children: &[Vec<usize>], order: &mut Vec<usize>) {
452            order.push(i);
453            for &c in &children[i] {
454                walk(c, children, order);
455            }
456        }
457        let mut order = Vec::with_capacity(rows.len());
458        for r in roots {
459            walk(r, &children, &mut order);
460        }
461        let mut slots: Vec<Option<Object>> = rows.into_iter().map(Some).collect();
462        Ok(order
463            .into_iter()
464            .map(|i| Self::tag(slots[i].take().expect("visited once"), Target::Blocks))
465            .collect())
466    }
467
468    fn jattr(row: &Value, k: &str) -> Value {
469        match parse_json(col(row, "attrs")) {
470            Value::Object(o) => o.get(k).cloned().unwrap_or(Value::Undefined),
471            _ => Value::Undefined,
472        }
473    }
474
475    /// `Ok(None)` when `key` is not a relation of `t`.
476    fn relation(&self, row: &Value, t: Target, key: &str) -> oqx::Result<Option<Value>> {
477        let path = || SqlValue::Text(col_str(row, "__path"));
478        let doc_id = || SqlValue::Text(col_str(row, "doc_id"));
479        let doc_path = || SqlValue::Text(col_str(row, "path"));
480        let block_id = || SqlValue::Text(col_str(row, "block_id"));
481        let repo = || SqlValue::Text(self.repo_id.clone());
482        Ok(Some(match (t, key) {
483            (Target::Docs, "nodes") => {
484                // Document order: block-less nodes first, then by the owning
485                // block's pre-order rank, `span_start`, `node_id`.
486                let rows = self.all(
487                    "SELECT n.*, ?1 AS __path FROM nodes n WHERE n.doc_id = ?2 ORDER BY n.node_id",
488                    &[doc_path(), doc_id()],
489                )?;
490                let blocks = self.doc_blocks_preorder(&col_str(row, "doc_id"), &col_str(row, "path"))?;
491                let rank: HashMap<String, usize> = blocks
492                    .iter()
493                    .enumerate()
494                    .map(|(i, b)| (col_str(b, "block_id"), i))
495                    .collect();
496                let mut keyed: Vec<((usize, usize, f64, String), Object)> = rows
497                    .into_iter()
498                    .map(|r| {
499                        let block = r.get("block_id").and_then(Value::as_str);
500                        let (has_block, rk) = match block {
501                            None => (0, 0),
502                            Some(b) => (1, rank.get(b).copied().unwrap_or(usize::MAX)),
503                        };
504                        let span = r.get("span_start").and_then(Value::as_f64).unwrap_or(-1.0);
505                        let id = r.get("node_id").and_then(Value::as_str).unwrap_or("").to_owned();
506                        ((has_block, rk, span, id), r)
507                    })
508                    .collect();
509                keyed.sort_by(|a, b| {
510                    a.0.0
511                        .cmp(&b.0.0)
512                        .then(a.0.1.cmp(&b.0.1))
513                        .then(a.0.2.total_cmp(&b.0.2))
514                        .then(a.0.3.cmp(&b.0.3))
515                });
516                Value::Array(keyed.into_iter().map(|(_, r)| Self::tag(r, Target::Nodes)).collect())
517            }
518            (Target::Docs, "blocks") => {
519                Value::Array(self.doc_blocks_preorder(&col_str(row, "doc_id"), &col_str(row, "path"))?)
520            }
521            (Target::Docs, "out") => Self::tag_all(
522                self.all(
523                    "SELECT DISTINCT d2.* FROM docs d2 JOIN edges e ON e.dst_node = d2.doc_id
524                     WHERE e.src_doc = ?1 AND e.to_commit IS NULL AND d2.repo_id = ?2 AND d2.deleted_commit IS NULL ORDER BY d2.path, d2.doc_id",
525                    &[doc_id(), repo()],
526                )?,
527                Target::Docs,
528            ),
529            (Target::Docs, "in") => Self::tag_all(
530                self.all(
531                    "SELECT DISTINCT d2.* FROM docs d2 JOIN edges e ON e.src_doc = d2.doc_id
532                     WHERE e.dst_node = ?1 AND e.to_commit IS NULL AND d2.repo_id = ?2 AND d2.deleted_commit IS NULL ORDER BY d2.path, d2.doc_id",
533                    &[doc_id(), repo()],
534                )?,
535                Target::Docs,
536            ),
537            (Target::Docs, "out_edges") => Self::tag_all(
538                self.all(
539                    "SELECT e.*, ?1 AS __path FROM edges e WHERE e.src_doc = ?2 AND e.to_commit IS NULL ORDER BY e.predicate, e.edge_id",
540                    &[doc_path(), doc_id()],
541                )?,
542                Target::Edges,
543            ),
544            (Target::Docs, "in_edges") => Self::tag_all(
545                self.all(
546                    "SELECT e.*, d.path AS __path FROM edges e JOIN docs d ON d.doc_id = e.src_doc
547                     WHERE e.dst_node = ?1 AND e.to_commit IS NULL AND d.deleted_commit IS NULL ORDER BY e.predicate, e.edge_id",
548                    &[doc_id()],
549                )?,
550                Target::Edges,
551            ),
552            (Target::Blocks, "children") => Self::tag_all(
553                self.all(
554                    "SELECT b.*, ?1 AS __path FROM blocks b WHERE b.parent_block = ?2 AND b.deleted_commit IS NULL ORDER BY b.ordinal, b.block_id",
555                    &[path(), block_id()],
556                )?,
557                Target::Blocks,
558            ),
559            (Target::Blocks, "nodes") => Self::tag_all(
560                self.all(
561                    "SELECT n.*, ?1 AS __path FROM nodes n WHERE n.block_id = ?2 ORDER BY n.span_start, n.node_id",
562                    &[path(), block_id()],
563                )?,
564                Target::Nodes,
565            ),
566            (Target::Blocks, "out_edges") => Self::tag_all(
567                self.all(
568                    "SELECT e.*, ?1 AS __path FROM edges e WHERE e.src_block = ?2 AND e.to_commit IS NULL ORDER BY e.predicate, e.edge_id",
569                    &[path(), block_id()],
570                )?,
571                Target::Edges,
572            ),
573            (Target::Blocks, "section") => {
574                let top = to_sql(&self.top_ordinal(row)?);
575                Self::tag_all(
576                    self.all(
577                        "SELECT n.*, ?1 AS __path FROM nodes n WHERE n.doc_id = ?2 AND n.kind = 'md:section'
578                           AND json_extract(n.attrs,'$.first_ordinal') <= ?3 AND json_extract(n.attrs,'$.last_ordinal') >= ?4
579                         ORDER BY json_extract(n.attrs,'$.first_ordinal'), n.node_id",
580                        &[path(), doc_id(), top.clone(), top],
581                    )?,
582                    Target::Nodes,
583                )
584            }
585            (Target::Nodes, "blocks") => {
586                let (f, l) = (Self::jattr(row, "first_ordinal"), Self::jattr(row, "last_ordinal"));
587                if f.is_absent() || l.is_absent() {
588                    return Ok(Some(Value::Array(Vec::new())));
589                }
590                let (f, l) = (
591                    f.as_f64().unwrap_or(f64::NAN),
592                    l.as_f64().unwrap_or(f64::NAN),
593                );
594                let rows = self.doc_blocks_preorder(&col_str(row, "doc_id"), &col_str(row, "__path"))?;
595                let mut kept: Vec<Value> = Vec::new();
596                for b in rows {
597                    let t = self.top_ordinal(&b)?.as_f64().unwrap_or(f64::NAN);
598                    if t >= f && t <= l {
599                        kept.push(b);
600                    }
601                }
602                Value::Array(kept)
603            }
604            (Target::Nodes, "subsections") => {
605                let (f, l, lvl) = (
606                    Self::jattr(row, "first_ordinal"),
607                    Self::jattr(row, "last_ordinal"),
608                    Self::jattr(row, "level"),
609                );
610                if f.is_absent() {
611                    return Ok(Some(Value::Array(Vec::new())));
612                }
613                Self::tag_all(
614                    self.all(
615                        "SELECT n.*, ?1 AS __path FROM nodes n WHERE n.doc_id = ?2 AND n.kind = 'md:section'
616                           AND json_extract(n.attrs,'$.first_ordinal') >= ?3 AND json_extract(n.attrs,'$.last_ordinal') <= ?4
617                           AND json_extract(n.attrs,'$.level') > ?5 ORDER BY json_extract(n.attrs,'$.first_ordinal'), n.node_id",
618                        &[path(), doc_id(), to_sql(&f), to_sql(&l), to_sql(&lvl)],
619                    )?,
620                    Target::Nodes,
621                )
622            }
623            (Target::Nodes, "children") => {
624                let (f, l, lvl) = (
625                    Self::jattr(row, "first_ordinal"),
626                    Self::jattr(row, "last_ordinal"),
627                    Self::jattr(row, "level"),
628                );
629                if f.is_absent() {
630                    return Ok(Some(Value::Array(Vec::new())));
631                }
632                Self::tag_all(
633                    self.all(
634                        "SELECT i.*, ?1 AS __path FROM nodes i WHERE i.doc_id = ?2 AND i.kind = 'md:section'
635                           AND json_extract(i.attrs,'$.level') > ?3
636                           AND json_extract(i.attrs,'$.first_ordinal') >= ?4 AND json_extract(i.attrs,'$.last_ordinal') <= ?5
637                           AND NOT EXISTS (SELECT 1 FROM nodes m WHERE m.doc_id = i.doc_id AND m.kind = 'md:section'
638                             AND json_extract(m.attrs,'$.level') > ?6 AND json_extract(m.attrs,'$.level') < json_extract(i.attrs,'$.level')
639                             AND json_extract(m.attrs,'$.first_ordinal') <= json_extract(i.attrs,'$.first_ordinal')
640                             AND json_extract(m.attrs,'$.last_ordinal') >= json_extract(i.attrs,'$.last_ordinal'))
641                         ORDER BY json_extract(i.attrs,'$.first_ordinal'), i.node_id",
642                        &[path(), doc_id(), to_sql(&lvl), to_sql(&f), to_sql(&l), to_sql(&lvl)],
643                    )?,
644                    Target::Nodes,
645                )
646            }
647            _ => return Ok(None),
648        }))
649    }
650
651    fn owning_doc(&self, row: &Value) -> oqx::Result<Value> {
652        let id = match col(row, "doc_id") {
653            Value::Undefined | Value::Null => col(row, "src_doc").clone(),
654            v => v.clone(),
655        };
656        Ok(self
657            .one("SELECT * FROM docs WHERE doc_id = ?1", &[to_sql(&id)])?
658            .map_or(Value::Undefined, |o| Self::tag(o, Target::Docs)))
659    }
660
661    fn owning_block(&self, row: &Value) -> oqx::Result<Value> {
662        let id = col(row, "block_id");
663        if !id.truthy() {
664            return Ok(Value::Undefined);
665        }
666        Ok(self
667            .one(
668                "SELECT b.*, d.path AS __path FROM blocks b JOIN docs d ON d.doc_id = b.doc_id WHERE b.block_id = ?1",
669                &[to_sql(id)],
670            )?
671            .map_or(Value::Undefined, |o| Self::tag(o, Target::Blocks)))
672    }
673
674    // ---- intrinsics ---------------------------------------------------------------------
675
676    fn null_if_absent(v: Value) -> Value {
677        if v.is_absent() { Value::Null } else { v }
678    }
679
680    fn intrinsic(&self, row: &Value, t: Target, name: &str) -> oqx::Result<Value> {
681        if name == "$self" {
682            return Ok(row.clone());
683        }
684        let c = |k: &str| col(row, k).clone();
685        Ok(match (t, name) {
686            (Target::Docs, "$id") => c("doc_id"),
687            (Target::Docs, "$path") => c("path"),
688            (Target::Docs, "$content_hash") => Self::null_if_absent(c("file_hash")),
689            (Target::Docs, "$updated_at") => Self::null_if_absent(self.scalar(
690                "SELECT c.ts FROM revisions r JOIN commits c ON c.commit_id = r.commit_id WHERE r.rev_id = ?1",
691                &[to_sql(&c("current_rev"))],
692            )?),
693            (Target::Docs, "$body") => {
694                match omgbase_store::read::reconstruct(self.conn, &col_str(row, "doc_id")) {
695                    Ok(Some(s)) => Value::Str(s),
696                    Ok(None) => Value::Null,
697                    Err(e) => return Err(OqxError::eval(e.to_string())),
698                }
699            }
700            (Target::Docs, "$title") => {
701                Self::null_if_absent(self.doc_prop(&col_str(row, "doc_id"), "$title", Some("computed"))?)
702            }
703            (Target::Docs, "$tags") => {
704                Self::null_if_absent(self.doc_prop(&col_str(row, "doc_id"), "$tags", Some("computed"))?)
705            }
706            (Target::Blocks, "$id") => c("block_id"),
707            (Target::Blocks, "$doc") => c("doc_id"),
708            (Target::Blocks, "$path") => c("__path"),
709            (Target::Blocks, "$ordinal") => c("ordinal"),
710            (Target::Blocks, "$depth") => c("depth"),
711            (Target::Blocks, "$body") => c("text"),
712            (Target::Blocks, "$content_hash") => Self::null_if_absent(c("raw_hash")),
713            (Target::Blocks, "$updated_at") => Self::null_if_absent(self.scalar(
714                "SELECT MAX(c.ts) FROM block_changes bc JOIN commits c ON c.commit_id = bc.commit_id WHERE bc.block_id = ?1",
715                &[to_sql(&c("block_id"))],
716            )?),
717            (Target::Nodes, "$id" | "$node_id") => c("node_id"),
718            (Target::Nodes, "$doc_id") => c("doc_id"),
719            (Target::Nodes, "$block_id") => c("block_id"),
720            (Target::Nodes, "$path") => c("__path"),
721            (Target::Edges, "$id") => c("edge_id"),
722            (Target::Edges, "$src") => c("src_doc"),
723            (Target::Edges, "$dst") => c("dst_node"),
724            (Target::Edges, "$src_block") => c("src_block"),
725            (Target::Edges, "$via") => c("via_node"),
726            (Target::Edges, "$from_commit") => c("from_commit"),
727            (Target::Edges, "$path") => c("__path"),
728            (Target::Edges, "$dst_path") => Self::null_if_absent(self.scalar(
729                "SELECT path FROM docs WHERE doc_id = ?1",
730                &[to_sql(&c("dst_node"))],
731            )?),
732            (Target::Edges, "$dst_uri") => Self::null_if_absent(self.scalar(
733                "SELECT uri FROM external_nodes WHERE node_id = ?1",
734                &[to_sql(&c("dst_node"))],
735            )?),
736            _ => Value::Undefined,
737        })
738    }
739
740    // ---- row functions (methods on `$self`) ----------------------------------------------
741
742    fn filter_invalid(msg: String) -> Option<oqx::Result<Value>> {
743        Some(Err(OqxError::eval(msg)))
744    }
745
746    fn require_target(t: Target, want: Target, name: &str) -> Option<oqx::Result<Value>> {
747        (t != want).then(|| {
748            Err(OqxError::eval(format!(
749                "{name}() is only available on the {} target",
750                want.as_str()
751            )))
752        })
753    }
754
755    fn sql_result(r: oqx::Result<bool>) -> oqx::Result<Value> {
756        r.map(Value::Bool)
757    }
758
759    fn row_method(
760        &self,
761        name: &str,
762        row: &Value,
763        t: Target,
764        args: &[Value],
765    ) -> Option<oqx::Result<Value>> {
766        let c = |k: &str| col(row, k).clone();
767        match name {
768            "text" => Some(self.text_match(t, row, &arg_or_empty(args, 0))),
769            "semantic" => Some(self.semantic_score(t, row, &arg_or_empty(args, 0))),
770            "has_anchor" => Self::require_target(t, Target::Blocks, name).or_else(|| {
771                Some(Self::sql_result(self.exists(
772                    "SELECT 1 FROM edges WHERE src_block = ?1 AND anchor IS NOT NULL LIMIT 1",
773                    &[to_sql(&c("block_id"))],
774                )))
775            }),
776            "child_count" => Self::require_target(t, Target::Blocks, name).or_else(|| {
777                Some(self.scalar(
778                    "SELECT COUNT(*) FROM blocks WHERE parent_block = ?1 AND deleted_commit IS NULL",
779                    &[to_sql(&c("block_id"))],
780                ))
781            }),
782            "parent_type" => Self::require_target(t, Target::Blocks, name).or_else(|| {
783                Some(
784                    self.scalar(
785                        "SELECT type FROM blocks WHERE block_id = ?1",
786                        &[to_sql(&c("parent_block"))],
787                    )
788                    .map(Self::null_if_absent),
789                )
790            }),
791            "has_edge" => {
792                let pred = js_string(args.first().unwrap_or(&Value::Undefined));
793                let (src_col, src_val) = if t == Target::Blocks {
794                    ("src_block", c("block_id"))
795                } else {
796                    ("src_doc", c("doc_id"))
797                };
798                let r = if args.len() >= 2 {
799                    self.exists(
800                        &format!("SELECT 1 FROM edges WHERE {src_col} = ?1 AND predicate = ?2 AND to_commit IS NULL AND dst_node = ?3 LIMIT 1"),
801                        &[to_sql(&src_val), SqlValue::Text(pred), to_sql(&args[1])],
802                    )
803                } else {
804                    self.exists(
805                        &format!("SELECT 1 FROM edges WHERE {src_col} = ?1 AND predicate = ?2 AND to_commit IS NULL LIMIT 1"),
806                        &[to_sql(&src_val), SqlValue::Text(pred)],
807                    )
808                };
809                Some(Self::sql_result(r))
810            }
811            "under" => Self::require_target(t, Target::Blocks, name).or_else(|| {
812                let target = js_string(args.first().unwrap_or(&Value::Undefined));
813                let ap = col_str(row, "ancestor_path");
814                Some(Ok(Value::Bool(
815                    ap.contains(&format!("/{target}/")) || col_str(row, "block_id") == target,
816                )))
817            }),
818            "under_heading" => Self::require_target(t, Target::Blocks, name).or_else(|| {
819                let text = js_string(args.first().unwrap_or(&Value::Undefined));
820                let top = match self.top_ordinal(row) {
821                    Ok(v) => to_sql(&v),
822                    Err(e) => return Some(Err(e)),
823                };
824                Some(Self::sql_result(self.exists(
825                    "SELECT 1 FROM sections s JOIN blocks hb ON hb.block_id = s.heading_block
826                     WHERE s.doc_id = ?1 AND lower(hb.text) LIKE '%' || lower(?2) || '%' AND s.first_ordinal <= ?3 AND s.last_ordinal >= ?4 LIMIT 1",
827                    &[to_sql(&c("doc_id")), SqlValue::Text(text), top.clone(), top],
828                )))
829            }),
830            "within" => Self::require_target(t, Target::Blocks, name).or_else(|| {
831                let target = js_string(args.first().unwrap_or(&Value::Undefined));
832                if target.starts_with("d_") {
833                    return Some(Ok(Value::Bool(col_str(row, "doc_id") == target)));
834                }
835                if target.contains('*') {
836                    let like = glob_to_like(&target, false);
837                    return Some(Self::sql_result(self.exists(
838                        "SELECT 1 WHERE ?1 LIKE ?2 ESCAPE '\\'",
839                        &[SqlValue::Text(col_str(row, "__path")), SqlValue::Text(like)],
840                    )));
841                }
842                Some(Ok(Value::Bool(col_str(row, "__path") == target)))
843            }),
844            "under_kind" => Self::require_target(t, Target::Blocks, name).or_else(|| {
845                let kind = js_string(args.first().unwrap_or(&Value::Undefined));
846                let ap: Vec<String> = col_str(row, "ancestor_path")
847                    .split('/')
848                    .filter(|s| !s.is_empty())
849                    .map(str::to_owned)
850                    .collect();
851                if ap.is_empty() {
852                    return Some(Ok(Value::Bool(false)));
853                }
854                let placeholders: Vec<String> = (1..=ap.len()).map(|i| format!("?{i}")).collect();
855                let placeholders = placeholders.join(",");
856                let mut params: Vec<SqlValue> = ap.into_iter().map(SqlValue::Text).collect();
857                let n = params.len();
858                params.push(SqlValue::Text(kind));
859                let r = match args.get(1) {
860                    Some(v) if !v.is_absent() => {
861                        let nm = js_string(v);
862                        params.push(SqlValue::Text(nm.clone()));
863                        params.push(SqlValue::Text(nm));
864                        self.exists(
865                            &format!(
866                                "SELECT 1 FROM blocks WHERE block_id IN ({placeholders}) AND type = ?{} AND (lower(text) LIKE '%' || lower(?{}) || '%' OR json_extract(attrs,'$.key') = ?{}) LIMIT 1",
867                                n + 1,
868                                n + 2,
869                                n + 3
870                            ),
871                            &params,
872                        )
873                    }
874                    _ => self.exists(
875                        &format!(
876                            "SELECT 1 FROM blocks WHERE block_id IN ({placeholders}) AND type = ?{} LIMIT 1",
877                            n + 1
878                        ),
879                        &params,
880                    ),
881                };
882                Some(Self::sql_result(r))
883            }),
884            "yaml_path" => Self::require_target(t, Target::Blocks, name).or_else(|| {
885                Some(Ok(Self::key_path(row, &js_string(args.first().unwrap_or(&Value::Undefined)), "yaml")))
886            }),
887            "json_pointer" => Self::require_target(t, Target::Blocks, name).or_else(|| {
888                Some(Ok(Self::key_path(row, &js_string(args.first().unwrap_or(&Value::Undefined)), "json")))
889            }),
890            _ => None,
891        }
892    }
893
894    fn key_path(row: &Value, path: &str, kind: &str) -> Value {
895        let key = if kind == "json" {
896            let mut p = path;
897            p = p.strip_prefix('#').unwrap_or(p);
898            p = p.strip_prefix('/').unwrap_or(p);
899            p.split('/').collect::<Vec<_>>().join(".")
900        } else {
901            path.to_owned()
902        };
903        let leaf = key.rsplit('.').next().unwrap_or("").to_owned();
904        if !col_str(row, "type").starts_with(&format!("{kind}:")) {
905            return Value::Bool(false);
906        }
907        let k = Self::jattr(row, "key");
908        Value::Bool(k == Value::Str(leaf) || k == Value::Str(key))
909    }
910
911    fn text_match(&self, t: Target, row: &Value, terms: &str) -> oqx::Result<Value> {
912        if t == Target::Edges {
913            return Err(OqxError::eval(
914                "text(...) is not available on the edges target",
915            ));
916        }
917        let m = sanitize_fts_query(terms);
918        if m.is_empty() {
919            return Ok(Value::Bool(false));
920        }
921        let r = match t {
922            Target::Docs => self.exists(
923                "SELECT 1 FROM blocks_fts JOIN blocks b ON b.rowid = blocks_fts.rowid WHERE b.doc_id = ?1 AND blocks_fts MATCH ?2 LIMIT 1",
924                &[to_sql(col(row, "doc_id")), SqlValue::Text(m)],
925            ),
926            Target::Nodes => self.exists(
927                "SELECT 1 FROM nodes_fts WHERE rowid = (SELECT rowid FROM nodes WHERE node_id = ?1) AND nodes_fts MATCH ?2",
928                &[to_sql(col(row, "node_id")), SqlValue::Text(m)],
929            ),
930            _ => self.exists(
931                "SELECT 1 FROM blocks_fts WHERE rowid = (SELECT rowid FROM blocks WHERE block_id = ?1) AND blocks_fts MATCH ?2",
932                &[to_sql(col(row, "block_id")), SqlValue::Text(m)],
933            ),
934        };
935        Self::sql_result(r)
936    }
937
938    fn semantic_score(&self, t: Target, row: &Value, phrase: &str) -> oqx::Result<Value> {
939        if matches!(t, Target::Nodes | Target::Edges) {
940            return Err(OqxError::eval(
941                "semantic(...) is available on the docs and blocks targets",
942            ));
943        }
944        let Some(resolved) = self.semantic.get(phrase) else {
945            return Err(OqxError::eval(format!(
946                "semantic({}) needs an embedding provider; none is configured for this query",
947                serde_json::Value::String(phrase.to_owned())
948            )));
949        };
950        let vec: oqx::Result<Option<Vec<u8>>> = match t {
951            Target::Docs => self
952                .conn
953                .query_row(
954                    "SELECT vec FROM doc_embeddings WHERE doc_id = ?1 AND model = ?2",
955                    rusqlite::params![col_str(row, "doc_id"), resolved.model],
956                    |r| r.get(0),
957                )
958                .optional()
959                .map_err(sql_err),
960            // The row for the block's current `(raw_hash, ctx_hash)` only
961            // (`spec/search` §3, 1.1): a stale context row is never read.
962            _ => omgbase_store::block_vector(self.conn, &col_str(row, "block_id"), &resolved.model)
963                .map_err(|e| OqxError::eval(e.to_string())),
964        };
965        match vec? {
966            Some(v) => Ok(Value::Number(cosine_bytes(&v, &resolved.vec))),
967            None => Ok(Value::Null),
968        }
969    }
970}
971
972/// Run `sql` and read every row as a column object (blobs as hex, integers
973/// and reals as numbers) — the one shape a store row ever has in a query.
974pub(crate) fn fetch_rows(
975    conn: &Connection,
976    sql: &str,
977    params: &[SqlValue],
978) -> rusqlite::Result<Vec<Object>> {
979    let mut stmt = conn.prepare_cached(sql)?;
980    let names: Vec<String> = stmt
981        .column_names()
982        .iter()
983        .map(|s| (*s).to_owned())
984        .collect();
985    let rows = stmt.query_map(params_from_iter(params.iter()), |r| {
986        let mut o = Object::with_capacity(names.len());
987        for (i, name) in names.iter().enumerate() {
988            o.insert(name.as_str(), sql_value(r.get_ref(i)?));
989        }
990        Ok(o)
991    })?;
992    rows.collect()
993}
994
995/// Tag a store row with its target so the context resolves it (the
996/// reference's `tagRows`; the planner hands produced rows back this way).
997pub(crate) fn tag_row(mut row: Object, t: Target) -> Value {
998    row.insert(TAG_KEY, Value::Str(t.as_str().to_owned()));
999    Value::Object(row)
1000}
1001
1002/// [`tag_row`] over a result set.
1003pub(crate) fn tag_rows(rows: Vec<Object>, t: Target) -> Vec<Value> {
1004    rows.into_iter().map(|r| tag_row(r, t)).collect()
1005}
1006
1007/// `cur[seg] = {}` down the path, then the leaf (a scalar in the way is
1008/// replaced by an object; an existing key keeps its position).
1009fn set_nested(out: &mut Object, path: &[&str], leaf: Value) {
1010    let Some((first, rest)) = path.split_first() else {
1011        return;
1012    };
1013    if rest.is_empty() {
1014        out.insert(*first, leaf);
1015        return;
1016    }
1017    let mut child = match out.get(first) {
1018        Some(Value::Object(o)) => o.clone(),
1019        _ => Object::new(),
1020    };
1021    set_nested(&mut child, rest, leaf);
1022    out.insert(*first, Value::Object(child));
1023}
1024
1025/// A `*` glob as a `LIKE` pattern with `ESCAPE '\'`; `escape_backslash`
1026/// also escapes `\` (the list surfaces do, `within` does not).
1027#[must_use]
1028pub fn glob_to_like(glob: &str, escape_backslash: bool) -> String {
1029    let mut out = String::with_capacity(glob.len() + 4);
1030    for ch in glob.chars() {
1031        match ch {
1032            '%' | '_' => {
1033                out.push('\\');
1034                out.push(ch);
1035            }
1036            '\\' if escape_backslash => out.push_str("\\\\"),
1037            '*' => out.push('%'),
1038            c => out.push(c),
1039        }
1040    }
1041    out
1042}
1043
1044impl DataContext for StoreContext<'_> {
1045    fn root(&self, name: &str) -> Value {
1046        if let Some(rows) = self.rows_root.as_ref().filter(|_| name == oqx::ROWS_ROOT) {
1047            return Value::Array(rows.clone());
1048        }
1049        if name == "$repo" {
1050            return self.repo_root();
1051        }
1052        let Some(t) = Target::parse(name) else {
1053            return Value::Undefined;
1054        };
1055        // No error channel here: a failed scan is served empty and reported
1056        // by the runner (see `take_root_failure`).
1057        match self.root_scan(t) {
1058            Ok(rows) => rows,
1059            Err(e) => {
1060                let mut slot = self.root_failure.borrow_mut();
1061                if slot.is_none() {
1062                    *slot = Some(e);
1063                }
1064                Value::Array(Vec::new())
1065            }
1066        }
1067    }
1068
1069    fn get(&self, row: &Value, key: &str) -> oqx::Result<Value> {
1070        if row.is_absent() {
1071            return Ok(Value::Undefined);
1072        }
1073        // `$repo` is an intrinsic of EVERY scope, so a correlated subquery at any
1074        // depth reaches the repository root without scope climbing.
1075        if key == "$repo" {
1076            return Ok(self.repo_root());
1077        }
1078        if is_repo_root(row) {
1079            if key == "$id" {
1080                return Ok(Value::Str(self.repo_id.clone()));
1081            }
1082            return match Target::parse(key) {
1083                Some(t) => self.root_scan(t),
1084                None => Ok(Value::Undefined),
1085            };
1086        }
1087        let Some(t) = target_of(row) else {
1088            // A plain value (parsed attrs, a property bag, a lifted element).
1089            return Ok(oqx::DefaultContext::read(row, key));
1090        };
1091        if key.starts_with('$') {
1092            return self.intrinsic(row, t, key);
1093        }
1094        // self-alias namespaces
1095        match (t, key) {
1096            (Target::Docs, "doc") | (Target::Blocks, "block") | (Target::Nodes, "section") => {
1097                return Ok(row.clone());
1098            }
1099            (_, "doc") => return self.owning_doc(row),
1100            (Target::Nodes, "block") => return self.owning_block(row),
1101            _ => {}
1102        }
1103        if let Some(v) = self.relation(row, t, key)? {
1104            return Ok(v);
1105        }
1106        let c = |k: &str| col(row, k).clone();
1107        Ok(match t {
1108            Target::Docs => {
1109                if key == "format" {
1110                    return Ok(c("format"));
1111                }
1112                let doc_id = col_str(row, "doc_id");
1113                if key == "frontmatter" || key == "inline" {
1114                    return self.doc_prop_bag(&doc_id, key);
1115                }
1116                if RESERVED_DOC_BASENAMES.contains(&key) {
1117                    // The reference's `FilterInvalid` thrown from `get`; the
1118                    // runner maps this eval error to `filter_invalid` with
1119                    // the same message.
1120                    return Err(OqxError::eval(format!(
1121                        "bare '{key}' reads a frontmatter key; did you mean the intrinsic ${key}? (use frontmatter.{key} to force the property)"
1122                    )));
1123                }
1124                return self.doc_prop(&doc_id, key, None);
1125            }
1126            Target::Blocks => match key {
1127                "type" => c("type"),
1128                "text" => c("text"),
1129                "attrs" => parse_json(&c("attrs")),
1130                _ => Self::jattr(row, key),
1131            },
1132            Target::Nodes => match key {
1133                "kind" => c("kind"),
1134                "name" => c("name"),
1135                "value" => c("value"),
1136                "attrs" => parse_json(&c("attrs")),
1137                _ => Self::jattr(row, key),
1138            },
1139            Target::Edges => match key {
1140                "predicate" | "provenance" | "dst_kind" | "anchor" | "src_field" => c(key),
1141                _ => Value::Undefined,
1142            },
1143        })
1144    }
1145
1146    fn to_rows(&self, value: &Value) -> Vec<Value> {
1147        match value {
1148            Value::Undefined | Value::Null => Vec::new(),
1149            Value::Array(a) => a.clone(),
1150            other => vec![other.clone()],
1151        }
1152    }
1153
1154    fn identity(&self, row: &Value) -> Value {
1155        match target_of(row) {
1156            Some(Target::Docs) => col(row, "doc_id").clone(),
1157            Some(Target::Blocks) => col(row, "block_id").clone(),
1158            Some(Target::Nodes) => col(row, "node_id").clone(),
1159            Some(Target::Edges) => col(row, "edge_id").clone(),
1160            None => row.clone(),
1161        }
1162    }
1163
1164    fn call_function(&self, name: &str, args: &[Value]) -> Option<oqx::Result<Value>> {
1165        if name == "range" {
1166            let x = args.first().unwrap_or(&Value::Undefined);
1167            return Some(Ok(match x {
1168                Value::Range(_) => x.clone(),
1169                Value::Str(s) => match omgbase_properties::detect_range(s) {
1170                    Some(r) => {
1171                        let b = |b: &Bound| match b {
1172                            Bound::Open => Value::Undefined,
1173                            Bound::Num(n) => Value::Number(*n),
1174                            Bound::Iso(s) => Value::Str(s.clone()),
1175                        };
1176                        Value::from(make_range(b(&r.lo), b(&r.hi), r.exclusive_end))
1177                    }
1178                    None => Value::Null,
1179                },
1180                _ => Value::Null,
1181            }));
1182        }
1183        builtin_function(name, args)
1184    }
1185
1186    fn call_method(&self, name: &str, recv: &Value, args: &[Value]) -> Option<oqx::Result<Value>> {
1187        if let Some(t) = target_of(recv) {
1188            if let Some(r) = self.row_method(name, recv, t, args) {
1189                return Some(r);
1190            }
1191        } else if matches!(
1192            name,
1193            "text"
1194                | "semantic"
1195                | "under"
1196                | "under_heading"
1197                | "within"
1198                | "under_kind"
1199                | "yaml_path"
1200                | "json_pointer"
1201                | "has_edge"
1202                | "has_anchor"
1203                | "child_count"
1204                | "parent_type"
1205        ) {
1206            return Self::filter_invalid(format!("{name}() needs a docs/blocks/nodes/edges row"));
1207        }
1208        builtin_method_with(RegexDialect::Oqx, name, recv, args)
1209    }
1210}
1211
1212#[cfg(test)]
1213mod tests {
1214    use super::*;
1215
1216    #[test]
1217    fn glob_to_like_escapes() {
1218        assert_eq!(glob_to_like("a*/b_%", true), "a%/b\\_\\%");
1219        assert_eq!(glob_to_like("a\\b*", true), "a\\\\b%");
1220        assert_eq!(glob_to_like("a\\b*", false), "a\\b%");
1221    }
1222
1223    #[test]
1224    fn nested_property_objects_rebuild() {
1225        let mut o = Object::new();
1226        set_nested(&mut o, &["a", "b"], Value::Number(1.0));
1227        set_nested(&mut o, &["a", "c"], Value::Number(2.0));
1228        set_nested(&mut o, &["d"], Value::Str("x".into()));
1229        let a = o.get("a").unwrap().as_object().unwrap();
1230        assert_eq!(a.get("b"), Some(&Value::Number(1.0)));
1231        assert_eq!(a.get("c"), Some(&Value::Number(2.0)));
1232        assert_eq!(o.get("d"), Some(&Value::Str("x".into())));
1233        // A scalar in the way is replaced by an object.
1234        set_nested(&mut o, &["d", "e"], Value::Bool(true));
1235        assert!(o.get("d").unwrap().as_object().is_some());
1236    }
1237
1238    #[test]
1239    fn json_and_sql_bridges() {
1240        assert_eq!(
1241            parse_json(&Value::Str("{\"a\":1}".into()))
1242                .as_object()
1243                .unwrap()
1244                .get("a"),
1245            Some(&Value::Number(1.0))
1246        );
1247        assert_eq!(
1248            parse_json(&Value::Str("nope".into())),
1249            Value::Str("nope".into())
1250        );
1251        assert_eq!(parse_json(&Value::Null), Value::Undefined);
1252        assert_eq!(arg_or_empty(&[], 0), "");
1253        assert_eq!(arg_or_empty(&[Value::Number(2.0)], 0), "2");
1254    }
1255}