Operators
Sail supports all Spark SQL operators in SQL expressions.
Operator Precedence
The precedence of an operator determines the order in which expressions are evaluated. For example, in the expression 3 + 4 * 5
, the multiplication operator (*
) has a higher precedence than the addition operator (+
), so the multiplication is performed first, resulting in 3 + (4 * 5) = 23
.
Operators that have higher precedence are evaluated before operators with lower precedence. Operators with the same precedence are evaluated based on their associativity.
The following table lists the precedence and associativity of all the SQL operators in Sail. The operators are listed in order of decreasing precedence, where 1 is the highest precedence.
Precedence | Operator | Operation | Associativity |
---|---|---|---|
1 | . [] :: | Member access Element access Cast | Left to right |
2 | + - ~ | Unary plus Unary minus Bitwise not | Right to left |
3 | * / % DIV | Multiplication Division Modulo Integral division | Left to right |
4 | + - || | Addition Subtraction Concatenation | Left to right |
5 | << >> >>> | Bitwise shift left Bitwise shift right Bitwise shift right unsigned | Left to right |
6 | & | Bitwise and | Left to right |
7 | ^ | Bitwise xor (exclusive or) | Left to right |
8 | | | Bitwise or (inclusive or) | Left to right |
9 | = , == <> , != < <= > >= | Comparison operators | Left to right |
10 | BETWEEN IN RLIKE , REGEXP ILIKE LIKE IS [NOT ]NULL IS [NOT ]TRUE IS [NOT ]FALSE IS [NOT ]DISTINCT FROM | Other predicates | Left to right |
11 | NOT , ! EXISTS | Logical not Existence | Right to left |
12 | AND | Logical and (conjunction) | Left to right |
13 | OR | Logical or (disjunction) | Left to right |
INFO
In the original Spark documentation, the logical not operator (NOT
or !
) has a higher precedence than predicates such as LIKE
, but this does not seem to be consistent with the actual implementation.
In both the Sail documentation here and the Sail implementation, the logical not operator has a lower precedence than other predicates. This provides a more intuitive behavior, where NOT a LIKE b
is interpreted as NOT (a LIKE b)
instead of (NOT a) LIKE b
.