BigQuery Remote Functions
A BigQuery remote function lets you call code that lives outside BigQuery from inside a Google Standard SQL query, by wiring the query to a Cloud Run function. You deploy an HTTP function in Cloud Run functions, then invoke it from SQL - BigQuery passes each row's arguments to the function and gets a value back per row.
The bridge is a BigQuery connection of type CLOUD_RESOURCE: you create the connection, create a remote function that points at the connection plus the function's URL, and then use that function in queries.
Configuring the connection
- Enable the BigQuery Connection API.
- Ensure you have the necessary IAM role permissions (for example,
roles/bigquery.admin). - Create a connection of type
CLOUD_RESOURCEusing the Google Cloud console, thebqCLI, or the Connection API. - Grant the connection's service account the invoker role on the function (see the gotcha below).
Grant the connection's service account:
- Cloud Functions Invoker (
roles/cloudfunctions.invoker) on a 1st gen function, or - Cloud Run Invoker (
roles/run.invoker) on a 2nd gen function.
Pick the wrong one for the generation and the query fails with a permission error at invocation time.
Creating the remote function
- Ensure you have the required role permissions on the dataset where you create the remote function and on the connection it uses (for example,
roles/bigquery.admin). - Create the remote function with the
CREATE FUNCTIONstatement, specifying the BigQuery connection name and the function URL endpoint.
Invoking the function from BigQuery
roles/bigquery.dataVieweron the dataset.roles/bigquery.connectionUseron the connection used by the remote function.
Then use the remote function in a query, providing any required arguments. The sample function adds the second argument to the first and returns the result for each row: