Skip to content

Commit c5e68b6

Browse files
committed
MDEV-27212 Crash in Item_equal::sort on second execution of stored procedure
This bug could cause a crash of the server at the second call of a stored procedure when it executed a query containing a mergeable derived table / view whose specification used another mergeable derived_table or view and a subquery with outer reference in the select list of the specification. Such queries could cause the same problem when they were executed for the second time in a prepared mode. The problem appeared due to a typo mistake in the legacy code of the function create_view_field() that prevented building Item_direct_view_ref wrapper for the mentioned outer reference at the second execution of the query and setting the depended_from field for the outer reference. Approved by Oleksandr Byelkin <sanja@mariadb.com>
1 parent 1bcdc3e commit c5e68b6

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

mysql-test/r/derived_view.result

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3586,4 +3586,64 @@ f2 f3
35863586
DROP PROCEDURE p1;
35873587
DROP VIEW v1,v2,v3;
35883588
DROP TABLE t1;
3589+
#
3590+
# MDEV-27212: 2-nd execution of PS for select with embedded derived tables
3591+
# and correlated subquery in select list of outer derived
3592+
#
3593+
create table t1 ( id int, id2 int ) engine=myisam;
3594+
create table t2 ( x3 int , x1 int , x2 int, a1 int) engine=myisam;
3595+
insert into t1 values (3, 2), (4, 2), (3, 4);
3596+
insert into t2 values (1, 2, 2, 1), (1, 3, 3, 2), (2, 3, 3, 1);
3597+
prepare stmt from "select id from t1
3598+
join
3599+
( select dt2.x1,
3600+
( select sum(a1) from t2 where t2.x1 = dt2.x1 ) m
3601+
from ( select x1 from t2 u where x3 = 1 ) dt2
3602+
) dt
3603+
on t1.id = dt.x1
3604+
where t1.id2 < dt.m";
3605+
execute stmt;
3606+
id
3607+
3
3608+
execute stmt;
3609+
id
3610+
3
3611+
deallocate prepare stmt;
3612+
create procedure sp1() select id from t1
3613+
join
3614+
( select dt2.x1,
3615+
( select sum(a1) from t2 where t2.x1 = dt2.x1 ) m
3616+
from ( select x1 from t2 u where x3 = 1 ) dt2
3617+
) dt
3618+
on t1.id = dt.x1
3619+
where t1.id2 < dt.m;
3620+
call sp1();
3621+
id
3622+
3
3623+
call sp1();
3624+
id
3625+
3
3626+
create view v2 as select x1 from t2 u where x3 = 1;
3627+
create view v as
3628+
select v2.x1,
3629+
( select sum(a1) from t2 where t2.x1 = v2.x1 ) m from v2;
3630+
prepare stmt from "select id from t1 join v on t1.id = v.x1 where t1.id2 < v.m";
3631+
execute stmt;
3632+
id
3633+
3
3634+
execute stmt;
3635+
id
3636+
3
3637+
deallocate prepare stmt;
3638+
create procedure sp2() select id from t1 join v on t1.id = v.x1 where t1.id2 < v.m;
3639+
call sp2();
3640+
id
3641+
3
3642+
call sp2();
3643+
id
3644+
3
3645+
drop procedure sp1;
3646+
drop procedure sp2;
3647+
drop view v, v2;
3648+
drop table t1,t2;
35893649
# End of 10.2 tests

mysql-test/t/derived_view.test

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,4 +2376,56 @@ DROP PROCEDURE p1;
23762376
DROP VIEW v1,v2,v3;
23772377
DROP TABLE t1;
23782378

2379+
--echo #
2380+
--echo # MDEV-27212: 2-nd execution of PS for select with embedded derived tables
2381+
--echo # and correlated subquery in select list of outer derived
2382+
--echo #
2383+
create table t1 ( id int, id2 int ) engine=myisam;
2384+
create table t2 ( x3 int , x1 int , x2 int, a1 int) engine=myisam;
2385+
insert into t1 values (3, 2), (4, 2), (3, 4);
2386+
insert into t2 values (1, 2, 2, 1), (1, 3, 3, 2), (2, 3, 3, 1);
2387+
2388+
let $q=
2389+
select id from t1
2390+
join
2391+
( select dt2.x1,
2392+
( select sum(a1) from t2 where t2.x1 = dt2.x1 ) m
2393+
from ( select x1 from t2 u where x3 = 1 ) dt2
2394+
) dt
2395+
on t1.id = dt.x1
2396+
where t1.id2 < dt.m;
2397+
2398+
eval prepare stmt from "$q";
2399+
execute stmt;
2400+
execute stmt;
2401+
deallocate prepare stmt;
2402+
2403+
eval create procedure sp1() $q;
2404+
call sp1();
2405+
call sp1();
2406+
2407+
create view v2 as select x1 from t2 u where x3 = 1;
2408+
create view v as
2409+
select v2.x1,
2410+
( select sum(a1) from t2 where t2.x1 = v2.x1 ) m from v2;
2411+
2412+
let $q=
2413+
select id from t1 join v on t1.id = v.x1 where t1.id2 < v.m;
2414+
2415+
eval prepare stmt from "$q";
2416+
execute stmt;
2417+
execute stmt;
2418+
deallocate prepare stmt;
2419+
2420+
eval create procedure sp2() $q;
2421+
call sp2();
2422+
call sp2();
2423+
2424+
drop procedure sp1;
2425+
drop procedure sp2;
2426+
2427+
drop view v, v2;
2428+
2429+
drop table t1,t2;
2430+
23792431
--echo # End of 10.2 tests

sql/table.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5984,7 +5984,7 @@ Item *Field_iterator_view::create_item(THD *thd)
59845984
Item *create_view_field(THD *thd, TABLE_LIST *view, Item **field_ref,
59855985
const char *name)
59865986
{
5987-
bool save_wrapper= thd->lex->select_lex.no_wrap_view_item;
5987+
bool save_wrapper= thd->lex->current_select->no_wrap_view_item;
59885988
Item *field= *field_ref;
59895989
DBUG_ENTER("create_view_field");
59905990

0 commit comments

Comments
 (0)