Logical Functions
Overview
Logical functions perform logical operations on Boolean expressions and return results of the BOOLEAN type. AI DataLake supports three-valued logical operations (AND, OR, and NOT) in the SQL standard, IS TRUE/IS FALSE series Boolean judgment, and IF conditional functions, which are the basis for constructing complex query conditions.
For more information, see Logical Functions.
Application Scenarios
- Combining filter conditions: Uses AND/OR in the WHERE clause to combine multiple conditions.
- Condition negation: Uses NOT to negate a condition.
- Boolean value determination: Uses IS TRUE/IS FALSE to safely determine Boolean values, avoiding interference from NULL.
- Condition-based value assignment: Uses the IF function to return different expression values based on conditions.
Notes and Constraints
- AND and OR follow the three-valued logic of SQL. When any operand is NULL, the result may be NULL.
- The result of NOT NULL is still NULL and will not be changed to TRUE or FALSE.
- IS TRUE/IS NOT TRUE/IS FALSE/IS NOT FALSE always returns a definite TRUE or FALSE for NULL, and does not produce a NULL result.
- The types of the true_value and false_value parameters of the IF function must be compatible. Otherwise, explicit CAST conversion is required.
Syntax Structure
| Syntax | Return Type | Description | Example |
|---|---|---|---|
| boolean1 OR boolean2 | BOOLEAN | Logical OR. If either operand is TRUE, the result is TRUE. If both operands are FALSE, the result is FALSE. When NULL is involved: if the other operand is TRUE, the result is TRUE; if the other operand is FALSE, the result is NULL. That is, TRUE OR NULL is TRUE, and FALSE OR NULL is NULL. | SELECT TRUE OR FALSE; -- TRUE |
| boolean1 AND boolean2 | BOOLEAN | Logical AND. If both operands are TRUE, the result is TRUE. If either operand is FALSE, the result is FALSE. When NULL is involved: if the other operand is FALSE, the result is FALSE; if the other operand is TRUE, the result is NULL. That is, FALSE AND NULL is FALSE, and TRUE AND NULL is NULL. | SELECT TRUE AND FALSE; -- FALSE |
| NOT boolean | BOOLEAN | Logical NOT. It changes TRUE to FALSE and FALSE to TRUE. The result of NOT NULL is still NULL. | SELECT NOT TRUE; -- FALSE |
| boolean IS TRUE | BOOLEAN | Determines whether the value is TRUE. Returns TRUE if the boolean is TRUE, returns FALSE if the boolean is FALSE, and returns FALSE if the boolean is NULL. Unlike using boolean directly, IS TRUE returns a definite FALSE instead of NULL for NULL. | SELECT NULL IS TRUE; -- FALSE |
| boolean IS NOT TRUE | BOOLEAN | Determines whether the value is not TRUE. Returns FALSE if the boolean is TRUE, and returns TRUE if the boolean is FALSE or NULL. This is equivalent to NOT(boolean IS TRUE), but the result is always a definite TRUE or FALSE. | SELECT NULL IS NOT TRUE; -- TRUE |
| boolean IS FALSE | BOOLEAN | Determines whether the value is FALSE. Returns TRUE if the boolean is FALSE, returns FALSE if the boolean is TRUE, and returns FALSE if the boolean is NULL. Returns a definite FALSE instead of NULL for NULL. | SELECT NULL IS FALSE; -- FALSE |
| boolean IS NOT FALSE | BOOLEAN | Determines whether the value is not FALSE. Returns FALSE if the boolean is FALSE, and returns TRUE if the boolean is TRUE or NULL. This is equivalent to NOT(boolean IS FALSE), but the result is always a definite TRUE or FALSE. | SELECT NULL IS NOT FALSE; -- TRUE |
| IF(condition, true_value, false_value) | Consistent with true_value/false_value | Conditional function. If condition is of the BOOLEAN type, true_value is returned if condition is TRUE, and false_value is returned if condition is FALSE or NULL. The types of true_value and false_value must be compatible. | SELECT IF(3 > 2, 'yes', 'no'); -- 'yes' |
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot