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

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 1.5.6.

INSTALL evalexpr_rhai FROM community;
LOAD evalexpr_rhai;

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.

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

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_64 aarch64
  • 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 1.5.6.

Checked .