Tabular SQL UDFs (UDTFs)¶
Snowflake supports SQL UDFs that return a set of rows, consisting of 0, 1, or multiple rows, each of which has 1 or more columns. Such UDFs are called tabular UDFs, table UDFs, or, most frequently, UDTFs (user-defined table functions).
A UDTF can be accessed in the FROM clause of a query.
Syntax¶
For a more detailed description of the general syntax for all UDFs, including SQL UDTFs, see CREATE FUNCTION.
Arguments¶
name:This should be valid database object name that follows the rules described at: Identifier requirements.
arguments:This must be an expression, for example a column name, a literal, or an expression that can be evaluated to a single value. Typically, a function takes one argument, which is a column name. You can pass more than one value, for example, more than one column name, or a column name and one or more literal values.
It is possible to pass a constant or no value at all. However, in most cases, if the input is the same every time, then the output is the same every time.
RETURNS TABLE(...)Specifies that the UDF should return a table. Inside the parentheses, specify name-and-type pairs for columns (as described below) to include in the returned table.
output_col_name:The name of an output column to include in the returned table. There must be at least one output column.
output_col_type:The data type of the output column.
sql_expression:A valid SQL expression or statement that returns a table with zero or more rows, each of which has one or more columns. The outputs must match the number and data types specified in the RETURNS clause.
Usage notes¶
The main body (aka “definition”) of a SQL UDTF must be a SELECT expression.
Although the delimiters around the
sql_expressionare typically single quotes, you can use a pair of dollar signs$$as the delimiter. The closing delimiter must match the opening delimiter. A pair of dollar signs is convenient when thesql_expressioncontains single quotes. An example using a pair of dollar signs is included in the Examples section below.If the delimiter is a single quote, and the body contains a single quote, you can escape the single quote in the body by using the backslash character
\as the escape character. An example is included in the Examples section below.The columns defined in the UDTF can appear anywhere that a normal table column can be used.
The return types specified in the RETURNS clause determine the names and types of the columns in the tabular results and must match the types of the expressions in the corresponding positions of the SELECT statement in the function body.
When calling a UDTF, you must include the UDTF name and arguments inside parentheses following the TABLE keyword. For more, see Calling a SQL UDTF.
Note
Tabular functions (UDTFs) have a limit of 500 input arguments and 500 output columns.
Calling a SQL UDTF¶
When calling a UDTF in the FROM clause of a query, specify the UDTF’s name and arguments inside the parentheses that follow the TABLE keyword.
In other words, use a form such as the following for the TABLE keyword when calling a UDTF:
Sample SQL UDTFs¶
Basic examples¶
This is an artificially simple example of a UDTF, which hard-codes the output. This also illustrates the
use of $$ as a delimiter:
This example is similar to the preceding example, but it uses single quotes as the delimiter, and uses the \
escape character to escape the single quotes in the body of the UDTF:
This is another basic example of a UDTF. It queries a table and returns two of the columns from that table:
This same functionality can also be implemented using a view.
Examples with joins¶
Create and use a SQL UDTF that returns country information (COUNTRY_CODE and COUNTRY_NAME) for a specified user ID:
Create a SQL UDTF that returns the favorite color for a specified year:
Use the UDTF in a query:
Use the UDTF in a join with another table; note that the join column from the table is passed as an argument to the function.
Use a WHERE clause, rather than ON, for additional predicates:
Use the UDTF with a constant in a join expression; note that a WHERE clause, rather than ON, must be used for additional join conditions: