UUID functions
Use these functions to generate random or time-ordered UUIDs, and to read the version or the creation time that a UUID holds. The functions that generate a UUID return a new value on each call, so their results vary.
uuid function
Returns a new random universally unique identifier (UUID) of version 4, as a value of type UUID. Each call returns a different value, even within one query, so when a query returns several rows, each row gets its own UUID. As text, a UUID is 36 characters long: 32 hexadecimal digits in five groups separated by hyphens.
gen_random_uuid and uuidv4 are aliases of uuid. For a UUID that starts with its creation time, use the uuidv7 function.
Syntax
uuid()
gen_random_uuid()
uuidv4()
Return type
UUID
Examples
The following SQL query shows properties of a new UUID. The value varies from run to run, so the query shows its version and its length as text instead of the value.
SELECT uuid_extract_version(uuid()) AS version, length(CAST(uuid() AS VARCHAR)) AS text_length
The query returns the following result.
| version | text_length |
|---|---|
|
|
The following SQL query creates a UUID for each of three rows and counts the distinct values.
SELECT count(DISTINCT u) AS distinct_values FROM ( SELECT uuid() AS u FROM (VALUES (1), (2), (3)) AS v(x) ) AS t
The query returns the following result.
| distinct_values |
|---|
|
The following SQL query uses the gen_random_uuid alias and shows that two calls return different values.
SELECT gen_random_uuid() = gen_random_uuid() AS is_equal
The query returns the following result.
| is_equal |
|---|
|
Differences from Athena engine version 3
Athena engine version 3 cannot query a Hive table that has a UUID column. If you write the result of uuid to a Hive table that Athena engine version 3 or another engine also reads, cast it to VARCHAR first, as in CAST(uuid() AS VARCHAR).
uuid_extract_timestamp function
Returns the creation time that the version 7 UUID uuid holds in its first 48 bits, as a TIMESTAMP WITH TIME ZONE in UTC. A version 7 UUID stores its creation time in milliseconds, so the result has millisecond precision. If uuid is NULL, the result is NULL.
To create a version 7 UUID, use the uuidv7 function. To find the version of a UUID, use the uuid_extract_version function.
Syntax
uuid_extract_timestamp(uuid)
Arguments
The uuid_extract_timestamp function takes the following argument:
uuid-
The version 7 UUID to read the creation time from, as a
UUIDvalue. A string literal, such as'019482e4-1441-7aad-8127-eec99573b0a0', is converted toUUIDautomatically. AVARCHARcolumn or otherVARCHARexpression is not converted, so cast it first, as inuuid_extract_timestamp(CAST(.columnAS UUID))
Return type
TIMESTAMP WITH TIME ZONE
Examples
The following SQL query returns the creation time of a version 7 UUID.
SELECT uuid_extract_timestamp( '019482e4-1441-7aad-8127-eec99573b0a0' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query checks the version before it extracts the time. The UUID is version 1, so the result is NULL instead of an error.
SELECT CASE WHEN uuid_extract_version(u) = 7 THEN uuid_extract_timestamp(u) END AS result FROM (VALUES ( CAST( '12151fd2-7586-11e9-8f9e-2a86e4085a59' AS UUID ) ) ) AS v(u)
The query returns the following result.
| result |
|---|
|
Considerations
If uuid is not a version 7 UUID, the query fails. When a column can hold UUIDs of other versions, check the version first, as in CASE WHEN uuid_extract_version(.u) = 7 THEN uuid_extract_timestamp(u) END
uuid_extract_version function
Returns the version number that is stored in uuid, such as 4 for a random UUID or 7 for a UUID that starts with its creation time. If uuid is NULL, the result is NULL.
Syntax
uuid_extract_version(uuid)
Arguments
The uuid_extract_version function takes the following argument:
uuid-
The value whose version you want, as a
UUIDvalue. A string literal, such as'019482e4-1441-7aad-8127-eec99573b0a0', is converted toUUIDautomatically, and its text can use uppercase or lowercase letters, leave out the hyphens, or be enclosed in braces. AVARCHARcolumn or otherVARCHARexpression is not converted, so cast it first, as inuuid_extract_version(CAST(.columnAS UUID))
Return type
UINTEGER
Examples
The following SQL query returns the version of a version 7 UUID.
SELECT uuid_extract_version( '019482e4-1441-7aad-8127-eec99573b0a0' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the version of a version 1 UUID, written in uppercase letters.
SELECT uuid_extract_version( '12151FD2-7586-11E9-8F9E-2A86E4085A59' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the versions of the UUIDs that uuid and uuidv7 create.
SELECT uuid_extract_version(uuid()) AS uuid_version, uuid_extract_version(uuidv7()) AS uuidv7_version
The query returns the following result.
| uuid_version | uuidv7_version |
|---|---|
|
|
Considerations
If uuid is a string that is not a valid UUID, the query fails.
uuidv7 function
Returns a new UUID of version 7, as a value of type UUID. Its first 48 bits hold the time at which the function created it, in milliseconds. Apart from the few bits that hold the version and the variant, the rest of its bits are random. Each call returns a different value, even within one query.
Because the creation time comes first, a UUID from a later millisecond compares as greater than one from an earlier millisecond. To read the creation time back, use the uuid_extract_timestamp function. For a random UUID that holds no creation time, use the uuid function.
Syntax
uuidv7()
Return type
UUID
Examples
The following SQL query shows the version of a new UUID. The value varies from run to run, so the query shows its version instead of the value.
SELECT uuid_extract_version(uuidv7()) AS version
The query returns the following result.
| version |
|---|
|
The following SQL query reads the creation time back from a new UUID and shows that it is within the last minute.
SELECT uuid_extract_timestamp(uuidv7()) > now() - INTERVAL 1 MINUTE AS is_recent
The query returns the following result.
| is_recent |
|---|
|
The following SQL query compares two version 7 UUIDs whose creation times are one millisecond apart. The later UUID is greater.
SELECT CAST('019482e4-1441-7aad-8127-eec99573b0a0' AS UUID) < CAST( '019482e4-1442-7aad-8127-eec99573b0a0' AS UUID ) AS is_earlier
The query returns the following result.
| is_earlier |
|---|
|
Considerations
Two UUIDs from the same millisecond, such as two calls in one row, do not have a guaranteed order.
Differences from Athena engine version 3
Athena engine version 3 cannot query a Hive table that has a UUID column. If you write the result of uuidv7 to a Hive table that Athena engine version 3 or another engine also reads, cast it to VARCHAR first, as in CAST(uuidv7() AS VARCHAR).