Skip to content

Execute functions in the Rhai (evalexpr_rhai) DuckDB extension

Function category

Execute

1 function

Run [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.

evalexpr_rhai

Scalar function Execute
Signature
2 overloaded forms · click to inspect
Arguments (Positional)
Argument expression Type VARCHAR Mode Positional Description
Description
1 Simple expression
SELECT evalexpr_rhai('5 + 6').ok;
-- 11
2 Per-row evaluation with context
SELECT name,
       evalexpr_rhai('context.salary > 100000',
                     { 'salary': salary }).ok AS high_earner
FROM employees;
3 Full function definition inside the script
SELECT evalexpr_rhai('fn sq(x) { x * x }  sq(context.n)',
                     { 'n': 7 }).ok;
-- 49
4 Read the error arm on failure (script keeps the row)
SELECT evalexpr_rhai('1 / 0').error;
-- 'Runtime error: ...'