Operators
GaussDB is compatible with most MySQL operators, but there are some differences. If not listed, the operator behavior is the native behavior of GaussDB by default. Currently, there are statements that are not supported by MySQL but supported by GaussDB. In GaussDB, they are usually used inside the system, so they are not recommended.
Operator Differences
- NULL values in ORDER BY are sorted in different ways. MySQL sorts NULL values first, while GaussDB sorts NULL values last. In GaussDB, you can use NULLS FIRST and NULLS LAST to set the sorting sequence of NULL values.
- If ORDER BY is used, the output sequence in GaussDB is consistent with that in MySQL except for the aforementioned special scenarios. If ORDER BY is not used, the output sequence in GaussDB may be different from that in MySQL.
- When using MySQL operators, use parentheses to ensure the combination of expressions. Otherwise, an error is reported. For example, SELECT 1 regexp ('12345' regexp '123').
The GaussDB operators can be successfully executed without using parentheses to strictly combine expressions.
- NULL values are displayed in different ways. MySQL displays a NULL value as "NULL". GaussDB displays a NULL value as empty.
MySQL output:
mysql> SELECT NULL; +------+ | NULL | +------+ | NULL | +------+ 1 row in set (0.00 sec)
GaussDB output:m_db=# SELECT NULL; ?column? ---------- (1 row)
- After the operator is executed, the column names are displayed in different ways. MySQL displays a NULL value as "NULL". GaussDB displays a NULL value as empty.
- When character strings are being converted to the double type but there is an invalid one, the alarm is reported differently. MySQL reports an error when there is an invalid constant character string, but does not report an error for an invalid column character string. GaussDB reports an error in either situation.
- The results returned by the comparison operator are different. For MySQL, 1 or 0 is returned. For GaussDB, t or f is returned.
Operators |
Differences Compared with MySQL |
---|---|
<> |
MySQL supports indexes, but GaussDB does not. |
<=> |
MySQL supports indexes, but GaussDB does not support indexes, hash joins, or merge joins. |
Row expressions |
GaussDB: m_db=# SELECT (1,2) <=> row(2,3); ERROR: could not determine interpretation of row comparison operator <=> LINE 1: SELECT (1,2) <=> row(2,3); ^ HINT: unsupported operator. m_db=# SELECT (1,2) < NULL; ?column? ---------- (1 row) m_db=# SELECT (1,2) <> NULL; ?column? ---------- (1 row) m_db=# SELECT (1, 2) IS NULL; ?column? ---------- f (1 row) m_db=# SELECT ISNULL((1, 2)); ?column? ---------- f (1 row) m_db=# SELECT ROW(0,0) BETWEEN ROW(1,1) AND ROW(2,2); ERROR: un support type m_db=# SELECT ROW(NULL) AS x; x ---- () (1 row) MySQL: mysql> SELECT (1,2) <=> row(2,3); +--------------------+ | (1,2) <=> row(2,3) | +--------------------+ | 0 | +--------------------+ 1 row in set (0.00 sec) mysql> SELECT (1,2) < NULL; ERROR 1241 (21000): Operand should contain 2 column(s) mysql> SELECT (1,2) <> NULL; ERROR 1241 (21000): Operand should contain 2 column(s) mysql> SELECT (1, 2) IS NULL; ERROR 1241 (21000): Operand should contain 1 column(s) mysql> SELECT ISNULL((1, 2)); ERROR 1241 (21000): Operand should contain 1 column(s) mysql> SELECT NULL BETWEEN NULL AND ROW(2,2); ERROR 1241 (21000): Operand should contain 1 column(s) mysql> SELECT ROW(NULL) AS x; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') as x' at line 1 |
-- |
MySQL indicates that an operand is negated twice and the result is equal to the original operand. GaussDB indicates a comment. |
!! |
MySQL: The meaning of !! is the same as that of !, indicating NOT. GaussDB: ! indicates NOT. If there is a space between two exclamation marks (! !), it indicates NOT for twice. If there is no space between them (!!), it indicates factorial.
NOTE:
|
[NOT] REGEXP |
|
LIKE |
MySQL: The left operand of LIKE can only be an expression of a bitwise or arithmetic operation, or expression consisting of parentheses. The right operand of LIKE can only be an expression consisting of unary operators (excluding NOT) or parentheses. GaussDB: The left and right operands of LIKE can be any expression. |
[NOT] BETWEEN AND |
MySQL: [NOT] BETWEEN AND is nested from right to left. The first and second operands of [NOT] BETWEEN AND can only be expressions of bitwise or arithmetic operations, or expressions consisting of parentheses. GaussDB: [NOT] BETWEEN AND is nested from left to right. The first and second operands of [NOT] BETWEEN AND can be any expression. |
IN |
MySQL: The left operand of IN can only be an expression of a bitwise or arithmetic operation, or expression consisting of parentheses. GaussDB: The left operand of IN can be any expression. The query in ROW IN (ROW,ROW...) format is not supported. When precision transfer is enabled and the in operator is used for data in a table, if the data in the table is of the FLOAT or DOUBLE type and includes the corresponding precision and scale, such as float (4,2) or double (4,2), GaussDB compares values based on the precision and scale, but MySQL reads values in the memory, which are distorted values, causing unequal comparison results. -- GaussDB: m_db=# CREATE TABLE test1(t_float float(4,2)); CREATE TABLE m_db=# INSERT INTO test1 VALUES(1.42),(2.42); INSERT 0 2 m_db=# SELECT t_float, t_float in (1.42,2.42) FROM test1; t_float | ?column? ---------+---------- 1.42 | t 2.42 | t (2 rows) --MySQL: mysql> CREATE TABLE test1(t_float float(4,2)); Query OK, 0 rows affected (0.01 sec) mysql> INSERT INTO test1 VALUES(1.42),(2.42); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> SELECT t_float, t_float in (1.42,2.42) FROM test1; +---------+------------------------+ | t_float | t_float in (1.42,2.42) | +---------+------------------------+ | 1.42 | 0 | | 2.42 | 0 | +---------+------------------------+ 2 rows in set (0.00 sec) |
! |
MySQL: The operand of ! can only be an expression consisting of unary operators (excluding NOT) or parentheses. GaussDB: The operand of ! can be any expression. |
# |
MySQL supports the comment tag (#), but GaussDB does not. |
BINARY |
Expressions (including some functions and operators) supported by GaussDB are different from those supported by MySQL. For GaussDB-specific expressions such as "~" and "IS DISTINCT FROM", due to the higher priority of the BINARY keyword, when BINARY expr is used, BINARY is combined with the left parameters of "~" and "IS DISTINCT FROM" first. As a result, an error is reported. |
Negation (-) |
The type and precision of the negation result are inconsistent with those in the MySQL. CREATE TABLE t AS SELECT - -1;
When precision transfer is enabled (m_format_behavior_compat_options is set to 'enable_precision_decimal'), the precision of the negative constant data type may be different from that in MySQL. In MySQL 5.7, when the expression contains negation operators, the max_length of the result precision increases based on the number of the negation operators, but this will not happen in GaussDB. For example:
|
/**/ |
Comments enclosed by /**/ are not supported in GaussDB statements. |
xor |
The behavior of XOR in GaussDB is different from that in MySQL. The GaussDB optimizer performs constant optimization. As a result, the results that are constants are calculated first. GaussDB: m_db=# SELECT 1 xor null xor pow(200, 2000000) FROM dual; ERROR: value out of range: overflow m_db=# CREATE TABLE t1(a int, b int); CREATE TABLE m_db=# INSERT INTO t1 VALUES(2,2), (200, 2000000000); INSERT 0 2 m_db=# m_db=# m_db=# SELECT 1 xor null xor pow(a, b) FROM t1; ?column? ---------- (2 rows) MySQL: mysql> SELECT 1 xor null xor pow(200, 2000000) FROM dual; +----------------------------------+ | 1 xor null xor pow(200, 2000000) | +----------------------------------+ | NULL | +----------------------------------+ 1 row in set (0.00 sec) ysql> CREATE TABLE t1(a int, b int); Query OK, 0 rows affected (0.04 sec) mysql> INSERT INTO t1 VALUES(2,2), (200, 2000000000); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> SELECT 1 xor null xor pow(a, b) FROM t1; +--------------------------+ | 1 xor null xor pow(a, b) | +--------------------------+ | NULL | | NULL | +--------------------------+ 2 rows in set (0.00 sec) |
IS NULL and IS NOT NULL |
In MySQL, these operators have a lower priority than logical operators, but they have a higher priority than logical operators in GaussDB. |
AND(&& ), OR (||), XOR, |, & , < , > , <=, >=, =, and != |
The execution mechanism of MySQL is as follows: After the left operand is executed, the system checks whether the result is empty and then determines whether to execute the right operand. As for the execution mechanism of GaussDB, after the left and right operands are executed, the system checks whether the result is empty. If the result of the left operand is empty and an error is reported during the execution of the right operand, MySQL does not report an error but directly returns an error. GaussDB reports an error during the execution. Behavior in MySQL: mysql> SELECT version(); +------------------+ | version() | +------------------+ | 5.7.44-debug-log | +------------------+ 1 row in set (0.00 sec) mysql> DROP TABLE IF EXISTS data_type_table; Query OK, 0 rows affected (0.02 sec) mysql> CREATE TABLE data_type_table ( -> MyBool BOOL, -> MyBinary BINARY(10), -> MyYear YEAR -> ); Query OK, 0 rows affected (0.02 sec) mysql> INSERT INTO data_type_table VALUES (TRUE, 0x1234567890, '2021'); Query OK, 1 row affected (0.00 sec) mysql> SELECT (MyBool % MyBinary) | (MyBool - MyYear) FROM data_type_table; +-----------------------------------------+ | (MyBool % MyBinary) | (MyBool - MyYear) | +-----------------------------------------+ | NULL | +-----------------------------------------+ 1 row in set, 2 warnings (0.00 sec) Behavior in GaussDB: m_db=# DROP TABLE IF EXISTS data_type_table; DROP TABLE m_db=# CREATE TABLE data_type_table ( m_db(# MyBool BOOL, m_db(# MyBinary BINARY(10), m_db(# MyYear YEAR m_db(# ); CREATE TABLE m_db=# INSERT INTO data_type_table VALUES (TRUE, 0x1234567890, '2021'); INSERT 0 1 m_db=# SELECT (MyBool % MyBinary) | (MyBool - MyYear) FROM data_type_table; WARNING: Truncated incorrect double value: '4Vx ' CONTEXT: referenced column: (MyBool % MyBinary) | (MyBool - MyYear) WARNING: division by zero CONTEXT: referenced column: (MyBool % MyBinary) | (MyBool - MyYear) ERROR: Bigint is out of range. CONTEXT: referenced column: (MyBool % MyBinary) | (MyBool - MyYear) |
+, -, *, /, %, mod, div |
When the b "constant is embedded in the CREATE VIEW AS SELECT arithmetic operator ('+', '-', '*', '/', '%', 'mod', or 'div'), the return type in MySQL 5.7 may contain the unsigned identifier, but in GaussDB, the return type does not contain the unsigned identifier. MySQL output: mysql> CREATE VIEW v22 as SELECT b'101' / b'101' c22; Query OK, 0 rows affected (0.00 sec) mysql> DESC v22; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | c22 | decimal(5,4) unsigned | YES | | NULL | | +-------+-----------------------+------+-----+---------+-------+ 1 row in set (0.01 sec)
GaussDB output:
m_db=# CREATE VIEW v22 AS SELECT b'101' / b'101' c22; CREATE VIEW m_db=# DESC v22; Field | Type | Null | Key | Default | Extra -------+--------------+------+-----+---------+------- c22 | decimal(5,4) | YES | | | (1 row) |
Example of Operator Combination |
MySQL |
GaussDB |
Description |
---|---|---|---|
SELECT 1 LIKE 3 & 1; |
Not supported |
Supported |
The right operand of LIKE cannot be an expression consisting of bitwise operators. |
SELECT 1 LIKE 1 +1; |
Not supported |
Supported |
The right operand of LIKE cannot be an expression consisting of arithmetic operators. |
SELECT 1 LIKE NOT 0; |
Not supported |
Supported |
The right operand of LIKE can only be an expression consisting of unary operators (such as +, -, or ! but except NOT) or parentheses. |
SELECT 1 BETWEEN 1 AND 2 BETWEEN 2 AND 3; |
Right-to-left combination |
Left-to-right combination |
You are advised to add parentheses to specify the calculation priority to prevent result deviation caused by sequence differences. |
SELECT 2 BETWEEN 1=1 AND 3; |
Not supported |
Supported |
The second operand of BETWEEN cannot be an expression consisting of comparison operators. |
SELECT 0 LIKE 0 BETWEEN 1 AND 2; |
Not supported |
Supported |
The first operand of BETWEEN cannot be an expression consisting of pattern matching operators. |
SELECT 1 IN (1) BETWEEN 0 AND 3; |
Not supported |
Supported |
The first operand of BETWEEN cannot be an expression consisting of IN operators. |
SELECT 1 IN (1) IN (1); |
Not supported |
Supported |
The second left operand of the IN expression cannot be an expression consisting of INs. |
SELECT ! NOT 1; |
Not supported |
Supported |
The operand of ! can only be an expression consisting of unary operators (such as +, -, or ! but except NOT) or parentheses. |
Index Differences
- Currently, GaussDB supports only UB-tree and B-tree indexes.
- When LIKE fuzzy match is executed and the execution plan is printed using EXPLAIN, the execution plan displays the minimum/maximum character weight codes of the collation corresponding to the current index column. The displayed codes may vary depending on the character set of a client, but it does not affect the accuracy of the LIKE fuzzy match query result.
- In the B-tree/UB-tree index scenario, the original logic of the native GaussDB is retained. That is, index scan supports comparison of types in the same operator family, but does not support other index types currently.
- When GaussDB JDBC is used to connect to the database, the YEAR type of GaussDB cannot use indexes in the PBE scenario that contains bind parameters.
- In the operation scenarios involving index column type and constant type, the conditions that indexes of a WHERE clause are supported in GaussDB is different from those in MySQL, as shown in Table 3. For example, GaussDB does not support indexes in the following statement:
CREATE TABLE t(_int int); CREATE INDEX idx ON t(_int) USING BTREE; SELECT * FROM t WHERE _int > 2.0;
- In the operation scenarios involving index column type and constant type in the WHERE clause, you can use the cast function to explicitly convert the constant type to the column type for indexing.
SELECT * FROM t WHERE _int > cast(2.0 AS signed);
- During LIKE fuzzy match, the maximum length of a prefix index created in GaussDB is 2676 bytes (3072 bytes for MySQL) in the single-byte character set scenario. If the length exceeds the maximum, you are advised to create a non-prefix index.
Table 3 Differences in index support Index Column Type
Constant Type
Supported in GaussDB
Supported in MySQL
BIT
BIT
No
Yes
Integer
No
Yes
Floating-point
No
Yes
String
No
Yes
Binary
No
Yes
Time with date
No
No
TIME
No
Yes
YEAR
No
Yes
SET/ENUM
String
No
No
Binary
No
No
Integer
No
No
Floating-point
No
No
Fixed-point
No
No
Time with date
No
No
TIME
No
No
YEAR
No
No
TIME
TIME
Yes
Yes
Time with date
Yes
Yes
YEAR
Yes
Yes
Integer (that can be converted to the TIME type)
Yes
Yes
Integer (that cannot be converted to the TIME type)
No
No
Character string (that can be converted to the TIME type)
Yes
Yes
Character string (that cannot be converted to the TIME type)
No
No
Binary (that can be converted to the TIME type)
Yes
Yes
Binary (that cannot be converted to the TIME type)
No
No
Floating-point (that can be converted to the TIME type)
Yes
Yes
Floating-point (that cannot be converted to the TIME type)
No
No
Fixed-point (that can be converted to the TIME type)
Yes
Yes
Fixed-point (that cannot be converted to the TIME type)
No
No
YEAR
YEAR
Yes
Yes
Integer (that can be converted to the YEAR type)
Yes
Yes
Integer (that cannot be converted to YEAR type and is negative)
No
Yes
Floating-point (that can be converted to the YEAR type)
Yes
Yes
Floating-point (that cannot be converted to the YEAR type)
No
No
Fixed-point (that can be converted to the YEAR type)
Yes
Yes
Fixed-point (that cannot be converted to the YEAR type)
No
No
Character string (that can be converted to the YEAR type)
Yes
Yes
Character string (that cannot be converted to the YEAR type)
No
No
Binary (that can be converted to the YEAR type)
Yes
Yes
Binary (that cannot be converted to the YEAR type)
No
No
Time with date
No
Yes
TIME
No
Yes
Time with date
Time with date
Yes
Yes
TIME
Yes
Yes
YEAR
Yes
No
Integer (that can be converted to the time type with date)
Yes
Yes
Integer (that cannot be converted to the time type with date)
No
No
Floating-point (that can be converted to the time type with date)
Yes
Yes
Floating-point (that cannot be converted to the time type with date)
No
No
Fixed-point (that can be converted to the time type with date)
Yes
Yes
Fixed-point (that cannot be converted to the time type with date)
No
No
Character string (that can be converted to the time type with date)
Yes
Yes
Character string (that cannot be converted to the time type with date)
No
No
Binary (that can be converted to the time type with date)
Yes
Yes
Binary (that cannot be converted to the time type with date)
No
No
Fixed-point
Fixed-point
Yes
Yes
Integer
Yes
Yes
Floating-point
No
Yes
String
No
Yes
Binary
No
Yes
Time with date
No
Yes
TIME
No
Yes
YEAR
Yes
Yes
Floating-point
Floating-point
Yes
Yes
Integer
Yes
Yes
Fixed-point
Yes
Yes
String
Yes
Yes
Binary
Yes
Yes
Time with date
Yes
Yes
TIME
Yes
Yes
YEAR
Yes
Yes
Integer
Integer
Yes
Yes
Floating-point
No
Yes
Fixed-point
Yes
Yes
Character string (can be converted to the integer type)
Yes
Yes
Character string (that cannot be converted to the integer type)
No
Yes
Binary type (can be converted to the integer type)
Yes
Yes
Binary type (cannot be converted to the integer type)
No
Yes
Time with date
Yes
Yes
TIME
Yes
Yes
YEAR
Yes
Yes
Binary
Binary
Yes
Yes
String
Yes
Yes
Time with date
No
No
TIME
Yes
No
YEAR
No
No
Integer
No
No
Floating-point
No
No
Fixed-point
No
No
String
String
Yes
Yes
Time with date
No
No
TIME
Yes
No
YEAR
No
No
Binary
No
No
Integer
No
No
Floating-point
No
No
Fixed-point
No
No
- Only when the disable_int_cmp_num_index option is not enabled for the GUC parameter m_format_behavior_compat_options, indexes can be used to compare integer index columns with fixed-point constants.
- Fixed-point constants cannot be user-defined variables, case when expressions, or subquery return values if indexes are used to compare integer index columns with fixed-point constants.
- In the operation scenarios involving index column type and constant type in the WHERE clause, you can use the cast function to explicitly convert the constant type to the column type for indexing.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot