Bug #108520 Return different result due to different optimizer switch
Submitted: 16 Sep 2022 7:54 Modified: 16 Sep 2022 8:04
Reporter: ting du Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version:8.0, 8.0.30, 5.7.39 OS:Linux
Assigned to: CPU Architecture:Any

[16 Sep 2022 7:54] ting du
Description:
The same query return different result due to the optimizer switch(derived_merge). Although the optimizer switch is different, but for the result should be same for the same query. 

How to repeat:
mysql> create table t1(double_col double);
Query OK, 0 rows affected (0.03 sec)

mysql> insert into t1 values(-1.7976931348623157e308);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+-------------------------+
| double_col              |
+-------------------------+
| -1.7976931348623157e308 |
+-------------------------+
1 row in set (0.00 sec)

mysql> set optimizer_switch="derived_merge=on";
Query OK, 0 rows affected (0.00 sec)

mysql> explain select * from (select reverse(double_col) from t1) as t;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
|  1 | SIMPLE      | t1    | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    1 |   100.00 | NULL  |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

mysql> select * from (select reverse(double_col) from t1) as t;
+-------------------------+
| reverse(double_col)     |
+-------------------------+
| 803e7513268431396797.1- |
+-------------------------+
1 row in set (0.00 sec)

mysql> set optimizer_switch="derived_merge=off";
Query OK, 0 rows affected (0.00 sec)

mysql> explain select * from (select reverse(double_col) from t1) as t;
+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table      | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+-------+
|  1 | PRIMARY     | <derived2> | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    2 |   100.00 | NULL  |
|  2 | DERIVED     | t1         | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    1 |   100.00 | NULL  |
+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+-------+
2 rows in set, 1 warning (0.00 sec)

mysql> select * from (select reverse(double_col) from t1) as t;
+------------------------+
| reverse(double_col)    |
+------------------------+
| 803e7513268431396797.1 |
+------------------------+
1 row in set (0.00 sec)
[16 Sep 2022 8:04] MySQL Verification Team
Hello ting du,

Thank you for the report and test case.

regards,
Umesh