Search Shortcut cmd + k | ctrl + k
luajit

In-process LuaJIT UDFs for DuckDB — 12 functions, JIT-compiled, GROUP BY, batch mode ~3.6x native (single-thread)

Maintainer(s): alitrack

Installing and Loading

INSTALL luajit FROM community;
LOAD luajit;

Example

-- Load and compile a UDF in one shot
LOAD luajit;
SELECT message FROM luajit_module(
    mode := 'quick_compile',
    source := 'return function(x) return x * 2 + 1 end',
    sql_name := 'x2p1'
);
-- Use it immediately (auto-macro generated)
SELECT x2p1(5);
-- → 11

About luajit

luajit — JIT-compiled Lua UDFs for DuckDB

Write and run Lua functions directly in SQL. LuaJIT compiles them to native machine code with trace-based JIT, near-native performance (batch mode ~3.6× native SQL single-threaded, ~2500× faster than Python UDF).

12 Functions:

  • luajit_i / luajit_f / luajit_b — typed scalar UDFs (BIGINT, DOUBLE, BOOLEAN)
  • luajit_vchunk-batched scalar (1 Lua call per chunk, ~3.6× native single-thread)
  • luajit_m — mixed-type auto-cast
  • luajit_l / luajit_s / luajit_map — LIST/STRUCT/MAP type bridges
  • luajit_agg — aggregate UDFs with GROUP BY support
  • luajit_table — table-generating UDFs
  • luajit_module — module management (10 modes)

Features:

  • Compile + auto-type + auto-macro in one call (quick_compile)
  • Aggregate UDFs receive FULL value table, enabling median, stddev, percentile, etc.
  • GROUP BY support with per-group Lua invocation
  • File-based persistence (save/load) with auto-restore on LOAD
  • Lua→DuckDB callback (_duckdb_call) for SQL execution from Lua
  • LIST, STRUCT, MAP type bridges between DuckDB and Lua

Performance (500K rows, single-threaded):

  • luajit_v (batch): 4.9ms (~3.6× native SQL)
  • luajit_i (row-by-row): 24.3ms (~18× native)
  • Python UDF: >160s (~200,000× native)
  • Note: shared Lua state serializes under parallel execution; single-thread only

Design:

  • LuaJIT 2.1 self-contained (~700KB binary)
  • Zero external dependencies at runtime
  • MIT licensed
  • ~1050 lines of C

Platforms: Linux x64/arm64, macOS x64/arm64, Windows (MSVC). WASM excluded (LuaJIT requires GNU Make + GCC).

Added Functions

function_name function_type description comment examples
luajit scalar NULL NULL  
luajit_agg aggregate NULL NULL  
luajit_b scalar NULL NULL  
luajit_f scalar NULL NULL  
luajit_i scalar NULL NULL  
luajit_l scalar NULL NULL  
luajit_m scalar NULL NULL  
luajit_map scalar NULL NULL  
luajit_module table NULL NULL  
luajit_s scalar NULL NULL  
luajit_table table NULL NULL  
luajit_v scalar NULL NULL  

Overloaded Functions

This extension does not add any function overloads.

Added Types

This extension does not add any types.

Added Settings

This extension does not add any settings.