WASM UDFs
PyroSQL supports WebAssembly User-Defined Functions (WASM UDFs) — custom SQL functions compiled to the WebAssembly binary format and executed inside the database engine. WASM UDFs run in a sandboxed, deterministic environment with near-native performance and no external network access. Once registered, they are callable from any SQL statement.
Common use cases:
- ML inference — sentiment analysis, classification, or scoring functions compiled from Python or Rust.
- Text processing — tokenisation, stemming, or custom normalisation logic.
- Domain-specific calculations — financial formulae, geo computations, or physics models that are expensive to round-trip to the application layer.
- Data validation — complex constraints that cannot be expressed in SQL.
WasmUdfManager
WasmUdfManager provides a PHP interface for registering, inspecting, and dropping WASM UDFs in PyroSQL.
use Weaver\ORM\PyroSQL\WasmUdf\WasmUdfManager;
use Weaver\ORM\PyroSQL\PyroSqlDriver;
$driver = new PyroSqlDriver($connection);
$manager = new WasmUdfManager($connection, $driver);
Registering functions
registerFromFile()
Register a WASM UDF from a .wasm binary file. The file is read from disk, base64-encoded, and sent to PyroSQL in a CREATE FUNCTION statement.
$manager->registerFromFile(
name: 'sentiment_score',
wasmPath: '/var/app/wasm/sentiment.wasm',
returnType: 'FLOAT',
args: ['TEXT'],
);
Executes:
CREATE FUNCTION "sentiment_score"(TEXT) RETURNS FLOAT
LANGUAGE wasm AS '<base64-encoded wasm binary>'
Pass $replace = true to overwrite an existing function without raising an error:
$manager->registerFromFile(
name: 'sentiment_score',
wasmPath: '/var/app/wasm/sentiment_v2.wasm',
returnType: 'FLOAT',
args: ['TEXT'],
replace: true,
);
Executes:
CREATE OR REPLACE FUNCTION "sentiment_score"(TEXT) RETURNS FLOAT
LANGUAGE wasm AS '...'
registerFromBase64()
Register a WASM UDF from a base64-encoded binary string. Useful when the .wasm binary is stored in a secrets manager, a database, or arrives over a network.
$base64 = base64_encode(file_get_contents('/var/app/wasm/classify.wasm'));
$manager->registerFromBase64(
name: 'classify_intent',
base64: $base64,
returnType: 'TEXT',
args: ['TEXT'],
replace: false,
);
Supported SQL types
Both $returnType and $args accept standard SQL type strings. The type validator accepts TEXT, INT, FLOAT, DOUBLE PRECISION, BOOLEAN, CHAR(n), VARCHAR(n), and similar ANSI-style type expressions.
// Multi-argument function
$manager->registerFromFile(
name: 'levenshtein_similarity',
wasmPath: '/var/app/wasm/levenshtein.wasm',
returnType: 'FLOAT',
args: ['TEXT', 'TEXT'],
);