Updated on 2026-11-06 GMT+08:00

CREATE FUNCTION

Overview

The CREATE FUNCTION statement is used to register user-defined functions (UDFs) in the current catalog, including scalar functions, table functions, and aggregate functions. The registered functions can be used in SQL queries like built-in functions.

Application Scenarios

  • Business logic encapsulation: Complex business computing logic is encapsulated into UDFs and invoked concisely in SQL.
  • Format conversion: User-defined data format parsing functions are used to process non-standard data such as JSON and XML.
  • Encryption and anonymization: User-defined encryption and anonymization algorithms are implemented to meet data security requirements.

Notes and Constraints

  • AI DataLake does not support the TEMPORARY keyword. Therefore, temporary functions cannot be created.
  • The JAR file of the user-defined function must be uploaded to the cluster in advance to ensure that the class path is accessible.
  • The function class must implement the UDF interface corresponding to Flink (such as ScalarFunction, TableFunction, and AggregateFunction).
  • The function name must be unique in the same database.

Syntax Structure

CREATE [SYSTEM] FUNCTION
[IF NOT EXISTS] [catalog_name.][db_name.]function_name
AS class_identifier
[LANGUAGE JAVA|SCALA|PYTHON]

Syntax Description

Parameter

Mandatory

Description

SYSTEM

No

Specifies that the function is a system function. The system function has a higher resolution priority than the built-in function and can override the behavior of the built-in function with the same name.

IF NOT EXISTS

No

Specifies that no error is reported when a function with the same name already exists. If this parameter is not specified, an exception is thrown when a function with the same name already exists.

function_name

Yes

Specifies the function name, which must be unique in the database. You can specify the catalog_name and db_name prefixes.

class_identifier

Yes

Specifies the fully qualified class name of the UDF implementation class. The class must implement the UDF interface corresponding to Flink, and the JAR file must be available in the cluster.

LANGUAGE

No

Specifies the implementation language of the UDF. The supported languages are Java, Scala, and Python. The default language is Java. For Python UDFs, you need to configure the Python environment.

Usage Example

Example: Create a Java UDF.

-- Create a DataGen source table.
CREATE TABLE datagen_source (
id BIGINT,
raw_text STRING
) WITH (
'connector' = 'datagen',
'rows-per-second' = '10',
'fields.id.kind' = 'sequence',
'fields.id.start' = '1',
'fields.id.end' = '1000',
'fields.raw_text.length' = '10'
);
-- Create the Print result table.
CREATE TABLE print_sink (
id BIGINT,
processed_text STRING
) WITH (
'connector' = 'print'
);
-- Register the UDF.
CREATE FUNCTION IF NOT EXISTS my_udf AS 'com.example.MyScalarFunction';
-- Submit the job.
INSERT INTO print_sink
SELECT id, my_udf(raw_text) AS processed_text
FROM datagen_source;