CREATE FUNCTION (SQL Functions)

Lets you store SQL expressions as functions in Vertica for use in queries. These functions are useful for executing complex queries or combining Vertica built-in functions. You simply call the function name you assigned.

Note: This topic describes how to use CREATE FUNCTION to create a SQL function. If you want to create a user-defined scalar function (UDSF), see CREATE FUNCTION (UDF).

In addition, if you want to see how to create a user-defined transform function (UDTF), see CREATE TRANSFORM FUNCTION.

Syntax

CREATE [ OR REPLACE ] FUNCTION
... [schema.]function-name ( [ argname argtype  [, ...] ] )
... RETURN rettype
... AS 
... BEGIN
...... RETURN expression;
... END;

Parameters

schema

Specifies a schema. If multiple schemas are defined in the database, include the schema name. For example:

myschema.thisDbObject
function‑name

Specifies a name for the SQL function to create, where function-name conforms to conventions described in Identifiers. When using more than one schema, specify the schema that contains the function, as noted above.

argname

Specifies the name of the argument.

argtype

Specifies the data type for argument that is passed to the function. Argument types must match Vertica type names. See SQL Data Types.

rettype

Specifies the data type to be returned by the function.

RETURN expression;

Specifies the SQL function (function body), which must be in the form of ‘RETURN expression.’ expression can contain built-in functions, operators, and argument names specified in the CREATE FUNCTION statement.

A semicolon at the end of the expression is required.

Note: Only one RETURN expression is allowed in the CREATE FUNCTION definition. FROM, WHERE, GROUP BY, ORDER BY, LIMIT, aggregation, analytics, and meta function are not allowed.

Privileges

See GRANT (User Defined Extension) and REVOKE (User Defined Extension).

Notes

Example

This following statement creates a SQL function called myzeroifnull that accepts an INTEGER argument and returns an INTEGER result.

=> CREATE FUNCTION myzeroifnull(x INT) RETURN INT
   AS BEGIN 
     RETURN (CASE WHEN (x IS NOT NULL) THEN x ELSE 0 END); 
   END;

You can use the new SQL function (myzeroifnull) anywhere you use an ordinary SQL expression. For example, create a simple table:

=> CREATE TABLE tabwnulls(col1 INT);
=> INSERT INTO tabwnulls VALUES(1);
=> INSERT INTO tabwnulls VALUES(NULL);
=> INSERT INTO tabwnulls VALUES(0);
=> SELECT * FROM tabwnulls;
 a
---
 1
 0
(3 rows)

Use the myzeroifnull function in a SELECT statement, where the function calls col1 from table tabwnulls:

=> SELECT myzeroifnull(col1) FROM tabwnulls;
 myzeroifnull 
--------------
          1
          0
          0
(3 rows)

Use the myzeroifnull function in the GROUP BY clause:

=> SELECT COUNT(*) FROM tabwnulls GROUP BY myzeroifnull(col1);
 count
-------
     2
     1
(2 rows)

If you want to change a SQL function's body, use the CREATE OR REPLACE syntax. The following command modifies the CASE expression:

=> CREATE OR REPLACE FUNCTION zerowhennull(x INT) RETURN INT    AS BEGIN 
     RETURN (CASE WHEN (x IS NULL) THEN 0 ELSE x END); 
   END;

To see how this information is stored in the Vertica catalog, see Viewing Information About SQL Functions in Extending Vertica.