Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random_value UDF result is re-used by query optimizer when invoked in subquery #54

Open
danieldeleo opened this issue Apr 24, 2020 · 0 comments
Labels
bug Something isn't working

Comments

@danieldeleo
Copy link
Collaborator

BigQuery query optimizer will invoke once and re-use the output of random_value UDF if the UDF is called within a SQL subquery.

https://github.com/GoogleCloudPlatform/bigquery-utils/blob/master/udfs/community/random_value.sql

Possible Solution:

random_value(arr ANY TYPE, col ANY TYPE) AS
(
(
SELECT val[OFFSET(0)]
FROM(
SELECT
[value, col] AS val
FROM
UNNEST(arr) value
ORDER BY
RAND()
LIMIT 1
)
));

Caveat: the second input arg must have the same type as the array elements in first arg

@afleisc afleisc added the bug Something isn't working label May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants