Expansion operators¶
Expansion operators expand a query expression that represents a list into the individual values in
the list. Currently, the spread operator (**) is the only expansion operator supported by Snowflake.
Spread¶
The spread operator expands an array into a list of individual values. This operator is useful for the following use cases:
- Queries containing IN clauses.
- Calls to system-defined functions that take a list of values as arguments, such as COALESCE, GREATEST, and LEAST.
- SQL user-defined functions that use an argument to provide an array of values.
- Snowflake Scripting stored procedures that use a bind variable to provide an array of values. For more information about using bind variables in Snowflake Scripting, see Using a variable in a SQL statement (binding) and Using an argument in a SQL statement (binding).
For more information about these use cases, see the Snowflake Introduces SQL Spread Operator (**) blog post.
Syntax¶
Limitations¶
-
The input must be an array of constant values, which can be an array of literal values or a bind variable that represents an array of literal values.
-
Each value in a semi-structured array is of type VARIANT. A VARIANT value can contain a value of any other data type. The spread operator supports the following data types for the value stored in the VARIANT value:
- Numeric (for example, INTEGER and NUMERIC)
- String & binary (for example, VARCHAR and BINARY)
- Logical (for example, BOOLEAN)
- Date & time (for example, DATE, TIME, and TIMESTAMP)
-
User-defined functions and stored procedures written in languages other than SQL can’t use the spread operator.
-
Expanding very large arrays with the spread operator might degrade performance.
Examples¶
Some of the examples use the data the following table:
Create a table and insert data:
The following examples use the spread operator.
- Expand an array of literal values in an IN clause
- Expand an array of literal values in a system-defined function call
- Use the spread operator with a bind variable in a SQL user-defined function
- Use the spread operator with a bind variable in a Snowflake Scripting stored procedure
Expand an array of literal values in an IN clause¶
Expand an array of numbers using the spread operator in a query on the spread_demo table
created previously:
Expand an array of strings using the spread operator:
Use an IN clause in a query with a mix of INTEGER values and expanded array values:
Expand an array of literal values in a system-defined function call¶
Expand an array of strings in a call to the COALESCE function:
Expand an array of numbers in a call to the GREATEST function:
Use the spread operator with a bind variable in a SQL user-defined function¶
Create a SQL user-defined function that uses the spread operator. The function takes an array as
an argument and then expands the array values to query the spread_demo table
created previously:
Query the table using the function:
Use the spread operator with a bind variable in a Snowflake Scripting stored procedure¶
Create a Snowflake Scripting stored procedure that uses the spread operator. The stored procedure takes
an array as an argument and then expands the array values in a bind variable to query the spread_demo
table created previously:
Call the stored procedure: