Overview
Learn general aspects of logical operations.
This section breaks down how CQL evaluates logical operations across different scenarios and operand types.
Type conversion
For your convenience, all logical operations automatically convert their operands to boolean values whenever possible.
For example, 0 and 1 are typically used to represent the boolean values false and true, respectively. That is why the following expression is valid:
1 and true // true
Without type conversion, you would have to handle this case manually, which would make your queries more verbose and less readable. For more information about which values can be converted to boolean, see boolean conversion.
Be aware that not all values can be converted to boolean. However, boolean operations are designed to be error-tolerant and can still return a value, as explained in more detail below.
Error tolerance
Boolean expressions in CQL are designed to be error-tolerant, meaning that they can handle specific errors by treating the result of the faulty operand as false.
For example, consider the following query:
page's query's coupon is "10OFF"
Without error tolerance, this query would fail if the coupon parameter was missing from the query string. To handle this, you would need something like the or else operator to provide a default value:
(page's query's example or else null) is "10OFF"
As you can see, this query is more verbose and less readable than the original. You can still use this approach if you need to handle errors in a specific way, but in most cases, you can rely on error tolerance to simplify your queries.
The errors that are tolerated by boolean expressions include:
- Undefined values
When a variable, attribute, or parameter is missing. - Conversion errors
When a value doesn't satisfy the boolean conversion criteria. - Comparison errors
When the operands involved in a comparison are not comparable.
Depending on the boolean operator, the result of the entire expression may vary:
- In an and operation, if one of the operands fails, the result will always be false, as both expressions must be true for the operation to be true.
- In an or operation, if one of the operands succeeds, the result will always be true, as only one expression needs to be true for the operation to be true.
- In a not operation, if the operand fails, the result will be true.
Note that the behavior mentioned above does not differ from the standard behavior of the boolean operators. It is simply a consequence of treating the result of a faulty operand as false.
Short-circuiting
The conjunction and disjunction operations employ short-circuiting, an optimization technique that stops evaluating an expression as soon as its result can be determined without evaluating the remaining operands.
This is how short-circuiting works:
- In an and operation, if the first operand is false, the operation will short-circuit and return false without evaluating the second operand. This is because the result of an and operation is always false if any of its operands are false.
- In a or operation, if the first operand is true, the operation will short-circuit and return true without evaluating the second operand. This is because the result of an or operation is always true if any of its operands are true.
To illustrate the benefits of short-circuiting, consider the following query:
today's weekday is "monday" and user is returning
In this query, we used the and operator to combine two conditions: checking if today's weekday is Monday and if a user is returning.
With short-circuiting, if today is not Monday, the CQL engine will not even check the rest. This can be very efficient because checking if a user is returning may involve retrieving user data, which can be time-consuming. This can save resources and increase the efficiency of your queries.