Details
-
Bug
-
Status: Closed (View Workflow)
-
Blocker
-
Resolution: Fixed
-
10.3(EOL), 10.4(EOL), 10.5, 10.6, 10.7(EOL)
Description
test case, that was reported in MDEV-27132 by mhadji@gmail.com
--source include/have_innodb.inc
|
|
CREATE TABLE raw_a ( |
date varchar(100), |
v varchar(100), |
c char(5), |
foo bigint(20) |
) ENGINE=InnoDB;
|
|
INSERT INTO raw_a VALUES ('20211231','A','bh',1),('20211231','B','bh',0),('20211231','C','bh',0),('20211231','D','bh',0),('20211231','E','bh',0),('20211231','G','bh',0),('20211231','H','bh',0),('20211231','I','bh',3),('20211231','J','bh',0),('20211231','K','bh',0),('20211231','A','bl',9),('20211231','B','bl',0),('20211231','C','bl',0),('20211231','D','bl',0),('20211231','E','bl',0),('20211231','G','bl',0),('20211231','H','bl',0),('20211231','I','bl',7),('20211231','J','bl',0),('20211231','K','bl',0),('20211231','M','bl',0),('20211231','N','bl',3),('20211231','R','bl',9),('20211231','S','bl',0),('20211231','T','bl',0),('20211231','U','bl',0),('20211231','V','bl',0); |
|
CREATE TABLE raw_b ( |
date int(11) NOT NULL, |
v varchar(50), |
t varchar(512), |
c varchar(50), |
o varchar(10), |
sh varchar(10), |
p char(5), |
s varchar(20), |
foo bigint(20), |
PRIMARY KEY (date,v,t,c,o,sh,p,s), |
KEY date (date,c,v) |
) ENGINE=InnoDB;
|
|
INSERT INTO raw_b VALUES (20211231,'A','a','bl','a','f','0','[0-5]',2),(20211231,'A','a','bl','a','f','0','[6-9)',2),(20211231,'A','b','bl','a','f','0','[0-5]',2),(20211231,'A','b','bl','a','f','0','[6-9)',2),(20211231,'A','c','bh','a','f','0','[0-5]',2),(20211231,'A','c','bh','a','f','0','[10-15)',2),(20211231,'A','c','bh','a','f','0','[15-20)',2),(20211231,'A','c','bh','a','f','0','[20-30)',2),(20211231,'A','c','bh','a','f','0','[6-9)',2),(20211231,'A','c','bl','a','f','0','[0-5]',2),(20211231,'A','c','bl','a','f','0','[20-30)',2),(20211231,'A','c','bl','a','f','0','[6-9)',2),(20211231,'A','cc','bl','a','f','0','[0-5]',2),(20211231,'A','cc','bl','a','f','0','[10-15)',2),(20211231,'A','cc','bl','a','f','0','[15-20)',2),(20211231,'A','cc','bl','a','f','0','[6-9)',2),(20211231,'A','cc','bl','a','LO','0','[0-5]',2),(20211231,'A','ccc','bl','a','f','0','[0-5]',2),(20211231,'A','d','bh','a','f','0','[0-5]',2),(20211231,'A','d','bh','a','f','0','[10-15)',2),(20211231,'A','d','bh','a','f','0','[20-30)',2),(20211231,'A','d','bh','a','f','0','[6-9)',2),(20211231,'A','e','bh','a','f','0','[0-5]',2),(20211231,'A','e','bh','a','f','0','[10-15)',2),(20211231,'A','e','bh','a','f','0','[20-30)',2),(20211231,'A','e','bh','a','f','0','[6-9)',2),(20211231,'A','f','bh','a','f','0','[6-9)',2),(20211231,'A','g','bh','a','f','0','[0-5]',2),(20211231,'A','g','bh','a','f','0','[10-15)',2),(20211231,'A','g','bh','a','f','0','[6-9)',2),(20211231,'A','h','bh','a','f','0','[0-5]',2),(20211231,'A','h','bh','a','f','0','[10-15)',2),(20211231,'A','h','bh','a','f','0','[15-20)',2),(20211231,'A','h','bh','a','f','0','[20-30)',2),(20211231,'A','h','bh','a','f','0','[30-inf)',2),(20211231,'A','h','bh','a','f','0','[6-9)',2),(20211231,'A','h','bh','a','f','1-4','[10-15)',2),(20211231,'A','h','bh','a','f','<0','[0-5]',2),(20211231,'A','i','bh','a','f','0','[0-5]',2),(20211231,'A','i','bh','a','f','0','[20-30)',2),(20211231,'A','i','bh','a','f','0','[6-9)',2),(20211231,'A','j','bh','a','f','0','[0-5]',2),(20211231,'A','j','bh','a','f','0','[6-9)',2),(20211231,'A','k','bh','a','f','0','[0-5]',2),(20211231,'A','k','bh','a','f','0','[10-15)',2),(20211231,'A','k','bh','a','f','0','[15-20)',2),(20211231,'A','k','bh','a','f','0','[20-30)',2),(20211231,'A','k','bh','a','f','0','[30-inf)',2),(20211231,'A','k','bh','a','f','0','[6-9)',2),(20211231,'A','l','bh','a','f','0','[0-5]',2),(20211231,'A','m','bh','a','f','0','[0-5]',2),(20211231,'A','m','bh','a','f','0','[10-15)',2),(20211231,'A','m','bh','a','f','0','[15-20)',2),(20211231,'A','m','bh','a','f','0','[20-30)',2),(20211231,'A','m','bh','a','f','0','[6-9)',2),(20211231,'A','m','bh','a','f','>4','[10-15)',2),(20211231,'A','n','bh','a','f','0','[0-5]',2),(20211231,'A','n','bl','a','f','0','[0-5]',2),(20211231,'A','o','bh','a','f','0','[0-5]',2),(20211231,'A','o','bh','a','f','0','[6-9)',2),(20211231,'A','p','bh','a','f','0','[0-5]',2),(20211231,'A','p','bh','a','f','0','[10-15)',2),(20211231,'A','p','bh','a','f','0','[15-20)',2),(20211231,'A','p','bh','a','f','0','[6-9)',2),(20211231,'A','p','bh','a','f','<0','[0-5]',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[0-5]',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[10-15)',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[15-20)',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[20-30)',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[30-inf)',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[6-9)',2); |
|
SELECT a.date, a.c, b.foo, a.foo |
FROM |
(SELECT date,c,SUM(foo) as foo |
FROM raw_b |
WHERE date >= 20211231 |
GROUP BY date,c |
) as b, |
(SELECT date, c, SUM(foo) as foo |
FROM raw_a |
WHERE date >= 20211231 |
GROUP BY date,c |
) as a where a.date = b.date AND a.c = b.c; |
|
set optimizer_switch="split_materialized=off"; |
|
SELECT a.date, a.c, b.foo, a.foo |
FROM |
(SELECT date,c,SUM(foo) as foo |
FROM raw_b |
WHERE date >= 20211231 |
GROUP BY date,c |
) as b, |
(SELECT date, c, SUM(foo) as foo |
FROM raw_a |
WHERE date >= 20211231 |
GROUP BY date,c |
) as a where a.date = b.date AND a.c = b.c; |
MariaDB [test]> SELECT a.date, a.c, b.foo, a.foo FROM (SELECT date,c,SUM(foo) as foo FROM raw_b WHERE date >= 20211231 GROUP BY date,c ) as b, (SELECT date, c, SUM(foo) as foo FROM raw_a WHERE date >= 20211231 GROUP BY date,c ) as a where a.date = b.date AND a.c = b.c;
|
+----------+------+------+------+
|
| date | c | foo | foo |
|
+----------+------+------+------+
|
| 20211231 | bl | 142 | 28 |
|
+----------+------+------+------+
|
1 row in set (0.016 sec)
|
|
MariaDB [test]> set optimizer_switch="split_materialized=off";
|
Query OK, 0 rows affected (0.001 sec)
|
|
MariaDB [test]> SELECT a.date, a.c, b.foo, a.foo FROM (SELECT date,c,SUM(foo) as foo FROM raw_b WHERE date >= 20211231 GROUP BY date,c ) as b, (SELECT date, c, SUM(foo) as foo FROM raw_a WHERE date >= 20211231 GROUP BY date,c ) as a where a.date = b.date AND a.c = b.c;
|
+----------+------+------+------+
|
| date | c | foo | foo |
|
+----------+------+------+------+
|
| 20211231 | bh | 114 | 4 |
|
| 20211231 | bl | 28 | 28 |
|
+----------+------+------+------+
|
2 rows in set (0.008 sec)
|
MariaDB [test]> explain extended SELECT a.date, a.c, b.foo, a.foo FROM (SELECT date,c,SUM(foo) as foo FROM raw_b WHERE date >= 20211231 GROUP BY date,c ) as b, (SELECT date, c, SUM(foo) as foo FROM raw_a WHERE date >= 20211231 GROUP BY date,c ) as a where a.date = b.date AND a.c = b.c;
|
+------+-----------------+------------+------+---------------+---------+---------+------------+------+----------+----------------------------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|
+------+-----------------+------------+------+---------------+---------+---------+------------+------+----------+----------------------------------------------+
|
| 1 | PRIMARY | <derived3> | ALL | NULL | NULL | NULL | NULL | 27 | 100.00 | Using where |
|
| 1 | PRIMARY | <derived2> | ref | key0 | key0 | 56 | a.date,a.c | 2 | 100.00 | Using where |
|
| 3 | DERIVED | raw_a | ALL | NULL | NULL | NULL | NULL | 27 | 100.00 | Using where; Using temporary; Using filesort |
|
| 2 | LATERAL DERIVED | raw_b | ref | PRIMARY,date | PRIMARY | 4 | a.date | 1 | 100.00 | Using where; Using temporary; Using filesort |
|
+------+-----------------+------------+------+---------------+---------+---------+------------+------+----------+----------------------------------------------+
|
4 rows in set, 1 warning (0.005 sec)
|
|
Note (Code 1003): /* select#1 */ select `a`.`date` AS `date`,`a`.`c` AS `c`,`b`.`foo` AS `foo`,`a`.`foo` AS `foo` from (/* select#2 */ select `test`.`raw_b`.`date` AS `date`,`test`.`raw_b`.`c` AS `c`,sum(`test`.`raw_b`.`foo`) AS `foo` from `test`.`raw_b` where `test`.`raw_b`.`date` >= 20211231 and `test`.`raw_b`.`date` = `a`.`date` and `test`.`raw_b`.`c` = `a`.`c` group by `test`.`raw_b`.`date`) `b` join (/* select#3 */ select `test`.`raw_a`.`date` AS `date`,`test`.`raw_a`.`c` AS `c`,sum(`test`.`raw_a`.`foo`) AS `foo` from `test`.`raw_a` where `test`.`raw_a`.`date` >= 20211231 group by `test`.`raw_a`.`date`,`test`.`raw_a`.`c`) `a` where `a`.`date` = `b`.`date` and `a`.`c` = `b`.`c`
|
Attachments
Issue Links
- causes
-
MDEV-32784 Perfromance Degradation of Joins on VIEWs after v10.4.22
-
- Confirmed
-
- duplicates
-
MDEV-27694 regression? Join using derived with aggregation returns incorrect results
-
- Closed
-
- is duplicated by
-
MDEV-26749 Optimizer_switch split_materialized Returns Wrong Results
-
- Closed
-
- relates to
-
MDEV-27132 Wrong result from query when using split optimization
-
- Closed
-
-
MDEV-26965 Left join to derived table on multiple clauses with aggregation returns missing/incorrect results
-
- Closed
-
Activity
Field | Original Value | New Value |
---|---|---|
Link |
This issue relates to |
Assignee | Igor Babaev [ igor ] |
Status | Open [ 1 ] | In Progress [ 3 ] |
Description |
test case, that was reported in {code:sql} --source include/have_innodb.inc CREATE TABLE raw_a ( date varchar(100), v varchar(100), c char(5), foo bigint(20) ) ENGINE=InnoDB; INSERT INTO raw_a VALUES ('20211231','A','bh',1),('20211231','B','bh',0),('20211231','C','bh',0),('20211231','D','bh',0),('20211231','E','bh',0),('20211231','G','bh',0),('20211231','H','bh',0),('20211231','I','bh',3),('20211231','J','bh',0),('20211231','K','bh',0),('20211231','A','bl',9),('20211231','B','bl',0),('20211231','C','bl',0),('20211231','D','bl',0),('20211231','E','bl',0),('20211231','G','bl',0),('20211231','H','bl',0),('20211231','I','bl',7),('20211231','J','bl',0),('20211231','K','bl',0),('20211231','M','bl',0),('20211231','N','bl',3),('20211231','R','bl',9),('20211231','S','bl',0),('20211231','T','bl',0),('20211231','U','bl',0),('20211231','V','bl',0); CREATE TABLE raw_b ( date int(11) NOT NULL, v varchar(50), t varchar(512), c varchar(50), o varchar(10), sh varchar(10), p char(5), s varchar(20), foo bigint(20), PRIMARY KEY (date,v,t,c,o,sh,p,s), KEY date (date,c,v) ) ENGINE=InnoDB; INSERT INTO raw_b VALUES (20211231,'A','a','bl','a','f','0','[0-5]',2),(20211231,'A','a','bl','a','f','0','[6-9)',2),(20211231,'A','b','bl','a','f','0','[0-5]',2),(20211231,'A','b','bl','a','f','0','[6-9)',2),(20211231,'A','c','bh','a','f','0','[0-5]',2),(20211231,'A','c','bh','a','f','0','[10-15)',2),(20211231,'A','c','bh','a','f','0','[15-20)',2),(20211231,'A','c','bh','a','f','0','[20-30)',2),(20211231,'A','c','bh','a','f','0','[6-9)',2),(20211231,'A','c','bl','a','f','0','[0-5]',2),(20211231,'A','c','bl','a','f','0','[20-30)',2),(20211231,'A','c','bl','a','f','0','[6-9)',2),(20211231,'A','cc','bl','a','f','0','[0-5]',2),(20211231,'A','cc','bl','a','f','0','[10-15)',2),(20211231,'A','cc','bl','a','f','0','[15-20)',2),(20211231,'A','cc','bl','a','f','0','[6-9)',2),(20211231,'A','cc','bl','a','LO','0','[0-5]',2),(20211231,'A','ccc','bl','a','f','0','[0-5]',2),(20211231,'A','d','bh','a','f','0','[0-5]',2),(20211231,'A','d','bh','a','f','0','[10-15)',2),(20211231,'A','d','bh','a','f','0','[20-30)',2),(20211231,'A','d','bh','a','f','0','[6-9)',2),(20211231,'A','e','bh','a','f','0','[0-5]',2),(20211231,'A','e','bh','a','f','0','[10-15)',2),(20211231,'A','e','bh','a','f','0','[20-30)',2),(20211231,'A','e','bh','a','f','0','[6-9)',2),(20211231,'A','f','bh','a','f','0','[6-9)',2),(20211231,'A','g','bh','a','f','0','[0-5]',2),(20211231,'A','g','bh','a','f','0','[10-15)',2),(20211231,'A','g','bh','a','f','0','[6-9)',2),(20211231,'A','h','bh','a','f','0','[0-5]',2),(20211231,'A','h','bh','a','f','0','[10-15)',2),(20211231,'A','h','bh','a','f','0','[15-20)',2),(20211231,'A','h','bh','a','f','0','[20-30)',2),(20211231,'A','h','bh','a','f','0','[30-inf)',2),(20211231,'A','h','bh','a','f','0','[6-9)',2),(20211231,'A','h','bh','a','f','1-4','[10-15)',2),(20211231,'A','h','bh','a','f','<0','[0-5]',2),(20211231,'A','i','bh','a','f','0','[0-5]',2),(20211231,'A','i','bh','a','f','0','[20-30)',2),(20211231,'A','i','bh','a','f','0','[6-9)',2),(20211231,'A','j','bh','a','f','0','[0-5]',2),(20211231,'A','j','bh','a','f','0','[6-9)',2),(20211231,'A','k','bh','a','f','0','[0-5]',2),(20211231,'A','k','bh','a','f','0','[10-15)',2),(20211231,'A','k','bh','a','f','0','[15-20)',2),(20211231,'A','k','bh','a','f','0','[20-30)',2),(20211231,'A','k','bh','a','f','0','[30-inf)',2),(20211231,'A','k','bh','a','f','0','[6-9)',2),(20211231,'A','l','bh','a','f','0','[0-5]',2),(20211231,'A','m','bh','a','f','0','[0-5]',2),(20211231,'A','m','bh','a','f','0','[10-15)',2),(20211231,'A','m','bh','a','f','0','[15-20)',2),(20211231,'A','m','bh','a','f','0','[20-30)',2),(20211231,'A','m','bh','a','f','0','[6-9)',2),(20211231,'A','m','bh','a','f','>4','[10-15)',2),(20211231,'A','n','bh','a','f','0','[0-5]',2),(20211231,'A','n','bl','a','f','0','[0-5]',2),(20211231,'A','o','bh','a','f','0','[0-5]',2),(20211231,'A','o','bh','a','f','0','[6-9)',2),(20211231,'A','p','bh','a','f','0','[0-5]',2),(20211231,'A','p','bh','a','f','0','[10-15)',2),(20211231,'A','p','bh','a','f','0','[15-20)',2),(20211231,'A','p','bh','a','f','0','[6-9)',2),(20211231,'A','p','bh','a','f','<0','[0-5]',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[0-5]',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[10-15)',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[15-20)',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[20-30)',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[30-inf)',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[6-9)',2); SELECT a.date, a.c, b.foo, a.foo FROM (SELECT date,c,SUM(foo) as foo FROM raw_b WHERE date >= 20211231 GROUP BY date,c ) as b, (SELECT date, c, SUM(foo) as foo FROM raw_a WHERE date >= 20211231 GROUP BY date,c ) as a where a.date = b.date AND a.c = b.c; set optimizer_switch="split_materialized=off"; SELECT a.date, a.c, b.foo, a.foo FROM (SELECT date,c,SUM(foo) as foo FROM raw_b WHERE date >= 20211231 GROUP BY date,c ) as b, (SELECT date, c, SUM(foo) as foo FROM raw_a WHERE date >= 20211231 GROUP BY date,c ) as a where a.date = b.date AND a.c = b.c; {code} {noformat} MariaDB [test]> SELECT a.date, a.c, b.foo, a.foo FROM (SELECT date,c,SUM(foo) as foo FROM raw_b WHERE date >= 20211231 GROUP BY date,c ) as b, (SELECT date, c, SUM(foo) as foo FROM raw_a WHERE date >= 20211231 GROUP BY date,c ) as a where a.date = b.date AND a.c = b.c; +----------+------+------+------+ | date | c | foo | foo | +----------+------+------+------+ | 20211231 | bl | 142 | 28 | +----------+------+------+------+ 1 row in set (0.016 sec) MariaDB [test]> set optimizer_switch="split_materialized=off"; Query OK, 0 rows affected (0.001 sec) MariaDB [test]> SELECT a.date, a.c, b.foo, a.foo FROM (SELECT date,c,SUM(foo) as foo FROM raw_b WHERE date >= 20211231 GROUP BY date,c ) as b, (SELECT date, c, SUM(foo) as foo FROM raw_a WHERE date >= 20211231 GROUP BY date,c ) as a where a.date = b.date AND a.c = b.c; +----------+------+------+------+ | date | c | foo | foo | +----------+------+------+------+ | 20211231 | bh | 114 | 4 | | 20211231 | bl | 28 | 28 | +----------+------+------+------+ 2 rows in set (0.008 sec) {noformat} {noformat} MariaDB [test]> explain extended SELECT a.date, a.c, b.foo, a.foo FROM (SELECT date,c,SUM(foo) as foo FROM raw_b WHERE date >= 20211231 GROUP BY date,c ) as b, (SELECT date, c, SUM(foo) as foo FROM raw_a WHERE date >= 20211231 GROUP BY date,c ) as a where a.date = b.date AND a.c = b.c; +------+-----------------+------------+------+---------------+---------+---------+------------+------+----------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +------+-----------------+------------+------+---------------+---------+---------+------------+------+----------+----------------------------------------------+ | 1 | PRIMARY | <derived3> | ALL | NULL | NULL | NULL | NULL | 27 | 100.00 | Using where | | 1 | PRIMARY | <derived2> | ref | key0 | key0 | 56 | a.date,a.c | 2 | 100.00 | Using where | | 3 | DERIVED | raw_a | ALL | NULL | NULL | NULL | NULL | 27 | 100.00 | Using where; Using temporary; Using filesort | | 2 | LATERAL DERIVED | raw_b | ref | PRIMARY,date | PRIMARY | 4 | a.date | 1 | 100.00 | Using where; Using temporary; Using filesort | +------+-----------------+------------+------+---------------+---------+---------+------------+------+----------+----------------------------------------------+ 4 rows in set, 1 warning (0.005 sec) Note (Code 1003): /* select#1 */ select `a`.`date` AS `date`,`a`.`c` AS `c`,`b`.`foo` AS `foo`,`a`.`foo` AS `foo` from (/* select#2 */ select `test`.`raw_b`.`date` AS `date`,`test`.`raw_b`.`c` AS `c`,sum(`test`.`raw_b`.`foo`) AS `foo` from `test`.`raw_b` where `test`.`raw_b`.`date` >= 20211231 and `test`.`raw_b`.`date` = `a`.`date` and `test`.`raw_b`.`c` = `a`.`c` group by `test`.`raw_b`.`date`) `b` join (/* select#3 */ select `test`.`raw_a`.`date` AS `date`,`test`.`raw_a`.`c` AS `c`,sum(`test`.`raw_a`.`foo`) AS `foo` from `test`.`raw_a` where `test`.`raw_a`.`date` >= 20211231 group by `test`.`raw_a`.`date`,`test`.`raw_a`.`c`) `a` where `a`.`date` = `b`.`date` and `a`.`c` = `b`.`c` {noformat} |
test case, that was reported in {code:sql} --source include/have_innodb.inc CREATE TABLE raw_a ( date varchar(100), v varchar(100), c char(5), foo bigint(20) ) ENGINE=InnoDB; INSERT INTO raw_a VALUES ('20211231','A','bh',1),('20211231','B','bh',0),('20211231','C','bh',0),('20211231','D','bh',0),('20211231','E','bh',0),('20211231','G','bh',0),('20211231','H','bh',0),('20211231','I','bh',3),('20211231','J','bh',0),('20211231','K','bh',0),('20211231','A','bl',9),('20211231','B','bl',0),('20211231','C','bl',0),('20211231','D','bl',0),('20211231','E','bl',0),('20211231','G','bl',0),('20211231','H','bl',0),('20211231','I','bl',7),('20211231','J','bl',0),('20211231','K','bl',0),('20211231','M','bl',0),('20211231','N','bl',3),('20211231','R','bl',9),('20211231','S','bl',0),('20211231','T','bl',0),('20211231','U','bl',0),('20211231','V','bl',0); CREATE TABLE raw_b ( date int(11) NOT NULL, v varchar(50), t varchar(512), c varchar(50), o varchar(10), sh varchar(10), p char(5), s varchar(20), foo bigint(20), PRIMARY KEY (date,v,t,c,o,sh,p,s), KEY date (date,c,v) ) ENGINE=InnoDB; INSERT INTO raw_b VALUES (20211231,'A','a','bl','a','f','0','[0-5]',2),(20211231,'A','a','bl','a','f','0','[6-9)',2),(20211231,'A','b','bl','a','f','0','[0-5]',2),(20211231,'A','b','bl','a','f','0','[6-9)',2),(20211231,'A','c','bh','a','f','0','[0-5]',2),(20211231,'A','c','bh','a','f','0','[10-15)',2),(20211231,'A','c','bh','a','f','0','[15-20)',2),(20211231,'A','c','bh','a','f','0','[20-30)',2),(20211231,'A','c','bh','a','f','0','[6-9)',2),(20211231,'A','c','bl','a','f','0','[0-5]',2),(20211231,'A','c','bl','a','f','0','[20-30)',2),(20211231,'A','c','bl','a','f','0','[6-9)',2),(20211231,'A','cc','bl','a','f','0','[0-5]',2),(20211231,'A','cc','bl','a','f','0','[10-15)',2),(20211231,'A','cc','bl','a','f','0','[15-20)',2),(20211231,'A','cc','bl','a','f','0','[6-9)',2),(20211231,'A','cc','bl','a','LO','0','[0-5]',2),(20211231,'A','ccc','bl','a','f','0','[0-5]',2),(20211231,'A','d','bh','a','f','0','[0-5]',2),(20211231,'A','d','bh','a','f','0','[10-15)',2),(20211231,'A','d','bh','a','f','0','[20-30)',2),(20211231,'A','d','bh','a','f','0','[6-9)',2),(20211231,'A','e','bh','a','f','0','[0-5]',2),(20211231,'A','e','bh','a','f','0','[10-15)',2),(20211231,'A','e','bh','a','f','0','[20-30)',2),(20211231,'A','e','bh','a','f','0','[6-9)',2),(20211231,'A','f','bh','a','f','0','[6-9)',2),(20211231,'A','g','bh','a','f','0','[0-5]',2),(20211231,'A','g','bh','a','f','0','[10-15)',2),(20211231,'A','g','bh','a','f','0','[6-9)',2),(20211231,'A','h','bh','a','f','0','[0-5]',2),(20211231,'A','h','bh','a','f','0','[10-15)',2),(20211231,'A','h','bh','a','f','0','[15-20)',2),(20211231,'A','h','bh','a','f','0','[20-30)',2),(20211231,'A','h','bh','a','f','0','[30-inf)',2),(20211231,'A','h','bh','a','f','0','[6-9)',2),(20211231,'A','h','bh','a','f','1-4','[10-15)',2),(20211231,'A','h','bh','a','f','<0','[0-5]',2),(20211231,'A','i','bh','a','f','0','[0-5]',2),(20211231,'A','i','bh','a','f','0','[20-30)',2),(20211231,'A','i','bh','a','f','0','[6-9)',2),(20211231,'A','j','bh','a','f','0','[0-5]',2),(20211231,'A','j','bh','a','f','0','[6-9)',2),(20211231,'A','k','bh','a','f','0','[0-5]',2),(20211231,'A','k','bh','a','f','0','[10-15)',2),(20211231,'A','k','bh','a','f','0','[15-20)',2),(20211231,'A','k','bh','a','f','0','[20-30)',2),(20211231,'A','k','bh','a','f','0','[30-inf)',2),(20211231,'A','k','bh','a','f','0','[6-9)',2),(20211231,'A','l','bh','a','f','0','[0-5]',2),(20211231,'A','m','bh','a','f','0','[0-5]',2),(20211231,'A','m','bh','a','f','0','[10-15)',2),(20211231,'A','m','bh','a','f','0','[15-20)',2),(20211231,'A','m','bh','a','f','0','[20-30)',2),(20211231,'A','m','bh','a','f','0','[6-9)',2),(20211231,'A','m','bh','a','f','>4','[10-15)',2),(20211231,'A','n','bh','a','f','0','[0-5]',2),(20211231,'A','n','bl','a','f','0','[0-5]',2),(20211231,'A','o','bh','a','f','0','[0-5]',2),(20211231,'A','o','bh','a','f','0','[6-9)',2),(20211231,'A','p','bh','a','f','0','[0-5]',2),(20211231,'A','p','bh','a','f','0','[10-15)',2),(20211231,'A','p','bh','a','f','0','[15-20)',2),(20211231,'A','p','bh','a','f','0','[6-9)',2),(20211231,'A','p','bh','a','f','<0','[0-5]',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[0-5]',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[10-15)',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[15-20)',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[20-30)',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[30-inf)',2),(20211231,'Afdaskjlhfaslkjfhasdklfhdskla','j','bh','a','f','0','[6-9)',2); SELECT a.date, a.c, b.foo, a.foo FROM (SELECT date,c,SUM(foo) as foo FROM raw_b WHERE date >= 20211231 GROUP BY date,c ) as b, (SELECT date, c, SUM(foo) as foo FROM raw_a WHERE date >= 20211231 GROUP BY date,c ) as a where a.date = b.date AND a.c = b.c; set optimizer_switch="split_materialized=off"; SELECT a.date, a.c, b.foo, a.foo FROM (SELECT date,c,SUM(foo) as foo FROM raw_b WHERE date >= 20211231 GROUP BY date,c ) as b, (SELECT date, c, SUM(foo) as foo FROM raw_a WHERE date >= 20211231 GROUP BY date,c ) as a where a.date = b.date AND a.c = b.c; {code} {noformat} MariaDB [test]> SELECT a.date, a.c, b.foo, a.foo FROM (SELECT date,c,SUM(foo) as foo FROM raw_b WHERE date >= 20211231 GROUP BY date,c ) as b, (SELECT date, c, SUM(foo) as foo FROM raw_a WHERE date >= 20211231 GROUP BY date,c ) as a where a.date = b.date AND a.c = b.c; +----------+------+------+------+ | date | c | foo | foo | +----------+------+------+------+ | 20211231 | bl | 142 | 28 | +----------+------+------+------+ 1 row in set (0.016 sec) MariaDB [test]> set optimizer_switch="split_materialized=off"; Query OK, 0 rows affected (0.001 sec) MariaDB [test]> SELECT a.date, a.c, b.foo, a.foo FROM (SELECT date,c,SUM(foo) as foo FROM raw_b WHERE date >= 20211231 GROUP BY date,c ) as b, (SELECT date, c, SUM(foo) as foo FROM raw_a WHERE date >= 20211231 GROUP BY date,c ) as a where a.date = b.date AND a.c = b.c; +----------+------+------+------+ | date | c | foo | foo | +----------+------+------+------+ | 20211231 | bh | 114 | 4 | | 20211231 | bl | 28 | 28 | +----------+------+------+------+ 2 rows in set (0.008 sec) {noformat} {noformat} MariaDB [test]> explain extended SELECT a.date, a.c, b.foo, a.foo FROM (SELECT date,c,SUM(foo) as foo FROM raw_b WHERE date >= 20211231 GROUP BY date,c ) as b, (SELECT date, c, SUM(foo) as foo FROM raw_a WHERE date >= 20211231 GROUP BY date,c ) as a where a.date = b.date AND a.c = b.c; +------+-----------------+------------+------+---------------+---------+---------+------------+------+----------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +------+-----------------+------------+------+---------------+---------+---------+------------+------+----------+----------------------------------------------+ | 1 | PRIMARY | <derived3> | ALL | NULL | NULL | NULL | NULL | 27 | 100.00 | Using where | | 1 | PRIMARY | <derived2> | ref | key0 | key0 | 56 | a.date,a.c | 2 | 100.00 | Using where | | 3 | DERIVED | raw_a | ALL | NULL | NULL | NULL | NULL | 27 | 100.00 | Using where; Using temporary; Using filesort | | 2 | LATERAL DERIVED | raw_b | ref | PRIMARY,date | PRIMARY | 4 | a.date | 1 | 100.00 | Using where; Using temporary; Using filesort | +------+-----------------+------------+------+---------------+---------+---------+------------+------+----------+----------------------------------------------+ 4 rows in set, 1 warning (0.005 sec) Note (Code 1003): /* select#1 */ select `a`.`date` AS `date`,`a`.`c` AS `c`,`b`.`foo` AS `foo`,`a`.`foo` AS `foo` from (/* select#2 */ select `test`.`raw_b`.`date` AS `date`,`test`.`raw_b`.`c` AS `c`,sum(`test`.`raw_b`.`foo`) AS `foo` from `test`.`raw_b` where `test`.`raw_b`.`date` >= 20211231 and `test`.`raw_b`.`date` = `a`.`date` and `test`.`raw_b`.`c` = `a`.`c` group by `test`.`raw_b`.`date`) `b` join (/* select#3 */ select `test`.`raw_a`.`date` AS `date`,`test`.`raw_a`.`c` AS `c`,sum(`test`.`raw_a`.`foo`) AS `foo` from `test`.`raw_a` where `test`.`raw_a`.`date` >= 20211231 group by `test`.`raw_a`.`date`,`test`.`raw_a`.`c`) `a` where `a`.`date` = `b`.`date` and `a`.`c` = `b`.`c` {noformat} |
Labels | regression | |
Priority | Major [ 3 ] | Blocker [ 1 ] |
Summary | Wrong result from query when using split optimization | Query return wrong result when using split optimization |
Summary | Query return wrong result when using split optimization | Query returns wrong result when using split optimization |
Status | In Progress [ 3 ] | In Testing [ 10301 ] |
Assignee | Igor Babaev [ igor ] | Oleksandr Byelkin [ sanja ] |
Status | In Testing [ 10301 ] | Stalled [ 10000 ] |
Status | Stalled [ 10000 ] | In Testing [ 10301 ] |
Status | In Testing [ 10301 ] | Stalled [ 10000 ] |
Status | Stalled [ 10000 ] | In Review [ 10002 ] |
Assignee | Oleksandr Byelkin [ sanja ] | Igor Babaev [ igor ] |
Status | In Review [ 10002 ] | Stalled [ 10000 ] |
Fix Version/s | 10.3.33 [ 26805 ] | |
Fix Version/s | 10.4.23 [ 26807 ] | |
Fix Version/s | 10.5.14 [ 26809 ] | |
Fix Version/s | 10.6.6 [ 26811 ] | |
Fix Version/s | 10.7.2 [ 26813 ] | |
Fix Version/s | 10.3 [ 22126 ] | |
Fix Version/s | 10.4 [ 22408 ] | |
Fix Version/s | 10.5 [ 23123 ] | |
Fix Version/s | 10.6 [ 24028 ] | |
Fix Version/s | 10.7 [ 24805 ] | |
Resolution | Fixed [ 1 ] | |
Status | Stalled [ 10000 ] | Closed [ 6 ] |
Link |
This issue duplicates |
Link |
This issue relates to |
Link | This issue causes MDEV-32784 [ MDEV-32784 ] |
Link |
This issue is duplicated by |
This comment was originally added by dan.howard in the entry for the closed bug
MDEV-25714@Igor Babaev
On version 10.5.13, the test script below reliably exhibits the bug. I run the setup script to create the tables and populate them with some dummy data. The query below counts the number of transaction_item rows for each (ledger_id, charge_id) pair. I've been careful in my test data to ensure that there is only ever 1 transaction_item row for each (ledger_id, charge_id) pair. Usually when I first run the query, I get the correct results (this is obvious because we see from_num_rows=1 on every row in the results set). After a short time (less than 1 minute for me), I start getting the incorrect results, and we see from_num_rows=2 on every row in the results set. I've captured below the output from EXPLAIN FORMAT=JSON for the same query, before and after it starts failing.
Note that that the number of rows of dummy data I have seems to be significant. The more rows I have, the faster the query starts giving incorrect results.
Setup:
);
);
);
);
The query:
SELECT
charges.id,
charges.from_ledger_id,
charges.to_ledger_id,
transactions.ledger_id,
transaction_items.charge_id,
EXPLAIN result when the query is returning correct results:
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "charges",
"access_type": "ALL",
"possible_keys": ["fk_charge_to_ledger"],
"rows": 20,
"filtered": 40,
"attached_condition": "charges.to_ledger_id = 2"
},
"table": {
"table_name": "<derived2>",
"access_type": "ref",
"possible_keys": ["key0"],
"key": "key0",
"key_length": "18",
"used_key_parts": ["ledger_id", "charge_id"],
"ref": ["bugtest.charges.from_ledger_id", "bugtest.charges.id"],
"rows": 4,
"filtered": 100,
"materialized": {
"query_block": {
"select_id": 2,
"filesort": {
"sort_key": "transactions.ledger_id, transaction_items.charge_id",
"temporary_table": {
"table": {
"table_name": "transaction_items",
"access_type": "ALL",
"possible_keys": ["fk_items_transaction", "fk_items_charge"],
"rows": 40,
"filtered": 100
},
"table": {
"table_name": "transactions",
"access_type": "eq_ref",
"possible_keys": ["PRIMARY", "fk_transactions_ledger"],
"key": "PRIMARY",
"key_length": "8",
"used_key_parts": ["id"],
"ref": ["bugtest.transaction_items.transaction_id"],
"rows": 1,
"filtered": 100
}
}
}
}
}
}
}
After a short time (less than 1 minute usually), the query will start returning the wrong results.
EXPLAIN result when the query is returning incorrect results:
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "charges",
"access_type": "ALL",
"possible_keys": ["fk_charge_to_ledger"],
"rows": 20,
"filtered": 35,
"attached_condition": "charges.to_ledger_id = 2"
},
"table": {
"table_name": "<derived2>",
"access_type": "ref",
"possible_keys": ["key0"],
"key": "key0",
"key_length": "18",
"used_key_parts": ["ledger_id", "charge_id"],
"ref": ["bugtest.charges.from_ledger_id", "bugtest.charges.id"],
"rows": 2,
"filtered": 100,
"materialized": {
"lateral": 1,
"query_block": {
"select_id": 2,
"table": {
"table_name": "transaction_items",
"access_type": "ref",
"possible_keys": ["fk_items_transaction", "fk_items_charge"],
"key": "fk_items_charge",
"key_length": "9",
"used_key_parts": ["charge_id"],
"ref": ["bugtest.charges.id"],
"rows": 1,
"filtered": 100
},
"table": {
"table_name": "transactions",
"access_type": "eq_ref",
"possible_keys": ["PRIMARY", "fk_transactions_ledger"],
"key": "PRIMARY",
"key_length": "8",
"used_key_parts": ["id"],
"ref": ["bugtest.transaction_items.transaction_id"],
"rows": 1,
"filtered": 100
}
}
}
}
}
}