Execute functions in the Rhai (evalexpr_rhai) DuckDB extension
Function category
Execute
1 functionRun [Rhai](https://rhai.rs/) scripts inside SQL — single-function surface. Pass an optional `JSON` context to expose per-row variables as `context.<field>`. The result is a `UNION(ok JSON, "error" VARCHAR)` so script failures don't break the surrounding query — read `.ok` on success, `.error` on failure.
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
expression
|
Type
VARCHAR
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
expression
|
Type
VARCHAR
|
Mode Positional | Description |
Argument
context
|
Type
JSON
|
Mode Positional | Description |
Description
Evaluate a Rhai expression or full script and return the result as a DuckDB UNION of ok (the JSON-encoded result on success) or error (the error message on failure). With one argument, the script runs with no per-row context. With two arguments, the second is a JSON object exposed inside the script as context.<field> — the standard pattern for per-row evaluation. Constant expressions are parsed and cached the first time they're seen, so repeat evaluations skip the parse step.
SELECT evalexpr_rhai('5 + 6').ok;
-- 11
SELECT name,
evalexpr_rhai('context.salary > 100000',
{ 'salary': salary }).ok AS high_earner
FROM employees;
SELECT evalexpr_rhai('fn sq(x) { x * x } sq(context.n)',
{ 'n': 7 }).ok;
-- 49
SELECT evalexpr_rhai('1 / 0').error;
-- 'Runtime error: ...'