Code Engine - Powered by Wasm
On this page
The Code Engine feature in SingleStore supports creating functions (UDFs and TVFs) using code compiled to WebAssembly (Wasm).wasmtime
runtime to compile and run WebAssembly code.
This feature supports any language that can compile to the Wasm core specification, which allows you to create UDFs/TVFs in a language of your choice using existing code libraries and run them in a sandboxed environment for enhanced security.16MB
of memory.GROW TO
clause to modify the memory allocated to a Wasm function sandbox.
Once a Wasm function is added to SingleStore, it becomes a part of the database.
Why use Wasm-based Functions
SingleStore recommends using Wasm-based functions for integrating complex logic and data structures or using existing C/C++ or Rust code.
Key benefits to using Wasm:
-
Security: Run your code in a sandboxed environment for enhanced security.
-
Code reusability: Leverage existing code base without rewriting the logic in SQL.
-
Code integration: Integrate complex logic and data structures that cannot be expressed using PSQL.
If your logic and data structures can be implemented in Procedural SQL (PSQL) with reasonable effort in a way that performs well, use PSQL.
Use Cases
The following is a list of common real-world applications that have been implemented using Wasm-based functions:
-
Sentiment Analysis: Perform sentiment analysis using Valence Aware Dictionary and Sentiment Reasoner (VADER).
-
Bloom Filters: Generate Bloom filters from a column of string values and then run the Bloom filter on another column.
-
JSON Flatten: Perform flattening operations on JSON objects.
-
HyperLogLog (HLL) Sketches: Generate HLL sketches using different operations and algorithms and return diagnostic information.
-
Theta Sketches: Generate Theta sketches using different operations and algorithms and return diagnostic information.
Build Wasm-based Functions
You can build Wasm extensions for SingleStore on Mac, Windows, or Linux using the Docker development containers.
For more examples, refer to Create Wasm UDFs or Create Wasm TVFs.
Runtime Constraints
The Wasm UDF/TVF must not require runtime capabilities that the database does not provide out of the box.
Related Topics
Information on supported Wasm ABIs. |
|
Configure Wasm extensibility using engine variables. |
|
Create Wasm-based user-defined functions (UDFs). |
|
Create Wasm-based user-defined table-valued functions (TVFs). |
|
Understand when to use PSQL, Wasm-based, or external functions. |
|
Information on creating Procedural SQL (PSQL) functions or procedures. |
FAQs
-
Can I use a Wasm binary outside of the SingleStore database once it is imported into the database?
No.
Once a Wasm binary is imported into a SingleStore database, you cannot use it for applications outside the database. However, you can make a copy of the binary and use it instead. -
What happens if there is a mismatch between the WIT file and the generated code?
A mismatch may cause a Wasm trap or unexpected results.
The exact behavior depends on how the Wasm program interprets the arguments. -
Can I reverse engineer a Wasm binary once it is imported into the database?
No.
Once a Wasm binary is imported into the database, it is not possible to see the binary data, it can only be executed. Hence, you can only reverse engineer the Wasm binary behaviorally and not by disassembling the binary. -
What happens when a Wasm function crashes or runs out of memory?
If a Wasm function crashes or runs out of memory, it causes the query to fail and returns an error.
If possible, the error message may include a stack into the Wasm function.
In this section
Last modified: May 12, 2025