Skip to main content

User-Defined Function

User-Defined Functions (UDFs) in Databend allow you to create custom operations tailored to your specific data processing needs. This page highlights the commands you will use most often and helps you choose the right function type for your use case.

Function Management Commands

CommandDescription
CREATE SCALAR FUNCTIONScalar UDF (SQL/Python/JavaScript)
CREATE AGGREGATE FUNCTIONScript UDAF (JavaScript/Python runtimes)
CREATE TABLE FUNCTIONSQL-only table function returning result sets
SHOW USER FUNCTIONSLists all user-defined functions
ALTER FUNCTIONModifies existing functions
DROP FUNCTIONRemoves functions

Function Type Comparison

FeatureScalar (SQL)Scalar (Python/JavaScript)Aggregate (Script)Tabular SQL
Return TypeSingle valueSingle valueSingle valueTable/ResultSet
LanguageSQL expressionsPython/JavaScriptJavaScript/Python runtimesSQL queries
Performance⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
Enterprise RequiredNoPython runtime onlyPython runtime onlyNo
Package SupportNoPython: Yes (PACKAGES)Python: Yes (PACKAGES)No
Best ForMath calculations
String operations
Data formatting
Advanced algorithms
External libraries
Control flow logic
Custom aggregations that need scripting logicComplex queries
Multi-row results
Data transformations

Unified Syntax

All local UDF types use consistent $$ syntax:

-- Scalar Function
CREATE FUNCTION func_name(param TYPE) RETURNS TYPE AS $$ expression $$;

-- Tabular Function
CREATE FUNCTION func_name(param TYPE) RETURNS TABLE(...) AS $$ query $$;

-- Scalar Function (Python/JavaScript)
CREATE FUNCTION func_name(param TYPE) RETURNS TYPE
LANGUAGE python
HANDLER = 'handler' AS $$ code $$;
Try Databend Cloud for FREE

Multimodal, object-storage-native warehouse for BI, vectors, search, and geo.

Snowflake-compatible SQL with automatic scaling.

Sign up and get $200 in credits.

Try it today