Skip to main content
User-defined functions (UDF) allow users to extend the behavior of ClickHouse beyond what is offered by over a thousand different out-of-box functions. In ClickHouse Cloud, there are several ways to create and manage user-defined functions:
  1. Using SQL
  2. Using the UI and your own code (public beta)
  3. Using the Cloud API (beta)
  4. Using Terraform (beta)

SQL user-defined functions

SQL UDFs can be created using the CREATE FUNCTION statement from a lambda expression. In this example we’ll create a simple executable user-defined function, isBusinessHours. The function will check if a certain timestamp falls inside of regular business hours and return true if it does, otherwise false.
  1. Login to Cloud Console and open the SQL console
  2. Write the following SQL query to create the isBusinessHours function:
  1. Run the following below to test your newly created UDF:
You should get back the result:
  1. You can use the DROP FUNCTION command to remove the UDF you just created:
ImportantUDFs in ClickHouse Cloud do not inherit user-level settings. They execute with default system settings.
This means:
  • Session-level settings (set via SET statement) are not propagated to UDF execution context
  • User profile settings are not inherited by UDFs
  • Query-level settings do not apply within UDF execution

User-defined functions created via UI

ClickHouse Cloud offers a UI configuration experience for creating user-defined functions. In this example we’ll create the same simple executable user-defined function isBusinessHours that checks if a certain timestamp falls inside of regular business hours. Previously we created it using SQL, but this time we will create it using Python and configure it via the UI.
1

Create the Python file

Create a new file main.py locally:
If your Python script imports third-party packages, list them in a requirements.txt file and ClickHouse Cloud installs them for you. You can instead bundle dependencies directly in the ZIP, but then you must include cached packages for both CPU architectures, so requirements.txt is simpler. For example:
ClickHouse Cloud expects to find main.py in the zip file you will upload via the UI in the next step. If you name the file something else you will encounter an error.
2

Bundle dependencies and local files

To include dependency packages and any additional local files (such as wheel files, configuration files, or data files), place them in the same directory as your main.py and requirements.txt. When you create the ZIP archive, include all files:
You can reference the local bundled path base directory in your Python code using os.path.dirname(os.path.abspath(__file__)). This returns the absolute path to the directory where your main.py is located within the ZIP archive, allowing you to access other bundled files:
This is useful when you need to:
  • Access configuration files bundled with your UDF
  • Load wheel packages for custom dependencies
  • Reference additional scripts or data files
Now compress the file into a ZIP archive:
Symlinks are not allowedClickHouse Cloud rejects UDF archives that contain symbolic links. Make sure your ZIP bundle contains only regular files and directories — uploads with symlinks will fail validation.
3

Create a UDF via the UI

  1. From the Cloud console homepage, click on the name of your organization in the bottom-left menu.
  2. Select User-defined functions from the menu.
  3. On the user-defined functions page, click Set up a UDF. A configuration panel opens on the right side of the screen.
  4. Enter a function name. For this example, use isBusinessHours.
  5. Select a function type, either Executable pool or Executable:
    • Executable pool: A pool of persistent processes is maintained, and a process is taken from the pool for reads.
    • Executable: The script runs on every query.
  6. For this example, use the default settings. For a full list of configuration parameters, see Executable user-defined functions.
  7. Click Browse File to upload the .zip file created at the start of this tutorial.
  8. Add a new argument. For this example, add an argument timestamp with type DateTime.
  9. Select a return type. For this example, select Bool.
  10. Click Create UDF. A dialog displays the current build status.
    • If there are any problems, the status changes to error.
    • Otherwise, the status progresses from building to provisioning. Your service must be awake to complete provisioning. If your service is idle, click Wake Up Service in the UDF details panel next to the service name.
    • Once complete, the status changes to deployed.
4

Test your UDF

  1. return back to the home page of the SQL Console by clicking Settings - return to your service view from the top left corner of the page
  2. click SQL Console in the left hand menu
  3. write the following query:
You should see the result:
5

Create a new version

To change a UDF’s code, create a new version. The Edit panel only manages which services a UDF is assigned to; uploading a file there won’t replace the deployed code.
  1. From the Cloud console homepage, click on the name of your organization in the bottom-left menu.
  2. Select User-defined functions from the menu.
  3. Select the three dots under Actions for the isBusinessHours UDF, click Create new version
  4. Upload a zip with the modified code, or change settings and then click Create new version
You have successfully added your first user-defined function via the UI, confirmed it runs and seen how to create a new version of it if needed.

Manage UDFs with the Cloud API

Everything available in the UI is also available programmatically through the ClickHouse Cloud API. The UDF endpoints let you script the full lifecycle of a UDF: uploading source archives, creating functions and versions, attaching them to services, and cleaning them up.
These endpoints are in beta and the API contract may change.
The typical workflow to create and deploy a UDF via the API is:
  1. Create an upload URL to receive a presigned application/zip upload URL, then upload your ZIP archive to it. Each upload ID may be used for only one create or version attempt; request a new upload URL when retrying.
  2. Create the UDF from the uploaded archive, specifying the function name, runtime, arguments, and return type.
  3. Attach the UDF to a service. When the version is omitted, the latest ready version is attached. The service must be running; idle services can be woken up first.
The full set of endpoints: See the UDF API reference for request and response schemas.

Manage UDFs with Terraform

The official ClickHouse Terraform provider includes two resources for managing UDFs as Infrastructure as Code:
  • clickhouse_udf manages the function itself. It takes a ZIP archive with the function source code and publishes a new version whenever the archive hash changes, waiting for the build to complete.
  • clickhouse_udf_attachment attaches a UDF version to a service. A service holds at most one version of a function at a time. You can pin a fixed version number, or reference clickhouse_udf.<name>.version to automatically roll services forward to the latest version.
These resources are available in provider version 3.24.0 and later. They are in beta and their behavior may change in future provider versions.
For example, to deploy the isBusinessHours UDF from the earlier example with Terraform:
Attaching only succeeds for versions that are ready, and can take several minutes; idle services are woken up automatically. Deleting a clickhouse_udf resource removes all versions of the function and detaches it from all services.
Last modified on August 20, 2026