Description:
For the following query:
mysql> CREATE TABLE v0 ( c1 TEXT NOT NULL );
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT INTO v0 VALUES ('100');
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM v0 WHERE c1 LIKE c1 ESCAPE '%';
+-----+
| c1 |
+-----+
| 100 |
+-----+
1 row in set (0.00 sec)
mysql> SELECT (c1 LIKE c1 ESCAPE '%') FROM v0;
+-------------------------+
| (c1 LIKE c1 ESCAPE '%') |
+-------------------------+
| 1 |
+-------------------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM v0 WHERE c1 LIKE c1 ESCAPE TRUE;
+-----+
| c1 |
+-----+
| 100 |
+-----+
1 row in set (0.00 sec)
mysql> SELECT (c1 LIKE c1 ESCAPE TRUE) FROM v0;
+--------------------------+
| (c1 LIKE c1 ESCAPE TRUE) |
+--------------------------+
| 0 |
+--------------------------+
1 row in set (0.00 sec)
-- Unexpected, the LIKE comparison result mismatched from the previous one.
mysql> SELECT * FROM v0 WHERE c1 LIKE c1 ESCAPE 1;
+-----+
| c1 |
+-----+
| 100 |
+-----+
1 row in set (0.00 sec)
mysql> SELECT (c1 LIKE c1 ESCAPE 1) FROM v0;
+-----------------------+
| (c1 LIKE c1 ESCAPE 1) |
+-----------------------+
| 0 |
+-----------------------+
1 row in set (0.00 sec)
-- Unexpected, the LIKE comparison result mismatched from the previous one.
-----------------------------------
From the query above, we create a table v0 with NOT NULL TEXT column c1. We insert value '100' into the table and then use SELECTs to access the contents. We know that LIKE ... ESCAPE ... keywords are used to match query patterns and the ESCAPE keyword takes string character as input. As demonstrated in the first and second SELECT statements, even if c1 doesn't have the special character '%', the comparison of c1 LIKE c1 is successfully and the value of c1 is returned. However, If we use non-string values in the ESCAPE, for example, using Boolean (the third/fourth SELECTs) or Integer (the fifth/sixth SELECTs), there are no errors reported and the results returned from the SELECT or WHERE clauses mismatched. This is unexpected, as we actually assume there will be error messages reported if we pass in non-string values in ESCAPE. Even if it accepts non-string values, the ESCAPE shouldn't affect the results from the LIKE comparison.
We are not sure whether it is an expected behavior for ESCAPE to accept a non-string value. And we cannot find documents that explain this behavior. Thus, we report the interesting query here and look forward to some explanation of this query. :-)
How to repeat:
In MySQL Server 8.0.27, use the following query commands:
mysql> CREATE TABLE v0 ( c1 TEXT NOT NULL );
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT INTO v0 VALUES ('100');
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM v0 WHERE c1 LIKE c1 ESCAPE '%';
+-----+
| c1 |
+-----+
| 100 |
+-----+
1 row in set (0.00 sec)
mysql> SELECT (c1 LIKE c1 ESCAPE '%') FROM v0;
+-------------------------+
| (c1 LIKE c1 ESCAPE '%') |
+-------------------------+
| 1 |
+-------------------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM v0 WHERE c1 LIKE c1 ESCAPE TRUE;
+-----+
| c1 |
+-----+
| 100 |
+-----+
1 row in set (0.00 sec)
mysql> SELECT (c1 LIKE c1 ESCAPE TRUE) FROM v0;
+--------------------------+
| (c1 LIKE c1 ESCAPE TRUE) |
+--------------------------+
| 0 |
+--------------------------+
1 row in set (0.00 sec)
-- Unexpected, the LIKE comparison result mismatched from the previous one.
mysql> SELECT * FROM v0 WHERE c1 LIKE c1 ESCAPE 1;
+-----+
| c1 |
+-----+
| 100 |
+-----+
1 row in set (0.00 sec)
mysql> SELECT (c1 LIKE c1 ESCAPE 1) FROM v0;
+-----------------------+
| (c1 LIKE c1 ESCAPE 1) |
+-----------------------+
| 0 |
+-----------------------+
1 row in set (0.00 sec)
-- Unexpected, the LIKE comparison result mismatched from the previous one.
Suggested fix:
As we expect Errors from the ESCAPE if we pass in non-string values:
mysql> CREATE TABLE v0 ( c1 TEXT NOT NULL );
mysql> INSERT INTO v0 VALUES ('100');
mysql> SELECT * FROM v0 WHERE c1 LIKE c1 ESCAPE '%';
+-----+
| c1 |
+-----+
| 100 |
+-----+
1 row in set (0.00 sec)
mysql> SELECT (c1 LIKE c1 ESCAPE '%') FROM v0;
+-------------------------+
| (c1 LIKE c1 ESCAPE '%') |
+-------------------------+
| 1 |
+-------------------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM v0 WHERE c1 LIKE c1 ESCAPE TRUE;
ERROR 1210 (HY000): Incorrect arguments to ESCAPE
mysql> SELECT (c1 LIKE c1 ESCAPE TRUE) FROM v0;
ERROR 1210 (HY000): Incorrect arguments to ESCAPE
mysql> SELECT * FROM v0 WHERE c1 LIKE c1 ESCAPE 1;
ERROR 1210 (HY000): Incorrect arguments to ESCAPE
mysql> SELECT (c1 LIKE c1 ESCAPE 1) FROM v0;
ERROR 1210 (HY000): Incorrect arguments to ESCAPE