Query 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
Query
3 functionsMembership tests, autocomplete (predictive prefix), and longest-prefix-match against an existing trie BLOB. All three primitives run in O(|key|) — lookup work is independent of dictionary size.
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
trie
|
Type
BLOB
|
Mode Positional | Description |
Argument
search
|
Type
VARCHAR
|
Mode Positional | Description |
Argument
max_results
|
Type
INTEGER
|
Mode Positional | Description |
Description
Returns VARCHAR[] — every string in the trie that is a prefix of the search string, capped at max_results. The inverse of marisa_predictive: useful for routing tables, IP / CIDR label lookups, and longest-prefix-match style queries ("which rules match this input?").
SELECT marisa_common_prefix(trie, 'USA', 10) AS matches
FROM country_codes_trie;
-- ['U', 'US', 'USA']
Related functions
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
trie
|
Type
BLOB
|
Mode Positional | Description |
Argument
search
|
Type
VARCHAR
|
Mode Positional | Description |
Description
Returns BOOLEAN — true iff the search string is in the trie. The fastest membership test in this extension; an exact, allocation-free yes/no against the BLOB. Use it for spell-check, dictionary membership, or anywhere a hash set would otherwise live.
SELECT marisa_lookup(trie, 'duckling') AS in_dictionary
FROM words_trie;
Related functions
- 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`
- marisa_trie() — Aggregate that builds a MARISA trie from a column of strings
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
trie
|
Type
BLOB
|
Mode Positional | Description |
Argument
prefix
|
Type
VARCHAR
|
Mode Positional | Description |
Argument
max_results
|
Type
INTEGER
|
Mode Positional | Description |
Description
Returns VARCHAR[] — every string in the trie that starts with the given prefix, capped at max_results. The autocomplete primitive: feed it the user's partial input, get back the candidate completions. The cap is a hard cutoff so a short prefix on a million-entry dictionary doesn't return everything.
SELECT marisa_predictive(trie, 'duck', 10) AS suggestions
FROM words_trie;