⌘+k ctrl+k
1.1.3 (stable)
Search Shortcut cmd + k | ctrl + k
Sharing Macros

DuckDB has a powerful macro mechanism that allows creating shorthands for common tasks. For example, we can define a macro that pretty-prints a non-negative integer as a short string that contains billions, millions, and thousands (without rounding) as follows:

duckdb pretty_print_integer_macro.duckdb
CREATE MACRO pretty_print_integer(n) AS
    CASE
        WHEN n >= 1_000_000_000 THEN printf('%dB', n // 1_000_000_000)
        WHEN n >= 1_000_000     THEN printf('%dM', n // 1_000_000)
        WHEN n >= 1_000         THEN printf('%dk', n // 1_000)
        ELSE printf('%d', n)
    END;

SELECT pretty_print_integer(25_500_000) AS x;
┌─────────┐
│    x    │
│ varchar │
├─────────┤
│ 25M     │
└─────────┘

As one would expect, the macro gets persisted in the database. But this also means that we can host it on an HTTPS endpoint and share it with anyone! We have published this macro on blobs.duckdb.org

Let's start a new DuckDB session and try it:

duckdb

We can now attach to the remote endpoint and use the macro:

ATTACH 'https://blobs.duckdb.org/data/pretty_print_integer_macro.duckdb' AS db;
USE db;
SELECT pretty_print_integer(42_123) AS x;
┌─────────┐
│    x    │
│ varchar │
├─────────┤
│ 42k     │
└─────────┘

Warning Currently, sharing table macros via attaching is not supported.