- Using SQL
- Using the UI and your own code (public beta)
- Using the Cloud API (beta)
- Using Terraform (beta)
SQL user-defined functions
SQL UDFs can be created using theCREATE 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.
- Login to Cloud Console and open the SQL console
- Write the following SQL query to create the
isBusinessHoursfunction:
- Run the following below to test your newly created UDF:
- You can use the
DROP FUNCTIONcommand to remove the UDF you just created:
- Session-level settings (set via
SETstatement) 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 functionisBusinessHours 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 If your Python script imports third-party packages, list them in a
main.py locally: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 You can reference the local bundled path base directory in your Python code using This is useful when you need to:
main.py and requirements.txt. When you create the ZIP archive, include all files: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:- Access configuration files bundled with your UDF
- Load wheel packages for custom dependencies
- Reference additional scripts or data files
3
Create a UDF via the UI
- From the Cloud console homepage, click on the name of your organization in the bottom-left menu.
- Select User-defined functions from the menu.
- On the user-defined functions page, click Set up a UDF. A configuration panel opens on the right side of the screen.
- Enter a function name. For this example, use
isBusinessHours. - 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.
- For this example, use the default settings. For a full list of configuration parameters, see Executable user-defined functions.
- Click Browse File to upload the
.zipfile created at the start of this tutorial. - Add a new argument. For this example, add an argument
timestampwith typeDateTime. - Select a return type. For this example, select
Bool. - 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
- 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
- click SQL Console in the left hand menu
- write the following query:
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.
- From the Cloud console homepage, click on the name of your organization in the bottom-left menu.
- Select User-defined functions from the menu.
- Select the three dots under Actions for the
isBusinessHoursUDF, click Create new version - Upload a zip with the modified code, or change settings and then click Create new version
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.
- Create an upload URL to receive a presigned
application/zipupload 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. - Create the UDF from the uploaded archive, specifying the function name, runtime, arguments, and return type.
- 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.
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_udfmanages 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_attachmentattaches 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 referenceclickhouse_udf.<name>.versionto 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.
isBusinessHours UDF from the earlier example with Terraform:
clickhouse_udf resource removes all versions of the function and detaches it from all services.