pyarrow 14.0.2
pandas 2.1.4
python-dateutil 2.8.2
You can use the CREATE FUNCTION
option list to use modules other than those provided by the Python standard library and pre-installed packages. You can install packages from the Python Package Index (PyPI), or you can import Python files from Cloud Storage.
When you install a package, you must provide the package name, and you can optionally provide the package version using Python package version specifiers. If the package is in the runtime, that package is used unless a particular version is specified in the CREATE FUNCTION
option list. If a package version is not specified, and the package isn't in the runtime, the latest available version is used. Only packages with the wheels binary format are supported.
The following example shows you how to create a Python UDF that installs the Cloud Translation API client library package using the CREATE OR REPLACE FUNCTION
option list:
Go to the BigQuery page.
In the query editor, enter the following CREATE FUNCTION
statement:
CREATEFUNCTION`PROJECT_ID.DATASET_ID`.translate(srcSTRING)RETURNSSTRINGLANGUAGEpythonOPTIONS(entry_point='do_translate',runtime_version='python-3.11',packages=['google-cloud-translate>=3.11'])ASr"""from google.cloud import translatedef do_translate(src): # See the example in following section for the detail guide and # the implementation return """;
Replace PROJECT_ID.DATASET_ID with your project ID and dataset ID.
Click
Run.You can extend your Python UDFs using the Function option list by importing Python files from Cloud Storage.
In your UDF's Python code, you can import the Python files from Cloud Storage as modules by using the import statement followed by the path to the Cloud Storage object. For example, if you are importing gs://BUCKET_NAME/path/to/lib1.py
, then your import statement would be import path.to.lib1
.
The Python filename needs to be a Python identifier. Each folder
name in the object name (after the /
) should be a valid Python identifier. Within the ASCII range (U+0001..U+007F), the following characters can be used in identifiers:
The following example shows you how to create a Python UDF that imports the lib1.py
client library package from a Cloud Storage bucket named my_bucket
:
Go to the BigQuery page.
In the query editor, enter the following CREATE FUNCTION
statement:
CREATEFUNCTION`PROJECT_ID.DATASET_ID`.myFunc(aFLOAT64,bSTRING)RETURNSSTRINGLANGUAGEpythonOPTIONS(entry_point='compute',runtime_version='python-3.11',library=['gs://my_bucket/path/to/lib1.py'])ASr"""import path.to.lib1 as lib1def compute(a, b): # doInterestingStuff is a function defined in # gs://my_bucket/path/to/lib1.py return lib1.doInterestingStuff(a, b);""";
Replace PROJECT_ID.DATASET_ID with your project ID and dataset ID.
Click
Run.A Python UDF accesses a Google Cloud service or an external service by using the Cloud resource connection service account. The connection's service account must be granted permissions to access the service. The permissions required vary depending on the service that is accessed and the APIs that are called from your Python code.
If you create a Python UDF without using a Cloud resource connection, the function is executed in an environment that blocks network access. If your UDF accesses online services, you must create the UDF with a Cloud resource connection. If you don't, the UDF is blocked from accessing the network until an internal connection timeout is reached.
The following example shows you how to access the Cloud Translation service from a Python UDF. This example has two projects—a project named my_query_project
where you create the UDF and the Cloud resource connection, and a project where you are running the Cloud Translation named my_translate_project
.
First, you create a Cloud resource connection in my_query_project
. To create the cloud resource connection, follow the steps on the Create a Cloud resource connection page.
After you create the connection, open it, and in the Connection info pane, copy the service account ID. You need this ID when you configure permissions for the connection. When you create a connection resource, BigQuery creates a unique system service account and associates it with the connection.
To grant the Cloud resource connection service account access to your projects, grant the service account the Service usage consumer role (roles/serviceusage.serviceUsageConsumer
) in my_query_project
and the Cloud Translation API user role (roles/cloudtranslate.user
) in my_translate_project
.
Go to the IAM page.
Verify that my_query_project
is selected.
Click
Grant Access.In the New principals field, enter the Cloud resource connection's service account ID that you copied previously.
In the Select a role field, choose Service usage, and then select Service usage consumer.
Click Save.
In the project selector, choose my_translate_project
.
Go to the IAM page.
Click
Grant Access.In the New principals field, enter the Cloud resource connection's service account ID that you copied previously.
In the Select a role field, choose Cloud translation, and then select Cloud Translation API user.
Click Save.
In my_query_project
, create a Python UDF that calls the Cloud Translation service using your Cloud resource connection.
Go to the BigQuery page.
Enter the following CREATE FUNCTION
statement in the query editor:
CREATEFUNCTION`PROJECT_ID.DATASET_ID`.translate_to_es(xSTRING)RETURNSSTRINGLANGUAGEpythonWITHCONNECTION`PROJECT_ID.REGION.CONNECTION_ID`OPTIONS(entry_point='do_translate',runtime_version='python-3.11',packages=['google-cloud-translate>=3.11','google-api-core'])ASr"""from google.api_core.retry import Retryfrom google.cloud import translateproject = "my_translate_project"translate_client = translate.TranslationServiceClient()def do_translate(x : str) -> str: response = translate_client.translate_text( request={ "parent": f"projects/{project}/locations/us-central1", "contents": [x], "target_language_code": "es", "mime_type": "text/plain", }, retry=Retry(), ) return response.translations[0].translated_text""";-- Call the UDF.WITHtext_tableAS(SELECT"Hello"AStextUNIONALLSELECT"Good morning"AStextUNIONALLSELECT"Goodbye"AStext)SELECTtext,`PROJECT_ID.DATASET_ID`.translate_to_es(text)AStranslated_textFROMtext_table;
Replace the following:
PROJECT_ID.DATASET_ID
: your project ID and dataset IDREGION.CONNECTION_ID
: your connection's region and connection IDClick
Run.The output should look like the following:
+--------------------------+-------------------------------+ | text | translated_text | +--------------------------+-------------------------------+ | Hello | Hola | | Good morning | Buen dia | | Goodbye | Adios | +--------------------------+-------------------------------+
During preview, Python UDFs are supported in all BigQuery multi-region and regional locations except for the following:
northamerica-south1
region is not supported.europe-north2
region is not supported.Python UDFs are offered without any additional charges.
When billing is enabled, the following apply:
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-04-28 UTC.