1

Can I use a query in Google BigQuery User Defined Function to return some value? I've been searching docs and stackoverflow for hours without any luck and I have a very specific use case where I need to return a single scalar value based on the values of multiple columns.

Following will be the use case for the query:

SELECT campaign,source,medium, get_channel(campaign,source,medium)
FROM table_name

the get_channel() UDF will use these parameters and a complex select statement to return a single scalar value for the row. I've prepared the query, I just need to find a way to use that query in the UDF, for which I, honestly am at loss and without a cause.

Is my use case correct? Is this even possible? Are there any alternatives to do this?

2

1 Answer 1

6

Looks like you want to use UDF to select scalar value off of some lookup table. if so, NO - you cannot reference a table in UDF - see more in Limits and Limitations

But if you just want to have some complex manipulation with arguments - sure - see dummy example below

#standardSQL
CREATE TEMPORARY FUNCTION get_channel(campaign INT64, source INT64, medium INT64) AS ((
  SELECT campaign + source + medium as result_of_complex_select_statement
));
WITH `project.dataset.table_name` AS (
  SELECT 1 AS campaign, 2 AS source, 3 AS medium UNION ALL
  SELECT 4, 5, 6 UNION ALL
  SELECT 7, 8, 9
)
SELECT 
  campaign,
  source,
  medium, 
  get_channel(campaign,source,medium) AS channel
FROM `project.dataset.table_name`

You should rather use JOIN to achieve your goal

Sign up to request clarification or add additional context in comments.

7 Comments

Yes, i am trying to use these parameters to select a scalar value from a mapping table. I'll try your answer out when i am in office later tonight and update here if this works.
Then this doesn't solve my problem :( I'll try fresh with a different logic then
Yes, You should rather use JOIN to achieve your goal
But, the question is definitely answered - you asked if you can do this or not - right? :o)
Thanks @Mikhael, i initially DID try using joins but moved to using UDF because this was going to be a generic function used at multiple places. I thought UDFs in BigQuery will be somewhat same as UDFs in SQL Server. Well, back to storyboard then.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.