CREATE FUNCTION
Note:
Java scalar, aggregate, and table UDFs require a BYOC warehouse and service enablement. They are unavailable for SaaS warehouses. SQL alias functions do not require a Java JAR and have a separate applicability scope. Confirm Java artifact distribution with VeloDB Cloud support. Replace
<artifact-host>in the examples with an approved artifact host.
Description
This statement is used to create a custom function.
Syntax
CREATE [ GLOBAL ]
[{AGGREGATE | TABLES | ALIAS }] FUNCTION <function_name>
(<arg_type> [, ...])
[ RETURNS <ret_type> ]
[ INTERMEDIATE <inter_type> ]
[ WITH PARAMETER(<param> [,...]) AS <origin_function> ]
[ PROPERTIES ("<key>" = "<value>" [, ...]) ]
Required parameters
1. <function_name>
If
function_nameincludes a database name, such asdb1.my_func, the custom function will be created in the corresponding database. Otherwise, the function will be created in the database of the current session. The name and parameters of the new function must not be identical to an existing function in the current namespace; otherwise, the creation will fail.
2. <arg_type>
The input parameter types of the function. One or more fixed arguments can be declared. A future release removes variadic function declarations with
.... Use anARRAYparameter when a function needs a collection of values. This future restriction is not a claim that Core 26.1.3 rejects every variadic declaration.
3. <ret_type>
The return parameter type of the function. This is a required parameter for creating a new function. If creating an alias for an existing function, this parameter is not necessary.
Optional parameters
1. GLOBAL
If specified, the function is registered in the warehouse-wide global function namespace. It does not create a function across other warehouses or the organization.
2. AGGREGATE
If specified, the created function is an aggregate function.
3. TABLES
If specified, the created function is a table function.
4. ALIAS
If specified, the created function is an alias function.
If none of the above parameters representing the function type is selected, it indicates that the created function is a scalar function.
5. <inter_type>
Used to indicate the data type during the intermediate stage of an aggregate function.
6. <param>
The parameter names of an alias function. Zero-argument aliases are allowed. The names and argument types must have matching counts.
7. <origin_function>
Used to indicate the original function corresponding to the alias function.
8. <properties>
file: Indicates the JAR package containing the user-defined function (UDF). Use an immutable HTTPS artifact reachable from the required service nodes, with distribution approved by support. This parameter is mandatory for Java UDFs.symbol: Indicates the class name containing the UDF class. This parameter is mandatory.type: Indicates the UDF call type. UseJAVA_UDFfor a Java UDF. The engine also has a Native default, which does not establish a supported Cloud native-code upload workflow.always_nullable: Indicates whether the UDF result may contain NULL values. This is an optional parameter with a default value of true.volatility: Indicates how stable the function result is. It is present in Core 26.1.3. It is optional. The default value isvolatilefor scalar UDFs, andimmutablefor UDAF/UDTF. Valid values are:
immutable: The same input always produces the same output. Most deterministic UDFs should be marked asimmutableso that the optimizer can apply more plan optimizations.stable: The result is stable within a single SQL statement but may change between statements, similar tonow(). SQL cache and materialized view rewrite are disabled for this type of function.volatile: The result may change on each call, similar torandom(). SQL cache, materialized view rewrite, and many optimizer rewrite rules are disabled for this type of function.
Access control requirements
To execute this command, the user must have ADMIN_PRIV privileges.
Example
-
Create a custom UDF function. For more details, refer to JAVA-UDF.
CREATE FUNCTION java_udf_add_one(int) RETURNS int PROPERTIES ("file"="https://<artifact-host>/java-udf-demo.jar","symbol"="org.apache.doris.udf.AddOne","always_nullable"="true","type"="JAVA_UDF","volatility"="immutable"); -
Create a custom UDAF function.
CREATE AGGREGATE FUNCTION simple_sum(INT) RETURNS INT PROPERTIES ("file"="https://<artifact-host>/java-udaf.jar","symbol"="org.apache.doris.udf.demo.SimpleDemo","always_nullable"="true","type"="JAVA_UDF"); -
Create a custom UDTF function.
CREATE TABLES FUNCTION java_udtf(string, string) RETURNS array<string> PROPERTIES ("file"="https://<artifact-host>/java-udaf.jar","symbol"="org.apache.doris.udf.demo.UDTFStringTest","always_nullable"="true","type"="JAVA_UDF"); -
Create a custom alias function. For more information, refer to Alias Function.
CREATE ALIAS FUNCTION id_masking(INT) WITH PARAMETER(id) AS CONCAT(LEFT(id, 3), '****', RIGHT(id, 4)); -
Create a global custom alias function.
CREATE GLOBAL ALIAS FUNCTION id_masking(INT) WITH PARAMETER(id) AS CONCAT(LEFT(id, 3), '****', RIGHT(id, 4)); -
Create a volatile Python UDF. Functions such as
uuid.uuid4()that depend on randomness should keep the defaultvolatility = volatileand must not be incorrectly marked asimmutable.CREATE TABLE cte_uuid_seed (id INT) ENGINE=OLAP DUPLICATE KEY(id)DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES ("replication_num" = "1");INSERT INTO cte_uuid_seed VALUES (1),(2),(3);DROP FUNCTION IF EXISTS py_uuid_token(INT);CREATE FUNCTION py_uuid_token(INT)RETURNS STRINGPROPERTIES ("type" = "PYTHON_UDF","symbol" = "py_uuid_token_impl","always_nullable" = "false","runtime_version" = "3.12.11","volatility" = "volatile")AS $$import uuiddef py_uuid_token_impl(x):return f"{x}-{uuid.uuid4()}"$$;SET enable_cte_materialize = true;SET inline_cte_referenced_threshold = 10;WITH cte AS (SELECT id, py_uuid_token(id) AS token FROM cte_uuid_seed)SELECT id, COUNT(DISTINCT token) AS distinct_tokensFROM (SELECT id, token FROM cte UNION ALL SELECT id, token FROM cte) uGROUP BY id ORDER BY id;Correct result:
+------+-----------------+| id | distinct_tokens |+------+-----------------+| 1 | 1 || 2 | 1 || 3 | 1 |+------+-----------------+For this function, the following definition is incorrect:
DROP FUNCTION IF EXISTS py_uuid_token(INT);CREATE FUNCTION py_uuid_token(INT)RETURNS STRINGPROPERTIES ("type" = "PYTHON_UDF","symbol" = "py_uuid_token_impl","always_nullable" = "false","runtime_version" = "3.12.11","volatility" = "immutable")AS $$import uuiddef py_uuid_token_impl(x):return f"{x}-{uuid.uuid4()}"$$;Run the same query again:
WITH cte AS (SELECT id, py_uuid_token(id) AS token FROM cte_uuid_seed)SELECT id, COUNT(DISTINCT token) AS distinct_tokensFROM (SELECT id, token FROM cte UNION ALL SELECT id, token FROM cte) uGROUP BY id ORDER BY id;Incorrect result:
+------+-----------------+| id | distinct_tokens |+------+-----------------+| 1 | 2 || 2 | 2 || 3 | 2 |+------+-----------------+Why this is wrong: Because
py_uuid_tokenis volatile, each call touuid.uuid4()generates a new value. If the function is incorrectly marked asvolatility = immutable, the optimizer may treat repeated references as safe to rewrite and may choose a plan that evaluates the UDF separately on both sides ofUNION ALL. As a result, the sameidcan produce two differenttokenvalues, andCOUNT(DISTINCT token)changes from1to2.