SQL User-Defined Function
Function
SQL user-defined functions allow defining function logic using SQL expressions. They support scalar functions (returning a single value) and table-valued functions (returning a table result set).
Syntax
- Creating a Scalar function
CREATE [OR REPLACE] FUNCTION function_name ([parameter_name data_type [{ DEFAULT | = } default_value]] [, ...]) RETURNS return_data_type RETURN expression;
- Creating a table-valued function
CREATE [OR REPLACE] FUNCTION function_name ([parameter_name data_type [{ DEFAULT | = } default_value]] [, ...]) RETURNS TABLE (col_name col_type [, ...]) RETURN query_statement;
- Deleting a function
DROP FUNCTION [IF EXISTS] function_name;
- Viewing the function definition
DESCRIBE FUNCTION [EXTENDED] function_name;
Keywords
- OR REPLACE: Replaces the function if it already exists.
- RETURNS: Specifies the return value type.
- RETURN: The function body, a SQL expression or query statement.
- DEFAULT / =: Specifies a default value for a parameter.
Parameters
| Parameter | Mandatory | Description |
|---|---|---|
| function_name | Yes | Function name. |
| parameter_name | No | Parameter name. |
| data_type | No | The parameter data type. If omitted, the system infers the type from the default value. |
| default_value | No | The default value for the parameter. |
| return_data_type | Yes | The return value data type. |
| expression | Yes | The scalar function body, a SQL expression. |
| query_statement | Yes | The table-valued function body, a SELECT query statement. |
Examples
- Creating a simple scalar function
CREATE FUNCTION hello() RETURNS STRING RETURN 'Hello World'; SELECT hello(); -- Output: Hello World
- Creating a scalar function with parameters
CREATE FUNCTION add_tax(price DOUBLE) RETURNS DOUBLE RETURN price * 1.13; SELECT add_tax(100.0); -- Output: 113.0
- Creating a function with default parameters
CREATE FUNCTION greet(name STRING DEFAULT 'World') RETURNS STRING RETURN CONCAT('Hello, ', name, '!'); SELECT greet(); -- Output: Hello, World! SELECT greet('Spark'); -- Output: Hello, Spark! - Creating a table-valued function
CREATE FUNCTION range_n(n INT) RETURNS TABLE (id INT, value STRING) RETURN SELECT id, CONCAT('val_', id) FROM EXPLODE(SEQUENCE(1, n)) AS t(id); SELECT * FROM range_n(3); -- Output: (1, val_1), (2, val_2), (3, val_3) - Replacing an existing function
CREATE OR REPLACE FUNCTION add_tax(price DOUBLE) RETURNS DOUBLE RETURN price * 1.2;
- Deleting a function
DROP FUNCTION IF EXISTS add_tax;
Precautions
- SQL user-defined functions are a new feature in Spark 4.0.
- For scalar functions, RETURN is followed by a single SQL expression.
- For table-valued functions, RETURN is followed by a SELECT query statement.
- For functions with DEFAULT parameters, the parameter can be omitted when calling.
- Declared session variables can be referenced within the function body.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot