Execute functions in the Rhai (evalexpr_rhai) DuckDB extension
Sample data for the examples
Each browser example starts with these sample tables; sample files are shared across examples on this page. Run this setup first when trying the examples in your own DuckDB session.
-- Fixtures for evalexpr_rhai cookbook / technical-details / functions.json examples.
-- Small, deterministic, idempotent. Tables created here back the snippets that
-- read from `employees`, `orders`, and `jobs`. `group_membership` and
-- `eligibility` are intentionally NOT created here because the cookbook /
-- technical-details snippets `CREATE TABLE` them inline.
CREATE TABLE IF NOT EXISTS employees (
id INTEGER,
name TEXT,
salary INTEGER,
years INTEGER,
title TEXT
);
INSERT INTO employees
SELECT * FROM (VALUES
(1, 'Rusty', 150000, 7, 'Engineering Lead'),
(2, 'George', 120000, 6, 'Lead Engineer'),
(3, 'John', 95000, 4, 'Shift Lead'),
(4, 'Alex', 72000, 2, 'Engineer')
) AS v(id, name, salary, years, title)
WHERE NOT EXISTS (SELECT 1 FROM employees);
CREATE TABLE IF NOT EXISTS orders (
id INTEGER,
qty INTEGER,
price DOUBLE,
tier TEXT
);
INSERT INTO orders
SELECT * FROM (VALUES
(1, 2, 9.99, 'gold'),
(2, 5, 19.50, 'silver'),
(3, 1, 49.00, 'bronze'),
(4, 3, 12.25, 'gold')
) AS v(id, qty, price, tier)
WHERE NOT EXISTS (SELECT 1 FROM orders);
CREATE TABLE IF NOT EXISTS jobs (
id INTEGER,
rule TEXT,
x INTEGER
);
INSERT INTO jobs
SELECT * FROM (VALUES
(1, 'context.x * 2', 5),
(2, 'context.x + 10', 7),
(3, '1 / 0', 0),
(4, 'context.x > 0', 3)
) AS v(id, rule, x)
WHERE NOT EXISTS (SELECT 1 FROM jobs);
Function category
Execute
1 functionRun Rhai 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; a NULL context runs the script without a context variable. Compiled scripts are cached per thread, so repeat evaluations skip the parse step. In the DuckDB 2.0 build every evaluation is bounded by the evalexpr_rhai_max_operations, evalexpr_rhai_max_string_size, evalexpr_rhai_max_array_size, evalexpr_rhai_max_map_size and evalexpr_rhai_max_call_levels settings, a running script can be cancelled, and scripts have no filesystem access. The function is VOLATILE: scripts can call random-number functions, so every row is evaluated.
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: ...'
SELECT evalexpr_rhai('loop {}').error;
-- 'Too many operations (line 1, position ...)'