Expressions and operator precedence
What an expression can contain, how operators group, and where Raft DB's precedence or evaluation differs from MySQL.
An expression is a literal, a column name, a variable, a function call, a subquery, or those joined by operators and parentheses.
Syntax#
expr:
literal
| column_name | table.column | db.table.column
| @user_variable | @@system_variable | @user_variable := expr
| function(args)
| aggregate(...) [OVER (...)]
| CASE ... END
| CAST(expr AS type) | CONVERT(expr, type)
| EXTRACT(unit FROM expr) | TRIM([BOTH | LEADING | TRAILING] [str] FROM expr)
| INTERVAL expr unit
| (expr) | (expr, expr, ...) -- a row constructor
| (SELECT ...) | EXISTS (SELECT ...)
| expr op expr | op expr | expr IS [NOT] {NULL | TRUE | FALSE | UNKNOWN}
| expr [NOT] {IN (list | subquery) | BETWEEN expr AND expr | LIKE pattern | REGEXP pattern}
| expr COLLATE collationOperator precedence#
From the tightest to the loosest. Operators on one line group from left to right.
| Level | Operators |
|---|---|
| 1 | INTERVAL expr unit |
| 2 | COLLATE, BINARY, ->, ->>, MEMBER OF (the JSON operators are MySQL only) |
| 3 | unary -, +, ~ |
| 4 | ^ |
| 5 | *, /, DIV, %, MOD |
| 6 | -, + |
| 7 | <<, >> |
| 8 | & |
| 9 | | |
| 10 | =, <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN, BETWEEN |
| 11 | NOT, ! |
| 12 | AND, && |
| 13 | XOR, OR, || |
| 14 | := |
2 + 3 & 4 is (2 + 3) & 4 (5 & 4 is 4); 1 | 2 & 3 is 1 | (2 & 3); 2 * 3 ^ 2 is 2 * (3 ^ 2).
Differences from MySQL's table:
XORis read at the level ofOR, not betweenORandAND. Use parentheses when you mix them.!is read at the level ofNOT(MySQL gives it the tightest level of the unary operators); write(!a) = bwith parentheses.||is alwaysOR. The modePIPES_AS_CONCATis accepted and has no effect; useCONCAT.- The mode
HIGH_NOT_PRECEDENCEhas no effect:NOT a BETWEEN b AND cisNOT (a BETWEEN b AND c). BINARY expris read and ignored (see Cast and conversion functions); a binary or case-sensitiveCOLLATE(utf8mb4_bin,..._cs) is refused as not supported; the case-insensitive collations are read and change nothing.LIKE ... ESCAPEis not supported yet (use the default escape, the backslash).
Evaluation rules#
- Three-valued logic. A comparison with
NULLisNULL;NULL AND 0is 0,NULL OR 1is 1; see NULL handling. - Division.
/is exact:7 / 2is3.5000(a decimal with the dividend's scale plusdiv_precision_increment, 4 by default, rounded half away from zero);DIVis the integer division; division by zero andMODby zero giveNULL. - Numbers and text. In an arithmetic context text converts to a number (
'10' + 5is 15; text that does not start with digits is 0). In a comparison of two texts the texts are compared, so'10' < '9'is 1; a number and a text are compared as numbers (see Type conversion). - Exactness.
+ - * / % DIVon whole numbers and decimals are exact up to 128 bits; a float or double operand makes the result a double. - Row constructors.
(a, b) = (1, 2),<>,<,<=,>,>=,<=>and(a, b) IN ((1, 2), (3, 4))compare part by part (all equal; one differs; the first pair that differs decides). Rows with different numbers of parts are error 1241 (The operand must have n column(s)). A row can also be compared with a subquery. ANY,SOME,ALL.x op ANY (subquery); see Comparison operators.- Types of expressions. The type of a computed value (for
CREATE TABLE ... AS SELECTand functional indexes) is worked out as MySQL does:INTorBIGINTby width for whole-number arithmetic,DECIMAL(p+4, s+4)for/,DOUBLEfor float and text operands, merged types forCOALESCE,IF,CASE,GREATEST. This is checked against MySQL 9.7 for about 120 expressions. Not typed yet:DATE_ADDand interval arithmetic, window functions, expressions over derived tables, views, natural joins andUNION.
Examples#
SELECT 5 & 4, 5 | 2, 5 ^ 1, 1 << 3, 256 >> 2, ~0, 2 + 3 & 4, 1 | 2 & 3, 2 * 3 ^ 2;
SELECT '10' < '9', 10 < 9, '10' + 5, 'a' = 'A', NULL IS NULL, 1 <=> NULL, 7 DIV 2;
SELECT (1, 2) = (1, 2), (1, 3) < (1, 4), (2, 3) IN ((1, 2), (2, 3));