Morton (Z-order) functions in the Lindel DuckDB extension
Function category
Morton (Z-order)
2 functionsMorton-curve encoding (also known as Z-order). Faster to compute, slightly weaker locality. The classic Z-Order optimization that Delta Lake popularized.
Signature
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 component values from a Morton-encoded key. Supply the original dimension count; use (return_float, return_unsigned) = (false, true) for unsigned integers, (false, false) for signed integers, or (true, false) for floats. Preserve the encoded key’s SQL type. Community build 6435106 rejects one-dimensional signed-integer decoding; v1.5 source commit a92ec61 (extension version 2026100701) fixes it. Older community binaries may still be affected.
SELECT morton_decode(morton_encode([10, 20]::UINTEGER[2]), 2, false, true) AS roundtrip;
SELECT morton_decode(morton_encode([-10, 20]::INTEGER[2]), 2, false, false) AS roundtrip;
SELECT morton_decode(morton_encode([-1.5, 2.25]::DOUBLE[2]), 2, true, false) AS roundtrip;
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
values
|
Type
ANY[ANY]
|
Mode Positional | Description |
Description
Encode a fixed-size numeric array into an unsigned integer by interleaving its coordinate bits (Morton / Z-order). Accepts the same types and dimensions as hilbert_encode. Signed and floating-point values are encoded by their bit patterns without normalizing numeric distance.
SELECT x, y, morton_encode([x, y]::UINTEGER[2]) AS morton
FROM (VALUES (1, 1), (1, 2), (2, 1), (6, 7), (7, 6)) AS t(x, y)
ORDER BY morton;