Scalar Python UDFs - Amazon Redshift
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Scalar Python UDFs

A scalar Python UDF incorporates a Python program that runs when the function is called and returns a single value. The CREATE FUNCTION command defines the following parameters:

  • (Optional) Input arguments. Each argument must have a name and a data type.

  • One return data type.

  • One executable Python program.

The input and return data types can be SMALLINT, INTEGER, BIGINT, DECIMAL, REAL, DOUBLE PRECISION, BOOLEAN, CHAR, VARCHAR, DATE, or TIMESTAMP. In addition, Python UDFs can use the data type ANYELEMENT, which Amazon Redshift automatically converts to a standard data type based on the arguments supplied at runtime. For more information, see ANYELEMENT data type

When an Amazon Redshift query calls a scalar UDF, the following steps occur at runtime:

  1. The function converts the input arguments to Python data types.

    For a mapping of Amazon Redshift data types to Python data types, see Python UDF data types.

  2. The function runs the Python program, passing the converted input arguments.

  3. The Python code returns a single value. The data type of the return value must correspond to the RETURNS data type specified by the function definition.

  4. The function converts the Python return value to the specified Amazon Redshift data type, then returns that value to the query.

Note

Python 3 isn’t available for Python UDFs. To get Python 3 support for Amazon Redshift UDFs, use Scalar Lambda UDFs instead.