Skip to content

Commit fba385e

Browse files
committed
MDEV-9487: Server crashes in Time_and_counter_tracker::incr_loops with UNION in ALL subquery
Do not mark subquery as inexpensive when it is not optimized.
1 parent a02d402 commit fba385e

10 files changed

+103
-7
lines changed

mysql-test/r/derived_view.result

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
11011101
1 PRIMARY t3 system NULL NULL NULL NULL 1 100.00
11021102
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
11031103
Warnings:
1104-
Note 1003 select 6 AS `a`,5 AS `b` from `test`.`t3` where (not(<expr_cache><6,5>(<in_optimizer>((6,5),<exists>(select 7,5 having (trigcond(((<cache>(6) = 7) or isnull(7))) and trigcond(((<cache>(5) = 5) or isnull(5))) and trigcond(<is_not_null_test>(7)) and trigcond(<is_not_null_test>(5))))))))
1104+
Note 1003 select 6 AS `a`,5 AS `b` from `test`.`t3` where 1
11051105
SELECT t.a,t.b FROM t3 RIGHT JOIN ((SELECT * FROM t1) AS t, t2) ON t2.b != 0
11061106
WHERE (t.a,t.b) NOT IN (SELECT 7, 5);
11071107
a b
@@ -1115,7 +1115,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
11151115
1 PRIMARY t3 system NULL NULL NULL NULL 1 100.00
11161116
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
11171117
Warnings:
1118-
Note 1003 select 6 AS `a`,5 AS `b` from `test`.`t3` where (not(<expr_cache><6,5>(<in_optimizer>((6,5),<exists>(select 7,5 having (trigcond(((<cache>(6) = 7) or isnull(7))) and trigcond(((<cache>(5) = 5) or isnull(5))) and trigcond(<is_not_null_test>(7)) and trigcond(<is_not_null_test>(5))))))))
1118+
Note 1003 select 6 AS `a`,5 AS `b` from `test`.`t3` where 1
11191119
SELECT t.a,t.b FROM t3 RIGHT JOIN (v1 AS t, t2) ON t2.b != 0
11201120
WHERE (t.a,t.b) NOT IN (SELECT 7, 5);
11211121
a b
@@ -1129,7 +1129,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
11291129
1 PRIMARY t3 system NULL NULL NULL NULL 1 100.00
11301130
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
11311131
Warnings:
1132-
Note 1003 select 6 AS `a`,5 AS `b` from `test`.`t3` where (not(<expr_cache><6,5>(<in_optimizer>((6,5),<exists>(select 7,5 having (trigcond(((<cache>(6) = 7) or isnull(7))) and trigcond(((<cache>(5) = 5) or isnull(5))) and trigcond(<is_not_null_test>(7)) and trigcond(<is_not_null_test>(5))))))))
1132+
Note 1003 select 6 AS `a`,5 AS `b` from `test`.`t3` where 1
11331133
DROP VIEW v1;
11341134
DROP TABLE t1,t2,t3;
11351135
#

mysql-test/r/subselect.result

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7147,3 +7147,15 @@ sq
71477147
NULL
71487148
drop view v2;
71497149
drop table t1,t2;
7150+
#
7151+
# MDEV-9487: Server crashes in Time_and_counter_tracker::incr_loops
7152+
# with UNION in ALL subquery
7153+
#
7154+
SET NAMES utf8;
7155+
CREATE TABLE t1 (f VARCHAR(8)) ENGINE=MyISAM;
7156+
INSERT INTO t1 VALUES ('foo');
7157+
SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar' UNION SELECT 'baz' );
7158+
f
7159+
foo
7160+
drop table t1;
7161+
SET NAMES default;

mysql-test/r/subselect_no_exists_to_in.result

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7147,6 +7147,18 @@ sq
71477147
NULL
71487148
drop view v2;
71497149
drop table t1,t2;
7150+
#
7151+
# MDEV-9487: Server crashes in Time_and_counter_tracker::incr_loops
7152+
# with UNION in ALL subquery
7153+
#
7154+
SET NAMES utf8;
7155+
CREATE TABLE t1 (f VARCHAR(8)) ENGINE=MyISAM;
7156+
INSERT INTO t1 VALUES ('foo');
7157+
SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar' UNION SELECT 'baz' );
7158+
f
7159+
foo
7160+
drop table t1;
7161+
SET NAMES default;
71507162
set optimizer_switch=default;
71517163
select @@optimizer_switch like '%exists_to_in=off%';
71527164
@@optimizer_switch like '%exists_to_in=off%'

mysql-test/r/subselect_no_mat.result

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7140,6 +7140,18 @@ sq
71407140
NULL
71417141
drop view v2;
71427142
drop table t1,t2;
7143+
#
7144+
# MDEV-9487: Server crashes in Time_and_counter_tracker::incr_loops
7145+
# with UNION in ALL subquery
7146+
#
7147+
SET NAMES utf8;
7148+
CREATE TABLE t1 (f VARCHAR(8)) ENGINE=MyISAM;
7149+
INSERT INTO t1 VALUES ('foo');
7150+
SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar' UNION SELECT 'baz' );
7151+
f
7152+
foo
7153+
drop table t1;
7154+
SET NAMES default;
71437155
set optimizer_switch=default;
71447156
select @@optimizer_switch like '%materialization=on%';
71457157
@@optimizer_switch like '%materialization=on%'

mysql-test/r/subselect_no_opts.result

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7138,4 +7138,16 @@ sq
71387138
NULL
71397139
drop view v2;
71407140
drop table t1,t2;
7141+
#
7142+
# MDEV-9487: Server crashes in Time_and_counter_tracker::incr_loops
7143+
# with UNION in ALL subquery
7144+
#
7145+
SET NAMES utf8;
7146+
CREATE TABLE t1 (f VARCHAR(8)) ENGINE=MyISAM;
7147+
INSERT INTO t1 VALUES ('foo');
7148+
SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar' UNION SELECT 'baz' );
7149+
f
7150+
foo
7151+
drop table t1;
7152+
SET NAMES default;
71417153
set @optimizer_switch_for_subselect_test=null;

mysql-test/r/subselect_no_scache.result

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7153,6 +7153,18 @@ sq
71537153
NULL
71547154
drop view v2;
71557155
drop table t1,t2;
7156+
#
7157+
# MDEV-9487: Server crashes in Time_and_counter_tracker::incr_loops
7158+
# with UNION in ALL subquery
7159+
#
7160+
SET NAMES utf8;
7161+
CREATE TABLE t1 (f VARCHAR(8)) ENGINE=MyISAM;
7162+
INSERT INTO t1 VALUES ('foo');
7163+
SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar' UNION SELECT 'baz' );
7164+
f
7165+
foo
7166+
drop table t1;
7167+
SET NAMES default;
71567168
set optimizer_switch=default;
71577169
select @@optimizer_switch like '%subquery_cache=on%';
71587170
@@optimizer_switch like '%subquery_cache=on%'

mysql-test/r/subselect_no_semijoin.result

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7138,5 +7138,17 @@ sq
71387138
NULL
71397139
drop view v2;
71407140
drop table t1,t2;
7141+
#
7142+
# MDEV-9487: Server crashes in Time_and_counter_tracker::incr_loops
7143+
# with UNION in ALL subquery
7144+
#
7145+
SET NAMES utf8;
7146+
CREATE TABLE t1 (f VARCHAR(8)) ENGINE=MyISAM;
7147+
INSERT INTO t1 VALUES ('foo');
7148+
SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar' UNION SELECT 'baz' );
7149+
f
7150+
foo
7151+
drop table t1;
7152+
SET NAMES default;
71417153
set @optimizer_switch_for_subselect_test=null;
71427154
set @join_cache_level_for_subselect_test=NULL;

mysql-test/r/type_year.result

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ a
387387
00
388388
select a from t1 where a=(select 2000 from dual where 1);
389389
a
390+
00
390391
select a from t1 where a=y2k();
391392
a
392393
00

mysql-test/t/subselect.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6000,3 +6000,14 @@ SELECT ( SELECT MIN(v2.f2) FROM t1 ) AS sq FROM v2 GROUP BY sq;
60006000

60016001
drop view v2;
60026002
drop table t1,t2;
6003+
6004+
--echo #
6005+
--echo # MDEV-9487: Server crashes in Time_and_counter_tracker::incr_loops
6006+
--echo # with UNION in ALL subquery
6007+
--echo #
6008+
SET NAMES utf8;
6009+
CREATE TABLE t1 (f VARCHAR(8)) ENGINE=MyISAM;
6010+
INSERT INTO t1 VALUES ('foo');
6011+
SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar' UNION SELECT 'baz' );
6012+
drop table t1;
6013+
SET NAMES default;

sql/item_subselect.cc

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -561,22 +561,34 @@ bool Item_subselect::is_expensive()
561561
for (SELECT_LEX *sl= unit->first_select(); sl; sl= sl->next_select())
562562
{
563563
JOIN *cur_join= sl->join;
564+
565+
/* not optimized subquery */
564566
if (!cur_join)
565-
continue;
567+
return true;
568+
569+
/* very simple subquery */
570+
if (!cur_join->tables_list && !sl->first_inner_unit())
571+
return false;
572+
573+
/*
574+
If the subquery is not optimised or in the process of optimization
575+
it supposed to be expensive
576+
*/
577+
if (!cur_join->optimized)
578+
return true;
566579

567580
/*
568581
Subqueries whose result is known after optimization are not expensive.
569582
Such subqueries have all tables optimized away, thus have no join plan.
570583
*/
571-
if (cur_join->optimized &&
572-
(cur_join->zero_result_cause || !cur_join->tables_list))
584+
if ((cur_join->zero_result_cause || !cur_join->tables_list))
573585
return false;
574586

575587
/*
576588
If a subquery is not optimized we cannot estimate its cost. A subquery is
577589
considered optimized if it has a join plan.
578590
*/
579-
if (!(cur_join->optimized && cur_join->join_tab))
591+
if (!cur_join->join_tab)
580592
return true;
581593

582594
if (sl->first_inner_unit())

0 commit comments

Comments
 (0)