UUID functions
Generate and work with Universally Unique Identifiers.
UUID (Universally Unique Identifier) functions allow you to generate, manipulate, and work with UUIDs. These functions are useful for creating unique identifiers for records, tracking distributed transactions, or generating time-ordered unique IDs.
This page covers various UUID-related functions, including:
- Generating UUIDs (versions 4 and 7)
- Converting between UUID formats (string, binary, and native UUID type)
- Extracting information from UUIDs (such as timestamps from version 7)
ClickHouse function reference
generateUUIDv4
Generates a version 4 UUID (Universally Unique Identifier).
Syntax:
Arguments:
expr
(any type, optional): An optional expression used to bypass common subexpression elimination if the function is called multiple times in a query. The value of the expression does not affect the returned UUID.
Returns:
A value of type UUID
.
Example:
Result:
In this example, we generate two UUIDs: one without an argument and one with a ‘taco’ argument. The ‘taco’ argument doesn’t affect the UUID generation but ensures a unique UUID is created each time the function is called.
UUIDs generated by this function are random and are not guaranteed to be unique across multiple function calls or across distributed systems. For applications requiring guaranteed uniqueness, consider using a centralized UUID generation service or a different UUID version.
generateUUIDv7
Generates a version 7 UUID (Universally Unique Identifier).
Syntax:
Arguments:
expr
(any type, optional): An arbitrary expression used to bypass common subexpression elimination if the function is called multiple times in a query. The value of the expression does not affect the returned UUID.
Returns:
A value of type UUIDv7
.
Description:
The generated UUIDv7 contains the following components:
- Current Unix timestamp in milliseconds (48 bits)
- Version “7” (4 bits)
- Counter (42 bits, including a 2-bit variant field “2”) to distinguish UUIDs within a millisecond
- Random field (32 bits)
The function guarantees that the counter field within a timestamp increments monotonically across all function invocations in concurrently running threads and queries.
As of April 2024, version 7 UUIDs are in draft status and their layout may change in the future.
Example:
Result:
In this example, we generate a UUIDv7 as a unique identifier for a taco order.
Example with multiple UUIDs:
Result:
This example demonstrates generating multiple UUIDs in a single query, which could be used for creating unique identifiers for a batch of taco orders.
empty
Checks whether the input UUID is empty.
Syntax:
Arguments:
UUID
(UUID
): A UUID value.
Returns:
- 1 for an empty UUID (all zeros), 0 for a non-empty UUID. [
UInt8
]
Example:
Result:
This example generates a new UUIDv4 and checks if it’s empty. The result is 0 because a newly generated UUID is not empty.
The function also works for Arrays and Strings.
notEmpty
Checks whether the input UUID is non-empty.
Syntax:
Arguments:
UUID
(UUID
): A UUID value.
Returns:
1
for a non-empty UUID or0
for an empty UUID. [UInt8
]
A UUID is considered empty if it contains all zeros (zero UUID).
Example:
Result:
In this example, we generate a new UUIDv4 and check if it’s not empty. The result is 1
, indicating that the generated UUID is not empty.
This function also works for Array
s and String
s.
toUUID
Converts a string value to a UUID.
Syntax:
Arguments:
string
(String
): A string containing a UUID in the formatxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
.
Returns:
- The UUID type value.
Example:
Result:
In this example, we convert a string representation of a UUID (perhaps representing a unique taco order ID) to a UUID type.
If the input string is not a valid UUID format, the function will throw an exception.
toUUIDOrDefault
Converts a string to a UUID, returning a default value if the conversion fails.
Syntax:
Arguments:
string
(String
) — A string containing a UUID.default
(UUID
) — The UUID to return if the conversion fails.
Returns:
The UUID value converted from the string, or the default value if conversion fails.
Example:
Result:
In this example:
- The first
toUUIDOrDefault
call successfully converts a valid UUID string. - The second call returns the default UUID because ‘not-a-uuid’ is not a valid UUID string.
This function is useful when you need to handle potentially invalid UUID strings in your data, providing a fallback value to ensure query continuity.
toUUIDOrNull
Attempts to convert a string to a UUID. If the conversion fails, it returns NULL.
Syntax:
Arguments:
string
(String
): A string containing a UUID representation.
Returns:
The UUID value if the conversion is successful, or NULL if it fails. [Nullable(UUID
)]
Example:
Result:
In this example:
valid_uuid
successfully converts a valid UUID string.invalid_uuid
returns NULL for a completely invalid input.malformed_uuid
returns NULL for a UUID-like string with an extra character.
This function is useful when you need to handle potentially invalid UUID strings without causing errors in your query.
toUUIDOrZero
Converts a string to a UUID. If the conversion fails, it returns a zero UUID.
Syntax:
Arguments:
string
(String
) — A string containing a UUID.
Returns:
- The UUID value if the conversion is successful, or a zero UUID (00000000-0000-0000-0000-000000000000) if the conversion fails. [
UUID
]
Example:
Result:
In this example:
valid_uuid
successfully converts a valid UUID string.invalid_uuid
returns a zero UUID for an invalid input.taco_uuid
also returns a zero UUID, as “spicy-carnitas-taco” is not a valid UUID format.
This function is useful when you need to ensure that invalid UUID strings are converted to a consistent default value (zero UUID) rather than causing an error.
UUIDStringToNum
Converts a UUID string to its binary representation.
Syntax:
Arguments:
uuid
(String
): A string containing a UUID in the formatxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
.variant
(UInt8
, optional): Integer specifying the byte order variant.- 1 = Big-endian (default)
- 2 = Microsoft
Returns:
The binary representation of the UUID. (FixedString(16)
)
Example:
Result:
This example converts a UUID string representing a taco order ID to its binary representation. The binary output is displayed as a hexadecimal string for readability.
Example with Microsoft variant:
Result:
This example uses the Microsoft variant, which changes the byte order of the first three components of the UUID.
The binary representation is typically used for efficient storage and indexing of UUIDs in databases.
UUIDNumToString
Converts a binary representation of a UUID to its string format.
Syntax:
Arguments:
binary
(FixedString(16)
): A binary representation of a UUID.variant
(UInt8
, optional): Integer specifying the UUID format variant.- 1 = Big-endian (default)
- 2 = Microsoft
Returns:
A string containing 36 characters representing the UUID in text format. (String
)
Example:
Result:
In these examples, UUIDNumToString
converts the binary representation of a UUID to its string format. The second example demonstrates using the Microsoft variant.
This function is particularly useful when you need to convert UUID values stored in binary format back to their human-readable string representation, such as when working with UUIDs in taco order systems or customer databases.
UUIDToNum
Converts a UUID to its binary representation as a FixedString(16).
Syntax
Arguments
uuid
(UUID
): Value of type UUID.variant
(UInt8
, optional): Integer representing the byte order variant.- 1 = Big-endian (default)
- 2 = Microsoft (little-endian)
Returns
- A
FixedString(16)
containing the binary representation of the UUID.
Example
Result:
Using the Microsoft variant:
Result:
This function is useful when you need to work with the binary representation of UUIDs, such as for storage optimization or when interfacing with systems that expect UUIDs in binary format.
UUIDv7ToDateTime
Returns the timestamp component of a UUID version 7.
Syntax:
Arguments:
uuid
(UUID
): UUID of version 7.timezone
(String
, optional): Timezone name for the returned value.
Returns:
- Timestamp with milliseconds precision. If the UUID is not a valid version 7 UUID, it returns 1970-01-01 00:00:00.000. [
DateTime64(3)
]
Example:
Result:
This example extracts the timestamp from a UUIDv7, which could represent the time a taco order was placed.
Result:
This example shows the same taco order time converted to the Los Angeles timezone.
UUIDv7 contains a timestamp with millisecond precision, making it useful for generating time-based unique identifiers for events like taco orders.