Bug #117703 Incorrect Query Results When Using REPEAT (Branch Structure) in Subquery
Submitted: 14 Mar 9:00 Modified: 14 Mar 10:35
Reporter: zhiqiang cheng Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version: 9.1.0, 8.0.41, 8.4.4, 9.2.0 OS:Ubuntu (20.04)
Assigned to: CPU Architecture:Any

[14 Mar 9:00] zhiqiang cheng
Description:
When using REPEAT with a CASE statement inside a subquery, MySQL returns an incorrect result.

Expected behavior:

The output column c_1 should be equal to c_0, as the REPEAT function should generate a string of length c_0, and LENGTH(subq_0.c_1) should match c_0.

Actual behavior:

The returned value of LENGTH(subq_0.c_1) is incorrect and does not match c_0.

How to repeat:
Create Test Table and Insert Sample Data:
CREATE TABLE `t0` (
  `c1` int NOT NULL,
  `c2` int DEFAULT NULL,
  PRIMARY KEY (`c1`)
);
INSERT INTO `t0` VALUES (44,2);

Execute the Following Query:
select  
  subq_0.c_0 as c_0,
  length(subq_0.c_1) as c_1_len
from 
  (select  
        ref_0.c1 as c_0,
        REPEAT(case when (false) then ('w') else ('w') end,
          ref_0.c1) as c_1
      from 
        t0 as ref_0
      where (ref_0.c2) is not null) as subq_0
where (not ((subq_0.c_1) = ('abcd')));

Expected Result:
+-----+---------+
| c_0 | c_1_len |
+-----+---------+
|  44 |    44|
+-----+---------+
1 row in set (0.00 sec)
The output column c_1 should be equal to c_0, as the REPEAT function should generate a string of length c_0, and LENGTH(subq_0.c_1) should match c_0.

 Actual Result:
+-----+---------+
| c_0 | c_1_len |
+-----+---------+
|  44 |    1936 |
+-----+---------+
1 row in set (0.00 sec)
The returned value of LENGTH(subq_0.c_1) is incorrect and does not match c_0.
[14 Mar 10:35] MySQL Verification Team
Hello zhiqiang cheng,

Thank you for the report and test case.

regards,
Umesh