Skip to content

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 function

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: ...'
5 A runaway script hits the operation limit instead of hanging the query (DuckDB 2.0 build)
SELECT evalexpr_rhai('loop {}').error;
-- 'Too many operations (line 1, position ...)'