Operators
FQL supports a number of operators that can be used in expressions. There are comparison, logical, arithmetic, and the ternary operator.
Comparison operators
Comparison (or relational) operators compare two operands. They can be used with any input data types, and will return a boolean result value.The following comparison operators are supported:
==
equality!=
inequality<
less than<=
less or equal>
greater than>=
greater or equalIN
test if a value is contained in an arrayNOT IN
test if a value is not contained in an arrayLIKE
tests if a string value matches a patternNOT LIKE
tests if a string value does not match a pattern=~
tests if a string value matches a regular expression!~
tests if a string value does not match a regular expression
Each of the comparison operators returns a boolean value if the comparison can be evaluated and returns true if the comparison evaluates to true, and false otherwise.
The comparison operators accept any data types for the first and second operands. However, IN
and NOT IN
will only return a meaningful result if their right-hand operand is an array, and LIKE
will only execute if both operands are string values. The comparison operators will not perform any implicit type casts if the compared operands have different or non-sensible types.
Some examples for comparison operations in FQL:
The LIKE
operator checks whether its left operand matches the pattern specified in its right operand. The pattern can consist of wildcards. The list of supported wildcards can be found here.
The pattern matching performed by the LIKE
operator is case-sensitive.
The NOT LIKE
operator has the same characteristics as the LIKE
operator but with the result negated.
Array comparison operators
The comparison operators also exist as array variant. In the array variant, the operator is prefixed with one of the keywordsALL
, ANY
or NONE
. Using one of these keywords changes the operator behavior to execute the comparison operation for all, any, or none of its left hand argument values. It is therefore expected that the left hand argument of an array operator is an array.
Logical operators
The following logical operators are supported in FQL:&&
logical and operator||
logical or operator!
logical not/negation operator
FQL also supports the following alternative forms for the logical operators:
AND
logical and operatorOR
logical or operatorNOT
logical not/negation operator
The alternative forms are aliases and functionally equivalent to the regular operators.
The two-operand logical operators in FQL will be executed with short-circuit evaluation (except if one of the operands is or includes a subquery. In this case the subquery will be pulled out an evaluated before the logical operator).
The result of the logical operators in FQL is defined as follows:
lhs && rhs
will returnlhs
if it isfalse
or would befalse
when converted into a boolean. Iflhs
istrue
or would betrue
when converted to a boolean,rhs
will be returned.lhs || rhs
will returnlhs
if it istrue
or would betrue
when converted into a boolean. Iflhs
isfalse
or would befalse
when converted to a boolean,rhs
will be returned.! value
will return the negated value of value converted into a boolean
Passing non-boolean values to a logical operator is allowed. Any non-boolean operands will be casted to boolean implicitly by the operator, without making the query abort.
The conversion to a boolean value works as follows:
NONE
will be converted tofalse
- boolean values remain unchanged
- all numbers unequal to zero are
true
, zero isfalse
- an empty string is
false
, all other strings aretrue
- arrays, objects, binary and custom types are
true
, regardless of their contents
The result of logical and and logical or operations can now have any data type and is not necessarily a boolean value.
For example, the following logical operations will return boolean values:
whereas the following logical operations will not return boolean values:
Arithmetic operators
Arithmetic operators perform an arithmetic operation on two numeric operands. The result of an arithmetic operation is again a numeric value.FQL supports the following arithmetic operators:
+
addition-
subtraction*
multiplication/
division%
modulus
Unary plus and unary minus are supported as well:
For exponentiation, there is a numeric function POW(). The syntax base ** exp is not supported.
Some example arithmetic operations:
The arithmetic operators accept operands of any type. Passing non-numeric values to an arithmetic operator will cast the operands to numbers:
NONE
will be converted to0
false
will be converted to0
,true
will be converted to1
- a valid numeric value remains unchanged, but
NaN
and Infinity will be converted to0
- string values are converted to a number if they contain a valid string representation of a number. Any whitespace at the start or the end of the string is ignored. Strings with any other contents are converted to the number
0
- an empty array is converted to
0
, an array with one member is converted to the numeric representation of its sole member. Arrays with more members are converted to the number0
. - objects, binary and custom types are converted to the number
0
.
Here are a few examples:
Ternary operator
FQL also supports a ternary operator that can be used for conditional evaluation. The ternary operator expects a boolean condition as its first operand, and it returns the result of the second operand if the condition evaluates to true, and the third operand otherwise.There is also a shortcut variant of the ternary operator with just two operands. This variant can be used when the expression for the boolean condition and the return value should be the same:
Range operator
FQL supports expressing simple numeric ranges with the..
operator. This operator can be used to easily iterate over a sequence of numeric values.
The ..
operator will produce an array of the integer values in the defined range, with both bounding values included.
will produce the following result:
Operator precedence
The operator precedence in FQL is similar as in other familiar languages (lowest precedence first):? :
ternary operator||
logical or&&
logical and==
,!=
equality and inequalityIN
in operator<
,<=
,>=
,>
less than, less equal, greater equal, greater than+
,-
addition, subtraction*
,/
,%
multiplication, division, modulus!
,+
,-
logical negation, unary plus, unary minus()
function call.
member access[]
indexed value access
The parentheses (
and )
can be used to enforce a different operator evaluation order.