Skip to content

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 functions

hilbert_decode

Scalar function Hilbert
Signature
2 overloaded forms · click to inspect
Arguments (Positional)
Argument encoded_value Type ANY Mode Positional Description
Argument target_type Type VARCHAR Mode Positional Description
Description
1 Decode into a named type
SELECT hilbert_decode(hilbert_encode([-1.5, 2.25]::DOUBLE[2]), 'DOUBLE[2]') AS roundtrip;

Output

roundtrip
[-1.5, 2.25]
2 Decode dates
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
3 Original form: num_elements, return_float, return_unsigned
SELECT hilbert_decode(hilbert_encode([10, 20]::UINTEGER[2]), 2, false, true) AS roundtrip;

Output

roundtrip
[10, 20]
4 Signed integers need return_unsigned = false
SELECT hilbert_decode(hilbert_encode([-10, 20]::INTEGER[2]), 2, false, false) AS roundtrip;

Output

roundtrip
[-10, 20]

hilbert_decode_bounded

Scalar function Hilbert
Signature
hilbert_decode_bounded( encoded_value: ANY, min_values: ANY, max_values: ANY, bits: BIGINT ) → ANY
Arguments (Positional)
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
1 Recover the cell centre
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]

hilbert_decode_latlon

Scalar function Hilbert
Signature
2 overloaded forms · click to inspect
Arguments (Positional)
Argument encoded_value Type UHUGEINT Mode Positional Description
Description
1 Round-trip a location
SELECT hilbert_decode_latlon(hilbert_encode_latlon(52.52, 13.405)) AS decoded;

Output

decoded
{'lat': 52.520000010263175, 'lon': 13.40500000398606}

hilbert_encode

Scalar function Hilbert
Signature
2 overloaded forms · click to inspect
Arguments (Positional)
Argument values Type ANY[ANY] Mode Positional Description
Description
1 Order nonnegative integer coordinates by their Hilbert curve key
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
2 Pass coordinates as separate arguments, including negative values
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
3 Encode two dates
SELECT hilbert_encode(DATE '2024-03-01', DATE '1965-07-04') AS key;

Output

key
15372286727988370219

hilbert_encode_bounded

Scalar function Hilbert
Signature
hilbert_encode_bounded( values: ANY, min_values: ANY, max_values: ANY, bits: BIGINT ) → ANY
Arguments (Positional)
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
1 Quantize a point in a 100 × 100 area to 16 bits per axis
SELECT hilbert_encode_bounded([12.5, 40.25]::DOUBLE[2], [0, 0], [100, 100], 16) AS key;

Output

key
848669286
2 Encode the centre of a geometry's bounding box
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

hilbert_encode_latlon

Scalar function Hilbert
Signature
2 overloaded forms · click to inspect
Arguments (Positional)
Argument latitude Type DOUBLE Mode Positional Description
Argument longitude Type DOUBLE Mode Positional Description
Description
1 Encode a location with the default 32 bits per axis
SELECT hilbert_encode_latlon(52.52, 13.405) AS key;

Output

key
13413847537087438956
2 Use 16 bits per axis for a smaller key
SELECT hilbert_encode_latlon(52.52, 13.405, 16) AS key;

Output

key
3123154755

hilbert_ranges

Table function Hilbert
Signature
2 overloaded forms · click to inspect
Arguments (Positional)
Argument min_point Type ANY Mode Positional Description
Argument max_point Type ANY Mode Positional Description
Description
1 Ranges covering a small box
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
2 Cap the number of ranges; the cover then includes some keys outside the box
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

hilbert_ranges_bounded

Table function Hilbert
Signature
2 overloaded forms · click to inspect
Arguments (Positional)
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
Description
1 At most 8 ranges for a box on a 256 × 256 grid
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