Skip to content

DuckDB Extension

Rhai (evalexpr_rhai)

Evaluate Rhai scripts against row values in DuckDB SQL. Pass a context object into a script and inspect either its JSON result or its error.

37,239
extension loads · last 90 days
On this page
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);

Evaluate rules written in a small scripting language

Rhai is an embedded scripting language with variables, conditions, loops, and functions. This extension evaluates a Rhai expression from SQL and lets you pass row values into the script as JSON context.

Use it for configurable scoring rules or transformations that are awkward to express as SQL. The script can live in a literal or a column. Each evaluation returns either a JSON result or an error value, so you can inspect failed rows alongside successful ones.

Example

Apply a rule to each row

The script reads the salary from its context. The .ok field contains the successful result.

Tested with DuckDB 1.5.6. Browser examples use Haybarn-Wasm 2.0.0-rc12.

SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
  WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL evalexpr_rhai FROM query_farm;
LOAD evalexpr_rhai FROM query_farm;

WITH employees(name, salary) AS (
  VALUES ('Mina', 95000), ('Theo', 120000)
)
SELECT name,
       evalexpr_rhai('context.salary > 100000', {'salary': salary}).ok AS high_earner
FROM employees
ORDER BY name;

Expected result

name high_earner
Mina false
Theo true

The return value is a union of ok and error. Check the error arm when scripts or inputs may be invalid.

Common tasks

Deep Dive

Technical Details

Reference

Extension Contents

Quick reference to all available functions and settings organized by category.

Configuration
evalexpr_rhai_max_array_size Object type: Setting Maximum number of elements of any array created by an evalexpr_rhai() script (0 = unlimited)
evalexpr_rhai_max_call_levels Object type: Setting Maximum depth of nested function calls in an evalexpr_rhai() script (0 = unlimited)
evalexpr_rhai_max_map_size Object type: Setting Maximum number of properties of any object map created by an evalexpr_rhai() script (0 = unlimited)
evalexpr_rhai_max_operations Object type: Setting Maximum number of Rhai operations a single evalexpr_rhai() evaluation may perform (0 = unlimited)
evalexpr_rhai_max_string_size Object type: Setting Maximum length in bytes of any string created by an evalexpr_rhai() script (0 = unlimited)
Execute
evalexpr_rhai() Object type: Scalar function 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).

API Reference

Function Reference

Configuration

Settings

Configure the rhai (evalexpr_rhai) extension behavior using these settings.

evalexpr_rhai_max_array_size

Default Value: 1000000

evalexpr_rhai_max_call_levels

Default Value: 64

evalexpr_rhai_max_map_size

Default Value: 1000000

evalexpr_rhai_max_operations

Default Value: 100000000

evalexpr_rhai_max_string_size

Default Value: 16777216

Practical Examples

Cookbook

Real-world recipes and patterns for common use cases.

Platform Support

Compatibility

Extension availability may vary by platform and DuckDB version. Check below to ensure this extension supports your environment before installation.

Quick Facts

Release status Stable
Software License MIT
Pricing Free
Written In Rust
Source Available Yes
View on GitHub
Usage
37,239
loads · last 90 days

Platforms

DuckDB 2.0.0

  • Linux x86_64aarch64
  • Linux (musl) Not available
  • macOS Apple Silicon
  • Windows Not available
  • WASM Not available
Compiled binary sizes
Platform Architecture Size
Linux x86_64 6.71 MB
Linux aarch64 5.94 MB
macOS Apple Silicon 4.41 MB

Compressed download sizes for the release shown above.

DuckDB & Haybarn

Release calendar
  • DuckDB v2.0.0 Query.Farm repository Supported
  • Haybarn v1.5.6 Built-in community repository Supported

Browser examples use Haybarn-Wasm 2.0.0-rc12.

Checked .

Learn and explore

Tutorials and articles

Writing about Rhai (evalexpr_rhai): how it works, what changed, and why.