Generate a 64-bit integer id that is unique and strictly increasing within a single query execution.
MONOTONICALLY_INCREASING_ID()
## Overview Returns a 64-bit integer that is unique and strictly increasing within a single query execution. Ids are assigned per partition: within a partition they are contiguous, but across partitions the ids jump in large blocks so there are visible gaps. Use this function when you need a synthetic per-query row id (for debugging, temporary deduplication, or stable projection ordering), not when you need a persistent primary key. The assignment is non-deterministic across query executions: running the same query twice produces different id values. ## Behavior - Unique within a single query execution. - Monotonically increasing in partition-scan order, but with gaps between partitions. - Returns a BIGINT. - Not deterministic across executions; do not persist as a stable key. - Never returns NULL. - Side effect free. ## Compatibility - Standard analytical SQL primitive for per-query id assignment.
-- Assign row ids in a projection
SELECT MONOTONICALLY_INCREASING_ID() AS row_id, *
FROM retail.catalog.products;
-- Generate ids inside a CTE so they can be referenced later
WITH numbered AS (
SELECT MONOTONICALLY_INCREASING_ID() AS rn, name
FROM retail.catalog.products
)
SELECT rn, name FROM numbered WHERE rn < 100;
-- Combine with metadata columns to diagnose data layout
SELECT MONOTONICALLY_INCREASING_ID() AS id, INPUT_FILE_NAME() AS file
FROM obs.catalog.events;
-- Don't use as a surrogate primary key across runs; ids are per-query only
SELECT MONOTONICALLY_INCREASING_ID() AS tmp_id, *
FROM retail.catalog.products
LIMIT 10;