Add a user-defined function (UDF) resource to a Databricks app
Add user-defined functions (UDFs) registered in Unity Catalog as Databricks Apps resources to enable your app to execute registered SQL and Python functions. UDFs provide reusable business logic, data transformations, and custom operations that can be shared across your organization with centralized governance.
Privilege requirements
To run a UDF, the app's service principal must have the USE CATALOG privilege on the parent catalog, the USE SCHEMA privilege on the parent schema, and the EXECUTE privilege on the function. When you add the UDF resource, Databricks automatically grants these privileges to the app's service principal.
For this automatic granting to succeed, one of the following must be true for each privilege:
- For
USE CATALOG: Either all account users have theUSE CATALOGprivilege on the catalog, or you have theMANAGEprivilege on the catalog. - For
USE SCHEMA: Either all account users have theUSE SCHEMAprivilege on the schema, or you have theMANAGEprivilege on the schema. - For
EXECUTE: Either all account users have theEXECUTEprivilege on the function, or you have theMANAGEprivilege on the function.
See Unity Catalog privileges and securable objects.
Add a user-defined function (UDF) resource
Before you add a UDF as a resource, review the app resource prerequisites.
- When you create or edit an app, navigate to the Configure step.
- In the App resources section, click + Add resource.
- Select UC function as the resource type.
- Choose a UDF from the available functions in your workspace. The function must already be registered in Unity Catalog.
- Select the permission level for your app:
- Can execute: Grants the app permission to execute the UDF with the provided parameters. Corresponds to the
EXECUTEprivilege.
- Can execute: Grants the app permission to execute the UDF with the provided parameters. Corresponds to the
- (Optional) Specify a custom resource key, which is how you reference the UDF in your app configuration. The default key is
function.
Environment variables
When you deploy an app with a UDF resource, Databricks exposes the full three-level name through environment variables that you can reference using the valueFrom field.
Example configuration:
env:
- name: UC_FUNCTION_NAME
valueFrom: function # Use your custom resource key if different
Using the function in your application:
import os
from databricks.sdk import WorkspaceClient
# Access the function name
function_name = os.getenv("UC_FUNCTION_NAME")
# Initialize workspace client
w = WorkspaceClient()
# Execute the function via SQL
result = w.statement_execution.execute_statement(
warehouse_id="your_warehouse_id",
statement=f"SELECT {function_name}('parameter_value')"
)
# Process the result
print(f"Function result: {result}")
For more information, see Use environment variables to access resources.
Remove a user-defined function (UDF) resource
When you remove a UDF resource from an app, the app's service principal loses access to the function. The UDF itself remains unchanged and continues to be available for other users and applications that have appropriate permissions.
Best practices
Consider the following when you work with UDF resources:
- Make sure UDFs are well-documented with clear parameter descriptions and return types.
- Handle UDF errors gracefully in your app code, including parameter validation and exception handling.
- Test UDF behavior in the app context before deployment to verify expected results.
- Consider UDF dependencies on underlying tables or data sources when planning app permissions.