Skip to content
COMPARISON CHAIN

Error Code: COMPARISON CHAIN

This guide explains why a comparison chain error may occur and describes how to fix it.

Rel provides a collection of comparison operators, most notably =, !=, <, <=, >, and >=.

They can be used for various data types including any numerical data type (Number), any text data types, date, and time-related data types. Comparing values of different types with each other is not supported.

// read query
 
def output = 1 < 2

This returns (), i.e., true, confirming that 1 is smaller than 2.

Each comparison operator has a corresponding named relation. For example, < is equivalent to lt. See this table for a comprehensive list of all the mathematical operations and their named counterparts.

You can combine these operators within the same expression to create comparison chains. For instance, you can do the following:

// read query
 
def output = 1 < 2 < 3 <= 4

Since you used only < and <=, the values within the comparison chain need to increase (or stay the same) for the entire chain to evaluate to true. Hence, this is a monotonic comparison chain.

Non-monotonic Comparison Chains

Mixing less than and greater than operations within the same expression generates non-monotonic comparison chains.

🔎

The system does not support non-monotonic comparison chains.

Trying to write a non-monotonic comparison chain, whether or not the statement is true, results in a COMPARISON CHAIN error:

def output:error1 = 1 < 3 > 4  // Triggers an error.
def output:error2 = 1 < 3 > 2  // Triggers an error.
Error example from the RAI Console

To prevent this error, write each comparison separately connected with and:

// read query
 
def output:valid1 = 1 < 3 and 3 > 4
def output:valid2 = 1 < 3 and 3 > 2

Note that only :valid2 is returned because 3 > 4 is false, rendering the entire expression for :valid1 false.

Example

You can have chain comparisons involving variables like x, extending beyond comparisons solely between constants. Here’s an example where you join the relations R and T and impose some ordering on the variables involved:

def R = {(1, 2); (2, 3)}
def T = {(3, 1); (3, 5)}
 
def output(x, y, z) {
   R(x, y) and T(y, z) and x < y > z
}

Here again, you run into a non-monotonic comparison chain error because < and > appear together in x < y > z. Rewriting this chain as x < y and y > z fixes the error and the query returns the expected result: (2, 3, 2).

// read query
 
def R = {(1, 2); (2, 3)}
def T = {(3, 1); (3, 5)}
 
def output(x, y, z) {
   R(x, y) and T(y, z) and x < y and y > z
}
Was this doc helpful?