CREATE FUNCTION lets you register reusable SQL expressions as user-defined functions (UDFs) directly inside ClickHouse.
A ClickHouse user-defined function (UDF) is a named SQL expression stored in the system that you can call like any built-in function. UDFs encapsulate business logic, reduce query duplication, and improve readability.
Create a UDF when you repeatedly apply the same calculation—such as currency conversion, tax computation, or data cleaning—across many queries. Keeping logic in one place simplifies maintenance.
List parameters inside parentheses with optional types.If types are omitted, ClickHouse infers them from usage. The “->” arrow follows the parameters, pointing to the expression that returns the result.
ClickHouse does not support ALTER FUNCTION yet. To change a UDF, DROP it and CREATE it again with the new logic.
You need the CREATE FUNCTION privilege in the target database.Always restrict this right to trusted roles to avoid accidental or malicious changes.
Prefix UDF names with the team or project (e.g., ecom_calc_tax
). Document parameters in comments. Write deterministic expressions to keep queries cache-friendly.
Define an ecom_calc_tax
UDF once and call it from multiple dashboards that analyze the Orders
table.
.
Yes. OR REPLACE
lets you overwrite an existing UDF without a DROP.
Yes, nested UDF calls work as long as all referenced functions exist at compile time.
UDF definitions are stored in system.functions
and replicated if you use ReplicatedMergeTree and metadata replication mechanisms.