All Products
Search
Document Center

MaxCompute:SQL UDFs

Last Updated:Mar 26, 2026

SQL user-defined functions (SQL UDFs) let you define reusable functions directly in SQL, without writing Java or Python code. Unlike Java or Python UDFs, SQL UDFs require no compilation step, no resource upload, and no separate registration workflow — write the function body as a SQL expression and start calling it immediately.

SQL UDFs also support function-type parameters, which lets you pass built-in functions, other UDFs, or anonymous functions as arguments — similar to Lambda expressions.

Key concepts

ConceptDescription
Permanent SQL UDFCreated with CREATE SQL FUNCTION. Stored in the MaxCompute metadata system, visible in the function list, and callable from any script or session.
Temporary SQL UDFCreated with FUNCTION (no CREATE SQL prefix). Exists only within the script where it is defined and cannot be called elsewhere.
Function-type parameterAn input parameter that accepts a function as its value — a built-in function, another UDF, or an anonymous function.
SQL script modeThe execution mode required when defining SQL UDFs. Defining a UDF in common SQL editing mode may cause an error.

Prerequisites

Before you begin, ensure that you have:

Create a permanent SQL UDF

Permanent SQL UDFs are stored in the MaxCompute metadata system. After creation, they appear in the function list and can be called from any phase.

Syntax

CREATE SQL FUNCTION <function_name>(@<parameter_in1> <datatype>[, @<parameter_in2> <datatype>...])
[RETURNS @<parameter_out> <datatype>]
AS [BEGIN]
<function_expression>
[END];

Parameters

ParameterRequiredDescription
function_nameYesName of the SQL UDF. Must be unique within the project and cannot match any built-in function name. Each name can be registered only once. Run LIST FUNCTIONS to check for conflicts.
parameter_inYesInput parameters. Each is prefixed with @. Parameters can be of the function type — see Pass a function as a parameter.
datatypeYesData type of each input parameter. Must be a MaxCompute-supported data type.
RETURNS @parameter_outNoReturn variable. If omitted, the value of function_name is returned by default.
function_expressionYesThe SQL expression that implements the function logic. Can reference built-in operators, built-in functions, or other UDFs.
BEGIN / ENDNoOptional. Used to wrap multi-statement logic when the function body contains more than one statement.

Examples

Simple function — add 1 to a BIGINT input:

CREATE SQL FUNCTION my_add(@a BIGINT) AS @a + 1;

Multi-statement function using BEGIN / END:

CREATE SQL FUNCTION my_sum(@a BIGINT, @b BIGINT, @c BIGINT) RETURNS @my_sum BIGINT
AS BEGIN
    @temp := @a + @b;
    @my_sum := @temp + @c;
END;

Create a temporary SQL UDF

Temporary SQL UDFs are not stored in MaxCompute. They exist only within the SQL script where they are defined and cannot be called in other sessions or scripts.

Syntax

FUNCTION <function_name>(@<parameter_in1> <datatype>[, @<parameter_in2> <datatype>...])
[RETURNS @<parameter_out> <datatype>]
AS [BEGIN]
<function_expression>
[END];

The parameters are identical to those for CREATE SQL FUNCTION. Omit CREATE SQL to create a temporary UDF.

Example

FUNCTION my_add(@a BIGINT) AS @a + 1;

Query a SQL UDF

Only permanent SQL UDFs can be queried — temporary UDFs are not stored in MaxCompute.

To run DESC FUNCTION from the MaxCompute client (odpscmd), upgrade the client to version 0.34.0 or later. For installation and upgrade instructions, see MaxCompute client (odpscmd).

Syntax

DESC FUNCTION <function_name>;

Example

DESC FUNCTION my_add;

Output:

Name                  my_add
Owner                 ALIYUN$s***_****@**.aliyunid.com
Created Time          2021-05-08 11:26:02
SQL Definition Text   CREATE SQL FUNCTION MY_ADD(@a BIGINT) AS @a + 1

Call a SQL UDF

Call a SQL UDF the same way you call a built-in function.

  • Permanent SQL UDFs can be called in any phase.

  • Temporary SQL UDFs can only be called within the script where they are defined.

The data types of the arguments you pass must match the data types defined in the UDF.

Syntax

SELECT <function_name>(<column_name>[, ...]) FROM <table_name>;

Example

-- Create a table and insert sample data.
CREATE TABLE src (c BIGINT, d STRING);
INSERT INTO TABLE src VALUES (1, '100.1'), (2, '100.2'), (3, '100.3');

-- Call my_add on column c.
SELECT my_add(c) FROM src;

Output:

+------------+
| _c0        |
+------------+
| 2          |
| 3          |
| 4          |
+------------+

Drop a SQL UDF

Syntax

DROP FUNCTION <function_name>;

Example

DROP FUNCTION my_add;

Pass a function as a parameter

SQL UDFs support function-type parameters. When you declare a parameter as FUNCTION (<input_type>) RETURNS <output_type>, the caller can pass any compatible function — a built-in function, other UDFs (Java, Python, or SQL), or an anonymous function.

Example

-- Define a SQL UDF.
FUNCTION add(@a BIGINT) AS @a + 1;

-- Define a higher-order UDF that accepts a function as an argument.
FUNCTION op(@a BIGINT, @fun FUNCTION (BIGINT) RETURNS BIGINT) AS @fun(@a);

-- Call op, passing different functions as the second argument.
-- add is a SQL UDF; abs is a MaxCompute built-in function.
SELECT op(key, add), op(key, abs) FROM VALUES (1), (2) AS t(key);

Output:

+------------+------------+
| _c0        | _c1        |
+------------+------------+
| 2          | 1          |
| 3          | 2          |
+------------+------------+

_c0 is the result of add(key) (adds 1). _c1 is the result of abs(key) (absolute value). For details on the ABS function, see Mathematical functions.

For precautions on using Lambda expressions in MaxCompute, see Lambda functions.

Use anonymous functions

When calling a UDF with a function-type parameter, pass an inline anonymous function instead of a named function. The compiler infers the anonymous function's parameter types from the UDF signature.

Example

FUNCTION op(@a BIGINT, @fun FUNCTION (BIGINT) RETURNS BIGINT) AS @fun(@a);

-- Pass an anonymous function as the second argument.
SELECT op(key, FUNCTION (@a) AS @a + 1) FROM VALUES (1), (2) AS t(key);

FUNCTION (@a) AS @a + 1 is the anonymous function. Its parameter type is inferred from op's signature — no explicit type declaration is needed.

Example: simplify repeated SQL logic

Scenario: Convert date strings from yyyy-mm-dd to yyyymmdd. The input dates are 2020-11-21, 2020-1-01, 2019-5-1, and 19-12-1.

Using a SQL UDF (recommended)

Define the conversion logic once, then call the function wherever needed:

CREATE SQL FUNCTION y_m_d2yyyymmdd(@y_m_d STRING) RETURNS @yyyymmdd STRING
AS BEGIN
    @yyyymmdd := CONCAT(
        LPAD(SPLIT_PART(@y_m_d, '-', 1), 4, '0'),
        LPAD(SPLIT_PART(@y_m_d, '-', 2), 2, '0'),
        LPAD(SPLIT_PART(@y_m_d, '-', 3), 2, '0')
    );
END;

SELECT y_m_d2yyyymmdd(d) FROM VALUES ('2020-11-21'), ('2020-1-01'), ('2019-5-1'), ('19-12-1') AS t(d);

Output:

+------------+
| _c0        |
+------------+
| 20201121   |
| 20200101   |
| 20190501   |
| 00191201   |
+------------+

Without a SQL UDF

Inline the full expression at every call site — harder to maintain and error-prone when the logic needs to change:

SELECT CONCAT(
    LPAD(SPLIT_PART(d, '-', 1), 4, '0'),
    LPAD(SPLIT_PART(d, '-', 2), 2, '0'),
    LPAD(SPLIT_PART(d, '-', 3), 2, '0')
) FROM VALUES ('2020-11-21'), ('2020-1-01'), ('2019-5-1'), ('19-12-1') AS t(d);

Limits

LimitDetails
Execution modeSQL UDFs must be defined in SQL script mode. Defining a UDF in common SQL editing mode may cause an error. See SQL in script mode.
Data type compatibilityInput parameter data types must be MaxCompute-supported types. The data types of arguments passed when calling a SQL UDF must match the types defined in the UDF. See MaxCompute V2.0 data type edition.
PermissionsCreating, querying, calling, or dropping a SQL UDF requires function-level permissions. See MaxCompute permissions.
Function name uniquenessFunction names must be unique within a project and cannot match built-in function names. Each name can be registered only once.
Temporary UDF scopeTemporary SQL UDFs can only be called within the script where they are defined. They are not stored and cannot be queried in the function list.
Query client versionQuerying a SQL UDF with DESC FUNCTION from the odpscmd client requires client version 0.34.0 or later.

What's next