Build functions in the Marisa 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 marisa example snippets. Idempotent: uses CREATE OR REPLACE.
-- Source: cookbook.mdx "Build a trie"
CREATE OR REPLACE TABLE employees(name TEXT);
INSERT INTO employees VALUES
('Alice'),('Bob'),('Charlie'),('David'),('Eve'),('Frank'),
('Mallory'),('Megan'),('Oscar'),('Melissa');
-- Trie built from employees, referenced by lookup/predictive examples.
CREATE OR REPLACE TABLE employees_trie AS
SELECT marisa_trie(name) AS trie FROM employees;
-- Source: cookbook.mdx "Longest-prefix match"
CREATE OR REPLACE TABLE country_codes(code TEXT);
INSERT INTO country_codes VALUES ('U'), ('US'), ('USA');
CREATE OR REPLACE TABLE country_codes_trie AS
SELECT marisa_trie(code) AS trie FROM country_codes;
-- Stand-in for the /usr/share/dict/words wordlist used by the
-- "Big static dictionary" cookbook section. Small, deterministic
-- set with shared "duck" prefixes so marisa_predictive(..., 'duck', ...)
-- still returns plausible completions.
CREATE OR REPLACE TABLE words(word TEXT);
INSERT INTO words VALUES
('duck'), ('duckbill'), ('duckboard'), ('ducker'), ('ducking'),
('duckling'), ('ducks'), ('ducky'),
('rouge'), ('rough'), ('round'), ('route'), ('routine'),
('apple'), ('banana'), ('cherry');
CREATE OR REPLACE TABLE words_trie AS
SELECT marisa_trie(word) AS trie FROM words;
-- "dictionary" alias used by technical-details.mdx "What you can do with one query".
CREATE OR REPLACE TABLE dictionary(word TEXT);
INSERT INTO dictionary SELECT word FROM words;
-- Source: cookbook.mdx "One trie per partition / tenant"
CREATE OR REPLACE TABLE signups(day DATE, name TEXT);
INSERT INTO signups VALUES
(DATE '2024-01-01', 'Alice'),
(DATE '2024-01-01', 'Megan'),
(DATE '2024-01-02', 'Bob'),
(DATE '2024-01-02', 'Melissa'),
(DATE '2024-01-03', 'Charlie');
CREATE OR REPLACE TABLE name_tries AS
SELECT day,
marisa_trie(name) AS trie
FROM signups
GROUP BY day;
Function category
Build
1 functionThe aggregate that turns a column of strings into a single MARISA trie BLOB. Build once at ingest time, store the BLOB in any column type, query repeatedly. Updates require a rebuild — there is no incremental insert.
Signature
marisa_trie(value: VARCHAR) → BLOB
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
value
|
Type
VARCHAR
|
Mode Positional | Description |
Description
Aggregate that builds a MARISA trie from a column of strings. Returns a single self-contained BLOB you can store in any column type, persist in Parquet, or ship to another DuckDB instance. The trie is static — to add or remove keys, rebuild from the updated source set.
1
Build a trie from a column at ingest time
CREATE OR REPLACE TABLE employees_trie AS
SELECT marisa_trie(name) AS trie FROM employees;
2
One trie per logical partition (day, tenant, region)
CREATE OR REPLACE TABLE name_tries AS
SELECT day,
marisa_trie(name) AS trie
FROM signups
GROUP BY day;
Related functions
- marisa_lookup() — Returns `BOOLEAN` — `true` iff the search string is in the trie
- marisa_predictive() — Returns `VARCHAR[]` — every string in the trie that **starts with** the given prefix, capped at `max_results`
- marisa_common_prefix() — Returns `VARCHAR[]` — every string in the trie that is **a prefix of** the search string, capped at `max_results`