View a markdown version of this page

Data type and expression functions - Amazon Athena

Data type and expression functions

Use these functions to find the data type of an expression, to build and compare data types, and to convert a value to the type of another value. Other functions on this page return the column name that the engine gives an expression, and the statistics that it has for the expression when it plans the query.

For a description of the sample tables that the examples on this page use, see SELECT statement in DuckDB.

alias function

Returns the name of expression as a VARCHAR value. This is the name that the engine gives a result column for that expression when the column has no alias. For a column reference, the name is the column name, such as amount. For any other expression, the name is the text of the expression as the engine writes it, such as (42 + 1).

The function reads only the expression, not its value, so it returns the same name for every row, even when the value is NULL.

Syntax

alias(expression)

Arguments

The alias function takes the following argument:

expression

The expression whose name you want. It can be of any data type. The function uses only the expression itself, not its value.

Return type

VARCHAR

Examples

The following SQL query returns the name of an arithmetic expression. The call is inside concat, so the column alias result does not replace the name.

SELECT concat(alias(42 + 1)) AS result

The query returns the following result.

result

(42 + 1)

The following SQL query returns the name of a column and the name of an expression that uses the column.

SELECT concat(alias(amount)) AS column_name, concat(alias(amount * 2)) AS expression_name FROM (VALUES (10)) AS t(amount)

The query returns the following result.

column_name expression_name

amount

(amount * 2)

The following SQL query shows that when the call is the whole output column, the function returns the column alias, not the name of the expression.

SELECT alias(42 + 1) AS result

The query returns the following result.

result

result

Considerations

When the call to alias is a whole output column that you name with AS, the function returns that column alias instead of the name of expression. For example, SELECT alias(42 + 1) AS result returns result. To get the name of the expression, use the call inside another expression, such as concat(alias(42 + 1)).

can_cast_implicitly function

Returns true if the engine can convert a value of the data type of source_type to the data type of target_type implicitly, that is, without a CAST. Otherwise, it returns false. Implicit casts are the conversions that the engine makes on its own, for example when a function expects a BIGINT and you pass an INTEGER.

Only the data types of the arguments matter, not their values, so the usual way to call the function is with typed NULL values, such as CAST(NULL AS INTEGER). A NULL argument does not make the result NULL.

Syntax

can_cast_implicitly(source_type, target_type)

Arguments

The can_cast_implicitly function takes the following arguments:

source_type

A value whose data type is the type to convert from. The value can be of any data type, and the function uses only its data type.

target_type

A value whose data type is the type to convert to. The value can be of any data type, and the function uses only its data type.

Return type

BOOLEAN

Examples

The following SQL query checks whether the engine converts an INTEGER value to BIGINT implicitly. The :: operator is shorthand for CAST.

SELECT can_cast_implicitly( NULL::INTEGER, NULL::BIGINT ) AS result

The query returns the following result.

result

true

The following SQL query checks a conversion from BIGINT to the smaller INTEGER type and a conversion from a string to a number. The engine makes neither conversion implicitly.

SELECT can_cast_implicitly( CAST(NULL AS BIGINT), CAST(NULL AS INTEGER) ) AS bigint_to_integer, can_cast_implicitly( CAST(NULL AS VARCHAR), CAST(NULL AS INTEGER) ) AS varchar_to_integer

The query returns the following result.

bigint_to_integer varchar_to_integer

false

false

The following SQL query passes two columns. The function uses their data types, INTEGER and DOUBLE, and ignores their values.

SELECT can_cast_implicitly(order_id, amount) AS result FROM (VALUES (1, CAST(120.5 AS DOUBLE)) ) AS t(order_id, amount)

The query returns the following result.

result

true

Considerations

A false result does not mean that an explicit cast fails. For example, the engine does not convert a VARCHAR value to INTEGER implicitly, but CAST('42' AS INTEGER) works.

cast_to_type function

Returns value converted to the same data type as type. The function ignores the value of type, so you can pass a typed NULL, such as CAST(NULL AS INTEGER), or a column whose data type you want to match.

The conversion follows the same rules as CAST. If value is NULL, the result is NULL, with the data type of type.

Syntax

cast_to_type(value, type)

Arguments

The cast_to_type function takes the following arguments:

value

The value to convert, of any data type.

type

A value that gives the data type of the result. It can be of any data type. The function does not use its value.

Return type

The data type of type, for example INTEGER when type is NULL::INTEGER, or DECIMAL(10,2) when type is a DECIMAL(10,2) column.

Examples

The following SQL query converts the string '42' to INTEGER, the data type of NULL::INTEGER. The :: operator is shorthand for CAST.

SELECT cast_to_type('42', NULL::INTEGER) AS result

The query returns the following result.

result

42

The following SQL query converts a string to the data type of a column, DECIMAL(10,2).

SELECT cast_to_type('3.75', amount) AS result FROM (VALUES (CAST(1 AS DECIMAL(10,2)))) AS t(amount)

The query returns the following result.

result

3.75

The following SQL query converts a NULL value. The result is NULL, with the data type DATE.

SELECT cast_to_type( NULL, CAST(NULL AS DATE) ) IS NULL AS is_null, typeof( cast_to_type(NULL, CAST(NULL AS DATE)) ) AS result_type

The query returns the following result.

is_null result_type

true

DATE

Considerations

If value cannot be converted to the data type of type, the query fails, as with CAST. To get NULL instead, use TRY_CAST with the target data type written out, for example TRY_CAST('abc' AS INTEGER).

get_type function

Returns the data type of expression as a value of the TYPE type. The function looks at the type of the expression, not at its value, so a NULL in an INTEGER column has the type INTEGER.

A query result cannot hold a TYPE value. Cast the result to VARCHAR to see the type name, or compare it with another TYPE value, such as one that make_type returns. To get the type name as a VARCHAR in one step, use the typeof function.

Syntax

get_type(expression)

Arguments

The get_type function takes the following argument:

expression

The expression whose data type you want to describe. It can be of any type.

Return type

TYPE. Cast it to VARCHAR to return it in a query result.

Examples

The following SQL query returns the type of a string literal, cast to VARCHAR.

SELECT CAST(get_type('abc') AS VARCHAR) AS result

The query returns the following result.

result

VARCHAR

The following SQL query returns the types of an integer, a decimal number, a list, and a struct.

SELECT CAST(get_type(42) AS VARCHAR) AS int_type, CAST(get_type(42.5) AS VARCHAR) AS decimal_type, CAST(get_type([1, 2]) AS VARCHAR) AS list_type, CAST( get_type({'a': 1, 'b': 'x'}) AS VARCHAR ) AS struct_type

The query returns the following result.

int_type decimal_type list_type struct_type

INTEGER

DECIMAL(3,1)

INTEGER[]

STRUCT(a INTEGER, b VARCHAR)

The following SQL query compares types without casting them. The literal 42 is an INTEGER, so its type equals make_type('INTEGER') but not the type of a BIGINT value.

SELECT get_type(42) = make_type('INTEGER') AS is_integer, get_type(42) = get_type( CAST(43 AS BIGINT) ) AS same_as_bigint

The query returns the following result.

is_integer same_as_bigint

true

false

The following SQL query shows that a NULL in an INTEGER column has the type INTEGER.

SELECT x IS NULL AS is_null, CAST(get_type(x) AS VARCHAR) AS result FROM (VALUES (1), (NULL)) AS v(x) ORDER BY is_null

The query returns the following results.

is_null result

false

INTEGER

true

INTEGER

Considerations

A query that returns the result of get_type without a cast fails, because a query result cannot hold a TYPE value. Use CAST(get_type(expression) AS VARCHAR).

make_type function

Returns the data type that name and the parameters describe, as a value of type TYPE. For example, make_type('DECIMAL', 10, 2) describes DECIMAL(10,2), and make_type('LIST', make_type('INTEGER')) describes INTEGER[]. The type name is not case-sensitive.

To see the type, cast the result to VARCHAR. To test whether an expression has the type, compare the result with the result of get_type.

Syntax

make_type(name [, parameter, ...]) make_type('STRUCT', field_name := field_type [, ...])

Arguments

The make_type function takes the following arguments:

name

The name of the type to build, as a VARCHAR value, such as 'INTEGER', 'DECIMAL', 'LIST', 'MAP', or 'STRUCT'.

parameter

A parameter of the type. For DECIMAL, pass the precision and then the scale, as integers. For LIST, pass the element type. For MAP, pass the key type and then the value type. Pass each nested type as another make_type call. This argument is optional.

field_name

For STRUCT, the name of a field, followed by := and the field type, as in a := make_type('INTEGER'). Write the name without single quotation marks.

field_type

For STRUCT, the type of the field, as a make_type call.

Return type

TYPE

Examples

The following SQL query builds a DECIMAL type with a precision of 10 and a scale of 2, and casts it to VARCHAR to show it.

SELECT CAST( make_type('DECIMAL', 10, 2) AS VARCHAR ) AS result

The query returns the following result.

result

DECIMAL(10,2)

The following SQL query builds a list type whose elements are integers.

SELECT CAST( make_type( 'LIST', make_type('INTEGER') ) AS VARCHAR ) AS result

The query returns the following result.

result

INTEGER[]

The following SQL query builds a STRUCT type with two fields.

SELECT CAST( make_type( 'STRUCT', a := make_type('INTEGER'), b := make_type('VARCHAR') ) AS VARCHAR ) AS result

The query returns the following result.

result

STRUCT(a INTEGER, b VARCHAR)

The following SQL query tests whether a value has the type DECIMAL(10,2).

SELECT get_type( CAST(1.5 AS DECIMAL(10, 2)) ) = make_type('DECIMAL', 10, 2) AS is_match

The query returns the following result.

is_match

true

Considerations

A query cannot return a TYPE value as a result column. Cast the result to VARCHAR, as in CAST(make_type('DECIMAL', 10, 2) AS VARCHAR), or the query fails.

You cannot use a make_type call where SQL expects a type name, for example as the target type of CAST.

If name is not a type that DuckDB recognizes, or a parameter is out of range, such as a DECIMAL precision greater than 38, the query fails. A parameter that the type does not take also makes the query fail, as in make_type('INTEGER', 5). VARCHAR accepts a length but ignores it, so make_type('VARCHAR', 10) returns VARCHAR.

replace_type function

Returns value with every part whose type is the type of from_type cast to the type of to_type. The function looks inside STRUCT fields, LIST elements, and MAP keys and values, at any depth, so one call changes a type throughout a nested value. Parts of other types do not change. If value has no part of type from_type, the function returns value unchanged.

Pass the two types as typed NULL values, such as NULL::DOUBLE. The function uses only the types of these arguments, not their values.

Syntax

replace_type(value, from_type, to_type)

Arguments

The replace_type function takes the following arguments:

value

The value to convert, of any type. It is usually a STRUCT, LIST, or MAP.

from_type

A value of the type to replace, usually a typed NULL such as NULL::DOUBLE.

to_type

A value of the new type, usually a typed NULL such as NULL::DECIMAL(15,2).

Return type

The type of value, with each use of the type of from_type replaced by the type of to_type. For example, STRUCT(duck DOUBLE) becomes STRUCT(duck DECIMAL(15,2)).

Examples

The following SQL query changes the DOUBLE field of a struct to DECIMAL(15,2), which rounds the value to two decimal places.

SELECT replace_type( {duck: 3.141592653589793::DOUBLE}, NULL::DOUBLE, NULL::DECIMAL(15,2) ) AS result

The query returns the following result.

result

{'duck': 3.14}

The following SQL query replaces INTEGER with BIGINT in a struct that has a list field, and returns the type of the result. The list elements change too, and the VARCHAR field does not.

SELECT typeof( replace_type( {'a': 1, 'b': [2, 3], 'c': 'x'}, NULL::INTEGER, NULL::BIGINT ) ) AS result_type

The query returns the following result.

result_type

STRUCT(a BIGINT, b BIGINT[], c VARCHAR)

The following SQL query asks to replace DOUBLE in a struct that has no DOUBLE field, so the type does not change.

SELECT typeof( replace_type( {'a': 1}, NULL::DOUBLE, NULL::VARCHAR ) ) AS result_type

The query returns the following result.

result_type

STRUCT(a INTEGER)

Considerations

Only parts whose type matches from_type exactly change. For a DECIMAL, the precision and scale must match too, so NULL::DECIMAL(15,2) does not replace a DECIMAL(10,2) field.

Each part is converted as with CAST. A DOUBLE cast to DECIMAL(15,2) or INTEGER is rounded, and a part that cannot be converted, such as the string 'x' cast to INTEGER, makes the query fail.

stats function

Returns the statistics that DuckDB has for expression when it plans the query, as a VARIANT value that holds a struct. Depending on the type of expression, the fields can include the smallest and largest values (min and max) and whether the expression can be NULL (has_null). They can also include whether it can be a value other than NULL (has_no_null) and an estimate of the number of distinct values (approx_unique). For strings, they can include the shortest and longest lengths (min_string_length and max_string_length).

The statistics are what the engine knows before it reads the data, not values computed from the data. For a constant, they describe that one value. For a column, they can be incomplete, so has_null can be true even when no row is NULL. Use stats to see how the engine reads an expression, for example when you troubleshoot a query plan.

Syntax

stats(expression)

Arguments

The stats function takes the following argument:

expression

The expression whose statistics you want, such as a column, a constant, or a larger expression. It can be of any type.

Return type

VARIANT

Examples

The following SQL query returns the statistics of an integer constant.

SELECT CAST(stats(5) AS VARCHAR) AS result

The query returns the following result.

result

{'approx_unique': 1, 'has_no_null': true, 'has_null': false, 'max': 5, 'min': 5}

The following SQL query returns the statistics of a string constant, which include its length.

SELECT CAST(stats('abc') AS VARCHAR) AS result

The query returns the following result.

result

{'approx_unique': 1, 'has_no_null': true, 'has_null': false, 'has_unicode': false, 'max': abc, 'max_string_length': 3, 'min': abc, 'min_string_length': 3}

The following SQL query returns the statistics of a column. The engine does not know the smallest or largest value before it reads the rows, and it reports that the column can be NULL, although no row is.

SELECT DISTINCT CAST(stats(x) AS VARCHAR) AS result FROM (VALUES (1), (5)) AS v(x)

The query returns the following result.

result

{'has_no_null': true, 'has_null': true}

Considerations

To return the result of stats from a query, cast it to VARCHAR, as in CAST(stats(5) AS VARCHAR). Without the cast, the query fails.

typeof function

Identifies the data type of expression and returns the type name as a VARCHAR value, such as INTEGER, DECIMAL(2,1), or STRUCT(a INTEGER, b VARCHAR). The engine works out the type when it plans the query, so the result does not depend on the value and is the same for every row.

For a NULL of a known type, such as CAST(NULL AS INTEGER) or a NULL in a column, the result is that type. For a NULL literal that has no type, the result is "NULL", with the double quotation marks.

Use typeof to check how the engine reads a literal or an expression. For example, a literal with a decimal point, such as 1.5, is a DECIMAL, not a DOUBLE.

Syntax

typeof(expression)

Arguments

The typeof function takes the following argument:

expression

The expression whose data type you want. It can be of any type.

Return type

VARCHAR

Examples

The following SQL query returns the type of a string literal.

SELECT typeof('abc') AS result

The query returns the following result.

result

VARCHAR

The following SQL query returns the types of four numeric literals. A whole number that is too large for INTEGER is a BIGINT, a literal with a decimal point is a DECIMAL, and a literal with an exponent is a DOUBLE.

SELECT typeof(1) AS integer_literal, typeof(2147483648) AS large_literal, typeof(1.5) AS decimal_literal, typeof(1.5e0) AS exponent_literal

The query returns the following result.

integer_literal large_literal decimal_literal exponent_literal

INTEGER

BIGINT

DECIMAL(2,1)

DOUBLE

The following SQL query compares a NULL literal that has no type with a NULL cast to INTEGER.

SELECT typeof(NULL) AS untyped_null, typeof(CAST(NULL AS INTEGER)) AS typed_null

The query returns the following result.

untyped_null typed_null

"NULL"

INTEGER

The following SQL query returns the types of two columns of a table.

SELECT DISTINCT typeof(amount) AS amount_type, typeof(order_date) AS order_date_type FROM sales_db.sales_parquet

The query returns the following result.

amount_type order_date_type

DECIMAL(10,2)

DATE

Differences from Athena engine version 3

In Athena engine version 3, typeof returns type names in lowercase, such as varchar and timestamp(3). In DuckDB, it returns uppercase names, such as VARCHAR and TIMESTAMP. If your queries compare the result of typeof with a type name, update the names.

In Athena engine version 3, a literal with a decimal point, such as 1.5, is a double, so typeof(1.5) returns double. In DuckDB, the literal is a DECIMAL, and typeof(1.5) returns DECIMAL(2,1). To get a DOUBLE, write the literal with an exponent, as in 1.5e0, or cast it.