Skip to main content
Private Preview FeatureUser-defined function functionality is currently available as a private preview feature. To enable UDF functionality for your account, please contact Firebolt Support.
Creates a user-defined function (UDF) that extends Firebolt’s SQL capabilities with custom Python logic. UDFs allow you to encapsulate complex business logic, data transformations, or calculations that can be reused across queries. User-defined functions execute Python code within a secure, isolated environment. Each function invocation processes data row-by-row by default, or in batches when using vectorized execution with Pandas or PyArrow.

Syntax

You can also use dollar-quoted strings for the Python code to avoid escaping single quotes:

Parameters

Configuration options

Supported data types

The following SQL data types are supported for UDF parameters and return values: The following types are not supported in UDFs: GEOGRAPHY, STRUCT.

Python code requirements

The Python code must contain either a single function definition or a function named process. This function receives the SQL parameters as Python arguments and returns the result.
Key requirements:
  • Function naming: If your code contains multiple functions, the entry point must be named process. If there is only one function, it can have any name.
  • Parameter names: Python parameter names do not need to match SQL parameter names, but they are passed positionally in the order defined.
  • Return value: Return the computed value directly. Return None to represent SQL NULL.
  • NULL handling: NULL SQL values are passed as Python None. Your code should handle None values appropriately.

Examples

Basic arithmetic function

The following example creates a function that adds two integers:
Use the function in a query:
Returns: 8

Function with no parameters

Using external packages

The following example uses NumPy to compute the percentile of an array:
Use the function to find the median (50th percentile) of an array:
Returns: 12.9

Working with dates

Use the function to add days to a date:
Returns: 2026-02-02

Setting memory limit and description

Vectorized UDFs

For improved performance when processing large datasets, you can write vectorized UDFs that process data in batches using PyArrow or Pandas. Vectorized UDFs receive entire columns of data at once instead of individual values. Unlike scalar UDFs, the Python function in a vectorized UDF must always have a single argument annotated with type pandas.DataFrame or pyarrow.RecordBatch. This type annotation is what distinguishes vectorized UDFs from scalar UDFs.

Pandas vectorized UDF

A Pandas vectorized UDF receives a pandas.DataFrame and must return a pandas.Series: Column names and positions in the DataFrame match the parameter names and order from the SQL function declaration. You can access columns by name (e.g., df['param_name']) or by position (e.g., df.iloc[:, 0]), where position 0 corresponds to the first parameter, position 1 to the second, and so on.

PyArrow vectorized UDF

A PyArrow vectorized UDF receives a pyarrow.RecordBatch and must return a pyarrow.RecordBatch:
Vectorized UDFs automatically handle NULL values according to the library’s conventions.

Viewing user-defined functions

Query information_schema.routines to view all user-defined functions:

Limitations

  • Language: Only Python is supported. Other languages such as JavaScript or WebAssembly are not available.
  • Unsupported types: GEOGRAPHY and STRUCT types cannot be used as parameters or return types.
  • Schema restriction: User-defined functions can only be created in the public schema. Custom schemas are not supported.
  • Name conflicts: Function names cannot match existing built-in function names.
  • Overloading: Function overloading (multiple functions with the same name but different signatures) is not supported. Each function name must be unique.
  • Package availability: Only packages available on PyPI can be installed. Private or custom packages are not supported.
  • Execution context: UDFs run in isolated containers and cannot access external network resources, the file system, or other system resources.