Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google BigQuery - Bug using WITH and RAND()

I came upon an interesting behaviour when I use RAND() within WITH:

WITH
  t0 AS ( SELECT rand() AS c ),
  t1 AS ( SELECT c FROM t0 ),
  t2 AS ( SELECT c FROM t0 )

SELECT c FROM t1
   UNION ALL 
SELECT c FROM t2

-- 
-- returns:
-- 1    0.229810680294245    
-- 2    0.5750437237982657
-- instead of:
-- 1    0.229810680294245    
-- 2    0.229810680294245    
-- 

The same thing even happens when I do this:

WITH
  t0 AS ( SELECT rand() AS c )

SELECT c FROM t0
   UNION ALL 
SELECT c FROM t0

Any clues why that happens?

like image 471
Rupert Schiessl Avatar asked Nov 07 '22 13:11

Rupert Schiessl


1 Answers

Surfacing this reply from Elliott Brossard as a comment to a deleted answer:

RAND() is supposed to run only once when used in a WITH clause. WITH clauses are similar to macros--they are inlined into the query plan--and this is a known bug in that RAND() will be evaluated using different seeds when run on different tasks, but the fix is somewhat complicated.

like image 182
Felipe Hoffa Avatar answered Nov 15 '22 07:11

Felipe Hoffa