The FAILIF function allows for the return of true or a custom error message based on the condition expression. This topic describes how to use the FAILIF function.
Syntax
BOOLEAN FAILIF(BOOLEAN <condition>, STRING <errMsg>); Parameters
Parameter | Required | Description |
condition | Yes | The expression to be evaluated, of |
errMsg | Yes | The error message to be thrown, of |
Return value
If the condition expression evaluates to true, the specified error message
errMsgis returned.If the condition expression evaluates to false, the function returns true.
Examples
Example 1: when the condition x<0 is true:
SELECT x, FAILIF(x<0,'Error: x must be positive') FROM (SELECT -1 AS x);The following result is returned, displaying the error message specified in the FAILIF function:
ODPS-0130071:[0,0] Semantic analysis exception - physical plan generation failed: SQL Runtime Unretryable Error: ODPS-0121095:Invalid argument - Error: x must be positiveExample 2: when the condition x<0 is false:
SELECT x, FAILIF(x<0,'Error: x must be positive') FROM (SELECT 1 AS x);The following result is returned:
+------------+------+ | x | _c1 | +------------+------+ | 1 | true | +------------+------+