BigQuery · Utility function
BigQuery GENERATE_UUID
GENERATE_UUID() returns a random version 4 UUID as a lowercase 36-character STRING, such as 3f2b8c1e-9d4a-4e7b-8c21-5a6f0d9e7b13. Use it to give new rows unique IDs. Every call and every run returns a new value, so it is not a stable key for re-runnable pipelines.
GENERATE_UUID produces a random universally unique identifier: 32 lowercase hexadecimal digits in five groups (8-4-4-4-12) separated by hyphens. It follows RFC 4122 version 4, so 122 of the 128 bits are random and 6 are fixed. Snowflake's UUID_STRING, PostgreSQL's gen_random_uuid and SQL Server's NEWID do the same job.
The value is generated per call and per row. A pipeline that assigns IDs with GENERATE_UUID gets new IDs every time it re-runs or backfills, which breaks deduplication with MERGE and joins to earlier loads. When rows have a natural key, derive the ID from it instead: TO_HEX(SHA256(CONCAT(source, ':', external_id))) with SHA256, or FARM_FINGERPRINT for an INT64, return the same value on every run. GENERATE_UUID is also allowed as a column DEFAULT.
BigQuery executes a non-recursive CTE once for each reference, so a WITH clause that calls GENERATE_UUID and is referenced twice produces different IDs in each place. Write the rows and their IDs to a table, for example a temporary table in a script, before you use them more than once.
Syntax
GENERATE_UUID()Return value
STRING: a lowercase, 36-character version 4 UUID in the form 8-4-4-4-12.
Examples
Check the format
The value is random, but every UUID is 36 characters long and lowercase, with 4 as the version digit and 8, 9, a or b as the variant digit.
SELECT
LENGTH(GENERATE_UUID()) AS uuid_length,
REGEXP_CONTAINS(
GENERATE_UUID(),
r'^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'
) AS is_lowercase_v4;| uuid_length | is_lowercase_v4 |
|---|---|
| 36 | true |
Assign IDs while loading from staging
Each inserted row gets a new UUID. Running the same INSERT twice inserts the rows twice with different IDs, so guard re-runs separately.
INSERT INTO `project.dataset.events` (event_id, user_id, event_name, event_ts)
SELECT GENERATE_UUID(), user_id, event_name, event_ts
FROM `project.dataset.events_staging`;A CTE referenced twice gets different UUIDs
BigQuery executes the CTE once per reference, so the two sides of the join see two different UUIDs.
WITH new_orders AS (
SELECT GENERATE_UUID() AS order_uuid
)
SELECT a.order_uuid = b.order_uuid AS same_uuid
FROM new_orders AS a
CROSS JOIN new_orders AS b;| same_uuid |
|---|
| false |
UUID as a column default
Columns left out of the INSERT get their default values, so ticket_id and created_at are filled automatically.
CREATE TABLE `project.dataset.support_tickets` (
ticket_id STRING DEFAULT GENERATE_UUID(),
customer_id STRING,
subject STRING,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);
INSERT INTO `project.dataset.support_tickets` (customer_id, subject)
VALUES ('c-42', 'Invoice missing');Common mistakes
- Using GENERATE_UUID as the key in a re-runnable job. Every run creates new IDs, so retries and backfills add duplicates that MERGE can't match. Hash the natural key instead.
- Referencing a CTE that calls GENERATE_UUID more than once. Each reference runs the CTE again and gets new values.
- Comparing with uppercase UUIDs from other systems. GENERATE_UUID returns lowercase; compare with
LOWER(other_id). - Expecting UUIDs to sort by creation time. Version 4 UUIDs are random, so keep a timestamp column for ordering.
Cost and performance
- A UUID stored as STRING takes 38 logical bytes (2 plus 36 characters), against 8 for an INT64 key, which adds up in storage and in on-demand bytes scanned for large tables and joins. Stored as BYTES with
FROM_HEX(REPLACE(uuid, '-', ''))it takes 18. - GENERATE_UUID is non-deterministic, so BigQuery doesn't serve queries that call it from the query cache.
FAQ
How do I generate a UUID in BigQuery?
Call GENERATE_UUID(). It returns a lowercase version 4 UUID as a STRING, a new one for every row.
Which UUID version does GENERATE_UUID return?
Version 4, the random variant defined in RFC 4122: 122 bits are random and 6 bits hold the fixed version and variant.
How do I create a deterministic ID instead of a random UUID?
Hash the natural key: TO_HEX(SHA256(CONCAT(source, ':', id))) for a 64-character hex ID, or FARM_FINGERPRINT(CONCAT(source, ':', id)) for an INT64. Both return the same value on every run.
Can I use GENERATE_UUID as a default column value?
Yes. id STRING DEFAULT GENERATE_UUID() in CREATE TABLE fills the column whenever an INSERT leaves it out.
Related
More hash and utility functions
All BigQuery hash and utility functions · All functions · BigQuery error messages
