Operators let you compare values, perform calculations, manipulate strings, work with binary data, and combine logical conditions in MaxCompute SQL statements.
Overview
MaxCompute supports the following types of operators:
|
Operator type |
Description |
|
Compare values and return a BOOLEAN result. |
|
|
Concatenate strings. |
|
|
Perform mathematical calculations. |
|
|
Perform operations on binary representations of integers. |
|
|
Combine multiple conditions. Typically used with BOOLEAN expressions. |
Relational operators
Relational operators compare two values and return TRUE, FALSE, or NULL. Unless otherwise noted, if either operand is NULL, the result is NULL.
|
Operator |
Description |
|
|
Returns TRUE if A equals B. Returns NULL if A or B is NULL. |
|
|
NULL-safe equality. Returns TRUE if both A and B are NULL. Returns FALSE if only one is NULL. Otherwise, returns TRUE if A equals B, or FALSE if not. |
|
|
Returns TRUE if A is not equal to B. Returns NULL if A or B is NULL. |
|
|
Returns TRUE if A is less than B. Returns NULL if A or B is NULL. |
|
|
Returns TRUE if A is less than or equal to B. Returns NULL if A or B is NULL. |
|
|
Returns TRUE if A is greater than B. Returns NULL if A or B is NULL. |
|
|
Returns TRUE if A is greater than or equal to B. Returns NULL if A or B is NULL. |
|
|
Returns TRUE if A is NULL. Otherwise, returns FALSE. |
|
|
Returns TRUE if A is not NULL. Otherwise, returns FALSE. |
|
|
Pattern matching. Returns TRUE if string A matches pattern B. Returns NULL if A or B is NULL. The |
|
|
Regular expression matching. Returns TRUE if string A matches the regular expression B. Returns NULL if A or B is NULL. Returns an error if B is an empty string. |
|
|
Returns TRUE if A is in set B. Returns NULL if A is NULL. Set B must be a constant set that contains at least one element, and all elements must be of the same data type. |
|
|
Returns TRUE if A is greater than or equal to B and less than or equal to C (or the inverse with NOT). Returns NULL if A, B, or C is NULL. |
|
|
Compares two values, treating NULLs as equal. |
LIKE pattern examples
'aaa' LIKE 'a__' -- TRUE (underscore matches one character)
'aaa' LIKE 'a%' -- TRUE (percent matches zero or more characters)
'aaa' LIKE 'aab' -- FALSE
'a%b' LIKE 'a\\%b' -- TRUE (escaped percent matches literal %)
'axb' LIKE 'a\\%b' -- FALSE
Usage examples
SELECT * FROM user WHERE user_id = '0001';
SELECT * FROM user WHERE user_name <> 'maggie';
SELECT * FROM user WHERE age > '50';
SELECT * FROM user WHERE birth_day >= '1980-01-01 00:00:00';
SELECT * FROM user WHERE is_female IS NULL;
SELECT * FROM user WHERE is_female IS NOT NULL;
SELECT * FROM user WHERE user_id IN (0001, 0010);
SELECT * FROM user WHERE user_name LIKE 'M%';
Important notes
Mismatched types may return NULL in comparisons. Convert operands to compatible types before comparing. Type conversions.
Comparing DOUBLE values
Do not compare DOUBLE values with = directly due to limited precision. Instead, check whether the absolute difference is smaller than a threshold:
ABS(0.9999999999 - 1.0000000000) < 0.000000001
-- 0.9999999999 and 1.0000000000 have a precision of 10 decimal digits,
-- whereas 0.000000001 has a precision of 9 decimal digits.
-- 0.9999999999 is considered to be equal to 1.0000000000.
-
ABS is a built-in function that returns the absolute value of its input.
-
A DOUBLE value in MaxCompute typically provides 14 decimal digits of precision.
Comparing STRING and BIGINT values
Comparing STRING with BIGINT implicitly converts both to DOUBLE, which may lose precision. Use CAST(string_value AS BIGINT) to convert the STRING to BIGINT first.
String operators
|
Operator |
Description |
|
|
Concatenates strings A and B. For example, |
Example
SELECT first_name || ' ' || last_name AS full_name FROM employees;
Arithmetic operators
Arithmetic operators perform mathematical calculations on numeric values.
|
Operator |
Description |
|
|
Returns the sum of A and B. Returns NULL if A or B is NULL. |
|
|
Returns the difference of A minus B. Returns NULL if A or B is NULL. |
|
|
Returns the product of A multiplied by B. Returns NULL if A or B is NULL. |
|
|
Returns the quotient of A divided by B. Returns NULL if A or B is NULL. If both A and B are of the BIGINT type, the result is of the DOUBLE type. |
|
|
Returns the remainder of A divided by B. Returns NULL if A or B is NULL. |
|
|
Returns A (unary plus). |
|
|
Returns the negation of A. Returns NULL if A is NULL. |
|
|
Performs integer division of A by B, discarding the fractional part. Returns NULL if A or B is NULL. |
Example
SELECT age + 10, age - 10, age % 10, -age, age * age, age / 10, age DIV 10 FROM user;
Data type rules
-
Only STRING, BIGINT, DOUBLE, and TIMESTAMP_NTZ operands support arithmetic operations. Date and BOOLEAN values are not supported. MaxCompute TIMESTAMP_NTZ data types.
-
STRING operands are implicitly converted to DOUBLE before calculation.
-
When BIGINT and DOUBLE operands are mixed, the BIGINT value is implicitly converted to DOUBLE and the result is DOUBLE.
-
If both A and B are BIGINT,
A / Breturns DOUBLE. All other arithmetic operations on two BIGINT operands return BIGINT.
Bitwise operators
Bitwise operators work on the binary representation of integers. Both operands must be BIGINT. No implicit type conversions apply.
|
Operator |
Syntax |
Description |
|
|
|
Returns the bitwise AND of A and B. For example, |
|
|
|
Returns the bitwise OR of A and B. For example, |
|
|
|
Returns the bitwise NOT of A. For example, |
|
|
|
Returns the bitwise XOR of A and B. For example, |
Logical operators
Logical operators combine BOOLEAN expressions. Only BOOLEAN operands are supported; no implicit type conversions apply.
The following truth tables show results for all combinations of TRUE, FALSE, and NULL:
AND
|
A |
B |
A AND B |
|
TRUE |
TRUE |
TRUE |
|
TRUE |
FALSE |
FALSE |
|
FALSE |
TRUE |
FALSE |
|
FALSE |
FALSE |
FALSE |
|
FALSE |
NULL |
FALSE |
|
NULL |
FALSE |
FALSE |
|
TRUE |
NULL |
NULL |
|
NULL |
TRUE |
NULL |
|
NULL |
NULL |
NULL |
OR
|
A |
B |
A OR B |
|
TRUE |
TRUE |
TRUE |
|
TRUE |
FALSE |
TRUE |
|
FALSE |
TRUE |
TRUE |
|
FALSE |
FALSE |
FALSE |
|
FALSE |
NULL |
NULL |
|
NULL |
FALSE |
NULL |
|
TRUE |
NULL |
TRUE |
|
NULL |
TRUE |
TRUE |
|
NULL |
NULL |
NULL |
NOT
|
A |
NOT A |
|
TRUE |
FALSE |
|
FALSE |
TRUE |
|
NULL |
NULL |
Operator precedence
When an expression contains multiple operators, MaxCompute evaluates them in the following order. Lower numbers have higher precedence. Operators at the same level are evaluated left to right.
|
Precedence |
Operators |
|
1 (highest) |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|| |
|
6 |
& |
|
7 |
| |
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 (lowest) |
|
Use parentheses to override the default precedence. For example:
a = 1 AND b = 1 OR c = 1 -- Evaluates AND first, then OR.
a = 1 AND (b = 1 OR c = 1) -- Evaluates OR first (inside parentheses), then AND.