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 FUNCTIONSQL-only scalar function (no external language)
CREATE AGGREGATE FUNCTIONScript UDAF (JavaScript/Python runtimes)
CREATE EMBEDDED FUNCTIONEmbedded scalar functions (Python/JavaScript/WASM)
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 SQLAggregate (Script)Tabular SQLEmbedded
Return TypeSingle valueSingle valueTable/ResultSetSingle value
LanguageSQL expressionsJavaScript/Python runtimesSQL queriesPython/JavaScript/WASM
Performance⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
Enterprise RequiredNoPython runtime onlyNoPython runtime only
Package SupportNoPython: Yes (PACKAGES)NoPython: Yes (PACKAGES)
Best ForMath calculations
String operations
Data formatting
Custom aggregations that need scripting logicComplex queries
Multi-row results
Data transformations
Advanced algorithms
External libraries
Control flow logic

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 $$;

-- Embedded Function
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