- 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).
Executable User Defined Functions
This feature is supported in private preview in ClickHouse Cloud.
Please contact ClickHouse Support at https://clickhouse.cloud/support to access.
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.
Executable User Defined Functions
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
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.