BigQuery GENERATE_UUID() and CTE's

Viewed 769

This behavior surprised me a little bit.

When you generate a uuid in a CTE (to make a row id, etc) and reference it in the future you'll find that it changes. It seems that generate_uuid() is being called twice instead of once. Anyone know why this is the case w/ BigQuery and what this is called?

I was using generate_uuid() to create a row_id and was finding that in my eventual joins that no matches were occurring because of this. Best way to get around it I've found is by just creating a table from the first CTE which cements the uuid in place for future use.

Still curious to know more about the why and what behind this.

with _first as (

  select generate_uuid() as row_id
  
)
,_second as (

  select * from _first
  
)

select row_id from _first
union all
select row_id from _second
1 Answers

curious to know more about the why and what behind this

This is by design:

WITH clauses are not materialized. Placing all your queries in WITH clauses and then running UNION ALL is a misuse of the WITH clause.
If a query appears in more than one WITH clause, it executes in each clause.

You can see in documentation - Do not treat WITH clauses as prepared statements

Related