diff --git a/CHANGELOG.md b/CHANGELOG.md index 672c284ee4..f133756494 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - **Fallible and re-entrant user functions** (#180, PR 1). `Minigraf::try_register_predicate` and `Minigraf::try_register_aggregate` take closures that return `Result<_, UdfError>` (`UdfError` is exported at the crate root). A call that returns `Err` fails the query with the new `QRY-014`, ``user function `` failed: ``, at the first failing call: it is never read as `false` or `nil`, and no later call of any user function is made for that query. `execute` and `PreparedQuery::execute` return it; a cursor treats it as any other read error, returning the rows of the batches finished before the failure, then the error, then `None`. `register_predicate` and `register_aggregate` are unchanged. A query now reads its functions from a snapshot of the registry taken when it opens, and holds no database lock while a user function runs: a function registered later is not seen by a query or cursor that is already open, and a user function may call back into its own database (query, prepare, read the fact log, write, define rules, register functions, checkpoint, verify, rebuild indexes). Its writes are not seen by the query that called it. Inside a `WriteTransaction` those calls return `INT-001` on that thread, except registration, which runs. Before, a user function that registered a function deadlocked. This is the core of UDF registration in the language bindings, which follows binding by binding. +- **User functions in `BrowserDb`** (#180, PR 7). `db.registerPredicate(name, fn)` and `db.registerAggregate(name, { init, step, finish })`, both synchronous. A function receives each value as a `{ type, value }` object, as `@minigraf/node`'s `FactValue` (an integer's `value` is a `bigint`, a ref's its UUID string, a set's an array of these); a predicate returns a boolean; `init()` returns an accumulator, `step(acc, value)` the next one and `finish(acc, count)` the result, as a plain value or a typed form, as a prepared query's binding takes it. A function that throws fails the query with `QRY-014` and the error's `message` (else `String(e)`), and so does a predicate returning a non-boolean or a `finish` result that is not a value. A UDF may call `query`, `prepare` and the `register*` methods synchronously; `execute`, `checkpoint` and `importGraph` started from one run after the outer query returns. The registry holds no JavaScript value, only ids into a table local to the registering thread, so the crate stays free of `unsafe`. - **`:limit N` bounds the rows a query returns** (#306). `[:find … :where … :limit 10]` returns at most 10 rows of the answer, and the engine stops reading once it has them: over 10M facts `:limit 10` reads one internal batch. Without `:order-by` which rows is unspecified, and may differ between plans; they are always rows of the unlimited answer. `:limit 0` returns no rows. It applies after aggregates and windows, to rule queries and to cursors, and a prepared query takes `:limit $n`, bound with `BindValue::Val(Value::Integer(n))`. A value that is not an integer `>= 0` or a slot fails with the new `PRS-115`; a repeated `:limit` with `INT-012` and a missing value with `INT-013`, as for the other query options. `:max-results` and `:max-derived-facts` still limit only the facts rules derive. - **`:order-by` and `:offset`** (#306). `:order-by [(count ?e) :desc ?name]` orders the rows a query returns by `:find` elements (plain variables, or aggregates and windows written as in `:find`), each `:asc` (the default) or `:desc`; ties are broken by the other `:find` columns ascending, so every position of an ordered answer is decided and every plan returns the same list. Values compare as windows and `min`/`max` compare them: numbers by value (an integer before an equal float, `-0.0` before `0.0`, NaN after every number), then strings, booleans, refs, keywords, `nil` and sets. `:offset N` skips the first `N` rows of the ordered answer, before `:limit`, so `:offset 20 :limit 10` is the third page of ten. A new `Sort` operator reads its whole input; under a `:limit` it keeps only the best `offset + limit` rows, so `:order-by [?v :desc] :limit 10` over 10M facts holds ten rows, not ten million. It applies after aggregates and windows, to rule queries, cursors and prepared queries (`:offset $o`, bound like `:limit`), and counts toward `query_memory_limit`. New codes: `PRS-116` (`:offset` without `:order-by`: without an order, pages could overlap or miss rows), `PRS-117` (an entry that is not a `:find` element) and `PRS-118` (`:order-by` is not a non-empty vector, names an element twice, or has a misplaced or unknown keyword). `PRS-115` now also covers `:offset`. Inside a window's `:over`, `:order-by` is unchanged. - **Set values: `#{...}`** (#316, PR 1 of 3). A fact's value can be a finite set of scalars, `[:alice :user/scopes #{:scope/read :scope/write}]`, written in query text, returned as `Value::Set(ValueSet)`, transacted, retracted and queried by exact value (`[?e :user/scopes #{:scope/write :scope/read}]`), joined on and grouped by. Duplicates are removed and element order does not matter. Two elements are the same when `Value`'s order says so: `0.0` and `-0.0` are one element (`-0.0` is kept), every NaN is one element, a keyword never equals a ref and `1` never equals `1.0`. A set cannot hold a set (`ValueSet::new` fails with the new `API-021`; in query text, `PRS-119`); its elements are literals only (`PRS-120`), and an unclosed `#{` is `PRS-121`. In format v8 a set has value tag `0x08`, after refs; a set whose encoded elements take at most 64 bytes is stored in its index keys, a longer one once in a value page, as a long string is. On a file database a stored set may take up to 4,068 bytes, counting each keyword element as 6 bytes and each ref as 10 (`WAL-003`). The REPL prints sets as `#{...}` and `BrowserDb` returns them as JSON arrays, in `Value`'s order. diff --git a/CLAUDE.md b/CLAUDE.md index ce7a24c964..442e4154ce 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -163,7 +163,7 @@ cargo run < demos/demo_negation.txt 8. **`src/error.rs`** — Structured error codes: `MinigrafError`, `ErrorCategory`, `ErrorCode` registry (PRS/QRY/STG/WAL/API/INT codes); must match `docs/ERROR_REFERENCE.md`. `MinigrafError::invalid_argument` (API-017) and `MinigrafError::closed` (API-018) exist for language bindings -9. **`src/browser/`** — Browser WASM backend (`browser` feature): `buffer.rs` (`BrowserBufferBackend`, in-memory pages with dirty tracking; free pages a pinned generation may read are withheld from release, `pins::split_released`), `indexeddb.rs` (IndexedDB persistence); `BrowserDb.query` returns a `BrowserCursor` (JSON row batches); `BrowserDb.prepare` returns a `BrowserPreparedQuery` (`execute`/`query` with a bindings object, read by `binds.rs`: plain JS values or `{type, value}` forms, #181; a set as `{type: 'set', value: [..]}` or a JS `Set`, #316). Tests here must be `#[wasm_bindgen_test]` (the module only builds for wasm32); `.github/workflows/wasm.yml` runs them in headless browsers, runs the WASI tests under Wasmtime, and lints both wasm targets. New file-backed integration test files need `#![cfg(not(target_arch = "wasm32"))]` +9. **`src/browser/`** — Browser WASM backend (`browser` feature): `buffer.rs` (`BrowserBufferBackend`, in-memory pages with dirty tracking; free pages a pinned generation may read are withheld from release, `pins::split_released`), `indexeddb.rs` (IndexedDB persistence); `BrowserDb.query` returns a `BrowserCursor` (JSON row batches); `BrowserDb.prepare` returns a `BrowserPreparedQuery` (`execute`/`query` with a bindings object, read by `binds.rs`: plain JS values or `{type, value}` forms, #181; a set as `{type: 'set', value: [..]}` or a JS `Set`, #316); `udfs.rs`: `registerPredicate`/`registerAggregate` (#180; values as `{type, value}` objects, a throw is `QRY-014`; JS values held in a thread-local table, the registry keeps ids). Tests here must be `#[wasm_bindgen_test]` (the module only builds for wasm32); `.github/workflows/wasm.yml` runs them in headless browsers, runs the WASI tests under Wasmtime, and lints both wasm targets. New file-backed integration test files need `#![cfg(not(target_arch = "wasm32"))]` ### Data Model diff --git a/docs/TEST_COVERAGE.md b/docs/TEST_COVERAGE.md index 2ca1f2368e..c8c18df115 100644 --- a/docs/TEST_COVERAGE.md +++ b/docs/TEST_COVERAGE.md @@ -472,10 +472,12 @@ These tests compare the database against an independent answer rather than a sin None of these are counted in the summary. The wasm32 targets run in CI (`.github/workflows/wasm.yml`). -- **In a browser** (`wasm-pack test --headless`, Firefox and Chrome): 43 `wasm_bindgen_test` tests. +- **In a browser** (`wasm-pack test --headless`, Firefox and Chrome): 58 `wasm_bindgen_test` tests. - Twenty-five for `BrowserDb`, including IndexedDB persistence, the cursor, and the flush returning every dirty page and failing on one it cannot read (#470). - Eleven for `BrowserBufferBackend` (which used `#[test]` and never ran before). - Two checking that IndexedDB page keys accept only whole numbers from 0 to 2^53. + - Fifteen for `BrowserDb` user functions (#180), below. +- **`BrowserDb.registerPredicate` / `registerAggregate`** (#180): a predicate filters rows; every value kind reaches JS as its `{type, value}` object (an integer as a `bigint`, a ref as its UUID, a set's elements in canonical order); an aggregate in `:find` and with `:over`; a plain `finish` result read as a bind value; a throw (an `Error`, a string), a predicate returning a non-boolean, a failing `init`/`step`/`finish` and a `finish` result that is a bad ref or not a value each fail with `QRY-014` (`execute`, cursor and prepared query; aggregates also as windows); `TypeError` for a non-function and `INT-040` for a taken or built-in name; from inside a UDF, a nested `query` running a UDF, a registration (not seen by the outer query, seen by the next), and `execute`/`checkpoint`/`importGraph` that resolve after the outer query with both answers right; a value missing from the thread's table fails the call (`called off the JavaScript thread`), and a dropped function frees its entry. - **`BrowserDb.prepare`** (#181): one prepared query run with three bindings; every plain JS value (boolean, whole and fractional `number`, `bigint`, string, `null`) and every `{type, value}` form; a prepared cursor gives the rows of `execute`; `TypeError` for a non-bind value or non-object bindings, `API-017` for a bad UUID, a negative tx count or a value on `null`/`anyValidTime`, `API-022`, `API-023`, `API-024` (both `execute` and `query`) and `API-005`; a later write is seen by the next run. - **`BrowserDb` cursor**: `query()` batches of 1, 7 and 1,000 give the rows of `execute()`; a cursor keeps the answer from when it opened and returns the end after `close()`; a `transact` is `API-012`. - **Page reuse** (#411, #440; five `BrowserDb` tests) diff --git a/docs/superpowers/specs/2026-10-24-udf-uniffi-design.md b/docs/superpowers/specs/2026-10-24-udf-uniffi-design.md index a48fcd3513..35db5c9f69 100644 --- a/docs/superpowers/specs/2026-10-24-udf-uniffi-design.md +++ b/docs/superpowers/specs/2026-10-24-udf-uniffi-design.md @@ -187,7 +187,7 @@ int minigraf_register_aggregate(MiniGrafDb *db, const char *name, minigraf_acc_n - JS can run only on the thread that owns its `napi_env`. Every binding method is synchronous on that thread, so every UDF call happens there. The binding stores the function as a napi reference wrapped in a type marked `Send + Sync`, and records the owning thread's id at registration. A call from any other thread is a failing call, ``called off the JavaScript thread``, and never touches the env. No current code path reaches that check: it guards future async APIs. It is tested by calling the wrapper from a spawned Rust thread in the binding's Rust tests. - Worker threads each have their own env, and a database object belongs to the env that opened it. A function registered on one is called only on that env's thread. -**Browser (`BrowserDb`, this repo)**: `registerPredicate(name, fn)` and `registerAggregate(name, { init, step, finish })`, with the same JS shapes as Node and `js_sys::Function`. wasm32 without atomics has one thread, so the wrapper is `unsafe impl Send + Sync` under `cfg(all(target_arch = "wasm32", not(target_feature = "atomics")))`, and a build with atomics does not compile it. `BrowserDb.execute` is `async` only for the IndexedDB flush, and the query and its UDF calls run synchronously before any `await`. Re-entry differs from §5 here. A query holds `inner.borrow()` (`browser/mod.rs` ~125) while it runs. Every method that takes `borrow_mut` (`checkpoint`, `importGraph`, `apply_write` under `execute`) is `async`, and wasm-bindgen-futures runs its body from a microtask, after the outer query has returned. So from inside a UDF, `query` runs synchronously (it takes only `borrow()`), and `registerPredicate`/`registerAggregate` run synchronously (snapshot rule). `execute`, `checkpoint` and `importGraph` return a Promise that starts only after the outer query, and the UDF cannot wait for it. The plan checks that no synchronous method takes `borrow_mut`, with a browser test per method. +**Browser (`BrowserDb`, this repo)**: `registerPredicate(name, fn)` and `registerAggregate(name, { init, step, finish })`, with the same JS shapes as Node and `js_sys::Function`. The registry needs `Send + Sync` and a `JsValue` is neither. The crate is `#![forbid(unsafe_code)]`, so the registry holds no JavaScript value: each function and live accumulator sits in a `thread_local!` table keyed by an id unique across threads, and the registered closures and the accumulator hold only that id, removing the entry on drop. A call on a thread whose table lacks the id (only a build with atomics could make one) is a failing call, ``called off the JavaScript thread``, as in Node. (An earlier draft of this spec had an `unsafe impl Send + Sync` wrapper under `cfg(not(target_feature = "atomics"))`; the crate's `forbid(unsafe_code)` rules it out, found in PR 7.) `BrowserDb.execute` is `async` only for the IndexedDB flush, and the query and its UDF calls run synchronously before any `await`. Re-entry differs from §5 here. A query holds `inner.borrow()` (`browser/mod.rs` ~125) while it runs. Every method that takes `borrow_mut` (`checkpoint`, `importGraph`, `apply_write` under `execute`) is `async`, and wasm-bindgen-futures runs its body from a microtask, after the outer query has returned. So from inside a UDF, `query` runs synchronously (it takes only `borrow()`), and `registerPredicate`/`registerAggregate` run synchronously (snapshot rule). `execute`, `checkpoint` and `importGraph` return a Promise that starts only after the outer query, and the UDF cannot wait for it. The plan checks that no synchronous method takes `borrow_mut`, with a browser test per method. - Values reach JS as `{type, value}` objects, built with `js_sys::Object` (no new dependency). `BrowserDb`'s registry (`browser/mod.rs:30`) moves to `Functions` with the core's in PR 1. ## 7. Tests diff --git a/src/browser/binds.rs b/src/browser/binds.rs index b26859f452..645c0c7d98 100644 --- a/src/browser/binds.rs +++ b/src/browser/binds.rs @@ -52,13 +52,23 @@ pub(super) fn read_bindings(bindings: &JsValue) -> Result Result { +/// The value `v`, in a bind value's plain or typed form; `what` names it in +/// errors. Used for a user function's result (#180). +pub(super) fn read_value(what: &str, v: &JsValue) -> Result { + match read_bind_value(what, v)? { + BindValue::Val(value) => Ok(value), + _ => Err(invalid(&format!("{what} is not a value"))), + } +} + +/// The bind value `v`; `what` names it in errors (`slot $name`). +fn read_bind_value(what: &str, v: &JsValue) -> Result { if let Some(b) = v.as_bool() { return Ok(BindValue::Val(Value::Boolean(b))); } @@ -69,29 +79,30 @@ fn read_bind_value(name: &str, v: &JsValue) -> Result { return Ok(BindValue::Val(Value::String(s))); } if v.is_bigint() || v.as_f64().is_some() { - return Ok(BindValue::Val(number(name, v)?)); + return Ok(BindValue::Val(number(what, v)?)); } if let Some(set) = v.dyn_ref::() { let mut elements = Vec::new(); set.for_each(&mut |e, _, _| elements.push(e)); - return value_set(name, &format!("slot ${name}"), elements); + return value_set(what, what, elements); } if v.is_object() && !Array::is_array(v) && let Some(kind) = Reflect::get(v, &JsValue::from_str("type"))?.as_string() - && let Some(bind) = read_typed(name, &kind, v)? + && let Some(bind) = read_typed(what, &kind, v)? { return Ok(bind); } Err(type_error(&format!( - "slot ${name}: a bind value is a boolean, number, bigint, string, null, Set or {{type, value}} object" + "{what}: a bind value is a boolean, number, bigint, string, null, Set or {{type, value}} object" ))) } /// A typed form, or `None` if `kind` names no type. -fn read_typed(name: &str, kind: &str, v: &JsValue) -> Result, JsValue> { +fn read_typed(what: &str, kind: &str, v: &JsValue) -> Result, JsValue> { let value = Reflect::get(v, &JsValue::from_str("value"))?; - let what = || format!("slot ${name} of type {kind:?}"); + let label = what; + let what = || format!("{label} of type {kind:?}"); let string = || value.as_string().ok_or_else(|| mismatch(&what())); Ok(Some(match kind { "string" => BindValue::Val(Value::String(string()?)), @@ -115,7 +126,7 @@ fn read_typed(name: &str, kind: &str, v: &JsValue) -> Result, "timestamp" => BindValue::Timestamp(integer(&what(), &value)?), "anyValidTime" if value.is_undefined() => BindValue::AnyValidTime, "set" => match value.dyn_ref::() { - Some(elements) => value_set(name, &what(), elements.iter().collect())?, + Some(elements) => value_set(label, &what(), elements.iter().collect())?, None => return Err(mismatch(&what())), }, // These two take no value. @@ -125,13 +136,13 @@ fn read_typed(name: &str, kind: &str, v: &JsValue) -> Result, } /// The set of `elements`, each read as a bind value that must be a value. -/// Element `i` is named `$name[i]` in errors. -fn value_set(name: &str, what: &str, elements: Vec) -> Result { +/// Element `i` is named `{label}[i]` in errors. +fn value_set(label: &str, what: &str, elements: Vec) -> Result { let values = elements .iter() .enumerate() .map( - |(i, e)| match read_bind_value(&format!("{name}[{i}]"), e)? { + |(i, e)| match read_bind_value(&format!("{label}[{i}]"), e)? { BindValue::Val(v) => Ok(v), _ => Err(invalid(&format!("{what}: element {i} is not a value"))), }, @@ -143,9 +154,9 @@ fn value_set(name: &str, what: &str, elements: Vec) -> Result Result { +fn number(what: &str, v: &JsValue) -> Result { if v.is_bigint() || Number::is_integer(v) { - integer(&format!("slot ${name}"), v).map(Value::Integer) + integer(what, v).map(Value::Integer) } else { Ok(Value::Float(v.as_f64().unwrap_or(f64::NAN))) } diff --git a/src/browser/mod.rs b/src/browser/mod.rs index 7c2284bd1e..6fcd00a954 100644 --- a/src/browser/mod.rs +++ b/src/browser/mod.rs @@ -10,6 +10,8 @@ mod binds; pub mod buffer; /// Async IndexedDB backend for browser WASM persistence. pub mod indexeddb; +/// User-defined functions from JavaScript (#180). +mod udfs; use crate::browser::buffer::BrowserBufferBackend; use crate::browser::indexeddb::IndexedDbBackend; diff --git a/src/browser/udfs.rs b/src/browser/udfs.rs new file mode 100644 index 0000000000..00f72d82d0 --- /dev/null +++ b/src/browser/udfs.rs @@ -0,0 +1,764 @@ +//! User-defined functions from JavaScript: `BrowserDb.registerPredicate` and +//! `BrowserDb.registerAggregate` (#180, spec §6.2). +//! +//! A function receives each value as a `{ type, value }` object, as Node's +//! `FactValue`: `type` is `'string'`, `'integer'` (`value` a `bigint`), +//! `'float'`, `'boolean'`, `'ref'` (an entity UUID string), `'keyword'`, +//! `'null'` (no `value`) or `'set'` (`value` an array of these, in canonical +//! order). An aggregate's `finish` returns a value in any form a prepared +//! query's binding accepts (`binds.rs`): a plain value or a typed form. +//! +//! A function that throws fails the query with `QRY-014`, carrying the +//! thrown error's `message` (else `String(e)`), and so does a predicate that +//! returns anything but a boolean, or a `finish` whose result is not a value. +//! +//! The registry is shared by every thread that queries the database, so it +//! holds no JavaScript value: each function and accumulator lives in a +//! table local to the thread that registered it ([`Held`]), and the +//! registry keeps its id. A call made on any other thread fails with +//! `called off the JavaScript thread` (only a build with atomics could make +//! one). This needs no `unsafe`, which the crate forbids. +//! +//! Every call runs synchronously inside the query that makes it. A query +//! holds `inner.borrow()`, and no synchronous `BrowserDb` method takes +//! `borrow_mut`, so a function may call `query`, `prepare`, `exportGraph` +//! and the `register*` methods. `execute`, `checkpoint` and `importGraph` +//! are `async`: their bodies run from a microtask, after the outer query has +//! returned. + +use super::BrowserDb; +use super::binds::read_value; +use crate::graph::types::Value; +use crate::query::datalog::functions::{UdfError, udf_aggregate, udf_predicate}; +use js_sys::{Array, Function, Reflect}; +use std::cell::RefCell; +use std::collections::HashMap; +use std::sync::atomic::{AtomicU64, Ordering}; +use wasm_bindgen::JsCast; +use wasm_bindgen::prelude::*; + +thread_local! { + /// The JavaScript values this thread's functions hold, by id. + static HELD: RefCell> = RefCell::new(HashMap::new()); +} + +/// Ids for [`Held`], unique across threads, so an id never names another +/// thread's value. +static NEXT_ID: AtomicU64 = AtomicU64::new(0); + +/// A JavaScript value in this thread's [`HELD`] table, removed when this +/// drops. `Send + Sync` as the registry needs, since it is only an id. +struct Held(u64); + +impl Held { + fn new(value: JsValue) -> Held { + let id = NEXT_ID.fetch_add(1, Ordering::Relaxed); + HELD.with(|h| h.borrow_mut().insert(id, value)); + Held(id) + } + + /// The value; never borrowed across a call into JavaScript, which may + /// register functions or open queries that hold new values. + fn get(&self) -> Result { + HELD.with(|h| h.borrow().get(&self.0).cloned()) + .ok_or_else(|| UdfError::new("called off the JavaScript thread")) + } + + fn set(&self, value: JsValue) { + HELD.with(|h| h.borrow_mut().insert(self.0, value)); + } + + fn function(&self) -> Result { + // Only functions are held under a function's id. + Ok(self.get()?.unchecked_into()) + } +} + +impl Drop for Held { + fn drop(&mut self) { + // On another thread, or while the table is torn down, nothing to do. + let _ = HELD.try_with(|h| { + if let Ok(mut h) = h.try_borrow_mut() { + h.remove(&self.0); + } + }); + } +} + +#[wasm_bindgen] +extern "C" { + /// The global `String`, which converts any value, as a template + /// literal does not for a `Symbol`. + #[wasm_bindgen(catch, js_name = String)] + fn js_string(v: &JsValue) -> Result; +} + +#[wasm_bindgen] +impl BrowserDb { + /// Register `fn` as a predicate named `name`, called in a query as + /// `[(name ?v)]`. `fn` receives a `{ type, value }` object and returns + /// a boolean. + /// + /// ```js + /// db.registerPredicate('adult?', (v) => v.type === 'integer' && v.value >= 18n); + /// ``` + /// + /// Synchronous. A query that is already open does not see the + /// function; every query opened afterwards does. Throws a `TypeError` + /// when `fn` is not a function, and `[INT-040]` when `name` is taken. + #[wasm_bindgen(js_name = registerPredicate)] + pub fn register_predicate(&self, name: &str, f: JsValue) -> Result<(), JsValue> { + let f = Held::new(function(f, "the predicate")?.into()); + let desc = udf_predicate(move |v: &Value| { + let result = f + .function()? + .call1(&JsValue::UNDEFINED, &value_to_js(v)) + .map_err(thrown)?; + result.as_bool().ok_or_else(|| { + UdfError::new(format!("returned {}, not a boolean", type_of(&result))) + }) + }); + let functions = self.inner.borrow().functions.clone(); + functions + .register(|reg| reg.register_predicate_desc(name.to_string(), desc)) + .map_err(super::to_js_error) + } + + /// Register an aggregate named `name`, used in `:find` as `(name ?v)` + /// and as a window function with `:over`. `aggregate` is + /// `{ init, step, finish }`: `init()` returns a new accumulator (one per + /// group or window partition), `step(acc, value)` returns the + /// accumulator after `value`, and `finish(acc, count)` returns the + /// result, `count` being the number of values stepped. They are called + /// as plain functions, with no `this`. + /// + /// ```js + /// db.registerAggregate('product', { + /// init: () => 1n, + /// step: (acc, v) => acc * v.value, + /// finish: (acc) => acc, + /// }); + /// ``` + /// + /// Synchronous. Throws a `TypeError` when `init`, `step` or `finish` is + /// not a function, and `[INT-040]` when `name` is taken. + #[wasm_bindgen(js_name = registerAggregate)] + pub fn register_aggregate(&self, name: &str, aggregate: JsValue) -> Result<(), JsValue> { + let member = |key: &str| -> Result { + let f = if aggregate.is_object() { + Reflect::get(&aggregate, &JsValue::from_str(key))? + } else { + JsValue::UNDEFINED + }; + function(f, &format!("the aggregate's {key}")).map(|f| Held::new(f.into())) + }; + let (init, step, finish) = (member("init")?, member("step")?, member("finish")?); + let desc = udf_aggregate( + move || { + let acc = init + .function()? + .call0(&JsValue::UNDEFINED) + .map_err(thrown)?; + Ok(Held::new(acc)) + }, + move |acc: &mut Held, v: &Value| { + let next = step + .function()? + .call2(&JsValue::UNDEFINED, &acc.get()?, &value_to_js(v)) + .map_err(thrown)?; + acc.set(next); + Ok(()) + }, + move |acc: &Held, count: usize| { + // A count past 2^53 is not exact as a number; no query gets near. + #[allow(clippy::cast_precision_loss)] + let count = JsValue::from_f64(count as f64); + let result = finish + .function()? + .call2(&JsValue::UNDEFINED, &acc.get()?, &count) + .map_err(thrown)?; + read_value("finish's result", &result).map_err(thrown) + }, + ); + let functions = self.inner.borrow().functions.clone(); + functions + .register(|reg| reg.register_aggregate_desc(name.to_string(), desc)) + .map_err(super::to_js_error) + } +} + +/// `f` as a function, or a `TypeError` naming `what`. +fn function(f: JsValue, what: &str) -> Result { + f.dyn_into::() + .map_err(|_| js_sys::TypeError::new(&format!("{what} must be a function")).into()) +} + +fn type_of(v: &JsValue) -> String { + v.js_typeof().as_string().unwrap_or_default() +} + +/// What a function threw, as a failed call: the error's `message` when it +/// has a string one, else `String(e)`. A message read from one of +/// `binds.rs`'s coded errors (`[API-017] ...`) drops the code, which +/// belongs to the bind API, not to the query's `QRY-014`. +fn thrown(e: JsValue) -> UdfError { + let message = e + .is_object() + .then(|| Reflect::get(&e, &JsValue::from_str("message")).ok()) + .flatten() + .and_then(|m| m.as_string()) + .or_else(|| e.as_string()) + .or_else(|| js_string(&e).ok()) + .unwrap_or_else(|| "user function failed".to_string()); + UdfError::new(strip_code(&message)) +} + +/// `message` without a leading `[ABC-123] ` error code. +fn strip_code(message: &str) -> &str { + let code = message.get(1..8).unwrap_or_default(); + let is_code = code.len() == 7 + && code.char_indices().all(|(i, c)| match i { + 0..=2 => c.is_ascii_uppercase(), + 3 => c == '-', + _ => c.is_ascii_digit(), + }); + match message.get(10..) { + Some(rest) if is_code && message.starts_with('[') && message.get(8..10) == Some("] ") => { + rest + } + _ => message, + } +} + +/// `v` as a `{ type, value }` object. +fn value_to_js(v: &Value) -> JsValue { + let (kind, value) = match v { + Value::String(s) => ("string", Some(JsValue::from_str(s))), + Value::Integer(i) => ("integer", Some(JsValue::from(*i))), + Value::Float(f) => ("float", Some(JsValue::from_f64(*f))), + Value::Boolean(b) => ("boolean", Some(JsValue::from_bool(*b))), + Value::Ref(id) => ("ref", Some(JsValue::from_str(&id.to_string()))), + Value::Keyword(k) => ("keyword", Some(JsValue::from_str(k))), + Value::Null => ("null", None), + Value::Set(set) => ( + "set", + Some(set.iter().map(value_to_js).collect::().into()), + ), + }; + let object = js_sys::Object::new(); + // `Reflect::set` fails only on a frozen or proxied target. + let _ = Reflect::set( + &object, + &JsValue::from_str("type"), + &JsValue::from_str(kind), + ); + if let Some(value) = value { + let _ = Reflect::set(&object, &JsValue::from_str("value"), &value); + } + object.into() +} + +#[cfg(all(target_arch = "wasm32", feature = "browser", test))] +mod tests { + use super::*; + use std::cell::RefCell; + use std::rc::Rc; + use wasm_bindgen_test::*; + + wasm_bindgen_test_configure!(run_in_browser); + + fn js_fn(args: &str, body: &str) -> JsValue { + Function::new_with_args(args, body).into() + } + + /// A function of one argument from a Rust closure. + fn rust_fn(f: impl FnMut(JsValue) -> JsValue + 'static) -> JsValue { + Closure:: JsValue>::new(f).into_js_value() + } + + fn clone_db(db: &BrowserDb) -> BrowserDb { + BrowserDb { + inner: db.inner.clone(), + } + } + + async fn rows(db: &BrowserDb, q: &str) -> serde_json::Value { + let r = db.execute(q.to_string()).await.expect("query"); + let v: serde_json::Value = serde_json::from_str(&r).unwrap(); + v["results"].clone() + } + + async fn rejection(db: &BrowserDb, q: &str) -> String { + db.execute(q.to_string()) + .await + .expect_err("query must reject") + .as_string() + .expect("rejection is a string") + } + + async fn numbers(db: &BrowserDb) { + db.execute( + "(transact [[:a :n 1] [:b :n 2] [:c :n 3] [:a :g :x] [:b :g :x] [:c :g :y]])".into(), + ) + .await + .expect("transact"); + } + + #[wasm_bindgen_test] + async fn a_predicate_filters_rows() { + let db = BrowserDb::open_in_memory().unwrap(); + numbers(&db).await; + db.register_predicate( + "big?", + js_fn("v", "return v.type === 'integer' && v.value > 1n"), + ) + .expect("register"); + let got = rows(&db, "(query [:find ?n :where [?e :n ?n] [(big? ?n)]])").await; + let mut got: Vec = got + .as_array() + .unwrap() + .iter() + .map(|r| r[0].as_i64().unwrap()) + .collect(); + got.sort_unstable(); + assert_eq!(got, vec![2, 3]); + } + + #[wasm_bindgen_test] + async fn values_reach_javascript_as_typed_objects() { + let db = BrowserDb::open_in_memory().unwrap(); + db.execute( + r#"(transact [[:a :v "s"] [:a :v 7] [:a :v 1.5] [:a :v true] [:a :v :k] [:a :v nil] [:a :v #{2 "x"}]])"#.into(), + ) + .await + .expect("transact"); + let seen = Rc::new(RefCell::new(Vec::::new())); + let log = seen.clone(); + db.register_predicate( + "see", + rust_fn(move |v| { + log.borrow_mut().push( + js_sys::JSON::stringify_with_replacer( + &v, + &js_fn( + "k, x", + "return typeof x === 'bigint' ? x.toString() + 'n' : x", + ), + ) + .unwrap() + .into(), + ); + JsValue::TRUE + }), + ) + .unwrap(); + rows(&db, "(query [:find ?v :where [:a :v ?v] [(see ?v)]])").await; + let mut seen = seen.borrow().clone(); + seen.sort(); + assert_eq!( + seen, + vec![ + r#"{"type":"boolean","value":true}"#, + r#"{"type":"float","value":1.5}"#, + r#"{"type":"integer","value":"7n"}"#, + r#"{"type":"keyword","value":":k"}"#, + r#"{"type":"null"}"#, + r#"{"type":"set","value":[{"type":"string","value":"x"},{"type":"integer","value":"2n"}]}"#, + r#"{"type":"string","value":"s"}"#, + ] + ); + } + + #[wasm_bindgen_test] + async fn a_ref_reaches_javascript_as_its_uuid() { + let db = BrowserDb::open_in_memory().unwrap(); + let kind = Rc::new(RefCell::new(String::new())); + let log = kind.clone(); + db.register_predicate( + "ref?", + rust_fn(move |v| { + let t = Reflect::get(&v, &"type".into()) + .unwrap() + .as_string() + .unwrap(); + let value = Reflect::get(&v, &"value".into()) + .unwrap() + .as_string() + .unwrap(); + *log.borrow_mut() = format!("{t} {}", uuid::Uuid::parse_str(&value).is_ok()); + JsValue::TRUE + }), + ) + .unwrap(); + db.execute("(transact [[:a :friend :b] [:b :name \"b\"]])".into()) + .await + .unwrap(); + rows(&db, "(query [:find ?e :where [?e :name _] [(ref? ?e)]])").await; + assert_eq!(*kind.borrow(), "ref true"); + } + + #[wasm_bindgen_test] + async fn an_aggregate_in_find_and_over() { + let db = BrowserDb::open_in_memory().unwrap(); + numbers(&db).await; + let product = js_sys::Object::new(); + Reflect::set(&product, &"init".into(), &js_fn("", "return 1n")).unwrap(); + Reflect::set( + &product, + &"step".into(), + &js_fn("acc, v", "return acc * v.value"), + ) + .unwrap(); + Reflect::set( + &product, + &"finish".into(), + &js_fn( + "acc, n", + "return { type: 'integer', value: acc * BigInt(n) }", + ), + ) + .unwrap(); + db.register_aggregate("product", product.into()) + .expect("register"); + + let got = rows( + &db, + "(query [:find ?g (product ?n) :where [?e :n ?n] [?e :g ?g]])", + ) + .await; + let mut got: Vec<(String, i64)> = got + .as_array() + .unwrap() + .iter() + .map(|r| (r[0].as_str().unwrap().to_string(), r[1].as_i64().unwrap())) + .collect(); + got.sort(); + // x: 1 * 2, times 2 values; y: 3, times 1 value. + assert_eq!(got, vec![(":x".to_string(), 4), (":y".to_string(), 3)]); + + let got = rows( + &db, + "(query [:find ?n (product ?n :over (:order-by ?n)) :where [?e :n ?n]])", + ) + .await; + let mut got: Vec<(i64, i64)> = got + .as_array() + .unwrap() + .iter() + .map(|r| (r[0].as_i64().unwrap(), r[1].as_i64().unwrap())) + .collect(); + got.sort_unstable(); + // Running product times running count: 1*1, 2*2, 6*3. + assert_eq!(got, vec![(1, 1), (2, 4), (3, 18)]); + } + + #[wasm_bindgen_test] + async fn a_plain_finish_result_is_read_as_a_bind_value() { + let db = BrowserDb::open_in_memory().unwrap(); + numbers(&db).await; + let agg = js_sys::Object::new(); + Reflect::set(&agg, &"init".into(), &js_fn("", "return 0")).unwrap(); + Reflect::set( + &agg, + &"step".into(), + &js_fn("acc, v", "return acc + Number(v.value)"), + ) + .unwrap(); + Reflect::set(&agg, &"finish".into(), &js_fn("acc, n", "return acc / n")).unwrap(); + db.register_aggregate("mean", agg.into()).unwrap(); + let got = rows(&db, "(query [:find (mean ?n) :where [?e :n ?n]])").await; + assert_eq!(got, serde_json::json!([[2]])); + } + + #[wasm_bindgen_test] + async fn a_throw_fails_the_query_with_qry_014() { + let db = BrowserDb::open_in_memory().unwrap(); + numbers(&db).await; + db.register_predicate("boom?", js_fn("v", "throw new Error('bad ' + v.value)")) + .unwrap(); + db.register_predicate("str?", js_fn("v", "throw 'plain'")) + .unwrap(); + db.register_predicate("num?", js_fn("v", "return 1")) + .unwrap(); + let q = |p: &str| format!("(query [:find ?n :where [:a :n ?n] [({p} ?n)]])"); + assert_eq!( + rejection(&db, &q("boom?")).await, + "[QRY-014] user function `boom?` failed: bad 1" + ); + assert_eq!( + rejection(&db, &q("str?")).await, + "[QRY-014] user function `str?` failed: plain" + ); + assert_eq!( + rejection(&db, &q("num?")).await, + "[QRY-014] user function `num?` failed: returned number, not a boolean" + ); + + // Through a cursor, a prepared query and a prepared cursor too. + let mut cursor = db.query(&q("boom?")).expect("cursor opens"); + let err = cursor + .next_batch(10) + .err() + .and_then(|e| e.as_string()) + .unwrap(); + assert!(err.starts_with("[QRY-014]"), "cursor error has QRY-014"); + let pq = db.prepare(&q("boom?")).unwrap(); + let err = pq + .execute(JsValue::UNDEFINED) + .err() + .and_then(|e| e.as_string()) + .unwrap(); + assert!(err.starts_with("[QRY-014]"), "prepared error has QRY-014"); + } + + #[wasm_bindgen_test] + async fn aggregate_failures_fail_the_query() { + let db = BrowserDb::open_in_memory().unwrap(); + numbers(&db).await; + let ok_init = "return 0"; + let ok_step = "return acc"; + let ok_finish = "return 0"; + let bad = "throw new TypeError('no')"; + for (name, init, step, finish, message) in [ + ("bad-init", bad, ok_step, ok_finish, "no"), + ("bad-step", ok_init, bad, ok_finish, "no"), + ("bad-finish", ok_init, ok_step, bad, "no"), + ( + "bad-ref", + ok_init, + ok_step, + "return { type: 'ref', value: 'nope' }", + r#"invalid argument: finish's result of type "ref": "nope" is not a UUID"#, + ), + ( + "bad-entity", + ok_init, + ok_step, + "return { type: 'entity', value: '00000000-0000-0000-0000-000000000000' }", + "invalid argument: finish's result is not a value", + ), + ] { + let agg = js_sys::Object::new(); + Reflect::set(&agg, &"init".into(), &js_fn("", init)).unwrap(); + Reflect::set(&agg, &"step".into(), &js_fn("acc, v", step)).unwrap(); + Reflect::set(&agg, &"finish".into(), &js_fn("acc, n", finish)).unwrap(); + db.register_aggregate(name, agg.into()).unwrap(); + for q in [ + format!("(query [:find ({name} ?n) :where [?e :n ?n]])"), + format!("(query [:find ?n ({name} ?n :over (:order-by ?n)) :where [?e :n ?n]])"), + ] { + assert_eq!( + rejection(&db, &q).await, + format!("[QRY-014] user function `{name}` failed: {message}") + ); + } + } + } + + #[wasm_bindgen_test] + fn registration_errors() { + let db = BrowserDb::open_in_memory().unwrap(); + db.register_predicate("p?", js_fn("v", "return true")) + .unwrap(); + let held = || HELD.with(|h| h.borrow().len()); + let before = held(); + let err = db.register_predicate("p?", JsValue::from(1)).unwrap_err(); + assert!(err.is_instance_of::(), "not a function"); + let partial = js_sys::Object::new(); + Reflect::set(&partial, &"init".into(), &js_fn("", "return 0")).unwrap(); + let err = db.register_aggregate("a", partial.into()).unwrap_err(); + assert!(err.is_instance_of::(), "step missing"); + let err = db.register_aggregate("a", JsValue::NULL).unwrap_err(); + assert!(err.is_instance_of::(), "not an object"); + + let err = db + .register_predicate("p?", js_fn("v", "return true")) + .unwrap_err() + .as_string() + .unwrap(); + assert!(err.starts_with("[INT-040]"), "duplicate name"); + let err = db + .register_predicate("string?", js_fn("v", "return true")) + .unwrap_err() + .as_string() + .unwrap(); + assert!(err.starts_with("[INT-040]"), "built-in name"); + // A failed registration keeps none of the functions it was given. + assert_eq!(held(), before, "no entry left behind"); + } + + #[wasm_bindgen_test] + async fn a_function_may_query_the_same_database() { + let db = BrowserDb::open_in_memory().unwrap(); + numbers(&db).await; + let inner_db = clone_db(&db); + // A nested query that runs a UDF too: true only for ?n = 1. + db.register_predicate("one?", js_fn("v", "return v.value === 1n")) + .unwrap(); + db.register_predicate( + "nested?", + rust_fn(move |v| { + let n = Reflect::get(&v, &"value".into()).unwrap(); + let q = format!( + "(query [:find ?g :where [?e :n {n}] [?e :g ?g] [?e :n ?m] [(one? ?m)]])", + n = js_string(&n).unwrap() + ); + let mut cursor = inner_db.query(&q).expect("nested query"); + let found = cursor.next_batch(10).expect("nested batch").is_some(); + JsValue::from_bool(found) + }), + ) + .unwrap(); + let got = rows(&db, "(query [:find ?n :where [?e :n ?n] [(nested? ?n)]])").await; + assert_eq!(got, serde_json::json!([[1]])); + } + + #[wasm_bindgen_test] + async fn a_function_may_register_a_function() { + let db = BrowserDb::open_in_memory().unwrap(); + numbers(&db).await; + let inner_db = clone_db(&db); + let registered = Rc::new(RefCell::new(false)); + let flag = registered.clone(); + db.register_predicate( + "reg?", + rust_fn(move |_| { + if !*flag.borrow() { + inner_db + .register_predicate("later?", js_fn("v", "return true")) + .expect("register from a UDF"); + *flag.borrow_mut() = true; + } + JsValue::TRUE + }), + ) + .unwrap(); + // The outer query opened before `later?` existed. + let err = rejection( + &db, + "(query [:find ?n :where [?e :n ?n] [(reg? ?n)] [(later? ?n)]])", + ) + .await; + assert!(err.starts_with("[QRY-007]"), "unknown at open"); + rows(&db, "(query [:find ?n :where [?e :n ?n] [(reg? ?n)]])").await; + assert!(*registered.borrow()); + let got = rows(&db, "(query [:find ?n :where [?e :n ?n] [(later? ?n)]])").await; + assert_eq!( + got.as_array().unwrap().len(), + 3, + "visible to the next query" + ); + } + + /// `execute` and `checkpoint` started from a UDF run after + /// the outer query, as they do when JavaScript calls them there: their + /// promise's body starts from a microtask. + #[wasm_bindgen_test] + async fn async_methods_from_a_function_run_after_the_query() { + let db = BrowserDb::open_in_memory().unwrap(); + numbers(&db).await; + let promises = Rc::new(RefCell::new(Vec::::new())); + let started = promises.clone(); + let inner_db = clone_db(&db); + db.register_predicate( + "spawn?", + rust_fn(move |v| { + if Reflect::get(&v, &"value".into()).unwrap() == JsValue::from(1i64) { + let (a, b) = (clone_db(&inner_db), clone_db(&inner_db)); + let mut started = started.borrow_mut(); + started.push(wasm_bindgen_futures::future_to_promise(async move { + a.execute("(transact [[:d :n 4]])".into()) + .await + .map(JsValue::from) + })); + started.push(wasm_bindgen_futures::future_to_promise(async move { + b.checkpoint().await.map(|()| JsValue::UNDEFINED) + })); + } + JsValue::TRUE + }), + ) + .unwrap(); + let got = rows(&db, "(query [:find ?n :where [?e :n ?n] [(spawn? ?n)]])").await; + assert_eq!(got.as_array().unwrap().len(), 3, "the outer query's answer"); + let started: Vec<_> = promises.borrow_mut().drain(..).collect(); + assert_eq!(started.len(), 2); + for p in started { + wasm_bindgen_futures::JsFuture::from(p) + .await + .expect("resolves"); + } + let got = rows(&db, "(query [:find ?n :where [?e :n ?n]])").await; + assert_eq!(got.as_array().unwrap().len(), 4, "the write landed"); + } + + #[wasm_bindgen_test] + async fn import_graph_from_a_function_runs_after_the_query() { + let db = BrowserDb::open_in_memory().unwrap(); + numbers(&db).await; + let other = BrowserDb::open_in_memory().unwrap(); + other + .execute("(transact [[:z :n 9]])".into()) + .await + .unwrap(); + let blob = other.export_graph().unwrap(); + let promise = Rc::new(RefCell::new(None::)); + let slot = promise.clone(); + let inner_db = clone_db(&db); + db.register_predicate( + "swap?", + rust_fn(move |_| { + if slot.borrow().is_none() { + let d = clone_db(&inner_db); + let blob = blob.clone(); + *slot.borrow_mut() = + Some(wasm_bindgen_futures::future_to_promise(async move { + d.import_graph(blob).await.map(|()| JsValue::UNDEFINED) + })); + } + JsValue::TRUE + }), + ) + .unwrap(); + let got = rows(&db, "(query [:find ?n :where [?e :n ?n] [(swap? ?n)]])").await; + assert_eq!(got.as_array().unwrap().len(), 3, "the outer query's answer"); + let p = promise.borrow_mut().take().expect("started"); + wasm_bindgen_futures::JsFuture::from(p) + .await + .expect("resolves"); + let got = rows(&db, "(query [:find ?n :where [?e :n ?n]])").await; + assert_eq!(got, serde_json::json!([[9]]), "the import landed"); + } + + /// Another thread has its own table, where the id is missing; this + /// thread's table without the id stands in for it. + #[wasm_bindgen_test] + fn a_value_missing_from_the_table_fails_the_call() { + let held = Held::new(js_fn("", "return true")); + HELD.with(|h| h.borrow_mut().remove(&held.0)); + assert_eq!( + held.function() + .err() + .map(|e| e.message().to_string()) + .as_deref(), + Some("called off the JavaScript thread") + ); + } + + #[wasm_bindgen_test] + fn dropping_a_held_value_frees_its_entry() { + let held = Held::new(JsValue::TRUE); + let id = held.0; + assert!(HELD.with(|h| h.borrow().contains_key(&id))); + drop(held); + assert!(!HELD.with(|h| h.borrow().contains_key(&id))); + } + + #[wasm_bindgen_test] + fn strip_code_drops_only_a_leading_code() { + assert_eq!(strip_code("[API-017] bad"), "bad"); + assert_eq!(strip_code("[api-017] bad"), "[api-017] bad"); + assert_eq!(strip_code("bad [API-017] x"), "bad [API-017] x"); + assert_eq!(strip_code("[API-017]"), "[API-017]"); + } +} diff --git a/src/db.rs b/src/db.rs index 0760863113..e47e9c7080 100644 --- a/src/db.rs +++ b/src/db.rs @@ -23,10 +23,7 @@ use crate::graph::types::Value; use crate::query::datalog::evaluator::DEFAULT_MAX_DERIVED_FACTS; use crate::query::datalog::executor::DatalogExecutor; use crate::query::datalog::executor::QueryResult; -use crate::query::datalog::functions::{ - AggImpl, AggregateDesc, Functions, PredicateDesc, UdfError, UdfFinaliseFn, UdfInitFn, UdfOps, - UdfStepFn, -}; +use crate::query::datalog::functions::{Functions, UdfError, udf_aggregate, udf_predicate}; use crate::query::datalog::parser::parse_datalog_command; use crate::query::datalog::parser::statement::{self, Kind}; use crate::query::datalog::prepared::reject_unbound_slots; @@ -1564,27 +1561,7 @@ impl Minigraf { where Acc: Any + Send + 'static, { - let init_boxed: UdfInitFn = Arc::new(move || Ok(Box::new(init()?) as Box)); - let step_boxed: UdfStepFn = Arc::new(move |acc, v| { - // `init_boxed` always creates `Box`, so downcast is infallible. - match acc.downcast_mut::() { - Some(typed_acc) => step(typed_acc, v), - None => Ok(()), - } - }); - let finalise_boxed: UdfFinaliseFn = - Arc::new(move |acc, n| match acc.downcast_ref::() { - Some(typed_acc) => finalise(typed_acc, n), - None => Ok(Value::Null), - }); - - let desc = AggregateDesc { - impl_: AggImpl::Udf(UdfOps { - init: init_boxed, - step: step_boxed, - finalise: finalise_boxed, - }), - }; + let desc = udf_aggregate(init, step, finalise); self.inner .functions .register(|reg| reg.register_aggregate_desc(name.to_string(), desc)) @@ -1682,10 +1659,7 @@ impl Minigraf { name: &str, f: impl Fn(&Value) -> std::result::Result + Send + Sync + 'static, ) -> Result<()> { - let desc = PredicateDesc { - f: Arc::new(f), - is_builtin: false, - }; + let desc = udf_predicate(f); self.inner .functions .register(|reg| reg.register_predicate_desc(name.to_string(), desc)) diff --git a/src/query/datalog/functions.rs b/src/query/datalog/functions.rs index 2c56d3b14f..1b290f72f0 100644 --- a/src/query/datalog/functions.rs +++ b/src/query/datalog/functions.rs @@ -194,6 +194,47 @@ impl FunctionRegistry { } } +/// The descriptor of a user aggregate over a typed accumulator `Acc`, which +/// the registry holds type-erased. +pub(crate) fn udf_aggregate( + init: impl Fn() -> Result + Send + Sync + 'static, + step: impl Fn(&mut Acc, &Value) -> Result<(), UdfError> + Send + Sync + 'static, + finalise: impl Fn(&Acc, usize) -> Result + Send + Sync + 'static, +) -> AggregateDesc +where + Acc: Any + Send + 'static, +{ + let init_boxed: UdfInitFn = Arc::new(move || Ok(Box::new(init()?) as Box)); + let step_boxed: UdfStepFn = Arc::new(move |acc, v| { + // `init_boxed` always creates `Box`, so downcast is infallible. + match acc.downcast_mut::() { + Some(typed_acc) => step(typed_acc, v), + None => Ok(()), + } + }); + let finalise_boxed: UdfFinaliseFn = Arc::new(move |acc, n| match acc.downcast_ref::() { + Some(typed_acc) => finalise(typed_acc, n), + None => Ok(Value::Null), + }); + AggregateDesc { + impl_: AggImpl::Udf(UdfOps { + init: init_boxed, + step: step_boxed, + finalise: finalise_boxed, + }), + } +} + +/// The descriptor of a user predicate. +pub(crate) fn udf_predicate( + f: impl Fn(&Value) -> Result + Send + Sync + 'static, +) -> PredicateDesc { + PredicateDesc { + f: Arc::new(f), + is_builtin: false, + } +} + /// The registry a database shares between its handles (#180, spec §5). A /// query reads its functions from a [`snapshot`](Functions::snapshot) taken /// when it opens, so no lock is held while a user function runs, and a