Curious Minds

Clear answers to science and history questions

Snowflake Functions List by Return Value

· 6 min read · Science

Snowflake functions are not officially grouped by return value in the documentation, but they can be logically categorized by the data type they return: scalar functions return a single value of a specific type (e.g., numeric, string, boolean, date/time, variant), while aggregate and window functions return a single value computed over a set of rows, and table functions return a set of rows (a table). This categorization helps users choose the right function for a given context, especially when working with Snowflake’s semi-structured data types or when building complex queries.

Background: How Snowflake Organizes Functions

Snowflake’s function reference is organized primarily by category (aggregate, scalar, window, table, etc.) and then alphabetically. The return value is not a primary grouping in the official docs, but understanding it is useful because Snowflake enforces type compatibility in expressions and assignments. For example, a function that returns a VARIANT cannot be directly used in a numeric calculation without casting, and a table function must be used in the FROM clause. Knowing the return type helps avoid errors and write efficient SQL.

Snowflake supports a wide range of data types: numeric (NUMBER, INT, FLOAT), string (VARCHAR, CHAR), binary, boolean, date/time (DATE, TIME, TIMESTAMP), semi-structured (VARIANT, OBJECT, ARRAY), and geospatial (GEOGRAPHY, GEOMETRY). Each function’s return type is documented, and many functions are polymorphic—they return a type based on input arguments.

Scalar Functions by Return Type

Scalar functions return a single value per row. They are the most numerous and can be grouped by the type of value they return.

Numeric Return

  • Arithmetic and rounding: ABS, CEIL, FLOOR, ROUND, TRUNC, MOD, POWER, SQRT, EXP, LN, LOG, SIGN.
  • Trigonometric: SIN, COS, TAN, ASIN, ACOS, ATAN, ATAN2, DEGREES, RADIANS.
  • Statistical and hash: HASH (returns NUMBER), HASH_AGG (aggregate), WIDTH_BUCKET.
  • Conversion: TO_NUMBER, TO_DECIMAL, TO_DOUBLE, TO_NUMERIC.

String Return

  • Manipulation: CONCAT, SUBSTR, LEFT, RIGHT, TRIM, LTRIM, RTRIM, REPLACE, UPPER, LOWER, INITCAP, LPAD, RPAD, REPEAT, REVERSE, SPLIT_PART.
  • Search: POSITION, CHARINDEX, LIKE, REGEXP_SUBSTR, REGEXP_REPLACE.
  • Conversion: TO_CHAR, TO_VARCHAR, CAST (to string).
  • Encoding: BASE64_ENCODE, BASE64_DECODE (returns binary or string depending on variant).

Boolean Return

  • Comparison and logic: comparison operators (=, <, >, etc.) are not functions but return boolean; functions like IS_NULL, IS_NOT_NULL, EQUAL_NULL, BOOLAND, BOOLOR, BOOLNOT.
  • Pattern matching: REGEXP_LIKE, LIKE, CONTAINS, STARTSWITH, ENDSWITH.

Date/Time Return

  • Current time: CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, LOCALTIMESTAMP.
  • Construction: DATE_FROM_PARTS, TIME_FROM_PARTS, TIMESTAMP_FROM_PARTS.
  • Arithmetic: DATEADD, DATEDIFF, DATE_TRUNC, TIMEADD, TIMESTAMPADD, TIMESTAMPDIFF.
  • Extraction: DATE_PART, EXTRACT, YEAR, MONTH, DAY, HOUR, MINUTE, SECOND.

Variant (Semi-structured) Return

  • Parsing: PARSE_JSON, TRY_PARSE_JSON, TO_VARIANT.
  • Construction: OBJECT_CONSTRUCT, ARRAY_CONSTRUCT, ARRAY_APPEND, OBJECT_INSERT.
  • Access: GET, GET_PATH, ARRAY_SIZE, ARRAY_SLICE.
  • Conversion: TO_JSON, TO_XML, TO_OBJECT, TO_ARRAY.

Geospatial Return

  • Construction: ST_MAKEPOINT, ST_MAKELINE, ST_MAKEPOLYGON, ST_GEOGFROMTEXT, ST_GEOGFROMWKB.
  • Transformation: ST_BUFFER, ST_CENTROID, ST_INTERSECTION, ST_UNION, ST_DIFFERENCE.
  • Measurement: ST_AREA, ST_LENGTH, ST_DISTANCE, ST_X, ST_Y (return numeric).

Note: Some functions like TO_ARRAY return ARRAY, which is a variant type in Snowflake.

Aggregate Functions and Their Return Values

Aggregate functions operate on a set of rows and return a single value. Their return type often matches the input type or a numeric type for counts.

  • Numeric: COUNT (returns NUMBER), SUM, AVG, MIN, MAX, STDDEV, VARIANCE, MEDIAN, PERCENTILE_CONT, APPROX_PERCENTILE.
  • String: LISTAGG (returns VARCHAR), MIN/MAX on strings return string.
  • Boolean: BOOLAND_AGG, BOOLOR_AGG (return BOOLEAN).
  • Variant: ARRAY_AGG (returns ARRAY), OBJECT_AGG (returns OBJECT).
  • Approximate: APPROX_COUNT_DISTINCT (returns NUMBER), HLL (returns binary).

Aggregate functions can be used with GROUP BY, and their return type is determined by the function and input arguments.

Window Functions and Return Types

Window functions compute values across a set of rows related to the current row, returning a single value per row. They are similar to aggregate functions but do not collapse rows.

  • Ranking: ROW_NUMBER, RANK, DENSE_RANK, NTILE (return NUMBER).
  • Value: LAG, LEAD, FIRST_VALUE, LAST_VALUE, NTH_VALUE (return same type as input).
  • Aggregate window: SUM, AVG, COUNT, MIN, MAX used with OVER clause (return same as aggregate).

Window functions can return any data type supported by the underlying aggregate or value function.

Table Functions: Returning Sets of Rows

Table functions return a table (a set of rows) and are used in the FROM clause, often with the TABLE() keyword or directly. They are essential for flattening semi-structured data and for generating rows.

  • FLATTEN: Returns a table with columns SEQ, KEY, PATH, INDEX, VALUE, THIS. The VALUE column is VARIANT.
  • GENERATOR: Returns a table with rows generated based on parameters (e.g., ROWCOUNT, TIMELIMIT). Columns include SEQ and others depending on options.
  • RESULT_SCAN: Returns the result set of a previous query. Column types depend on the original query.
  • SPLIT_TO_TABLE: Returns a table with SEQ, INDEX, VALUE (VARCHAR).
  • ST_* table functions: Some geospatial functions like ST_GEOGFROMTEXT return a GEOGRAPHY scalar, but table functions like ST_INTERSECTS are scalar; table functions for geospatial include ST_MAKEPOINT? Actually, most geospatial functions are scalar. Table functions in geospatial are limited; e.g., ST_GEOGFROMWKB is scalar. So be cautious: not all geospatial functions are table functions.

Table functions are distinct because they return multiple rows and columns, and their output schema is fixed or depends on input.

How to Choose the Right Function by Return Type

When writing SQL, the return type guides how you can use the function’s result. For example, if you need a numeric value for aggregation, choose a function that returns a number. If you need to manipulate JSON, use functions that return VARIANT. Here are practical steps:

  1. Identify the desired output type for your expression (e.g., string for concatenation, number for calculation).
  2. Check the function’s documented return type in Snowflake’s function reference.
  3. If the function returns VARIANT, use casting or extraction functions (e.g., VALUE::STRING) to convert to the needed type.
  4. For table functions, ensure you use them in the FROM clause with appropriate aliasing.
  5. Test with a small query using SELECT (...) to verify the return type and behavior.

A common misconception is that all functions returning VARIANT are interchangeable with scalar types; in reality, VARIANT requires explicit casting for most operations.

  • Implicit conversion failures: Using a VARIANT in a numeric context without casting can cause errors. For example, SELECT ARRAY_CONSTRUCT(1,2)[0] + 1 fails because the array element is VARIANT; you need to cast: SELECT ARRAY_CONSTRUCT(1,2)[0]::NUMBER + 1.
  • Table function misuse: Calling a table function in the SELECT list instead of FROM clause results in an error.
  • Aggregate vs. window confusion: Using an aggregate function without GROUP BY when a window function is intended can lead to unexpected results.
  • Date/time precision: Functions like CURRENT_TIMESTAMP return TIMESTAMP_LTZ, which may behave differently across time zones; use TO_TIMESTAMP with explicit time zone if needed.

To troubleshoot, use DESCRIBE RESULT or check the query profile to see inferred types.

Frequently Asked Questions

What is the return type of the FLATTEN function?

FLATTEN returns a table with several columns: SEQ (NUMBER), KEY (VARCHAR), PATH (VARCHAR), INDEX (NUMBER), VALUE (VARIANT), THIS (VARIANT). The VALUE column contains the flattened element, which can be of any type stored in VARIANT.

Can I use a function that returns VARIANT in a WHERE clause?

Yes, but you typically need to cast or use variant extraction functions. For example, WHERE GET_PATH(data, 'key')::STRING = 'value' works, but direct comparison of VARIANT to a string may not behave as expected.

Do all aggregate functions return numeric values?

No. COUNT returns NUMBER, but MIN and MAX return the same type as the input column (e.g., string, date). LISTAGG returns VARCHAR, and ARRAY_AGG returns ARRAY.

What is the difference between a scalar and a table function?

A scalar function returns a single value per row and is used in SELECT, WHERE, etc. A table function returns a set of rows and is used in the FROM clause, often to flatten semi-structured data or generate rows.

How can I find the return type of a function?

Snowflake’s documentation lists the return type for each function. You can also run a simple query like SELECT TYPEOF((...)) to see the data type of the result, or use DESCRIBE RESULT after executing a query.

Are window functions considered scalar or aggregate?

Window functions are a separate category. They return a single value per row like scalar functions but operate on a window of rows like aggregates. Their return type depends on the specific function (e.g., ROW_NUMBER returns NUMBER, LAG returns the input type).

In this article
  1. Background: How Snowflake Organizes Functions
  2. Scalar Functions by Return Type
  3. Aggregate Functions and Their Return Values
  4. Window Functions and Return Types
  5. Table Functions: Returning Sets of Rows
  6. How to Choose the Right Function by Return Type
  7. Common Problems and Type-Related Errors
  8. Frequently Asked Questions