Skip to main content
Since version 4.1, the database supports creating SQL user-define-functions(UDFs), allowing users to create functions using SQL expressions. SQL UDF is a lightweight function definition method that encapsulates a SQL expression into a reusable function, which is dynamically expanded into the actual SQL expression at query time.

Overview

Main features of SQL UDF:
  • Dynamic Expansion: Expands function calls into actual SQL expressions during the query optimization phase
  • Type Safety: Supports explicit declaration of parameter types and return types
  • Parameterized: Supports named parameters to improve function readability
Applicable scenarios:
  • Complex SQL expressions that need to be reused
  • Simple data transformation and calculation logic
  • Standardized data processing rules

Syntax

Creating SQL UDF

Parameters

Dropping SQL UDF

Showing SQL UDF

Usage Examples

Example 1: String Processing Function

Example 2: Multi-parameter Calculation Function

Example 3: Complex Expression Encapsulation

Example 4: Conditional Logic Function

Example 5: Global SQL UDF

Advanced Features

1. Nested Usage

SQL UDF supports nested calls:

2. Type Conversion

SQL UDF supports implicit type conversion:

3. Combination with Built-in Functions

SQL UDF can be freely combined with the database built-in functions:

Viewing and Managing

Showing All Functions

Dropping Functions

Replacing Functions

Use the OR REPLACE keyword to replace an existing function:

Performance Characteristics

SQL UDF is expanded into actual SQL expressions during the query optimization phase, with the following performance characteristics:
  1. Zero Function Call Overhead: No function call overhead like traditional UDFs
  2. Optimizer Visibility: The optimizer can see the complete expression for better optimization
  3. Predicate Pushdown: Supports predicate pushdown and other optimization rules
For example, the following query:
Will be expanded by the optimizer as:

Limitations and Considerations

  1. Parameter Count Limitation: Parameter count should match variables used in expression
  2. Type Matching: Parameter types should match usage in expression