Skip to content

Operators

Conditions and values in expressions are rarely a single variable. A condition usually compares a variable with a value, or combines several tests, and a value is often built from several parts. Operators do this work: arithmetic operators compute and concatenate, comparison operators test, and logical operators combine the tests. This page lists the operators, explains how values of different types are treated, and gives the order in which operators are applied.

The arithmetic operators work on numbers, and + also on strings and arrays:

OperatorMeaning
+Addition, or string and array concatenation when either operand is not a number.
-Subtraction, or unary negation at the start of a subexpression.
*Multiplication.
/Division.
%Remainder (modulo).

Because + also concatenates, it is the common way to build identifiers and addresses from context variables. Examples are 'sql_' + listener and 'postmaster@' + system('domain').

The operators -, *, / and % parse a string operand as a number, and a string that is not a number counts as 0. Division by zero produces 0 rather than an error, for both / and %.

The remainder of two integers is an integer and never negative, so -7 % 3 is 2. When either operand is a decimal number, the remainder is one as well: 7.5 % 2 is 1.5. A remainder selects every n-th case of a counter, which makes it useful for alternating between choices. The following route expression delivers local recipients locally and alternates the other delivery attempts between the recipients’ MX hosts and a fallback route, so that every odd-numbered retry goes through the fallback:

{
"route": {
"match": {
"0": {"if": "is_local_domain(rcpt_domain)", "then": "'local'"},
"1": {"if": "retry_num % 2 == 1", "then": "'fallback'"}
},
"else": "'mx'"
}
}

The logical operators combine conditions. Each has a symbolic form, and some accept a shorter synonym:

OperatorMeaning
&&Logical AND. & is accepted as a synonym.
||Logical OR. | is accepted as a synonym.
^Logical XOR.
!Logical NOT, written before its operand.

A value that is not a boolean is converted when it is used in a logical context. The empty string, an empty array, 0 and 0.0 are treated as false, and every other value as true.

The comparison operators compare two values and produce a boolean:

OperatorMeaning
==Equal. = is accepted as a synonym.
!=Not equal.
>Greater than.
<Less than.
>=Greater than or equal. => is accepted as a synonym.
<=Less than or equal. =< is accepted as a synonym.

Operands must be of compatible types, although a mismatch raises no error, and that makes mismatches easy to miss. When a string is compared with a number, the string is parsed as a number, and a string that is not a number counts as 0. A comparison such as sender == 0 is therefore true for every sender that is not numeric. Compare numbers with numbers, and strings with strings.

When an expression contains several operators without parentheses, they are applied in the following order, from highest precedence to lowest:

  1. Unary ! and unary -.
  2. *, /, %.
  3. +, -.
  4. >, <, >=, <=.
  5. ==, !=.
  6. ^.
  7. &&.
  8. ||.

Parentheses override precedence, as in the following condition:

(retry_num > 3 || notify_num > 1) && is_local_domain(rcpt_domain)

Without the parentheses, && would bind tighter than ||, and the condition would mean retry_num > 3 || (notify_num > 1 && is_local_domain(rcpt_domain)).