View a markdown version of this page

Conditional and error functions - Amazon Athena

Conditional and error functions

Use these functions to choose a value based on a condition or a NULL test, or to fail a query when the data breaks a rule. For more information about CASE and COALESCE expressions, see Expressions and operators in DuckDB. For a simple CASE that looks up a value in a constant map, use the switch function.

assert_true function

Checks condition. If condition is true, the function returns NULL and the query continues. If condition is false or NULL, the query fails. When you pass message, the error message includes its text. Without message, the error message is a generic assertion message.

Use assert_true to stop a query when the data breaks a rule that you expect to hold, for example that every amount is positive. The function checks the condition for each row that it runs on.

Syntax

assert_true(condition [, message ])

Arguments

The assert_true function takes the following arguments:

condition

The condition to check, as a BOOLEAN expression. A value that can be cast to BOOLEAN, such as the INTEGER value 1, also works.

message

The text of the error message, as a VARCHAR value. The function includes it in the error when condition is not true. This argument is optional.

Return type

Always NULL. The data type of the result is "NULL".

Examples

The following SQL query checks a condition that is true, so the function returns NULL.

SELECT assert_true(1 < 2) IS NULL AS passed

The query returns the following result.

passed

true

The following SQL query checks every row and passes a message for the error. Every row meets the condition, so the function returns NULL for each row. If an amount is 0 or less, the query fails, and the error message includes amount must be positive.

SELECT order_id, assert_true( amount > 0, 'amount must be positive' ) AS check_result FROM (VALUES (1, 120.5), (2, 75.0) ) AS t(order_id, amount) ORDER BY order_id

The query returns the following results.

order_id check_result

1

NULL

2

NULL

The following SQL query uses the check in a WHERE clause, so the query returns a total only when every amount is positive.

SELECT sum(amount) AS total FROM (VALUES (120.5), (75.0)) AS t(amount) WHERE assert_true( amount > 0, 'amount must be positive' ) IS NULL

The query returns the following result.

total

195.5

Considerations

A NULL condition makes the query fail, the same as a false condition. To let NULL values pass, include them in the condition, for example assert_true(amount IS NULL OR amount > 0).

constant_or_null function

Returns value unless one of the arguments after it is NULL. If any of them is NULL, the function returns NULL. The function checks the later arguments only for NULL; their values do not otherwise change the result.

Use constant_or_null to return a fixed value only for the rows where other columns have values.

Syntax

constant_or_null(value, check [, ...])

Arguments

The constant_or_null function takes the following arguments:

value

The value to return, of any data type.

check

One or more values to check, of any data type. If any of them is NULL, the result is NULL.

Return type

The data type of value, for example INTEGER when value is 42. If a later argument is the untyped literal NULL, the result has the data type "NULL".

Examples

The following SQL query returns the first argument, because the second argument is not NULL.

SELECT constant_or_null(42, 1) AS result

The query returns the following result.

result

42

The following SQL query returns NULL, because the second argument is NULL.

SELECT constant_or_null(42, NULL) AS result

The query returns the following result.

result

NULL

The following SQL query returns 100 only for the rows where both a and b have values.

SELECT id, constant_or_null(100, a, b) AS result FROM (VALUES (1, 1, 2), (2, 1, NULL), (3, NULL, 2) ) AS t(id, a, b) ORDER BY id

The query returns the following results.

id result

1

100

2

NULL

3

NULL

Considerations

If you pass the untyped literal NULL as a later argument, the result is always NULL, and its data type is "NULL" instead of the data type of value. To keep the data type, pass a typed value, such as CAST(NULL AS INTEGER), or a column.

error function

Fails the query with an error whose message is message. Use it in a conditional expression, such as CASE, if, or coalesce, to fail a query when the data breaks a rule that you expect it to follow. The function runs only for the rows that reach it, so rows that take another branch do not fail the query.

If message is NULL, the function returns NULL and the query does not fail.

Syntax

error(message)

Arguments

The error function takes the following argument:

message

The error message, as a VARCHAR value.

Return type

The function returns no value when it fails the query, and returns NULL when message is NULL. Its declared type is the NULL type. For this reason, in a CASE expression or in coalesce, the result has the type of the other branches, such as INTEGER when the other branches return INTEGER values.

Examples

The following SQL query returns x when it is positive and otherwise fails the query with the message x must be positive. Every value is positive, so the query returns both rows.

SELECT CASE WHEN x > 0 THEN x ELSE error( 'x must be positive' ) END AS result FROM (VALUES (1), (2)) AS v(x) ORDER BY result

The query returns the following results.

result

1

2

The following SQL query uses coalesce to fail the query when x is NULL. No value is NULL, so the query returns both rows.

SELECT coalesce(x, error('x is missing')) AS result FROM (VALUES (1), (2)) AS v(x) ORDER BY result

The query returns the following results.

result

1

2

The following SQL query passes a NULL message. The function returns NULL, and the query does not fail.

SELECT error(NULL) IS NULL AS result

The query returns the following result.

result

true

Considerations

The message that you pass becomes the error message of the failed query.

You cannot wrap error in try to keep the query from failing. The engine does not accept try around a volatile function such as error, so the query fails.

if function

Returns true_value if condition is true, and false_value otherwise. If condition is NULL, the function returns false_value, because a NULL condition is not true.

if(condition, true_value, false_value) returns the same result as CASE WHEN condition THEN true_value ELSE false_value END. To test more than one condition, use a CASE expression.

Syntax

if(condition, true_value, false_value)

Arguments

The if function takes the following arguments:

condition

The condition to test, as a BOOLEAN expression. A number is converted to BOOLEAN, so 0 is false and any other number is true.

true_value

The value to return when condition is true.

false_value

The value to return when condition is false or NULL. To return NULL in that case, pass NULL.

Return type

The common type of true_value and false_value. For example, if one is an INTEGER and the other is a DECIMAL, the result is a DECIMAL.

Examples

The following SQL query returns yes because the condition is true.

SELECT if(2 > 1, 'yes', 'no') AS result

The query returns the following result.

result

yes

The following SQL query labels each amount that is greater than 100 as large and every other amount as small.

SELECT x AS amount, if(x > 100, 'large', 'small') AS size_label FROM (VALUES (120.5), (75.0)) AS v(x) ORDER BY x

The query returns the following results.

amount size_label

75.0

small

120.5

large

The following SQL query passes a NULL condition, so the function returns the third argument.

SELECT if(NULL, 'yes', 'no') AS result

The query returns the following result.

result

no

The following SQL query passes NULL as the third argument, so a false condition returns NULL.

SELECT if(1 > 2, 'yes', NULL) AS result

The query returns the following result.

result

NULL

Considerations

if always takes three arguments. A call with only a condition and one value fails.

Differences from Athena engine version 3

In Athena engine version 3, you can call if with two arguments, and it returns NULL when the condition is false. In DuckDB, if takes exactly three arguments, so a two-argument call fails. Pass NULL as the third argument, as in if(condition, true_value, NULL).

nullif function

Compares value1 with value2. If they are equal, the function returns NULL. Otherwise, it returns value1. If value2 is NULL, the values do not count as equal, so the function returns value1. If value1 is NULL, the result is NULL.

nullif(value1, value2) returns the same result as CASE WHEN value1 = value2 THEN NULL ELSE value1 END. Use it to turn a placeholder value, such as 0 or an empty string, into NULL, for example so that a division by zero returns NULL.

Syntax

nullif(value1, value2)

Arguments

The nullif function takes the following arguments:

value1

The value to return when the two values are not equal.

value2

The value to compare with value1. The engine converts both values to a common type before it compares them, so nullif(1, '1') returns NULL.

Return type

The type of value1.

Examples

The following SQL query compares two equal values, so the result is NULL.

SELECT nullif(1, 1) AS result

The query returns the following result.

result

NULL

The following SQL query turns the value 0 into NULL and keeps every other value.

SELECT x AS input, nullif(x, 0) AS result FROM (VALUES (0), (3)) AS v(x) ORDER BY x

The query returns the following results.

input result

0

NULL

3

3

The following SQL query divides by a value that is zero. Dividing by zero returns infinity, so the query uses nullif to return NULL instead.

SELECT 10 / nullif(0, 0) AS result

The query returns the following result.

result

NULL

The following SQL query passes NULL as the second value. The values do not count as equal, so the function returns the first value.

SELECT nullif(1, NULL) AS result

The query returns the following result.

result

1

Considerations

If value2 is a string that cannot be converted to the type of value1, such as nullif(1, 'x'), the query fails.