Code bundles¶
Any collaborator can bundle custom Python Procedures, UDFs or UDTFs with collaboration templates. Templates in turn reference the bundled code to perform complex data actions in the collaboration. Common usage includes machine learning or customized data manipulation within a query. Your uploaded code can import and use packages from an approved bundle of Python packages and the Snowpark API.
Custom code can be called only via templates, and not directly.
Note
Python is the only coding language supported for Code Bundles.
The following sections show you how to upload and use code bundles.
Implementing custom code bundles¶
Here is how to upload and use a code bundle:
The code submitter:
Creates and registers the code by calling REGISTER_CODE_SPEC.
The code can be inline in the spec, or linked from a stage.
Creates a template that references the code bundle spec by ID in the template’s
code_specsarray. Add this field as a peer of the template and parameters fields as shown in this example:Registers the template and then links the template into the collaboration.
The analysis runner:
Runs the template in the standard way by calling
RUN.
Important
Snowflake runs security checks on any uploaded bundles before deploying them into a clean room. If a security check fails, the template and its bundled code will not be deployed and available for use.
To confirm that a template with a code bundle is deployed and ready for use, take the following steps:
Find the name of the clean room application where you are trying to deploy the code bundle:
Check the
upgrade_statevalue in the DESCRIBE APPLICATION response. When the upgrade state is COMPLETE, the security checks have passed and the new template and bundle are available to use. Pass in the application name returned by the command in the previous step using SQL like the following example: SQL code:
Create and register the code bundle spec¶
The first step in uploading custom code is to create and register the code bundle spec.
Custom functions are defined in a YAML code bundle spec. Each code bundle exposes one or more functions that can be called by a template. The code bundle spec can either include the code in the spec inline, or link to code that lives on a Snowflake stage.
A collaborator registers a spec by calling REGISTRY.REGISTER_CODE_SPEC, which returns the bundle ID.
After the template that references the code bundle is linked into the collaboration, that code bundle is visible to anyone in the collaboration who can access a template that links the code bundle. Call VIEW_CODE_SPECS to list accessible code bundles in a collaboration.
Anyone who can see a code bundle in a collaboration can see and use it in their own templates in that collaboration. Any inline code can be viewed by any member of the collaboration, but staged artifact code can not be viewed by collaborators. Collaborators need to ensure that the content_hash of the referenced artifacts match for code integrity verification.
The following code bundle spec that exposes a single Python UDF called normalize_value, which calls the normalize function defined in that spec:
Create and register the calling template¶
After the code spec is registered, the collaborator then registers a template that uses this code bundle. To use a code bundle, add the bundle spec ID in the template’s code_specs field. Adding this template into the collaboration will also cause the code bundled to be available in the collaboration.
A template calls a custom function using the syntax cleanroom.spec_name$function_name. Note the literal . and $ name scoping marks.
Note
Use the spec name, not the spec ID, to reference a function in your template. This is so that you can quickly update the version of your code bundle without having to change all the references to it in your template.
In the following example, a template uses function normalize_value from the code bundle custom_udf:
Add the template to a collaboration¶
Add the template that calls your function to the collaboration in the standard way. For more information, see Templates.
Snowflake validates and uploads to the collaboration when the calling template is added to a collaboration. The following example shows a request to add a template to an existing collaboration:
Note
Installing a template with a code bundle triggers a Snowflake security check, and issues a new patch of the underlying clean room. The template will not be available or usable until the process is complete and the patch is installed.
To check the progress of the patch installation:
Find the name of the clean room application. Typically, this will be
SFDCR_<clean room name>, but you can search to be sure:Check the status of the patch install. Wait for
upgrade_stateis COMPLETE in the following query:
Versioning your code¶
Every registered code spec must have a unique name + version across all registries in your account. A template loads a specific name and version of a code spec. If you want to create or consume a new version of your code, you must submit a new version of the template that references the new code version in the code_specs field. You do not need to change the template body. For example:
Step 1: Consume version 1 of the code bundle:
Step 2: Update and register the new version of your code bundle, and then update your template to use the new version:
Notice that function names do not include the version, so you do not need to change the calling code in the template body when you upload a new version of a function.
Example specs¶
Inline UDF with code body¶
A simple UDF with inline Python code:
UDTF (User-Defined Table Function)¶
This example YAML defines a UDTF that returns multiple rows:
Staged artifact with wheel package¶
Be sure to read the stage_path documentation requirements for linking to staged code in your code spec.
This example YAML uses a staged Python wheel package:
Stored procedure¶
This example YAML defines a stored procedure for data processing:
Multiple Python files as staged artifacts¶
Be sure to read the stage_path documentation requirements for linking to staged code in your code spec.
This example YAML uses multiple staged Python source files: