ASSERT_TRUE Function
Description
The ASSERT_TRUE function validates whether a boolean expression is true. If the expression is true, it returns NULL; if the expression is false or NULL, it throws an exception. The optional message parameter allows a custom error message.
This function is commonly used for data quality checks, unit testing, and runtime assertion validation.
Parameters
expr: A boolean expression, required. The condition to validate.message: A string value, optional. A custom error message displayed when the assertion fails. If not specified orNULL, the default error message"ASSERT_TRUE expr is not true!"is used.
Returns
- When
expristrue: ReturnsNULL. - When
exprisfalseorNULL: Throws an exception with the error message.
Examples
-
Basic usage - assertion succeeds, returns NULL:
-
Using a custom error message - assertion succeeds:
-
Assertion fails - throws default error message:
-
Assertion fails - throws custom error message:
-
Input is NULL - treated as assertion failure:
Notes
- When
exprisNULL, it is treated as an assertion failure and throws an exception (rather than returning NULL). - When
messageisNULL, the default error message"ASSERT_TRUE expr is not true!"is used. - This function is compatible with Spark's
assert_truesemantics. - This function is not subject to constant folding optimization, ensuring that assertion checks are always executed at runtime.
