User-defined functions in Cloud
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 two ways to create user-defined functions:
- Using SQL
- Using the UI and your own code (private preview)
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.
- 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 should get back the result:
- You can use the
DROP FUNCTIONcommand to remove the UDF you just created:
UDFs in ClickHouse Cloud do not inherit user-level settings. They execute with default system settings.
This means:
- 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.
If you are interested in trying out this feature, please contact support to enroll in private preview.
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.
Create the Python file
Create a new file main.py locally:
Now compress the file into a ZIP archive:
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.
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:
You should see the result:
Create a new version
- 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
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.