- Executable UDFs start an external program or script (Python, Bash, etc.) and stream blocks of data to it over STDIN / STDOUT. Use them to integrate existing code or tooling without recompiling ClickHouse. They have higher per‑call overhead compared to in‑process options and are best for heavier logic or where a different runtime is required.
- SQL UDFs are defined with
CREATE FUNCTIONpurely in SQL. They are inlined/expanded into the query plan (no process boundary), making them lightweight and ideal for reusing expression logic or simplifying complex calculated columns. - Experimental WebAssembly UDFs run code compiled to WebAssembly inside a sandbox within the server process. They offer lower per‑call overhead than external executables with better isolation than native extensions, making them suitable for custom algorithms written in languages that can target WASM (e.g. C/C++/Rust).
- Experimental driver-based executable UDFs let an operator-supplied “driver” turn a code snippet supplied in
CREATE FUNCTION ... ENGINE = DriverName(...) AS '...'into an executable UDF at function-creation time (for example, by compiling it). They build on executable UDFs and require server-side driver configuration.
With the configuration installed by the packages, the server isolates itself from the filesystem of the host (see
filesystem_isolation), and the default Docker image has no shell or interpreter. Executable UDFs that run scripts and driver-based UDFs need additional configuration. See Executable UDFs with filesystem isolation.Executable User Defined Functions
In ClickHouse Cloud, executable UDFs are generally available and are created through the Cloud console, the Cloud API, or Terraform. See User-defined functions in Cloud for the Cloud-specific workflow, including the Native runtime for compiled binaries.
user_defined_executable_functions_config parameter.
A function configuration contains the following settings:
The command must read arguments from
STDIN and must output the result to STDOUT. The command must process arguments iteratively. That is after processing a chunk of arguments it must wait for the next chunk.
With the
TabSeparated format, ClickHouse escapes special characters in the arguments it sends and unescapes the command output, following the TabSeparated escaping rules. For example, a backslash in an argument reaches the command as \\, and a tab or a line feed as \t or \n. The command must escape its output in the same way. Otherwise, a backslash in the output, for example in JSON text, is read as the start of an escape sequence, and the result is wrong or fails to parse.If the command does not escape its output, use one of these formats instead:TabSeparatedRaw: values are not escaped, so they must not contain tabs or line feeds.JSONEachRow: strings can contain backslashes, tabs and line feeds. See the JSON example below.
Examples
UDF from inline script
Createtest_function_sum manually specifying execute_direct to 0 using either XML or YAML configuration.
- XML
- YAML
File
test_function.xml (/etc/clickhouse-server/test_function.xml with default path settings)./etc/clickhouse-server/test_function.xml
Query
Result
UDF from Python script
In this example we create a UDF which reads a value fromSTDIN and returns it as a string.
Create test_function using either XML OR YAML configuration.
- XML
- YAML
File
test_function.xml (/etc/clickhouse-server/test_function.xml with default path settings)./etc/clickhouse-server/test_function.xml
Create a script file
test_function.py inside user_scripts folder (/var/lib/clickhouse/user_scripts/test_function.py with default path settings).
Query
Result
Read two values from STDIN and return their sum as a JSON object
Create test_function_sum_json with named arguments and format JSONEachRow using either XML or YAML configuration.
- XML
- YAML
File
test_function.xml (/etc/clickhouse-server/test_function.xml with default path settings)./etc/clickhouse-server/test_function.xml
Create script file
test_function_sum_json.py inside the user_scripts folder (/var/lib/clickhouse/user_scripts/test_function_sum_json.py with default path settings).
Query
Result
Use parameters in command setting
Executable user defined functions can take constant parameters configured in command setting (this works only for user defined functions with executable type).
It also requires the execute_direct option to ensure no shell argument expansion vulnerability.
- XML
- YAML
File
test_function_parameter_python.xml (/etc/clickhouse-server/test_function_parameter_python.xml with default path settings)./etc/clickhouse-server/test_function_parameter_python.xml
Create script file
test_function_parameter_python.py inside the user_scripts folder (/var/lib/clickhouse/user_scripts/test_function_parameter_python.py with default path settings).
Query
Result
UDF from shell script
In this example, we create a shell script that multiplies each value by 2.- XML
- YAML
File
test_function_shell.xml (/etc/clickhouse-server/test_function_shell.xml with default path settings)./etc/clickhouse-server/test_function_shell.xml
Create a script file
test_shell.sh inside the user_scripts folder (/var/lib/clickhouse/user_scripts/test_shell.sh with default path settings).
/var/lib/clickhouse/user_scripts/test_shell.sh
Query
Result
Executable UDFs with filesystem isolation
The server settingfilesystem_isolation makes the server isolate itself from the filesystem of the host at startup. Only the paths that the server needs stay visible to it. The config.xml installed by the packages sets it to best_effort, so the isolation is applied wherever the kernel allows it.
The programs that the server starts run inside the same isolated filesystem. This covers executable UDFs, the executable table function, the Executable and ExecutablePool table engines, and the executable and executable_pool dictionary sources. For these programs:
- The directory
user_scripts_pathis visible, read-only and executable. It is mounted as a whole, so scripts that are added to it or changed on the host after the startup are visible as well. The server itself cannot modify them. - Every other path that the server can write to is mounted with
noexec. A program placed there cannot be started. - There is no shell, no interpreter, and no shared library other than those of the server itself. A script with
#!/usr/bin/python3or#!/bin/bashcannot be started, and neither can a command withexecute_direct=0, because it runs through/bin/sh -c. /tmpis a private and empty directory.- A file that a program reads outside of the visible paths, such as a model, a reference dataset, a virtual environment, or a configuration file in
/etc, is not visible.
88 means that starting the program failed:
disabled means that the server runs without the isolation:
Isolated the filesystem of this process.
Making interpreters and data visible
Make the interpreter of the scripts, the libraries it needs, and the files the scripts read visible with thefilesystem_isolation_paths section of the server configuration. For example, for Python and shell scripts on Debian or Ubuntu:
/etc/clickhouse-server/config.d/udf_isolation.xml
- Paths that do not exist, such as
/lib64on AArch64, are skipped. - Symbolic links on the way to a path, such as
/bin -> usr/bin, are recreated inside, and their targets are made visible too. - A
<read_write>path is mounted withnoexec, the same as the other writable paths. Keep programs inuser_scripts_pathor in a<read_only>path. - The isolation is applied once, at startup. A change in
filesystem_isolation_pathstakes effect after a restart of the server.
/usr or /opt.
A statically linked program, such as one written in Go, in Rust with the musl target, or in C linked with -static, needs no interpreter and no shared library. It works in user_scripts_path without any additional paths, and it also works in the Docker image without a shell. WebAssembly UDFs run inside the server process, so they are not affected by the isolation at all.
If none of this is possible, disable the isolation. The server then loses the protection described in filesystem_isolation:
/etc/clickhouse-server/config.d/filesystem_isolation.xml
Executable UDFs in Docker
The defaultclickhouse/clickhouse-server image (the tags without a suffix) is built FROM scratch. It contains only the statically linked clickhouse binary, its configuration, and a few system files. There is no shell, no C library, no Python, and no other program, so a script cannot run in this image, regardless of the isolation.
The image uses the same config.xml as the packages, with filesystem_isolation set to best_effort. Container runtimes such as Docker do not let a process create a user namespace unless it has CAP_SYS_ADMIN. So, by default, the server in a container runs without the isolation and logs a warning with the reason. If the container has CAP_SYS_ADMIN (--cap-add=SYS_ADMIN) and its security profile allows creating namespaces and mounting filesystems, the server applies the isolation, and the programs need the paths from the previous section as well.
Choose one of these options to run executable UDFs in a container:
-
Use statically linked programs. They work in the default image. Mount them into
user_scripts_path, which is/var/lib/clickhouse/user_scripts/in the image, and mount the configuration of the functions into/etc/clickhouse-server/. The configuration file must match the*_function.*mlpattern ofuser_defined_executable_functions_config: -
For scripts, build an image on top of the image with the
-ubuntusuffix, which has a shell, and install the interpreter that the scripts need:Then run it with the same-voptions as above. -
For scripts that need only
/bin/shand its builtin commands, mount a statically linked shell, such asbusyboxfrom thebusybox-staticpackage of Debian and Ubuntu, into the default image with-v /bin/busybox:/bin/sh:ro.
Error Handling
Some functions might throw an exception if the data is invalid. In this case, the query is canceled and an error text is returned to the client. For distributed processing, when an exception occurs on one of the servers, the other servers also attempt to abort the query.Evaluation of Argument Expressions
In almost all programming languages, one of the arguments might not be evaluated for certain operators. This is usually the operators&&, ||, and ?:.
In ClickHouse, arguments of functions (operators) are always evaluated.
This is because entire parts of columns are evaluated at once, instead of calculating each row separately.
Performing Functions for Distributed Query Processing
For distributed query processing, as many stages of query processing as possible are performed on remote servers, and the rest of the stages (merging intermediate results and everything after that) are performed on the requestor server. This means that functions can be performed on different servers. For example, in the querySELECT f(sum(g(x))) FROM distributed_table GROUP BY h(y),
- if a
distributed_tablehas at least two shards, the functions ‘g’ and ‘h’ are performed on remote servers, and the function ‘f’ is performed on the requestor server. - if a
distributed_tablehas only one shard, all the ‘f’, ‘g’, and ‘h’ functions are performed on this shard’s server.
hostName function, which returns the name of the server it is running on in order to make GROUP BY by servers in a SELECT query.
If a function in a query is performed on the requestor server, but you need to perform it on remote servers, you can wrap it in an ‘any’ aggregate function or add it to a key in GROUP BY.
SQL User Defined Functions
Custom functions from lambda expressions can be created using the CREATE FUNCTION statement. To delete these functions use the DROP FUNCTION statement.WebAssembly User Defined Functions
WebAssembly User Defined Functions (WASM UDFs) allow you to run custom code compiled to WebAssembly inside the ClickHouse server process.Quick Start
Enable experimental WebAssembly support in your ClickHouse configuration:More Information
Refer to the documentation on WebAssembly User Defined Functions for more details.Driver-based Executable User Defined Functions
This is an experimental feature that may change in backward-incompatible ways in future releases. Enable it with the
allow_experimental_executable_udf_drivers server setting.ENGINE = DriverName(...), ClickHouse runs the driver’s create_command, passing it the function signature and the code body; the driver compiles or otherwise processes the body and prints an executable UDF configuration, which ClickHouse then stores and loads.
This lets administrators offer users a safe, narrow way to define functions in an arbitrary language (for example, C compiled inside a sandboxed container) without giving them access to the server’s configuration files or filesystem. The set of available drivers is entirely controlled by the operator.
Enabling drivers
Driver-based executable UDFs are disabled by default. To enable them:-
Set the experimental gate in the server configuration:
-
Point
user_defined_executable_function_drivers_configat one or more driver configuration files (a glob is supported), and optionally setdynamic_user_defined_executable_functions_path, the directory where the generated executable UDF configurations are stored:
SYSTEM RELOAD CONFIG, so drivers can be added, changed, or removed without restarting the server.
Drivers and filesystem isolation
A driver generates the program of a function in the working directory of the function, insidedynamic_user_defined_executable_functions_path. With filesystem_isolation, every path that the server writes to is mounted with noexec, so by default that program could never be started. While the dynamic directory is not executable:
CREATE FUNCTION ... ENGINE = DriverName(...)fails with theSUPPORT_IS_DISABLEDerror.- At startup, a persisted
ATTACH FUNCTIONwhose generated configuration is missing cannot be recreated, because that would run the driver. - Functions that were created before the isolation was enabled are still loaded from their generated configurations, but calling them fails, because their programs cannot be started.
<read_write_executable> in filesystem_isolation_paths, and make the drivers’ runtime visible. The drivers are started with /usr/bin/env, and a driver written as a shell script needs /bin/sh:
/etc/clickhouse-server/config.d/udf_drivers_isolation.xml
dynamic_user_defined_executable_functions_path, and the change takes effect after a restart. A <read_write_executable> path lets the server run the files it writes there, which the isolation otherwise prevents. Declare only the dynamic directory this way, not a broader path. To use drivers without this, disable the isolation instead:
/etc/clickhouse-server/config.d/filesystem_isolation.xml
docker, runsc or cc. None of these is in the default Docker image, which is built FROM scratch. To use drivers in a container, build an image on top of the image with the -ubuntu suffix and install the tools that the drivers need (see Executable UDFs in Docker).
Driver configuration
A driver is described by an XML (or YAML) file with a top-level<driver> element. The following fields are supported:
Example driver configuration:
Driver invocation contract
WhenCREATE FUNCTION runs, create_command is invoked with the configured env variables set and the following arguments:
--name <function_name>--return <return_type>(if aRETURNSclause is present)--args <signature>(if anARGUMENTSclause is present), where the signature is the declared argument list, for examplex UInt8, y DateTime--<key> <value>for every declared engine argument supplied inENGINE = DriverName(key = value)
AS) is sent to the command’s standard input. The command must print the configuration of an executable UDF to its standard output. The format is auto-detected: output that starts with < is treated as XML, otherwise as YAML. The defined function name in the generated configuration must match the name being created. If create_command exits with a non-zero status, the statement fails with an exception that includes the exit code and the driver’s standard error.
drop_command, when present, is invoked the same way (without a code body on stdin) when the function is dropped.
Creating a function
create_command, writes the generated configuration into dynamic_user_defined_executable_functions_path, and the existing executable UDF loader picks it up. The function can then be called like any other function.
Dropping a function
DROP FUNCTION invokes the driver’s drop_command (if present), removes the generated dynamic configuration and the per-function working directory, reloads the executable UDF loader, and removes the persisted query.
Persistence and restart
The originating query is persisted as anATTACH FUNCTION ... statement in the user-defined SQL objects directory, so the function survives a server restart. On start, the generated configurations in dynamic_user_defined_executable_functions_path are loaded directly without re-running the driver. If a persisted ATTACH FUNCTION has no matching generated configuration (for example, the dynamic directory was lost), the driver is re-run to recreate it.
Limitations
- The feature is experimental and gated behind
allow_experimental_executable_udf_drivers. - With
filesystem_isolation, which theconfig.xmlof the packages enables, drivers needdynamic_user_defined_executable_functions_pathdeclared as<read_write_executable>. See Drivers and filesystem isolation. - Driver-based functions are not supported with replicated user-defined function storage (
ON CLUSTERand<user_defined_zookeeper_path>), because only the originating query is replicated, not the generated artifacts. RESTOREof a backed-up driver-based function persists the query but does not re-run the driver; the generated configuration is materialized later by restart recovery.
Example C drivers
The source tree ships proof-of-concept drivers underprograms/server/user_defined_executable_function_drivers_config.d/ that compile and run a C function body. They are examples and are not installed by packages:
DockerC- compiles and runs the code inside sandboxed Docker containers (--network=none --read-only --cap-drop=ALL --security-opt=no-new-privileges, plus memory/CPU/PID limits), emitting anexecutable_poolUDF.GVisorC- a variant that runs the compiled binary under the gVisorrunscruntime.UnsafeC- compiles and runs the code directly on the host without a sandbox. As the name indicates, it provides no isolation and is intended only for trusted environments and testing.
Third-party drivers
Drivers can also come from outside ClickHouse. These are maintained by their authors, not by ClickHouse; review them as you would the example drivers before exposing them to untrusted users:-
HyperlightPython- runs a Python function body in a Hyperlight micro-VM with its own kernel. Hyperlight is a Cloud Native Computing Foundation sandbox project. Each block of rows starts from a snapshot of the guest, so the code has no network, no access to the server’s files, and no state between queries. Requires Linux with/dev/kvm.