Bug #117703 Incorrect Query Results When Using REPEAT (Branch Structure) in Subquery

Submitted: 14 Mar 2025 9:00 Modified: 14 Mar 2025 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 2025 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 2025 10:35] MySQL Verification Team
Hello zhiqiang cheng,

Thank you for the report and test case.

regards,
Umesh

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.040 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.