Execute functions in the QuickJS 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 quickjs cookbook / technical-details / functions.json examples.
-- Small, deterministic, idempotent. Backs snippets that read from `events`
-- (raw event rows with a JSON `payload` column) and `transformed_events`
-- (the result-shaped table that holds an `extracted` JSON column).
CREATE OR REPLACE TABLE events (
id INTEGER,
event_type TEXT,
payload TEXT
);
INSERT INTO events VALUES
(1, 'login', '{"event":"login","user":"alice","meta":{"device":"phone","os":"ios"}}'),
(2, 'purchase', '{"event":"purchase","user":"bob","amount":42.50,"meta":{"item":"book","os":"android","currency":"USD"}}'),
(3, 'login', '{"event":"login","user":"carol","meta":{"device":"laptop","os":"linux"}}');
CREATE OR REPLACE TABLE transformed_events (
id INTEGER,
extracted JSON
);
INSERT INTO transformed_events VALUES
(1, '{"user":"alice","os":"ios"}'),
(2, '{"user":"bob","os":"android"}');
Function category
Execute
2 functionsRun JavaScript code from SQL — quickjs(...) for general expressions and array → table fan-out, quickjs_eval(fn, ...args) for invoking a JS function with positional arguments and getting JSON back.
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
code
|
Type
VARCHAR
|
Mode Positional | Description |
Arguments
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
code
|
Type
VARCHAR
|
Mode Named | Description |
Argument
varargs
|
Type
ANY
|
Mode Positional | Description Varargs |
Description
Evaluate JavaScript code and return its completion value (the value of the last expression or statement). Has a scalar form (returns the value converted to VARCHAR; each row runs in a fresh JavaScript context) and a table form (the code must evaluate to an array and each element becomes a row of the JSON column result). In the table form extra arguments are available as arg0, arg1, … and parsed_arg0 (arg0 parsed as JSON when it is a JSON string, otherwise arg0 itself); the code may contain several statements. A returned Promise is awaited. Execution is bounded by quickjs_memory_limit, quickjs_timeout_ms and max_execution_time, and can be cancelled.
SELECT quickjs('2 + 2');
SELECT quickjs('let words = ["duck", "db"]; words.map(w => w.toUpperCase()).join(" ")');
SELECT * FROM quickjs('[1, 2, 3, 4, 5]');
SELECT * FROM quickjs('parsed_arg0.map(x => x * arg1)', '[1, 2, 3, 4, 5]', 3);
SELECT * FROM quickjs('const out = []; for (const x of arg0) out.push({x, square: x * x}); out', [1, 2, 3]);
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
function
|
Type
VARCHAR
|
Mode Positional | Description |
Argument
varargs
|
Type
ANY
|
Mode Positional | Description Varargs |
Description
Call a JavaScript function with the given arguments and return its result as JSON. The first argument is a JS function expression; the remaining arguments are passed positionally as native JavaScript values: integers and DECIMAL as numbers (BIGINT/HUGEINT beyond 2^53 as BigInt), VARCHAR as strings, BOOLEAN as booleans, DATE and TIMESTAMP types as Date, BLOB as Uint8Array, LIST/ARRAY as arrays, STRUCT/MAP as objects, NULL as null; other types arrive as their string form. The result is serialized with JSON.stringify (BigInt as a JSON number); undefined returns SQL NULL. Async functions are awaited. The function is compiled once per thread and reused while the script text stays the same.
SELECT quickjs_eval('(a, b) => a + b', 5, 3);
SELECT quickjs_eval('(tags, user) => tags.map(t => user.name + ":" + t)', ['a', 'b'], {'name': 'duck'});
SELECT quickjs_eval('(d) => d.getUTCDay()', DATE '2024-07-04');
SELECT quickjs_eval('(payload) => { const d = JSON.parse(payload); return { user: d.user, os: d.meta.os }; }', payload) AS extracted FROM events;
SELECT quickjs_eval('(x) => x > 0 ? x : undefined', -1) IS NULL;