Logical operations

Learn how to perform logical operations in CQL.

Logical operations play a crucial role in evaluating conditions and making decisions in CQL. They allow you to combine and manipulate boolean values to control the flow and result of your queries.

In this reference, we explore the syntax and semantics of these operations, along with practical examples of how to use them in your conditions.

Operations

This is the summary of the available logic operations for quick reference:

OperationDescriptionOperator
NotNegates an expression's boolean value.! not
AndEvaluates if both expressions are true.&& and
OrEvaluates if either of the expressions is true.|| or

In the following sections, we visit each of these operations in detail, including their syntax, semantics, and practical examples.

Not

The logical not operation inverts a boolean value. It is typically used to invert a condition or negate the result of an expression.

A negation is denoted by the ! sign before an expression:

!/*<value>*/
Try in Playground

Here is a practical example:

!true // false
Try in Playground

You can also use not for a more readable alternative:

not /*<value>*/
Try in Playground

In the above examples, the boolean value true is negated, resulting in false, as the following expression attests:

not true is false
Try in Playground

And

The logical and operation evaluates to true when both operands are true and false otherwise.

To use the and operator, place a && sign between two expressions:

/*<value>*/ && /*<value>*/
Try in Playground

For a more natural language feel, you can use and:

/*<value>*/ and /*<value>*/
Try in Playground

Here is a practical example:

true and false // false
Try in Playground

Note that the and operation has higher precedence than the or operation:

true or false and false
Try in Playground

The above expression is equivalent to the following:

true or (false and false)
Try in Playground

In other words, the and operator is evaluated first, and the result is used as the second operand of the or operator.

Or

The logical or operation evaluates to true when at least one of the operands is true and false otherwise.

To use the or operator, place a || sign between two expressions:

/*<value>*/ || /*<value>*/
Try in Playground

For a more natural language feel, you can use or:

/*<value>*/ or /*<value>*/
Try in Playground

Here is a practical example:

true or false // true
Try in Playground

Note that the or operation has lower precedence than the and operation:

true or false and false
Try in Playground

The above expression is equivalent to the following:

true or (false and false)
Try in Playground

In other words, the and operation is evaluated first, and the result is used as the second operand of the or operation.

General aspects

This section discusses some general aspects of logical operations in CQL that make your queries more flexible and efficient.

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
Try in Playground

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"
Try in Playground

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"
Try in Playground

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
Try in Playground

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.