View a markdown version of this page

UUID functions - Amazon Athena

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

4

36

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

3

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

false

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 UUID value. A string literal, such as '019482e4-1441-7aad-8127-eec99573b0a0', is converted to UUID automatically. A VARCHAR column or other VARCHAR expression is not converted, so cast it first, as in uuid_extract_timestamp(CAST(column AS 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

2025-01-20 08:46:13.057+00

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

NULL

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 UUID value. A string literal, such as '019482e4-1441-7aad-8127-eec99573b0a0', is converted to UUID automatically, and its text can use uppercase or lowercase letters, leave out the hyphens, or be enclosed in braces. A VARCHAR column or other VARCHAR expression is not converted, so cast it first, as in uuid_extract_version(CAST(column AS 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

7

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

1

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

4

7

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

7

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

true

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

true

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).