Bug #114564 Silent Subqueries: No Errors for Missing Fields
Submitted: 7 Apr 2024 8:16 Modified: 8 Apr 2024 9:53
Reporter: Chi Zhang Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: DML Severity:S3 (Non-critical)
Version:8.0.36 OS:Any
Assigned to: CPU Architecture:Any

[7 Apr 2024 8:16] Chi Zhang
Description:
When a subquery contains a field that does not exist in the inner table, no error is reported during the SQL statement execution.

How to repeat:
1.Create tables.
CREATE TABLE `content_thread_basic` (
  `id` bigint unsigned NOT NULL,
  `circle_id` bigint NOT NULL,
  `business_type_id` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;
INSERT INTO `content_thread_basic` VALUES(1, 10000006, '6fadb388-a51a-11ec-af1e-911b6d9f1ec');
 
CREATE TABLE `content_thread_circle_relation` (
  `thread_id` bigint NOT NULL,
  `circle_id` bigint NOT NULL,
  `update_time` bigint DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;
INSERT INTO `content_thread_circle_relation` VALUES(1, 10000006, 1712476782810);

2.Execute the select or update operation. The `thread_id` field of the subquery does not exist in the inner table.

mysql> select circle_id, update_time from content_thread_circle_relation where circle_id = 10000006 and thread_id in (select thread_id from content_thread_basic a where a.circle_id = 10000006 and a.business_type_id = '6fadb388-a51a-11ec-af1e-911b6d9f1ec');
+-----------+---------------+
| circle_id | update_time   |
+-----------+---------------+
|  10000006 | 1712476782810 |
+-----------+---------------+
1 row in set (0.00 sec)

mysql> update content_thread_circle_relation set circle_id = 10000094, update_time = replace(unix_timestamp(current_timestamp(3)), '.', '') where circle_id = 10000006 and thread_id in (select thread_id from content_thread_basic a where a.circle_id = 10000006 and a.business_type_id = '6fadb388-a51a-11ec-af1e-911b6d9f1ec');
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from content_thread_circle_relation;
+-----------+-----------+---------------+
| thread_id | circle_id | update_time   |
+-----------+-----------+---------------+
|         1 |  10000094 | 1712476932912 |
+-----------+-----------+---------------+
1 row in set (0.01 sec)

The select or update operation executes successfully, but my expectation is to get an error, like this:
mysql> select thread_id from content_thread_basic a where a.circle_id = 10000006 and a.business_type_id = '6fadb388-a51a-11ec-af1e-911b6d9f1ec';
ERROR 1054 (42S22): Unknown column 'thread_id' in 'field list'
[8 Apr 2024 9:53] MySQL Verification Team
Hi Mr. Zhang,

Thank you for your bug report.

However, this is not a bug.

Simply, SQL standard allows for a dependent nested query to contain a column from the outer query. Then the optimiser simply adds a condition which , in your case, is always satisfied. It is like adding a conditional expression:

content_thread_circle_relation.thread_id = content_thread_circle_relation.thread_id.

fully in accordance with SQL standard.

Not a bug.