Back to Trino

CREATE FUNCTION

docs/src/main/sphinx/sql/create-function.md

4801.4 KB
Original Source

CREATE FUNCTION

Synopsis

text
CREATE [OR REPLACE] FUNCTION
  udf_definition

Description

Create or replace a . The udf_definition is composed of the usage of and nested statements. The name of the UDF must be fully qualified with catalog and schema location, unless the default UDF storage catalog and schema are configured. The connector used in the catalog must support UDF storage.

The optional OR REPLACE clause causes the UDF to be replaced if it already exists rather than raising an error.

Examples

The following example creates the meaning_of_life UDF in the default schema of the example catalog:

sql
CREATE FUNCTION example.default.meaning_of_life()
  RETURNS bigint
  BEGIN
    RETURN 42;
  END;

If the default catalog and schema for UDF storage is configured, you can use the following more compact syntax:

sql
CREATE FUNCTION meaning_of_life() RETURNS bigint RETURN 42;

Further examples of varying complexity that cover usage of the FUNCTION statement in combination with other statements are available in the SQL UDF examples documentation.

See also