Hilbert functions in the Lindel 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 the lindel cookbook + technical-details + functions.json
-- examples. Small, deterministic, idempotent — re-running is a no-op.
-- A generic source table with every column the docs reach for: spatial
-- (lat, lon, alt), temporal (time as epoch seconds), and a categorical
-- id (symbol_id) for the Morton time-series example.
CREATE OR REPLACE TABLE source AS
SELECT
(40 + (i % 10) * 0.1)::DOUBLE AS lat,
(-74 + (i % 10) * 0.1)::DOUBLE AS lon,
(100 + i)::INTEGER AS alt,
(1700000000 + i * 60)::INTEGER AS time,
(i % 8)::INTEGER AS symbol_id
FROM range(0, 16) t(i);
-- Flight history fixture: same schema shape as the cookbook 4D example.
CREATE OR REPLACE TABLE flights AS
SELECT
(30 + (i % 12))::INTEGER AS lat,
(-100 + (i % 12))::INTEGER AS lon,
(10000 + i * 100)::INTEGER AS alt,
(1700000000 + i * 60)::INTEGER AS time
FROM range(0, 16) t(i);
-- 3D points fixture used by technical-details.
CREATE OR REPLACE TABLE points AS
SELECT
(i % 8)::INTEGER AS x,
((i * 3) % 8)::INTEGER AS y,
((i * 5) % 8)::INTEGER AS z
FROM range(0, 16) t(i);
Function category
Hilbert
8 functionsHilbert-curve keys, grids and range decomposition. Consecutive Hilbert keys are always neighbouring cells, so sorting by a Hilbert key usually produces tighter row groups than Morton.
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
encoded_value
|
Type
ANY
|
Mode Positional | Description |
Argument
target_type
|
Type
VARCHAR
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
encoded_value
|
Type
<numeric>
5 concrete typesUBIGINTUHUGEINTUINTEGERUSMALLINTUTINYINT
|
Mode Positional | Description |
Argument
num_elements
|
Type
UTINYINT
|
Mode Positional | Description |
Argument
return_float
|
Type
BOOLEAN
|
Mode Positional | Description |
Argument
return_unsigned
|
Type
BOOLEAN
|
Mode Positional | Description |
Description
Recover coordinates from a Hilbert curve key. Name the encoded type: hilbert_decode(key, 'DOUBLE[2]'), 'DATE[2]', or a scalar type for one dimension. The original form takes the dimension count and (return_float, return_unsigned): (false, true) for unsigned integers, (false, false) for signed integers, (true, false) for floats. Signedness must match the encoded values. Preserve the key's SQL type.
SELECT hilbert_decode(hilbert_encode([-1.5, 2.25]::DOUBLE[2]), 'DOUBLE[2]') AS roundtrip;
Output
| roundtrip |
|---|
| [-1.5, 2.25] |
SELECT d[1] AS first_date, d[2] AS second_date
FROM (SELECT hilbert_decode(hilbert_encode(DATE '2024-03-01', DATE '1965-07-04'), 'DATE[2]') AS d);
Output
| first_date | second_date |
|---|---|
| 2024-03-01 | 1965-07-04 |
SELECT hilbert_decode(hilbert_encode([10, 20]::UINTEGER[2]), 2, false, true) AS roundtrip;
Output
| roundtrip |
|---|
| [10, 20] |
SELECT hilbert_decode(hilbert_encode([-10, 20]::INTEGER[2]), 2, false, false) AS roundtrip;
Output
| roundtrip |
|---|
| [-10, 20] |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
encoded_value
|
Type
ANY
|
Mode Positional | Description |
Argument
min_values
|
Type
ANY
|
Mode Positional | Description |
Argument
max_values
|
Type
ANY
|
Mode Positional | Description |
Argument
bits
|
Type
BIGINT
|
Mode Positional | Description |
Description
Decode a key from hilbert_encode_bounded with the same bounds and bits into the centre of its grid cell, as a DOUBLE array. The result is within half a cell of the encoded value.
SELECT hilbert_decode_bounded(hilbert_encode_bounded([12.5, 40.25]::DOUBLE[2], [0, 0], [100, 100], 16), [0, 0]::DOUBLE[2], [100, 100]::DOUBLE[2], 16) AS cell_centre;
Output
| cell_centre |
|---|
| [12.500762939453125, 40.250396728515625] |
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
encoded_value
|
Type
UHUGEINT
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
encoded_value
|
Type
UHUGEINT
|
Mode Positional | Description |
Argument
bits
|
Type
BIGINT
|
Mode Positional | Description |
Description
Decode a key from hilbert_encode_latlon with the same bits (default 32) into the centre of its grid cell, as STRUCT(lat DOUBLE, lon DOUBLE).
SELECT hilbert_decode_latlon(hilbert_encode_latlon(52.52, 13.405)) AS decoded;
Output
| decoded |
|---|
| {'lat': 52.520000010263175, 'lon': 13.40500000398606} |
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
values
|
Type
ANY[ANY]
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
x
|
Type
ANY
|
Mode Positional | Description |
Argument
y
|
Type
ANY
|
Mode Positional | Description |
Argument
varargs
|
Type
ANY
|
Mode Positional | Description Varargs |
Description
Encode coordinates into one unsigned integer using the Hilbert curve. Pass a fixed-size array, or two or more separate arguments cast to their common type. Accepts 8–64-bit integers, FLOAT and DOUBLE, BOOLEAN, DATE, TIME, TIMESTAMP, DECIMAL (up to 18 digits) and ENUM, with at most 128 combined bits. Keys sort like the coordinates, including negative and floating-point values.
SELECT x, y, hilbert_encode([x, y]::UINTEGER[2]) AS hilbert
FROM (VALUES (1, 1), (1, 2), (2, 1), (6, 7), (7, 6)) AS t(x, y)
ORDER BY hilbert;
Output
| x | y | hilbert |
|---|---|---|
| 1 | 1 | 2 |
| 1 | 2 | 7 |
| 2 | 1 | 13 |
| 7 | 6 | 41 |
| 6 | 7 | 43 |
SELECT x, y, hilbert_encode(x, y) AS key
FROM (VALUES (-3, 2), (0, 0), (4, -1)) AS t(x, y)
ORDER BY key;
Output
| x | y | key |
|---|---|---|
| -3 | 2 | 9223372036854775799 |
| 0 | 0 | 9223372036854775808 |
| 4 | -1 | 15372286728091293007 |
SELECT hilbert_encode(DATE '2024-03-01', DATE '1965-07-04') AS key;
Output
| key |
|---|
| 15372286727988370219 |
Related functions
- hilbert_decode() — Recover coordinates from a Hilbert curve key
- hilbert_encode_bounded() — Scale each DOUBLE coordinate from [min, max] onto a grid of 2^bits cells and encode the cell with the Hilbert curve
- morton_encode() — Encode coordinates into one unsigned integer by interleaving their bits (Morton / Z-order)
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
values
|
Type
ANY
|
Mode Positional | Description |
Argument
min_values
|
Type
ANY
|
Mode Positional | Description |
Argument
max_values
|
Type
ANY
|
Mode Positional | Description |
Argument
bits
|
Type
BIGINT
|
Mode Positional | Description |
Description
Scale each DOUBLE coordinate from [min, max] onto a grid of 2^bits cells and encode the cell with the Hilbert curve. Returns the narrowest unsigned integer that holds dimensions × bits; every cell gets a distinct key from 0 to 2^(dimensions × bits) − 1. Values outside the bounds are clamped to the edge cells; NaN is an error. Up to 64 bits for 1–2 dimensions, 32 for 3–4, 16 for 5–8 and 8 for 9–16. A GEOMETRY value with 2-D bounds is encoded by the centre of its bounding box; empty geometries give NULL.
SELECT hilbert_encode_bounded([12.5, 40.25]::DOUBLE[2], [0, 0], [100, 100], 16) AS key;
Output
| key |
|---|
| 848669286 |
SELECT hilbert_encode_bounded('POLYGON ((10 10, 20 10, 20 30, 10 30, 10 10))'::GEOMETRY, [0, 0], [100, 100], 16) AS key;
Output
| key |
|---|
| 152801721 |
Related functions
- hilbert_decode_bounded() — Decode a key from hilbert_encode_bounded with the same bounds and bits into the centre of its grid cell, as a DOUBLE array
- hilbert_ranges_bounded() — Table function returning the key ranges (lo, hi) of hilbert_encode_bounded, with the given bounds and bits, that cover the box between min_point and max_point
- hilbert_encode_latlon() — Encode a latitude/longitude pair with the Hilbert curve over a grid of 2^bits cells per axis
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
latitude
|
Type
DOUBLE
|
Mode Positional | Description |
Argument
longitude
|
Type
DOUBLE
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
latitude
|
Type
DOUBLE
|
Mode Positional | Description |
Argument
longitude
|
Type
DOUBLE
|
Mode Positional | Description |
Argument
bits
|
Type
BIGINT
|
Mode Positional | Description |
Description
Encode a latitude/longitude pair with the Hilbert curve over a grid of 2^bits cells per axis. The default of 32 bits gives a UBIGINT key; 16 bits gives a UINTEGER. Latitude must be within [-90, 90] and longitude within [-180, 180]. Equivalent to hilbert_encode_bounded([lat, lon], [-90, -180], [90, 180], bits).
SELECT hilbert_encode_latlon(52.52, 13.405) AS key;
Output
| key |
|---|
| 13413847537087438956 |
SELECT hilbert_encode_latlon(52.52, 13.405, 16) AS key;
Output
| key |
|---|
| 3123154755 |
Related functions
- hilbert_decode_latlon() — Decode a key from hilbert_encode_latlon with the same bits (default 32) into the centre of its grid cell, as STRUCT(lat DOUBLE, lon DOUBLE)
- hilbert_encode_bounded() — Scale each DOUBLE coordinate from [min, max] onto a grid of 2^bits cells and encode the cell with the Hilbert curve
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
min_point
|
Type
ANY
|
Mode Positional | Description |
Argument
max_point
|
Type
ANY
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
min_point
|
Type
ANY
|
Mode Positional | Description |
Argument
max_point
|
Type
ANY
|
Mode Positional | Description |
Argument
max_ranges
|
Type
BIGINT
|
Mode Positional | Description |
Description
Table function returning the sorted, disjoint key ranges (lo, hi) of hilbert_encode that cover the box between min_point and max_point. The points are arrays of the encoded type. The cover is exact until max_ranges (default 64) is reached, then a superset that never misses a key in the box. Intended for systems that store or index only the key; inside DuckDB, a coordinate filter on key-sorted data is faster.
SELECT * FROM hilbert_ranges([2, 3]::UTINYINT[2], [5, 6]::UTINYINT[2]);
Output
| lo | hi |
|---|---|
| 9 | 10 |
| 28 | 28 |
| 31 | 35 |
| 45 | 46 |
| 50 | 55 |
SELECT count(*) AS ranges, sum(hi - lo + 1) AS keys FROM hilbert_ranges([2, 3]::UTINYINT[2], [100, 120]::UTINYINT[2], 4);
Output
| ranges | keys |
|---|---|
| 4 | 13999 |
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
min_point
|
Type
ANY
|
Mode Positional | Description |
Argument
max_point
|
Type
ANY
|
Mode Positional | Description |
Argument
min_values
|
Type
ANY
|
Mode Positional | Description |
Argument
max_values
|
Type
ANY
|
Mode Positional | Description |
Argument
bits
|
Type
BIGINT
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
min_point
|
Type
ANY
|
Mode Positional | Description |
Argument
max_point
|
Type
ANY
|
Mode Positional | Description |
Argument
min_values
|
Type
ANY
|
Mode Positional | Description |
Argument
max_values
|
Type
ANY
|
Mode Positional | Description |
Argument
bits
|
Type
BIGINT
|
Mode Positional | Description |
Argument
max_ranges
|
Type
BIGINT
|
Mode Positional | Description |
Description
Table function returning the key ranges (lo, hi) of hilbert_encode_bounded, with the given bounds and bits, that cover the box between min_point and max_point. Exact at grid-cell resolution until max_ranges (default 64) is reached, then a superset; filter fetched rows on their coordinates.
SELECT * FROM hilbert_ranges_bounded([10, 10], [20, 30], [0, 0], [100, 100], 8, 8);
Output
| lo | hi |
|---|---|
| 642 | 702 |
| 1666 | 1853 |
| 1986 | 2655 |
| 2896 | 2991 |
| 3266 | 3453 |
| 13728 | 13775 |
| 13813 | 14074 |
| 14597 | 14717 |