ULID functions
Generate and manipulate Universally Unique Lexicographically Sortable Identifiers.
ULID (Universally Unique Lexicographically Sortable Identifier) functions provide a way to generate and manipulate unique, sortable identifiers.
ULIDs are 128-bit identifiers that combine a timestamp with random data, offering advantages over traditional UUIDs such as:
- Lexicographic sorting: ULIDs can be sorted by their string representation, which corresponds to the order of their creation.
- Timestamp encoding: The first 48 bits of a ULID contain a millisecond-precision timestamp, allowing for easy extraction of creation time.
- Monotonicity: ULIDs generated in the same millisecond are guaranteed to be sortable and unique.
ClickHouse function reference
generateULID
Generates a ULID (Universally Unique Lexicographically Sortable Identifier).
Syntax:
Arguments:
x
(optional): An expression of any supported data type. The value is discarded, but the expression is used to bypass common subexpression elimination when the function is called multiple times in one query.
Returns:
A FixedString(26)
value representing the generated ULID.
Example:
Generate a single ULID:
Result:
Generate multiple ULIDs in one row:
Result:
In this example, we generate unique IDs for different types of tacos in a single query. The arguments 1
and 2
ensure that each generateULID()
call produces a distinct value.
ULIDs are sortable and contain a timestamp, making them useful for generating time-ordered unique identifiers for database records, distributed systems, or any application requiring unique, time-sortable IDs.
ULIDStringToDateTime
Extracts the timestamp from a ULID (Universally Unique Lexicographically Sortable Identifier).
Syntax:
Arguments:
ulid
(String
orFixedString(26)
): The input ULID.timezone
(String
, optional): The timezone name for the returned value.
Returns:
A timestamp with millisecond precision. [DateTime64(3)
]
Example:
Result:
In this example:
taco_order_time
extracts the timestamp from a ULID representing a taco order.new_order_id
generates a new ULID for the next order.
The ULID contains a timestamp with millisecond precision, which this function extracts. The remaining bits in the ULID are random and not used by this function.
Was this page helpful?