ZetaSQL supports the following debugging functions.
ERROR(error_message)
Description
Returns an error. The error_message
argument is a STRING
.
ZetaSQL treats ERROR
in the same way as any expression that may
result in an error: there is no special guarantee of evaluation order.
Return Data Type
ZetaSQL infers the return type in context.
Examples
In the following example, the query returns an error message if the value of the row does not match one of two defined values.
SELECT
CASE
WHEN value = 'foo' THEN 'Value is foo.'
WHEN value = 'bar' THEN 'Value is bar.'
ELSE ERROR(concat('Found unexpected value: ', value))
END AS new_value
FROM (
SELECT 'foo' AS value UNION ALL
SELECT 'bar' AS value UNION ALL
SELECT 'baz' AS value);
Found unexpected value: baz
In the following example, ZetaSQL may evaluate the ERROR
function
before or after the x > 0
condition, because ZetaSQL
generally provides no ordering guarantees between WHERE
clause conditions and
there are no special guarantees for the ERROR
function.
SELECT *
FROM (SELECT -1 AS x)
WHERE x > 0 AND ERROR('Example error');
In the next example, the WHERE
clause evaluates an IF
condition, which
ensures that ZetaSQL only evaluates the ERROR
function if the
condition fails.
SELECT *
FROM (SELECT -1 AS x)
WHERE IF(x > 0, true, ERROR(FORMAT('Error: x must be positive but is %t', x)));'
Error: x must be positive but is -1