diff --git a/mysql-test/main/win.result b/mysql-test/main/win.result index 96b13010516..668f7b793cb 100644 --- a/mysql-test/main/win.result +++ b/mysql-test/main/win.result @@ -4391,3 +4391,177 @@ row_number() OVER (order by a) 2 3 drop table t1; +# +# MDEV-29307: join of 2 derived tables over the same grouping view such +# that the first of the joined tables contains a window +# function and the view's specification contains a subquery +# with a set function aggregated on the top level +# +CREATE TABLE t1 ( +tst int NOT NULL, +flat tinyint unsigned NOT NULL, +type tinyint unsigned NOT NULL, +val int NOT NULL, +PRIMARY KEY (tst,flat,type) +) ENGINE=ARIA; +INSERT INTO t1 VALUES +(5, 20, 2, 100), +(7, 20, 2, 150), +(9, 20, 1, 200); +CREATE VIEW v1 AS ( +SELECT +flat, +type, +( SELECT val FROM t1 sw +WHERE sw.tst = MAX(w.tst) AND sw.flat = w.flat AND sw.type = w.type) +AS total +FROM t1 w +GROUP BY flat, type +); +EXPLAIN EXTENDED SELECT w2.total AS w2_total, w1.total AS w1_total +FROM +( +SELECT flat, type, total +FROM v1 +WHERE type = 1 +) AS w1 +JOIN +( +SELECT flat, type, total +FROM v1 +WHERE type = 2 +) AS w2 +ON w1.flat = w2.flat; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY ALL NULL NULL NULL NULL 3 100.00 Using where +1 PRIMARY ref key0 key0 1 v1.flat 2 100.00 Using where +6 DERIVED w index NULL PRIMARY 6 NULL 3 100.00 Using where; Using index; Using temporary; Using filesort +7 DEPENDENT SUBQUERY sw eq_ref PRIMARY PRIMARY 6 func,func,func 1 100.00 Using index condition +4 DERIVED w index NULL PRIMARY 6 NULL 3 100.00 Using where; Using index; Using temporary; Using filesort +5 DEPENDENT SUBQUERY sw eq_ref PRIMARY PRIMARY 6 func,func,func 1 100.00 Using index condition +Warnings: +Note 1276 Field or reference 'test.w.tst' of SELECT #5 was resolved in SELECT #4 +Note 1981 Aggregate function 'max()' of SELECT #5 belongs to SELECT #4 +Note 1276 Field or reference 'test.w.flat' of SELECT #5 was resolved in SELECT #4 +Note 1276 Field or reference 'test.w.type' of SELECT #5 was resolved in SELECT #4 +Note 1276 Field or reference 'test.w.tst' of SELECT #7 was resolved in SELECT #6 +Note 1981 Aggregate function 'max()' of SELECT #7 belongs to SELECT #6 +Note 1276 Field or reference 'test.w.flat' of SELECT #7 was resolved in SELECT #6 +Note 1276 Field or reference 'test.w.type' of SELECT #7 was resolved in SELECT #6 +Note 1003 /* select#1 */ select `v1`.`total` AS `w2_total`,`v1`.`total` AS `w1_total` from `test`.`v1` join `test`.`v1` where `v1`.`flat` = `v1`.`flat` and `v1`.`type` = 2 and `v1`.`type` = 1 +SELECT w2.total AS w2_total, w1.total AS w1_total +FROM +( +SELECT flat, type, total +FROM v1 +WHERE type = 1 +) AS w1 +JOIN +( +SELECT flat, type, total +FROM v1 +WHERE type = 2 +) AS w2 +ON w1.flat = w2.flat; +w2_total w1_total +150 200 +EXPLAIN EXTENDED SELECT w2.total AS w2_total, w1.total AS w1_total +FROM +( +SELECT flat, type, total, +COUNT(total) OVER (PARTITION BY type ORDER BY type) AS u +FROM v1 +WHERE type = 1 +) AS w1 +JOIN +( +SELECT flat, type, total +FROM v1 +WHERE type = 2 +) AS w2 +ON w1.flat = w2.flat; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY ALL NULL NULL NULL NULL 3 100.00 Using where +1 PRIMARY ref key0 key0 1 w1.flat 2 100.00 Using where +6 DERIVED w index NULL PRIMARY 6 NULL 3 100.00 Using where; Using index; Using temporary; Using filesort +7 DEPENDENT SUBQUERY sw eq_ref PRIMARY PRIMARY 6 func,func,func 1 100.00 Using index condition +2 DERIVED ALL NULL NULL NULL NULL 3 100.00 Using where; Using temporary +4 DERIVED w index NULL PRIMARY 6 NULL 3 100.00 Using where; Using index; Using temporary; Using filesort +5 DEPENDENT SUBQUERY sw eq_ref PRIMARY PRIMARY 6 func,func,func 1 100.00 Using index condition +Warnings: +Note 1276 Field or reference 'test.w.tst' of SELECT #5 was resolved in SELECT #4 +Note 1981 Aggregate function 'max()' of SELECT #5 belongs to SELECT #4 +Note 1276 Field or reference 'test.w.flat' of SELECT #5 was resolved in SELECT #4 +Note 1276 Field or reference 'test.w.type' of SELECT #5 was resolved in SELECT #4 +Note 1276 Field or reference 'test.w.tst' of SELECT #7 was resolved in SELECT #6 +Note 1981 Aggregate function 'max()' of SELECT #7 belongs to SELECT #6 +Note 1276 Field or reference 'test.w.flat' of SELECT #7 was resolved in SELECT #6 +Note 1276 Field or reference 'test.w.type' of SELECT #7 was resolved in SELECT #6 +Note 1003 /* select#1 */ select `v1`.`total` AS `w2_total`,`w1`.`total` AS `w1_total` from (/* select#2 */ select `v1`.`flat` AS `flat`,`v1`.`type` AS `type`,`v1`.`total` AS `total`,count(`v1`.`total`) over ( partition by `v1`.`type` order by `v1`.`type`) AS `u` from `test`.`v1` where `v1`.`type` = 1) `w1` join `test`.`v1` where `v1`.`flat` = `w1`.`flat` and `v1`.`type` = 2 +SELECT w2.total AS w2_total, w1.total AS w1_total +FROM +( +SELECT flat, type, total, +COUNT(total) OVER (PARTITION BY type ORDER BY type) AS u +FROM v1 +WHERE type = 1 +) AS w1 +JOIN +( +SELECT flat, type, total +FROM v1 +WHERE type = 2 +) AS w2 +ON w1.flat = w2.flat; +w2_total w1_total +150 200 +EXPLAIN EXTENDED SELECT w2.total AS w2_total, w1.total AS w1_total, u +FROM +( +SELECT flat, type, total, +COUNT(total) OVER (PARTITION BY flat ORDER BY flat) AS u +FROM v1 +) AS w1 +JOIN +( +SELECT flat, type, total +FROM v1 +) AS w2; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY ALL NULL NULL NULL NULL 3 100.00 +1 PRIMARY ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +6 DERIVED w index NULL PRIMARY 6 NULL 3 100.00 Using index; Using temporary; Using filesort +7 DEPENDENT SUBQUERY sw eq_ref PRIMARY PRIMARY 6 func,func,func 1 100.00 Using index condition +2 DERIVED ALL NULL NULL NULL NULL 3 100.00 Using temporary +4 DERIVED w index NULL PRIMARY 6 NULL 3 100.00 Using index; Using temporary; Using filesort +5 DEPENDENT SUBQUERY sw eq_ref PRIMARY PRIMARY 6 func,func,func 1 100.00 Using index condition +Warnings: +Note 1276 Field or reference 'test.w.tst' of SELECT #5 was resolved in SELECT #4 +Note 1981 Aggregate function 'max()' of SELECT #5 belongs to SELECT #4 +Note 1276 Field or reference 'test.w.flat' of SELECT #5 was resolved in SELECT #4 +Note 1276 Field or reference 'test.w.type' of SELECT #5 was resolved in SELECT #4 +Note 1276 Field or reference 'test.w.tst' of SELECT #7 was resolved in SELECT #6 +Note 1981 Aggregate function 'max()' of SELECT #7 belongs to SELECT #6 +Note 1276 Field or reference 'test.w.flat' of SELECT #7 was resolved in SELECT #6 +Note 1276 Field or reference 'test.w.type' of SELECT #7 was resolved in SELECT #6 +Note 1003 /* select#1 */ select `v1`.`total` AS `w2_total`,`w1`.`total` AS `w1_total`,`w1`.`u` AS `u` from (/* select#2 */ select `v1`.`flat` AS `flat`,`v1`.`type` AS `type`,`v1`.`total` AS `total`,count(`v1`.`total`) over ( partition by `v1`.`flat` order by `v1`.`flat`) AS `u` from `test`.`v1`) `w1` join `test`.`v1` +SELECT w2.total AS w2_total, w1.total AS w1_total, u +FROM +( +SELECT flat, type, total, +COUNT(total) OVER (PARTITION BY flat ORDER BY flat) AS u +FROM v1 +) AS w1 +JOIN +( +SELECT flat, type, total +FROM v1 +) AS w2; +w2_total w1_total u +150 150 2 +150 200 2 +200 150 2 +200 200 2 +DROP VIEW v1; +DROP TABLE t1; +# End of 10.5 tests diff --git a/mysql-test/main/win.test b/mysql-test/main/win.test index cba6b0fd7af..363bf8f8ad9 100644 --- a/mysql-test/main/win.test +++ b/mysql-test/main/win.test @@ -2873,3 +2873,96 @@ create table t1 (a int); insert into t1 values (1),(2),(3); SELECT row_number() OVER (order by a) FROM t1 order by NAME_CONST('myname',NULL); drop table t1; + +--echo # +--echo # MDEV-29307: join of 2 derived tables over the same grouping view such +--echo # that the first of the joined tables contains a window +--echo # function and the view's specification contains a subquery +--echo # with a set function aggregated on the top level +--echo # + +CREATE TABLE t1 ( + tst int NOT NULL, + flat tinyint unsigned NOT NULL, + type tinyint unsigned NOT NULL, + val int NOT NULL, + PRIMARY KEY (tst,flat,type) +) ENGINE=ARIA; + +INSERT INTO t1 VALUES +(5, 20, 2, 100), +(7, 20, 2, 150), +(9, 20, 1, 200); + +CREATE VIEW v1 AS ( + SELECT + flat, + type, + ( SELECT val FROM t1 sw + WHERE sw.tst = MAX(w.tst) AND sw.flat = w.flat AND sw.type = w.type) + AS total + FROM t1 w + GROUP BY flat, type +); + +let $q1= +SELECT w2.total AS w2_total, w1.total AS w1_total +FROM +( + SELECT flat, type, total + FROM v1 + WHERE type = 1 +) AS w1 +JOIN +( + SELECT flat, type, total + FROM v1 + WHERE type = 2 +) AS w2 +ON w1.flat = w2.flat; + +eval EXPLAIN EXTENDED $q1; +eval $q1; + +let $q2= +SELECT w2.total AS w2_total, w1.total AS w1_total +FROM +( + SELECT flat, type, total, + COUNT(total) OVER (PARTITION BY type ORDER BY type) AS u + FROM v1 + WHERE type = 1 +) AS w1 +JOIN +( + SELECT flat, type, total + FROM v1 + WHERE type = 2 +) AS w2 +ON w1.flat = w2.flat; + +eval EXPLAIN EXTENDED $q2; +eval $q2; + +let $q3= +SELECT w2.total AS w2_total, w1.total AS w1_total, u +FROM +( + SELECT flat, type, total, + COUNT(total) OVER (PARTITION BY flat ORDER BY flat) AS u + FROM v1 +) AS w1 +JOIN +( + SELECT flat, type, total + FROM v1 +) AS w2; + +eval EXPLAIN EXTENDED $q3; +--sorted_result +eval $q3; + +DROP VIEW v1; +DROP TABLE t1; + +--echo # End of 10.5 tests diff --git a/mysql-test/suite/encryption/r/tempfiles_encrypted.result b/mysql-test/suite/encryption/r/tempfiles_encrypted.result index 5b9dab62c01..b913b295ad3 100644 --- a/mysql-test/suite/encryption/r/tempfiles_encrypted.result +++ b/mysql-test/suite/encryption/r/tempfiles_encrypted.result @@ -4398,6 +4398,180 @@ row_number() OVER (order by a) 3 drop table t1; # +# MDEV-29307: join of 2 derived tables over the same grouping view such +# that the first of the joined tables contains a window +# function and the view's specification contains a subquery +# with a set function aggregated on the top level +# +CREATE TABLE t1 ( +tst int NOT NULL, +flat tinyint unsigned NOT NULL, +type tinyint unsigned NOT NULL, +val int NOT NULL, +PRIMARY KEY (tst,flat,type) +) ENGINE=ARIA; +INSERT INTO t1 VALUES +(5, 20, 2, 100), +(7, 20, 2, 150), +(9, 20, 1, 200); +CREATE VIEW v1 AS ( +SELECT +flat, +type, +( SELECT val FROM t1 sw +WHERE sw.tst = MAX(w.tst) AND sw.flat = w.flat AND sw.type = w.type) +AS total +FROM t1 w +GROUP BY flat, type +); +EXPLAIN EXTENDED SELECT w2.total AS w2_total, w1.total AS w1_total +FROM +( +SELECT flat, type, total +FROM v1 +WHERE type = 1 +) AS w1 +JOIN +( +SELECT flat, type, total +FROM v1 +WHERE type = 2 +) AS w2 +ON w1.flat = w2.flat; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY ALL NULL NULL NULL NULL 3 100.00 Using where +1 PRIMARY ref key0 key0 1 v1.flat 2 100.00 Using where +6 DERIVED w index NULL PRIMARY 6 NULL 3 100.00 Using where; Using index; Using temporary; Using filesort +7 DEPENDENT SUBQUERY sw eq_ref PRIMARY PRIMARY 6 func,func,func 1 100.00 Using index condition +4 DERIVED w index NULL PRIMARY 6 NULL 3 100.00 Using where; Using index; Using temporary; Using filesort +5 DEPENDENT SUBQUERY sw eq_ref PRIMARY PRIMARY 6 func,func,func 1 100.00 Using index condition +Warnings: +Note 1276 Field or reference 'test.w.tst' of SELECT #5 was resolved in SELECT #4 +Note 1981 Aggregate function 'max()' of SELECT #5 belongs to SELECT #4 +Note 1276 Field or reference 'test.w.flat' of SELECT #5 was resolved in SELECT #4 +Note 1276 Field or reference 'test.w.type' of SELECT #5 was resolved in SELECT #4 +Note 1276 Field or reference 'test.w.tst' of SELECT #7 was resolved in SELECT #6 +Note 1981 Aggregate function 'max()' of SELECT #7 belongs to SELECT #6 +Note 1276 Field or reference 'test.w.flat' of SELECT #7 was resolved in SELECT #6 +Note 1276 Field or reference 'test.w.type' of SELECT #7 was resolved in SELECT #6 +Note 1003 /* select#1 */ select `v1`.`total` AS `w2_total`,`v1`.`total` AS `w1_total` from `test`.`v1` join `test`.`v1` where `v1`.`flat` = `v1`.`flat` and `v1`.`type` = 2 and `v1`.`type` = 1 +SELECT w2.total AS w2_total, w1.total AS w1_total +FROM +( +SELECT flat, type, total +FROM v1 +WHERE type = 1 +) AS w1 +JOIN +( +SELECT flat, type, total +FROM v1 +WHERE type = 2 +) AS w2 +ON w1.flat = w2.flat; +w2_total w1_total +150 200 +EXPLAIN EXTENDED SELECT w2.total AS w2_total, w1.total AS w1_total +FROM +( +SELECT flat, type, total, +COUNT(total) OVER (PARTITION BY type ORDER BY type) AS u +FROM v1 +WHERE type = 1 +) AS w1 +JOIN +( +SELECT flat, type, total +FROM v1 +WHERE type = 2 +) AS w2 +ON w1.flat = w2.flat; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY ALL NULL NULL NULL NULL 3 100.00 Using where +1 PRIMARY ref key0 key0 1 w1.flat 2 100.00 Using where +6 DERIVED w index NULL PRIMARY 6 NULL 3 100.00 Using where; Using index; Using temporary; Using filesort +7 DEPENDENT SUBQUERY sw eq_ref PRIMARY PRIMARY 6 func,func,func 1 100.00 Using index condition +2 DERIVED ALL NULL NULL NULL NULL 3 100.00 Using where; Using temporary +4 DERIVED w index NULL PRIMARY 6 NULL 3 100.00 Using where; Using index; Using temporary; Using filesort +5 DEPENDENT SUBQUERY sw eq_ref PRIMARY PRIMARY 6 func,func,func 1 100.00 Using index condition +Warnings: +Note 1276 Field or reference 'test.w.tst' of SELECT #5 was resolved in SELECT #4 +Note 1981 Aggregate function 'max()' of SELECT #5 belongs to SELECT #4 +Note 1276 Field or reference 'test.w.flat' of SELECT #5 was resolved in SELECT #4 +Note 1276 Field or reference 'test.w.type' of SELECT #5 was resolved in SELECT #4 +Note 1276 Field or reference 'test.w.tst' of SELECT #7 was resolved in SELECT #6 +Note 1981 Aggregate function 'max()' of SELECT #7 belongs to SELECT #6 +Note 1276 Field or reference 'test.w.flat' of SELECT #7 was resolved in SELECT #6 +Note 1276 Field or reference 'test.w.type' of SELECT #7 was resolved in SELECT #6 +Note 1003 /* select#1 */ select `v1`.`total` AS `w2_total`,`w1`.`total` AS `w1_total` from (/* select#2 */ select `v1`.`flat` AS `flat`,`v1`.`type` AS `type`,`v1`.`total` AS `total`,count(`v1`.`total`) over ( partition by `v1`.`type` order by `v1`.`type`) AS `u` from `test`.`v1` where `v1`.`type` = 1) `w1` join `test`.`v1` where `v1`.`flat` = `w1`.`flat` and `v1`.`type` = 2 +SELECT w2.total AS w2_total, w1.total AS w1_total +FROM +( +SELECT flat, type, total, +COUNT(total) OVER (PARTITION BY type ORDER BY type) AS u +FROM v1 +WHERE type = 1 +) AS w1 +JOIN +( +SELECT flat, type, total +FROM v1 +WHERE type = 2 +) AS w2 +ON w1.flat = w2.flat; +w2_total w1_total +150 200 +EXPLAIN EXTENDED SELECT w2.total AS w2_total, w1.total AS w1_total, u +FROM +( +SELECT flat, type, total, +COUNT(total) OVER (PARTITION BY flat ORDER BY flat) AS u +FROM v1 +) AS w1 +JOIN +( +SELECT flat, type, total +FROM v1 +) AS w2; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY ALL NULL NULL NULL NULL 3 100.00 +1 PRIMARY ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +6 DERIVED w index NULL PRIMARY 6 NULL 3 100.00 Using index; Using temporary; Using filesort +7 DEPENDENT SUBQUERY sw eq_ref PRIMARY PRIMARY 6 func,func,func 1 100.00 Using index condition +2 DERIVED ALL NULL NULL NULL NULL 3 100.00 Using temporary +4 DERIVED w index NULL PRIMARY 6 NULL 3 100.00 Using index; Using temporary; Using filesort +5 DEPENDENT SUBQUERY sw eq_ref PRIMARY PRIMARY 6 func,func,func 1 100.00 Using index condition +Warnings: +Note 1276 Field or reference 'test.w.tst' of SELECT #5 was resolved in SELECT #4 +Note 1981 Aggregate function 'max()' of SELECT #5 belongs to SELECT #4 +Note 1276 Field or reference 'test.w.flat' of SELECT #5 was resolved in SELECT #4 +Note 1276 Field or reference 'test.w.type' of SELECT #5 was resolved in SELECT #4 +Note 1276 Field or reference 'test.w.tst' of SELECT #7 was resolved in SELECT #6 +Note 1981 Aggregate function 'max()' of SELECT #7 belongs to SELECT #6 +Note 1276 Field or reference 'test.w.flat' of SELECT #7 was resolved in SELECT #6 +Note 1276 Field or reference 'test.w.type' of SELECT #7 was resolved in SELECT #6 +Note 1003 /* select#1 */ select `v1`.`total` AS `w2_total`,`w1`.`total` AS `w1_total`,`w1`.`u` AS `u` from (/* select#2 */ select `v1`.`flat` AS `flat`,`v1`.`type` AS `type`,`v1`.`total` AS `total`,count(`v1`.`total`) over ( partition by `v1`.`flat` order by `v1`.`flat`) AS `u` from `test`.`v1`) `w1` join `test`.`v1` +SELECT w2.total AS w2_total, w1.total AS w1_total, u +FROM +( +SELECT flat, type, total, +COUNT(total) OVER (PARTITION BY flat ORDER BY flat) AS u +FROM v1 +) AS w1 +JOIN +( +SELECT flat, type, total +FROM v1 +) AS w2; +w2_total w1_total u +150 150 2 +150 200 2 +200 150 2 +200 200 2 +DROP VIEW v1; +DROP TABLE t1; +# End of 10.5 tests +# # MDEV-23867: select crash in compute_window_func # set @save_sort_buffer_size=@@sort_buffer_size; diff --git a/sql/item_sum.cc b/sql/item_sum.cc index 4cf403c1618..a7fae9ea4c0 100644 --- a/sql/item_sum.cc +++ b/sql/item_sum.cc @@ -177,7 +177,11 @@ bool Item_sum::check_sum_func(THD *thd, Item **ref) } if (window_func_sum_expr_flag) + { + thd->lex->in_sum_func= in_sum_func; return false; + } + /* The value of max_arg_level is updated if an argument of the set function contains a column reference resolved against a subquery whose level is