From abf95afa2a1c23f3c7aa42f44fc665450ebeeea1 Mon Sep 17 00:00:00 2001 From: halfspawn Date: Fri, 7 Jul 2017 17:50:09 +0200 Subject: [PATCH 1/8] MDEV-12137 DELETE statement with the same source and target single-table deletes only --- mysql-test/r/delete_use_source.result | 178 ++++++++++++++++++ mysql-test/r/lowercase_view.result | 3 - mysql-test/r/merge.result | 42 +++-- mysql-test/r/subselect.result | 5 +- mysql-test/r/subselect_no_exists_to_in.result | 5 +- mysql-test/r/subselect_no_mat.result | 5 +- mysql-test/r/subselect_no_opts.result | 5 +- mysql-test/r/subselect_no_scache.result | 5 +- mysql-test/r/subselect_no_semijoin.result | 5 +- mysql-test/r/view.result | 3 - mysql-test/t/delete_use_source.test | 172 +++++++++++++++++ mysql-test/t/lowercase_view.test | 4 +- mysql-test/t/merge.test | 61 ++++-- mysql-test/t/subselect.test | 8 +- mysql-test/t/view.test | 7 +- sql/sql_delete.cc | 88 +++++++-- sql/sql_delete.h | 3 +- sql/sql_prepare.cc | 4 +- 18 files changed, 529 insertions(+), 74 deletions(-) create mode 100644 mysql-test/r/delete_use_source.result create mode 100644 mysql-test/t/delete_use_source.test diff --git a/mysql-test/r/delete_use_source.result b/mysql-test/r/delete_use_source.result new file mode 100644 index 00000000000..c7ca43083cf --- /dev/null +++ b/mysql-test/r/delete_use_source.result @@ -0,0 +1,178 @@ +set sql_mode=oracle; +use test; +create or replace table tab_delete(c1 integer not null,c2 integer not null) engine=InnoDb; +create index tab_delete_c1 on tab_delete(c1); +create or replace view view_delete as select * from tab_delete where c1 in (0,1); +CREATE or replace PROCEDURE gendata(a int, count int ) AS +i INT:=0; +BEGIN +FOR i IN 1 .. count +LOOP +insert into tab_delete values (a,i); +END LOOP; +END; +/ +create or replace trigger trg after delete on tab_delete for each row +begin +declare c int; +begin +if old.c1 = 1 then +select count(*) into c from tab_delete where c1!=old.c1; +SIGNAL SQLSTATE '45000' set table_name=c; +end if; +end; +end; +/ +set @count=500; +call gendata(0,@count); +call gendata(1,50); +call gendata(2,20); +call gendata(3,20); +commit; +# +# Delete with limit (quick select - range acces) +# +start transaction; +delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 limit 1; +affected rows: 1 +delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 limit 1; +affected rows: 0 +select count(*) from view_delete where c1=0; +count(*) +499 +rollback; +# +# Delete +# +start transaction; +delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 ; +affected rows: 500 +rollback; +# +# Delete with exists +# +start transaction; +select count(*) from view_delete where c1=2; +count(*) +0 +delete from tab_delete where c1=2 and exists(select 'x' from tab_delete b where b.c2<10); +affected rows: 20 +delete from tab_delete where c1=2 and exists(select 'x' from tab_delete b where b.c2<10); +affected rows: 0 +select count(*) from view_delete where c1=2; +count(*) +0 +rollback; +# +# Delete throw a view with limit (range access) +# +start transaction; +# Acces by range (quick_select), initied = INDEX +# +------+--------------------+------------+-------+---------------+---------------+---------+--------------------+------+-------------+ +# | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +# +------+--------------------+------------+-------+---------------+---------------+---------+--------------------+------+-------------+ +# | 1 | PRIMARY | tab_delete | range | tab_delete_c1 | tab_delete_c1 | 4 | NULL | 550 | Using where | +# | 2 | DEPENDENT SUBQUERY | b | ref | tab_delete_c1 | tab_delete_c1 | 4 | test.tab_delete.c1 | 73 | Using index | +# +------+--------------------+------------+-------+---------------+---------------+---------+--------------------+------+-------------+ +delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 limit 1; +affected rows: 1 +delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 limit 1; +affected rows: 0 +select count(*) from view_delete where c1=0; +count(*) +499 +rollback; +# +# Delete throw a view (ALL access) +# +start transaction; +# Acces by pointer, initied = RND +# +------+--------------------+------------+------+---------------+---------------+---------+--------------------+------+-------------+ +# | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +# +------+--------------------+------------+------+---------------+---------------+---------+--------------------+------+-------------+ +# | 1 | PRIMARY | tab_delete | ALL | tab_delete_c1 | NULL | NULL | NULL | 589 | Using where | +# | 2 | DEPENDENT SUBQUERY | b | ref | tab_delete_c1 | tab_delete_c1 | 4 | test.tab_delete.c1 | 295 | Using index | +# +------+--------------------+------------+------+---------------+---------------+---------+--------------------+------+-------------+ +delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 ; +affected rows: 500 +select count(*) from view_delete where c1=0; +count(*) +0 +rollback; +# +# Delete failed due to trigger +# +start transaction; +delete from tab_delete where c1=1 and (select count(*) from tab_delete b where b.c1=tab_delete.c1) > 0 order by c2 asc limit 10; +ERROR 45000: Unhandled user-defined exception condition +rollback; +start transaction; +delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) > 0 order by c1 desc limit 100; +ERROR 45000: Unhandled user-defined exception condition +select c1,count(*) from tab_delete group by c1; +c1 count(*) +0 500 +1 50 +2 20 +3 20 +rollback; +# +# Delete throw a view with returning +# +start transaction; +delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 order by c2 asc limit 10 returning c1,c2; +c1 c2 +0 1 +0 2 +0 3 +0 4 +0 5 +0 6 +0 7 +0 8 +0 9 +0 10 +rollback; +start transaction; +delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 order by c2 desc limit 10 returning c1,c2; +c1 c2 +0 491 +0 492 +0 493 +0 494 +0 495 +0 496 +0 497 +0 498 +0 499 +0 500 +rollback; +# +# Delete from table with more than 150000 rows +# +insert into tab_delete select * from tab_delete; +insert into tab_delete select * from tab_delete; +insert into tab_delete select * from tab_delete; +insert into tab_delete select * from tab_delete; +insert into tab_delete select * from tab_delete; +insert into tab_delete select * from tab_delete; +insert into tab_delete select * from tab_delete; +insert into tab_delete select * from tab_delete; +select count(*) from tab_delete; +count(*) +151040 +with high memory for sort_buffer_size +SET SESSION sort_buffer_size = 1024000; +start transaction; +delete from tab_delete where c1=0 and exists(select 'x' from tab_delete b where b.c1<10); +affected rows: 128000 +rollback; +with few memory for sort_buffer_size +SET SESSION sort_buffer_size = 1024; +start transaction; +delete from tab_delete where c1=0 and exists(select 'x' from tab_delete b where b.c1<10); +affected rows: 128000 +rollback; +drop procedure if exists gendata; +drop view if exists view_delete; +drop table if exists tab_delete; diff --git a/mysql-test/r/lowercase_view.result b/mysql-test/r/lowercase_view.result index df303807407..3b6aeb9548b 100644 --- a/mysql-test/r/lowercase_view.result +++ b/mysql-test/r/lowercase_view.result @@ -66,11 +66,8 @@ ERROR HY000: The definition of table 'v2aA' prevents operation UPDATE on table ' update v3aA set v3Aa.col1 = (select max(col1) from v3aA); ERROR HY000: Table 'v3aA' is specified twice, both as a target for 'UPDATE' and as a separate source for data delete from v2Aa where col1 = (select max(col1) from v1Aa); -ERROR HY000: The definition of table 'v1Aa' prevents operation DELETE on table 'v2Aa' delete from v2aA where col1 = (select max(col1) from t1Aa); -ERROR HY000: The definition of table 'v2aA' prevents operation DELETE on table 'v2aA' delete from v2Aa where col1 = (select max(col1) from v2aA); -ERROR HY000: Table 'v2Aa' is specified twice, both as a target for 'DELETE' and as a separate source for data delete v2Aa from v2aA,t2Aa where (select max(col1) from v1aA) > 0 and v2Aa.col1 = t2aA.col1; ERROR HY000: The definition of table 'v1aA' prevents operation DELETE on table 'v2aA' delete t1aA from t1Aa,t2Aa where (select max(col1) from v1Aa) > 0 and t1aA.col1 = t2aA.col1; diff --git a/mysql-test/r/merge.result b/mysql-test/r/merge.result index f468f47c4c9..6da7eb38655 100644 --- a/mysql-test/r/merge.result +++ b/mysql-test/r/merge.result @@ -3743,33 +3743,47 @@ ERROR HY000: The definition of table 'v1' prevents operation UPDATE on table 'm1 update m1 set a = ((select max(a) from tmp, v1)); ERROR HY000: The definition of table 'v1' prevents operation UPDATE on table 'm1' delete from m1 where a = (select max(a) from m1); -ERROR HY000: Table 'm1' is specified twice, both as a target for 'DELETE' and as a separate source for data +insert into t1 (a) values (1); +insert into t2 (a) values (1); delete from m1 where a = (select max(a) from m2); -ERROR HY000: Table 'm1' is specified twice, both as a target for 'DELETE' and as a separate source for data +insert into t1 (a) values (1); +insert into t2 (a) values (1); delete from m1 where a = (select max(a) from t1); -ERROR HY000: Table 'm1' is specified twice, both as a target for 'DELETE' and as a separate source for data +insert into t1 (a) values (1); +insert into t2 (a) values (1); delete from m1 where a = (select max(a) from t2); -ERROR HY000: Table 'm1' is specified twice, both as a target for 'DELETE' and as a separate source for data +insert into t1 (a) values (1); +insert into t2 (a) values (1); delete from m1 where a = (select max(a) from t3, m1); -ERROR HY000: Table 'm1' is specified twice, both as a target for 'DELETE' and as a separate source for data +insert into t1 (a) values (1); +insert into t2 (a) values (1); delete from m1 where a = (select max(a) from t3, m2); -ERROR HY000: Table 'm1' is specified twice, both as a target for 'DELETE' and as a separate source for data +insert into t1 (a) values (1); +insert into t2 (a) values (1); delete from m1 where a = (select max(a) from t3, t1); -ERROR HY000: Table 'm1' is specified twice, both as a target for 'DELETE' and as a separate source for data +insert into t1 (a) values (1); +insert into t2 (a) values (1); delete from m1 where a = (select max(a) from t3, t2); -ERROR HY000: Table 'm1' is specified twice, both as a target for 'DELETE' and as a separate source for data +insert into t1 (a) values (1); +insert into t2 (a) values (1); delete from m1 where a = (select max(a) from tmp, m1); -ERROR HY000: Table 'm1' is specified twice, both as a target for 'DELETE' and as a separate source for data +insert into t1 (a) values (1); +insert into t2 (a) values (1); delete from m1 where a = (select max(a) from tmp, m2); -ERROR HY000: Table 'm1' is specified twice, both as a target for 'DELETE' and as a separate source for data +insert into t1 (a) values (1); +insert into t2 (a) values (1); delete from m1 where a = (select max(a) from tmp, t1); -ERROR HY000: Table 'm1' is specified twice, both as a target for 'DELETE' and as a separate source for data +insert into t1 (a) values (1); +insert into t2 (a) values (1); delete from m1 where a = (select max(a) from tmp, t2); -ERROR HY000: Table 'm1' is specified twice, both as a target for 'DELETE' and as a separate source for data +insert into t1 (a) values (1); +insert into t2 (a) values (1); delete from m1 where a = (select max(a) from v1); -ERROR HY000: The definition of table 'v1' prevents operation DELETE on table 'm1' +insert into t1 (a) values (1); +insert into t2 (a) values (1); delete from m1 where a = (select max(a) from tmp, v1); -ERROR HY000: The definition of table 'v1' prevents operation DELETE on table 'm1' +insert into t1 (a) values (1); +insert into t2 (a) values (1); drop view v1; drop temporary table tmp; drop table t1, t2, t3, m1, m2; diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index 95a872f1498..98a279c28e9 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -607,11 +607,12 @@ select * from t1 where b = (select b from t2 where t1.a = t2.a); a b 2 12 delete from t1 where b in (select b from t1); -ERROR HY000: Table 't1' is specified twice, both as a target for 'DELETE' and as a separate source for data +affected rows: 3 +insert into t1 values (0, 10),(1, 11),(2, 12); delete from t1 where b = (select b from t2); ERROR 21000: Subquery returns more than 1 row delete from t1 where b = (select b from t2 where t1.a = t2.a); -select * from t1; +select * from t1 order by b; a b 0 10 1 11 diff --git a/mysql-test/r/subselect_no_exists_to_in.result b/mysql-test/r/subselect_no_exists_to_in.result index 7f07b974bb2..5cee4076ed8 100644 --- a/mysql-test/r/subselect_no_exists_to_in.result +++ b/mysql-test/r/subselect_no_exists_to_in.result @@ -611,11 +611,12 @@ select * from t1 where b = (select b from t2 where t1.a = t2.a); a b 2 12 delete from t1 where b in (select b from t1); -ERROR HY000: Table 't1' is specified twice, both as a target for 'DELETE' and as a separate source for data +affected rows: 3 +insert into t1 values (0, 10),(1, 11),(2, 12); delete from t1 where b = (select b from t2); ERROR 21000: Subquery returns more than 1 row delete from t1 where b = (select b from t2 where t1.a = t2.a); -select * from t1; +select * from t1 order by b; a b 0 10 1 11 diff --git a/mysql-test/r/subselect_no_mat.result b/mysql-test/r/subselect_no_mat.result index 57c7a979d61..e7409b0b09c 100644 --- a/mysql-test/r/subselect_no_mat.result +++ b/mysql-test/r/subselect_no_mat.result @@ -614,11 +614,12 @@ select * from t1 where b = (select b from t2 where t1.a = t2.a); a b 2 12 delete from t1 where b in (select b from t1); -ERROR HY000: Table 't1' is specified twice, both as a target for 'DELETE' and as a separate source for data +affected rows: 3 +insert into t1 values (0, 10),(1, 11),(2, 12); delete from t1 where b = (select b from t2); ERROR 21000: Subquery returns more than 1 row delete from t1 where b = (select b from t2 where t1.a = t2.a); -select * from t1; +select * from t1 order by b; a b 0 10 1 11 diff --git a/mysql-test/r/subselect_no_opts.result b/mysql-test/r/subselect_no_opts.result index 6e8be4a02a7..3bdc91686d7 100644 --- a/mysql-test/r/subselect_no_opts.result +++ b/mysql-test/r/subselect_no_opts.result @@ -610,11 +610,12 @@ select * from t1 where b = (select b from t2 where t1.a = t2.a); a b 2 12 delete from t1 where b in (select b from t1); -ERROR HY000: Table 't1' is specified twice, both as a target for 'DELETE' and as a separate source for data +affected rows: 3 +insert into t1 values (0, 10),(1, 11),(2, 12); delete from t1 where b = (select b from t2); ERROR 21000: Subquery returns more than 1 row delete from t1 where b = (select b from t2 where t1.a = t2.a); -select * from t1; +select * from t1 order by b; a b 0 10 1 11 diff --git a/mysql-test/r/subselect_no_scache.result b/mysql-test/r/subselect_no_scache.result index 1b437f6919d..3beba7c338d 100644 --- a/mysql-test/r/subselect_no_scache.result +++ b/mysql-test/r/subselect_no_scache.result @@ -613,11 +613,12 @@ select * from t1 where b = (select b from t2 where t1.a = t2.a); a b 2 12 delete from t1 where b in (select b from t1); -ERROR HY000: Table 't1' is specified twice, both as a target for 'DELETE' and as a separate source for data +affected rows: 3 +insert into t1 values (0, 10),(1, 11),(2, 12); delete from t1 where b = (select b from t2); ERROR 21000: Subquery returns more than 1 row delete from t1 where b = (select b from t2 where t1.a = t2.a); -select * from t1; +select * from t1 order by b; a b 0 10 1 11 diff --git a/mysql-test/r/subselect_no_semijoin.result b/mysql-test/r/subselect_no_semijoin.result index 6094c7e029d..f2d97078772 100644 --- a/mysql-test/r/subselect_no_semijoin.result +++ b/mysql-test/r/subselect_no_semijoin.result @@ -610,11 +610,12 @@ select * from t1 where b = (select b from t2 where t1.a = t2.a); a b 2 12 delete from t1 where b in (select b from t1); -ERROR HY000: Table 't1' is specified twice, both as a target for 'DELETE' and as a separate source for data +affected rows: 3 +insert into t1 values (0, 10),(1, 11),(2, 12); delete from t1 where b = (select b from t2); ERROR 21000: Subquery returns more than 1 row delete from t1 where b = (select b from t2 where t1.a = t2.a); -select * from t1; +select * from t1 order by b; a b 0 10 1 11 diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index 77794ac1c82..50a48ade3bd 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -994,11 +994,8 @@ ERROR HY000: The definition of table 'v2' prevents operation UPDATE on table 'v3 update v3 set v3.col1 = (select max(col1) from v3); ERROR HY000: Table 'v3' is specified twice, both as a target for 'UPDATE' and as a separate source for data delete from v2 where col1 = (select max(col1) from v1); -ERROR HY000: The definition of table 'v1' prevents operation DELETE on table 'v2' delete from v2 where col1 = (select max(col1) from t1); -ERROR HY000: The definition of table 'v2' prevents operation DELETE on table 'v2' delete from v2 where col1 = (select max(col1) from v2); -ERROR HY000: Table 'v2' is specified twice, both as a target for 'DELETE' and as a separate source for data delete v2 from v2,t2 where (select max(col1) from v1) > 0 and v2.col1 = t2.col1; ERROR HY000: The definition of table 'v1' prevents operation DELETE on table 'v2' delete t1 from t1,t2 where (select max(col1) from v1) > 0 and t1.col1 = t2.col1; diff --git a/mysql-test/t/delete_use_source.test b/mysql-test/t/delete_use_source.test new file mode 100644 index 00000000000..ddd7858ee77 --- /dev/null +++ b/mysql-test/t/delete_use_source.test @@ -0,0 +1,172 @@ +-- source include/have_innodb.inc +set sql_mode=oracle; +use test; +create or replace table tab_delete(c1 integer not null,c2 integer not null) engine=InnoDb; +create index tab_delete_c1 on tab_delete(c1); +create or replace view view_delete as select * from tab_delete where c1 in (0,1); +delimiter /; +CREATE or replace PROCEDURE gendata(a int, count int ) AS + i INT:=0; +BEGIN + FOR i IN 1 .. count + LOOP + insert into tab_delete values (a,i); + END LOOP; +END; +/ +create or replace trigger trg after delete on tab_delete for each row +begin + declare c int; + begin + if old.c1 = 1 then + select count(*) into c from tab_delete where c1!=old.c1; + SIGNAL SQLSTATE '45000' set table_name=c; + end if; + end; +end; +/ +delimiter ;/ +set @count=500; +call gendata(0,@count); +call gendata(1,50); +call gendata(2,20); +call gendata(3,20); +commit; + +--echo # +--echo # Delete with limit (quick select - range acces) +--echo # + +start transaction; +--enable_info +delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 limit 1; +delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 limit 1; +--disable_info +select count(*) from view_delete where c1=0; +rollback; + +--echo # +--echo # Delete +--echo # + +start transaction; +--enable_info +delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 ; +--disable_info +rollback; + +--echo # +--echo # Delete with exists +--echo # + +start transaction; +select count(*) from view_delete where c1=2; +--enable_info +delete from tab_delete where c1=2 and exists(select 'x' from tab_delete b where b.c2<10); +delete from tab_delete where c1=2 and exists(select 'x' from tab_delete b where b.c2<10); +--disable_info +select count(*) from view_delete where c1=2; +rollback; + +--echo # +--echo # Delete throw a view with limit (range access) +--echo # + +start transaction; +--echo # Acces by range (quick_select), initied = INDEX +--echo # +------+--------------------+------------+-------+---------------+---------------+---------+--------------------+------+-------------+ +--echo # | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +--echo # +------+--------------------+------------+-------+---------------+---------------+---------+--------------------+------+-------------+ +--echo # | 1 | PRIMARY | tab_delete | range | tab_delete_c1 | tab_delete_c1 | 4 | NULL | 550 | Using where | +--echo # | 2 | DEPENDENT SUBQUERY | b | ref | tab_delete_c1 | tab_delete_c1 | 4 | test.tab_delete.c1 | 73 | Using index | +--echo # +------+--------------------+------------+-------+---------------+---------------+---------+--------------------+------+-------------+ +# explain delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 limit 1; +--enable_info +delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 limit 1; +delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 limit 1; +--disable_info +select count(*) from view_delete where c1=0; +rollback; + +--echo # +--echo # Delete throw a view (ALL access) +--echo # + +start transaction; +--echo # Acces by pointer, initied = RND +--echo # +------+--------------------+------------+------+---------------+---------------+---------+--------------------+------+-------------+ +--echo # | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +--echo # +------+--------------------+------------+------+---------------+---------------+---------+--------------------+------+-------------+ +--echo # | 1 | PRIMARY | tab_delete | ALL | tab_delete_c1 | NULL | NULL | NULL | 589 | Using where | +--echo # | 2 | DEPENDENT SUBQUERY | b | ref | tab_delete_c1 | tab_delete_c1 | 4 | test.tab_delete.c1 | 295 | Using index | +--echo # +------+--------------------+------------+------+---------------+---------------+---------+--------------------+------+-------------+ +# explain delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500; +--enable_info +delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 ; +--disable_info +select count(*) from view_delete where c1=0; +rollback; + + +--echo # +--echo # Delete failed due to trigger +--echo # + +start transaction; +--enable_info +--error ER_SIGNAL_EXCEPTION +delete from tab_delete where c1=1 and (select count(*) from tab_delete b where b.c1=tab_delete.c1) > 0 order by c2 asc limit 10; +--disable_info +rollback; +start transaction; +--enable_info +--error ER_SIGNAL_EXCEPTION +delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) > 0 order by c1 desc limit 100; +--disable_info +select c1,count(*) from tab_delete group by c1; +rollback; + +--echo # +--echo # Delete throw a view with returning +--echo # + +start transaction; +delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 order by c2 asc limit 10 returning c1,c2; +rollback; +start transaction; +delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 order by c2 desc limit 10 returning c1,c2; +rollback; + + +--echo # +--echo # Delete from table with more than 150000 rows +--echo # +insert into tab_delete select * from tab_delete; +insert into tab_delete select * from tab_delete; +insert into tab_delete select * from tab_delete; +insert into tab_delete select * from tab_delete; +insert into tab_delete select * from tab_delete; +insert into tab_delete select * from tab_delete; +insert into tab_delete select * from tab_delete; +insert into tab_delete select * from tab_delete; +select count(*) from tab_delete; + +--echo with high memory for sort_buffer_size +SET SESSION sort_buffer_size = 1024000; +start transaction; +--enable_info +delete from tab_delete where c1=0 and exists(select 'x' from tab_delete b where b.c1<10); +--disable_info +rollback; + +--echo with few memory for sort_buffer_size +SET SESSION sort_buffer_size = 1024; +start transaction; +--enable_info +delete from tab_delete where c1=0 and exists(select 'x' from tab_delete b where b.c1<10); +--disable_info +rollback; + +drop procedure if exists gendata; +drop view if exists view_delete; +drop table if exists tab_delete; diff --git a/mysql-test/t/lowercase_view.test b/mysql-test/t/lowercase_view.test index 52be911cde0..6d53e6d9130 100644 --- a/mysql-test/t/lowercase_view.test +++ b/mysql-test/t/lowercase_view.test @@ -73,11 +73,9 @@ update v3aA set v3Aa.col1 = (select max(col1) from t1aA); update v3aA set v3Aa.col1 = (select max(col1) from v2aA); -- error 1093 update v3aA set v3Aa.col1 = (select max(col1) from v3aA); --- error 1443 +# Works since MDEV-12137 (no more error 1093) delete from v2Aa where col1 = (select max(col1) from v1Aa); --- error 1443 delete from v2aA where col1 = (select max(col1) from t1Aa); --- error 1093 delete from v2Aa where col1 = (select max(col1) from v2aA); -- error 1443 delete v2Aa from v2aA,t2Aa where (select max(col1) from v1aA) > 0 and v2Aa.col1 = t2aA.col1; diff --git a/mysql-test/t/merge.test b/mysql-test/t/merge.test index 09f313616f1..c35dd39170b 100644 --- a/mysql-test/t/merge.test +++ b/mysql-test/t/merge.test @@ -2763,39 +2763,66 @@ update m1 set a = ((select max(a) from tmp, t2)); update m1 set a = ((select max(a) from v1)); --error ER_VIEW_PREVENT_UPDATE update m1 set a = ((select max(a) from tmp, v1)); - - ---error ER_UPDATE_TABLE_USED +# Works since MDEV-12137 +# previously :ER_UPDATE_TABLE_USED delete from m1 where a = (select max(a) from m1); ---error ER_UPDATE_TABLE_USED +insert into t1 (a) values (1); +insert into t2 (a) values (1); +# previously :ER_UPDATE_TABLE_USED delete from m1 where a = (select max(a) from m2); ---error ER_UPDATE_TABLE_USED +insert into t1 (a) values (1); +insert into t2 (a) values (1); +# previously :ER_UPDATE_TABLE_USED delete from m1 where a = (select max(a) from t1); ---error ER_UPDATE_TABLE_USED +insert into t1 (a) values (1); +insert into t2 (a) values (1); +# previously :ER_UPDATE_TABLE_USED delete from m1 where a = (select max(a) from t2); +insert into t1 (a) values (1); +insert into t2 (a) values (1); ---error ER_UPDATE_TABLE_USED +# previously :ER_UPDATE_TABLE_USED delete from m1 where a = (select max(a) from t3, m1); ---error ER_UPDATE_TABLE_USED +insert into t1 (a) values (1); +insert into t2 (a) values (1); +# previously :ER_UPDATE_TABLE_USED delete from m1 where a = (select max(a) from t3, m2); ---error ER_UPDATE_TABLE_USED +insert into t1 (a) values (1); +insert into t2 (a) values (1); +# previously :ER_UPDATE_TABLE_USED delete from m1 where a = (select max(a) from t3, t1); ---error ER_UPDATE_TABLE_USED +insert into t1 (a) values (1); +insert into t2 (a) values (1); +# previously :ER_UPDATE_TABLE_USED delete from m1 where a = (select max(a) from t3, t2); +insert into t1 (a) values (1); +insert into t2 (a) values (1); ---error ER_UPDATE_TABLE_USED +# previously :ER_UPDATE_TABLE_USED delete from m1 where a = (select max(a) from tmp, m1); ---error ER_UPDATE_TABLE_USED +insert into t1 (a) values (1); +insert into t2 (a) values (1); +# previously :ER_UPDATE_TABLE_USED delete from m1 where a = (select max(a) from tmp, m2); ---error ER_UPDATE_TABLE_USED +insert into t1 (a) values (1); +insert into t2 (a) values (1); +# previously :ER_UPDATE_TABLE_USED delete from m1 where a = (select max(a) from tmp, t1); ---error ER_UPDATE_TABLE_USED +insert into t1 (a) values (1); +insert into t2 (a) values (1); +# previously :ER_UPDATE_TABLE_USED delete from m1 where a = (select max(a) from tmp, t2); - ---error ER_VIEW_PREVENT_UPDATE +insert into t1 (a) values (1); +insert into t2 (a) values (1); + +# previously : ER_VIEW_PREVENT_UPDATE delete from m1 where a = (select max(a) from v1); ---error ER_VIEW_PREVENT_UPDATE +insert into t1 (a) values (1); +insert into t2 (a) values (1); +# previously : ER_VIEW_PREVENT_UPDATE delete from m1 where a = (select max(a) from tmp, v1); +insert into t1 (a) values (1); +insert into t2 (a) values (1); drop view v1; drop temporary table tmp; diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test index 282013222de..59694635c70 100644 --- a/mysql-test/t/subselect.test +++ b/mysql-test/t/subselect.test @@ -340,12 +340,16 @@ insert into t1 values (0, 10),(1, 11),(2, 12); insert into t2 values (1, 21),(2, 12),(3, 23); select * from t1; select * from t1 where b = (select b from t2 where t1.a = t2.a); --- error ER_UPDATE_TABLE_USED +# Works since MDEV-12137 +# previously : ER_UPDATE_TABLE_USED +--enable_info delete from t1 where b in (select b from t1); +--disable_info +insert into t1 values (0, 10),(1, 11),(2, 12); -- error ER_SUBQUERY_NO_1_ROW delete from t1 where b = (select b from t2); delete from t1 where b = (select b from t2 where t1.a = t2.a); -select * from t1; +select * from t1 order by b; drop table t1, t2; #multi-delete with subselects diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index fa130afc84b..9ed37a2bf65 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -914,11 +914,12 @@ update v3 set v3.col1 = (select max(col1) from t1); update v3 set v3.col1 = (select max(col1) from v2); -- error ER_UPDATE_TABLE_USED update v3 set v3.col1 = (select max(col1) from v3); --- error ER_VIEW_PREVENT_UPDATE +# Works since MDEV-12137 +# Previously error ER_VIEW_PREVENT_UPDATE delete from v2 where col1 = (select max(col1) from v1); --- error ER_VIEW_PREVENT_UPDATE +# Previously error ER_VIEW_PREVENT_UPDATE delete from v2 where col1 = (select max(col1) from t1); --- error ER_UPDATE_TABLE_USED +# Previously error ER_UPDATE_TABLE_USED delete from v2 where col1 = (select max(col1) from v2); -- error ER_VIEW_PREVENT_UPDATE delete v2 from v2,t2 where (select max(col1) from v1) > 0 and v2.col1 = t2.col1; diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index eb5f0d7a477..8d4757e6d31 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -45,6 +45,8 @@ // end_read_record #include "sql_partition.h" // make_used_partitions_str +#define MEM_STRIP_BUF_SIZE current_thd->variables.sortbuff_size + /* @brief Print query plan of a single-table DELETE command @@ -246,6 +248,9 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, Delete_plan query_plan(thd->mem_root); query_plan.index= MAX_KEY; query_plan.using_filesort= FALSE; + Unique * deltempfile= NULL; + uint delete_while_scanning= 1; + uint delete_record= 0; DBUG_ENTER("mysql_delete"); create_explain_query(thd->lex, thd->mem_root); @@ -275,7 +280,8 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, query_plan.updating_a_view= MY_TEST(table_list->view); if (mysql_prepare_delete(thd, table_list, select_lex->with_wild, - select_lex->item_list, &conds)) + select_lex->item_list, &conds, + delete_while_scanning)) DBUG_RETURN(TRUE); if (with_select) @@ -556,16 +562,68 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, explain= (Explain_delete*)thd->lex->explain->get_upd_del_plan(); explain->tracker.on_scan_init(); - while (!(error=info.read_record(&info)) && !thd->killed && - ! thd->is_error()) + if (delete_while_scanning == 0) { - explain->tracker.on_record_read(); - thd->inc_examined_row_count(1); - if (table->vfield) - (void) table->update_virtual_fields(table->file, VCOL_UPDATE_FOR_DELETE); - if (!select || select->skip_record(thd) > 0) + /* + The table we are going to delete appears in join. + Instead of deleting the rows, first mark them deleted. + */ + ha_rows tmplimit=limit; + deltempfile= new Unique (refpos_order_cmp, + (void *) table->file, + table->file->ref_length, + MEM_STRIP_BUF_SIZE); + while (!(error=info.read_record(&info)) && !thd->killed && + ! thd->is_error()) + { + explain->tracker.on_record_read(); + thd->inc_examined_row_count(1); + if (table->vfield) + (void) table->update_virtual_fields(table->file, + VCOL_UPDATE_FOR_DELETE); + if (!select || select->skip_record(thd) > 0) + { + explain->tracker.on_record_after_where(); + table->file->position(table->record[0]); + if ((error= deltempfile->unique_add((char*) table->file->ref))) + { + error= 1; + goto terminate_delete; + } + if (!--tmplimit && using_limit) + { + break; + } + } + } + end_read_record(&info); + if (deltempfile->get(table) || + table->file->ha_index_or_rnd_end() || + init_read_record(&info, thd, table, NULL , &deltempfile->sort, 0, 1, + FALSE)) + { + error= 1; + goto terminate_delete; + } + delete_record= 1; + } + + while (!(error=info.read_record(&info)) && !thd->killed && + ! thd->is_error()) + { + if (delete_while_scanning == 1) + { + explain->tracker.on_record_read(); + thd->inc_examined_row_count(1); + if (table->vfield) + (void) table->update_virtual_fields(table->file, + VCOL_UPDATE_FOR_DELETE); + delete_record=(!select || select->skip_record(thd) > 0) ? 1 : 0; + if (delete_record) + explain->tracker.on_record_after_where(); + } + if (delete_record == 1) { - explain->tracker.on_record_after_where(); if (table->triggers && table->triggers->process_triggers(thd, TRG_EVENT_DELETE, TRG_ACTION_BEFORE, FALSE)) @@ -616,6 +674,7 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, else break; } +terminate_delete: killed_status= thd->killed; if (killed_status != NOT_KILLED || thd->is_error()) error= 1; // Aborted @@ -647,6 +706,8 @@ cleanup: thd->lex->current_select->first_cond_optimization= 0; } + delete deltempfile; + deltempfile=NULL; delete select; select= NULL; transactional_table= table->file->has_transactions(); @@ -746,7 +807,8 @@ l TRUE error */ int mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, - uint wild_num, List &field_list, Item **conds) + uint wild_num, List &field_list, Item **conds, + uint &delete_while_scanning) { Item *fake_conds= 0; SELECT_LEX *select_lex= &thd->lex->select_lex; @@ -775,10 +837,7 @@ l { TABLE_LIST *duplicate; if ((duplicate= unique_table(thd, table_list, table_list->next_global, 0))) - { - update_non_unique_table_error(table_list, "DELETE", duplicate); - DBUG_RETURN(TRUE); - } + delete_while_scanning= 0; } if (select_lex->inner_refs_list.elements && @@ -794,7 +853,6 @@ l Delete multiple tables from join ***************************************************************************/ -#define MEM_STRIP_BUF_SIZE current_thd->variables.sortbuff_size extern "C" int refpos_order_cmp(void* arg, const void *a,const void *b) { diff --git a/sql/sql_delete.h b/sql/sql_delete.h index 9cd09dc5722..d49b0114c52 100644 --- a/sql/sql_delete.h +++ b/sql/sql_delete.h @@ -27,7 +27,8 @@ typedef class Item COND; template class SQL_I_List; int mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, - uint wild_num, List &field_list, Item **conds); + uint wild_num, List &field_list, Item **conds, + uint &delete_while_scanning); bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, SQL_I_List *order, ha_rows rows, ulonglong options, select_result *result); diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc index 37d70bdc7ba..dc0dcbac43f 100644 --- a/sql/sql_prepare.cc +++ b/sql/sql_prepare.cc @@ -1496,6 +1496,7 @@ static bool mysql_test_delete(Prepared_statement *stmt, uint table_count= 0; THD *thd= stmt->thd; LEX *lex= stmt->lex; + uint delete_while_scanning=1; DBUG_ENTER("mysql_test_delete"); if (delete_precheck(thd, table_list) || @@ -1524,7 +1525,8 @@ static bool mysql_test_delete(Prepared_statement *stmt, DBUG_RETURN(mysql_prepare_delete(thd, table_list, lex->select_lex.with_wild, lex->select_lex.item_list, - &lex->select_lex.where)); + &lex->select_lex.where, + delete_while_scanning)); error: DBUG_RETURN(TRUE); } From c65cce3698edde272b8452c1fecec3d9522cea6e Mon Sep 17 00:00:00 2001 From: Sergei Golubchik Date: Fri, 7 Jul 2017 19:55:31 +0200 Subject: [PATCH 2/8] MDEV-12137 DELETE statement with the same source and target * various cleanups (mostly cosmetic) * remove useless tests (that were tesing the error condition) * optimize delete_use_source test (from 6 mins to 50 seconds, mainly by removing two huge rollbacks at the end). --- mysql-test/r/delete_use_source.result | 145 +++++++++------------- mysql-test/r/lowercase_view.result | 3 - mysql-test/r/merge.result | 42 ------- mysql-test/r/view.result | 3 - mysql-test/t/delete_use_source.test | 166 ++++++++++---------------- mysql-test/t/lowercase_view.test | 4 - mysql-test/t/merge.test | 60 ---------- mysql-test/t/view.test | 7 -- sql/sql_delete.cc | 102 ++++++++-------- sql/sql_delete.h | 2 +- sql/sql_prepare.cc | 4 +- 11 files changed, 173 insertions(+), 365 deletions(-) diff --git a/mysql-test/r/delete_use_source.result b/mysql-test/r/delete_use_source.result index c7ca43083cf..7886caf6ba4 100644 --- a/mysql-test/r/delete_use_source.result +++ b/mysql-test/r/delete_use_source.result @@ -1,43 +1,18 @@ -set sql_mode=oracle; -use test; -create or replace table tab_delete(c1 integer not null,c2 integer not null) engine=InnoDb; -create index tab_delete_c1 on tab_delete(c1); -create or replace view view_delete as select * from tab_delete where c1 in (0,1); -CREATE or replace PROCEDURE gendata(a int, count int ) AS -i INT:=0; -BEGIN -FOR i IN 1 .. count -LOOP -insert into tab_delete values (a,i); -END LOOP; -END; -/ -create or replace trigger trg after delete on tab_delete for each row -begin -declare c int; -begin -if old.c1 = 1 then -select count(*) into c from tab_delete where c1!=old.c1; -SIGNAL SQLSTATE '45000' set table_name=c; -end if; -end; -end; -/ -set @count=500; -call gendata(0,@count); -call gendata(1,50); -call gendata(2,20); -call gendata(3,20); -commit; +create table t1(c1 integer not null,c2 integer not null, key (c1)) engine=InnoDb; +create view v1 as select * from t1 where c1 in (0,1); +insert t1 select 0,seq from seq_1_to_500; +insert t1 select 1,seq from seq_1_to_50; +insert t1 select 2,seq from seq_1_to_20; +insert t1 select 3,seq from seq_1_to_20; # # Delete with limit (quick select - range acces) # start transaction; -delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 limit 1; +delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 limit 1; affected rows: 1 -delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 limit 1; +delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 limit 1; affected rows: 0 -select count(*) from view_delete where c1=0; +select count(*) from v1 where c1=0; count(*) 499 rollback; @@ -45,21 +20,21 @@ rollback; # Delete # start transaction; -delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 ; +delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 ; affected rows: 500 rollback; # # Delete with exists # start transaction; -select count(*) from view_delete where c1=2; +select count(*) from v1 where c1=2; count(*) 0 -delete from tab_delete where c1=2 and exists(select 'x' from tab_delete b where b.c2<10); +delete from t1 where c1=2 and exists(select 'x' from t1 b where b.c2<10); affected rows: 20 -delete from tab_delete where c1=2 and exists(select 'x' from tab_delete b where b.c2<10); +delete from t1 where c1=2 and exists(select 'x' from t1 b where b.c2<10); affected rows: 0 -select count(*) from view_delete where c1=2; +select count(*) from v1 where c1=2; count(*) 0 rollback; @@ -67,18 +42,15 @@ rollback; # Delete throw a view with limit (range access) # start transaction; -# Acces by range (quick_select), initied = INDEX -# +------+--------------------+------------+-------+---------------+---------------+---------+--------------------+------+-------------+ -# | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | -# +------+--------------------+------------+-------+---------------+---------------+---------+--------------------+------+-------------+ -# | 1 | PRIMARY | tab_delete | range | tab_delete_c1 | tab_delete_c1 | 4 | NULL | 550 | Using where | -# | 2 | DEPENDENT SUBQUERY | b | ref | tab_delete_c1 | tab_delete_c1 | 4 | test.tab_delete.c1 | 73 | Using index | -# +------+--------------------+------------+-------+---------------+---------------+---------+--------------------+------+-------------+ -delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 limit 1; +explain delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 limit 1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 range c1 c1 4 NULL 550 Using where +2 DEPENDENT SUBQUERY b ref c1 c1 4 test.t1.c1 82 Using index +delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 limit 1; affected rows: 1 -delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 limit 1; +delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 limit 1; affected rows: 0 -select count(*) from view_delete where c1=0; +select count(*) from v1 where c1=0; count(*) 499 rollback; @@ -86,41 +58,50 @@ rollback; # Delete throw a view (ALL access) # start transaction; -# Acces by pointer, initied = RND -# +------+--------------------+------------+------+---------------+---------------+---------+--------------------+------+-------------+ -# | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | -# +------+--------------------+------------+------+---------------+---------------+---------+--------------------+------+-------------+ -# | 1 | PRIMARY | tab_delete | ALL | tab_delete_c1 | NULL | NULL | NULL | 589 | Using where | -# | 2 | DEPENDENT SUBQUERY | b | ref | tab_delete_c1 | tab_delete_c1 | 4 | test.tab_delete.c1 | 295 | Using index | -# +------+--------------------+------------+------+---------------+---------------+---------+--------------------+------+-------------+ -delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 ; +explain delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL c1 NULL NULL NULL 717 Using where +2 DEPENDENT SUBQUERY b ref c1 c1 4 test.t1.c1 82 Using index +delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 ; affected rows: 500 -select count(*) from view_delete where c1=0; +select count(*) from v1 where c1=0; count(*) 0 rollback; # # Delete failed due to trigger # +create trigger trg after delete on t1 for each row +begin +declare c int; +begin +if old.c1 = 1 then +select count(*) into c from t1 where c1!=old.c1; +SIGNAL SQLSTATE '45000' set table_name=c; +end if; +end; +end; +/ start transaction; -delete from tab_delete where c1=1 and (select count(*) from tab_delete b where b.c1=tab_delete.c1) > 0 order by c2 asc limit 10; +delete from t1 where c1=1 and (select count(*) from t1 b where b.c1=t1.c1) > 0 order by c2 asc limit 10; ERROR 45000: Unhandled user-defined exception condition rollback; start transaction; -delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) > 0 order by c1 desc limit 100; +delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) > 0 order by c1 desc limit 100; ERROR 45000: Unhandled user-defined exception condition -select c1,count(*) from tab_delete group by c1; +select c1,count(*) from t1 group by c1; c1 count(*) 0 500 1 50 2 20 3 20 rollback; +drop trigger trg; # # Delete throw a view with returning # start transaction; -delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 order by c2 asc limit 10 returning c1,c2; +delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 order by c2 asc limit 10 returning c1,c2; c1 c2 0 1 0 2 @@ -134,7 +115,7 @@ c1 c2 0 10 rollback; start transaction; -delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 order by c2 desc limit 10 returning c1,c2; +delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 order by c2 desc limit 10 returning c1,c2; c1 c2 0 491 0 492 @@ -147,32 +128,24 @@ c1 c2 0 499 0 500 rollback; +drop view v1; +drop table t1; # # Delete from table with more than 150000 rows # -insert into tab_delete select * from tab_delete; -insert into tab_delete select * from tab_delete; -insert into tab_delete select * from tab_delete; -insert into tab_delete select * from tab_delete; -insert into tab_delete select * from tab_delete; -insert into tab_delete select * from tab_delete; -insert into tab_delete select * from tab_delete; -insert into tab_delete select * from tab_delete; -select count(*) from tab_delete; +create table t1(c1 integer not null,c2 integer not null, key (c1)); +insert t1 select 0,seq from seq_1_to_128000; +insert t1 select 1,seq from seq_1_to_25600; +select count(*) from t1; count(*) -151040 -with high memory for sort_buffer_size -SET SESSION sort_buffer_size = 1024000; -start transaction; -delete from tab_delete where c1=0 and exists(select 'x' from tab_delete b where b.c1<10); +153600 +# with a lot of memory for sort_buffer_size +set session sort_buffer_size = 1024000; +delete from t1 where c1=0 and exists(select 'x' from t1 b where b.c1<10); affected rows: 128000 -rollback; -with few memory for sort_buffer_size -SET SESSION sort_buffer_size = 1024; -start transaction; -delete from tab_delete where c1=0 and exists(select 'x' from tab_delete b where b.c1<10); +# with little memory for sort_buffer_size +insert t1 select 0,seq from seq_1_to_128000; +set session sort_buffer_size = 1024; +delete from t1 where c1=0 and exists(select 'x' from t1 b where b.c1<10); affected rows: 128000 -rollback; -drop procedure if exists gendata; -drop view if exists view_delete; -drop table if exists tab_delete; +drop table t1; diff --git a/mysql-test/r/lowercase_view.result b/mysql-test/r/lowercase_view.result index 3b6aeb9548b..6ccfe29b2cd 100644 --- a/mysql-test/r/lowercase_view.result +++ b/mysql-test/r/lowercase_view.result @@ -65,9 +65,6 @@ update v3aA set v3Aa.col1 = (select max(col1) from v2aA); ERROR HY000: The definition of table 'v2aA' prevents operation UPDATE on table 'v3aA' update v3aA set v3Aa.col1 = (select max(col1) from v3aA); ERROR HY000: Table 'v3aA' is specified twice, both as a target for 'UPDATE' and as a separate source for data -delete from v2Aa where col1 = (select max(col1) from v1Aa); -delete from v2aA where col1 = (select max(col1) from t1Aa); -delete from v2Aa where col1 = (select max(col1) from v2aA); delete v2Aa from v2aA,t2Aa where (select max(col1) from v1aA) > 0 and v2Aa.col1 = t2aA.col1; ERROR HY000: The definition of table 'v1aA' prevents operation DELETE on table 'v2aA' delete t1aA from t1Aa,t2Aa where (select max(col1) from v1Aa) > 0 and t1aA.col1 = t2aA.col1; diff --git a/mysql-test/r/merge.result b/mysql-test/r/merge.result index 6da7eb38655..f55251ac199 100644 --- a/mysql-test/r/merge.result +++ b/mysql-test/r/merge.result @@ -3742,48 +3742,6 @@ update m1 set a = ((select max(a) from v1)); ERROR HY000: The definition of table 'v1' prevents operation UPDATE on table 'm1' update m1 set a = ((select max(a) from tmp, v1)); ERROR HY000: The definition of table 'v1' prevents operation UPDATE on table 'm1' -delete from m1 where a = (select max(a) from m1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -delete from m1 where a = (select max(a) from m2); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -delete from m1 where a = (select max(a) from t1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -delete from m1 where a = (select max(a) from t2); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -delete from m1 where a = (select max(a) from t3, m1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -delete from m1 where a = (select max(a) from t3, m2); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -delete from m1 where a = (select max(a) from t3, t1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -delete from m1 where a = (select max(a) from t3, t2); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -delete from m1 where a = (select max(a) from tmp, m1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -delete from m1 where a = (select max(a) from tmp, m2); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -delete from m1 where a = (select max(a) from tmp, t1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -delete from m1 where a = (select max(a) from tmp, t2); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -delete from m1 where a = (select max(a) from v1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -delete from m1 where a = (select max(a) from tmp, v1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); drop view v1; drop temporary table tmp; drop table t1, t2, t3, m1, m2; diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index 50a48ade3bd..ef2e0935592 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -993,9 +993,6 @@ update v3 set v3.col1 = (select max(col1) from v2); ERROR HY000: The definition of table 'v2' prevents operation UPDATE on table 'v3' update v3 set v3.col1 = (select max(col1) from v3); ERROR HY000: Table 'v3' is specified twice, both as a target for 'UPDATE' and as a separate source for data -delete from v2 where col1 = (select max(col1) from v1); -delete from v2 where col1 = (select max(col1) from t1); -delete from v2 where col1 = (select max(col1) from v2); delete v2 from v2,t2 where (select max(col1) from v1) > 0 and v2.col1 = t2.col1; ERROR HY000: The definition of table 'v1' prevents operation DELETE on table 'v2' delete t1 from t1,t2 where (select max(col1) from v1) > 0 and t1.col1 = t2.col1; diff --git a/mysql-test/t/delete_use_source.test b/mysql-test/t/delete_use_source.test index ddd7858ee77..a54d12c3d65 100644 --- a/mysql-test/t/delete_use_source.test +++ b/mysql-test/t/delete_use_source.test @@ -1,37 +1,12 @@ --- source include/have_innodb.inc -set sql_mode=oracle; -use test; -create or replace table tab_delete(c1 integer not null,c2 integer not null) engine=InnoDb; -create index tab_delete_c1 on tab_delete(c1); -create or replace view view_delete as select * from tab_delete where c1 in (0,1); -delimiter /; -CREATE or replace PROCEDURE gendata(a int, count int ) AS - i INT:=0; -BEGIN - FOR i IN 1 .. count - LOOP - insert into tab_delete values (a,i); - END LOOP; -END; -/ -create or replace trigger trg after delete on tab_delete for each row -begin - declare c int; - begin - if old.c1 = 1 then - select count(*) into c from tab_delete where c1!=old.c1; - SIGNAL SQLSTATE '45000' set table_name=c; - end if; - end; -end; -/ -delimiter ;/ -set @count=500; -call gendata(0,@count); -call gendata(1,50); -call gendata(2,20); -call gendata(3,20); -commit; +--source include/have_sequence.inc +--source include/have_innodb.inc +create table t1(c1 integer not null,c2 integer not null, key (c1)) engine=InnoDb; +create view v1 as select * from t1 where c1 in (0,1); + +insert t1 select 0,seq from seq_1_to_500; +insert t1 select 1,seq from seq_1_to_50; +insert t1 select 2,seq from seq_1_to_20; +insert t1 select 3,seq from seq_1_to_20; --echo # --echo # Delete with limit (quick select - range acces) @@ -39,10 +14,10 @@ commit; start transaction; --enable_info -delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 limit 1; -delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 limit 1; +delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 limit 1; +delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 limit 1; --disable_info -select count(*) from view_delete where c1=0; +select count(*) from v1 where c1=0; rollback; --echo # @@ -50,9 +25,8 @@ rollback; --echo # start transaction; ---enable_info -delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 ; ---disable_info +--enable_info ONCE +delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 ; rollback; --echo # @@ -60,12 +34,12 @@ rollback; --echo # start transaction; -select count(*) from view_delete where c1=2; +select count(*) from v1 where c1=2; --enable_info -delete from tab_delete where c1=2 and exists(select 'x' from tab_delete b where b.c2<10); -delete from tab_delete where c1=2 and exists(select 'x' from tab_delete b where b.c2<10); +delete from t1 where c1=2 and exists(select 'x' from t1 b where b.c2<10); +delete from t1 where c1=2 and exists(select 'x' from t1 b where b.c2<10); --disable_info -select count(*) from view_delete where c1=2; +select count(*) from v1 where c1=2; rollback; --echo # @@ -73,19 +47,12 @@ rollback; --echo # start transaction; ---echo # Acces by range (quick_select), initied = INDEX ---echo # +------+--------------------+------------+-------+---------------+---------------+---------+--------------------+------+-------------+ ---echo # | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | ---echo # +------+--------------------+------------+-------+---------------+---------------+---------+--------------------+------+-------------+ ---echo # | 1 | PRIMARY | tab_delete | range | tab_delete_c1 | tab_delete_c1 | 4 | NULL | 550 | Using where | ---echo # | 2 | DEPENDENT SUBQUERY | b | ref | tab_delete_c1 | tab_delete_c1 | 4 | test.tab_delete.c1 | 73 | Using index | ---echo # +------+--------------------+------------+-------+---------------+---------------+---------+--------------------+------+-------------+ -# explain delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 limit 1; +explain delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 limit 1; --enable_info -delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 limit 1; -delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 limit 1; +delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 limit 1; +delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 limit 1; --disable_info -select count(*) from view_delete where c1=0; +select count(*) from v1 where c1=0; rollback; --echo # @@ -93,80 +60,73 @@ rollback; --echo # start transaction; ---echo # Acces by pointer, initied = RND ---echo # +------+--------------------+------------+------+---------------+---------------+---------+--------------------+------+-------------+ ---echo # | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | ---echo # +------+--------------------+------------+------+---------------+---------------+---------+--------------------+------+-------------+ ---echo # | 1 | PRIMARY | tab_delete | ALL | tab_delete_c1 | NULL | NULL | NULL | 589 | Using where | ---echo # | 2 | DEPENDENT SUBQUERY | b | ref | tab_delete_c1 | tab_delete_c1 | 4 | test.tab_delete.c1 | 295 | Using index | ---echo # +------+--------------------+------------+------+---------------+---------------+---------+--------------------+------+-------------+ -# explain delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500; ---enable_info -delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 ; ---disable_info -select count(*) from view_delete where c1=0; +explain delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500; +--enable_info ONCE +delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 ; +select count(*) from v1 where c1=0; rollback; - --echo # --echo # Delete failed due to trigger --echo # +delimiter /; +create trigger trg after delete on t1 for each row +begin + declare c int; + begin + if old.c1 = 1 then + select count(*) into c from t1 where c1!=old.c1; + SIGNAL SQLSTATE '45000' set table_name=c; + end if; + end; +end; +/ +delimiter ;/ + start transaction; ---enable_info --error ER_SIGNAL_EXCEPTION -delete from tab_delete where c1=1 and (select count(*) from tab_delete b where b.c1=tab_delete.c1) > 0 order by c2 asc limit 10; ---disable_info +delete from t1 where c1=1 and (select count(*) from t1 b where b.c1=t1.c1) > 0 order by c2 asc limit 10; rollback; start transaction; ---enable_info --error ER_SIGNAL_EXCEPTION -delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) > 0 order by c1 desc limit 100; ---disable_info -select c1,count(*) from tab_delete group by c1; +delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) > 0 order by c1 desc limit 100; +select c1,count(*) from t1 group by c1; rollback; +drop trigger trg; + --echo # --echo # Delete throw a view with returning --echo # start transaction; -delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 order by c2 asc limit 10 returning c1,c2; +delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 order by c2 asc limit 10 returning c1,c2; rollback; start transaction; -delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 order by c2 desc limit 10 returning c1,c2; +delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 order by c2 desc limit 10 returning c1,c2; rollback; +drop view v1; +drop table t1; --echo # --echo # Delete from table with more than 150000 rows --echo # -insert into tab_delete select * from tab_delete; -insert into tab_delete select * from tab_delete; -insert into tab_delete select * from tab_delete; -insert into tab_delete select * from tab_delete; -insert into tab_delete select * from tab_delete; -insert into tab_delete select * from tab_delete; -insert into tab_delete select * from tab_delete; -insert into tab_delete select * from tab_delete; -select count(*) from tab_delete; +create table t1(c1 integer not null,c2 integer not null, key (c1)); +insert t1 select 0,seq from seq_1_to_128000; +insert t1 select 1,seq from seq_1_to_25600; +select count(*) from t1; ---echo with high memory for sort_buffer_size -SET SESSION sort_buffer_size = 1024000; -start transaction; ---enable_info -delete from tab_delete where c1=0 and exists(select 'x' from tab_delete b where b.c1<10); ---disable_info -rollback; +--echo # with a lot of memory for sort_buffer_size +set session sort_buffer_size = 1024000; +--enable_info ONCE +delete from t1 where c1=0 and exists(select 'x' from t1 b where b.c1<10); ---echo with few memory for sort_buffer_size -SET SESSION sort_buffer_size = 1024; -start transaction; ---enable_info -delete from tab_delete where c1=0 and exists(select 'x' from tab_delete b where b.c1<10); ---disable_info -rollback; +--echo # with little memory for sort_buffer_size +insert t1 select 0,seq from seq_1_to_128000; +set session sort_buffer_size = 1024; +--enable_info ONCE +delete from t1 where c1=0 and exists(select 'x' from t1 b where b.c1<10); -drop procedure if exists gendata; -drop view if exists view_delete; -drop table if exists tab_delete; +drop table t1; diff --git a/mysql-test/t/lowercase_view.test b/mysql-test/t/lowercase_view.test index 6d53e6d9130..4c91383db60 100644 --- a/mysql-test/t/lowercase_view.test +++ b/mysql-test/t/lowercase_view.test @@ -73,10 +73,6 @@ update v3aA set v3Aa.col1 = (select max(col1) from t1aA); update v3aA set v3Aa.col1 = (select max(col1) from v2aA); -- error 1093 update v3aA set v3Aa.col1 = (select max(col1) from v3aA); -# Works since MDEV-12137 (no more error 1093) -delete from v2Aa where col1 = (select max(col1) from v1Aa); -delete from v2aA where col1 = (select max(col1) from t1Aa); -delete from v2Aa where col1 = (select max(col1) from v2aA); -- error 1443 delete v2Aa from v2aA,t2Aa where (select max(col1) from v1aA) > 0 and v2Aa.col1 = t2aA.col1; -- error 1443 diff --git a/mysql-test/t/merge.test b/mysql-test/t/merge.test index c35dd39170b..95c78caf034 100644 --- a/mysql-test/t/merge.test +++ b/mysql-test/t/merge.test @@ -2763,66 +2763,6 @@ update m1 set a = ((select max(a) from tmp, t2)); update m1 set a = ((select max(a) from v1)); --error ER_VIEW_PREVENT_UPDATE update m1 set a = ((select max(a) from tmp, v1)); -# Works since MDEV-12137 -# previously :ER_UPDATE_TABLE_USED -delete from m1 where a = (select max(a) from m1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -# previously :ER_UPDATE_TABLE_USED -delete from m1 where a = (select max(a) from m2); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -# previously :ER_UPDATE_TABLE_USED -delete from m1 where a = (select max(a) from t1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -# previously :ER_UPDATE_TABLE_USED -delete from m1 where a = (select max(a) from t2); -insert into t1 (a) values (1); -insert into t2 (a) values (1); - -# previously :ER_UPDATE_TABLE_USED -delete from m1 where a = (select max(a) from t3, m1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -# previously :ER_UPDATE_TABLE_USED -delete from m1 where a = (select max(a) from t3, m2); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -# previously :ER_UPDATE_TABLE_USED -delete from m1 where a = (select max(a) from t3, t1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -# previously :ER_UPDATE_TABLE_USED -delete from m1 where a = (select max(a) from t3, t2); -insert into t1 (a) values (1); -insert into t2 (a) values (1); - -# previously :ER_UPDATE_TABLE_USED -delete from m1 where a = (select max(a) from tmp, m1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -# previously :ER_UPDATE_TABLE_USED -delete from m1 where a = (select max(a) from tmp, m2); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -# previously :ER_UPDATE_TABLE_USED -delete from m1 where a = (select max(a) from tmp, t1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -# previously :ER_UPDATE_TABLE_USED -delete from m1 where a = (select max(a) from tmp, t2); -insert into t1 (a) values (1); -insert into t2 (a) values (1); - -# previously : ER_VIEW_PREVENT_UPDATE -delete from m1 where a = (select max(a) from v1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); -# previously : ER_VIEW_PREVENT_UPDATE -delete from m1 where a = (select max(a) from tmp, v1); -insert into t1 (a) values (1); -insert into t2 (a) values (1); drop view v1; drop temporary table tmp; diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index 9ed37a2bf65..00a040ccfdc 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -914,13 +914,6 @@ update v3 set v3.col1 = (select max(col1) from t1); update v3 set v3.col1 = (select max(col1) from v2); -- error ER_UPDATE_TABLE_USED update v3 set v3.col1 = (select max(col1) from v3); -# Works since MDEV-12137 -# Previously error ER_VIEW_PREVENT_UPDATE -delete from v2 where col1 = (select max(col1) from v1); -# Previously error ER_VIEW_PREVENT_UPDATE -delete from v2 where col1 = (select max(col1) from t1); -# Previously error ER_UPDATE_TABLE_USED -delete from v2 where col1 = (select max(col1) from v2); -- error ER_VIEW_PREVENT_UPDATE delete v2 from v2,t2 where (select max(col1) from v1) > 0 and v2.col1 = t2.col1; -- error ER_VIEW_PREVENT_UPDATE diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index 8d4757e6d31..a9275bd39fd 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -45,7 +45,7 @@ // end_read_record #include "sql_partition.h" // make_used_partitions_str -#define MEM_STRIP_BUF_SIZE current_thd->variables.sortbuff_size +#define MEM_STRIP_BUF_SIZE thd->variables.sortbuff_size /* @brief @@ -214,6 +214,22 @@ void Update_plan::save_explain_data_intern(MEM_ROOT *mem_root, } +static bool record_should_be_deleted(THD *thd, TABLE *table, SQL_SELECT *sel, + Explain_delete *explain) +{ + explain->tracker.on_record_read(); + thd->inc_examined_row_count(1); + if (table->vfield) + (void) table->update_virtual_fields(table->file, VCOL_UPDATE_FOR_DELETE); + if (!sel || sel->skip_record(thd) > 0) + { + explain->tracker.on_record_after_where(); + return true; + } + return false; +} + + /** Implement DELETE SQL word. @@ -246,12 +262,12 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, bool with_select= !select_lex->item_list.is_empty(); Explain_delete *explain; Delete_plan query_plan(thd->mem_root); + Unique * deltempfile= NULL; + bool delete_record, delete_while_scanning; + DBUG_ENTER("mysql_delete"); + query_plan.index= MAX_KEY; query_plan.using_filesort= FALSE; - Unique * deltempfile= NULL; - uint delete_while_scanning= 1; - uint delete_record= 0; - DBUG_ENTER("mysql_delete"); create_explain_query(thd->lex, thd->mem_root); if (open_and_lock_tables(thd, table_list, TRUE, 0)) @@ -281,7 +297,7 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, if (mysql_prepare_delete(thd, table_list, select_lex->with_wild, select_lex->item_list, &conds, - delete_while_scanning)) + &delete_while_scanning)) DBUG_RETURN(TRUE); if (with_select) @@ -562,67 +578,47 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, explain= (Explain_delete*)thd->lex->explain->get_upd_del_plan(); explain->tracker.on_scan_init(); - if (delete_while_scanning == 0) + if (!delete_while_scanning) { /* - The table we are going to delete appears in join. - Instead of deleting the rows, first mark them deleted. + The table we are going to delete appears in subqueries in the where + clause. Instead of deleting the rows, first mark them deleted. */ ha_rows tmplimit=limit; - deltempfile= new Unique (refpos_order_cmp, - (void *) table->file, - table->file->ref_length, - MEM_STRIP_BUF_SIZE); + deltempfile= new (thd->mem_root) Unique (refpos_order_cmp, table->file, + table->file->ref_length, + MEM_STRIP_BUF_SIZE); while (!(error=info.read_record(&info)) && !thd->killed && ! thd->is_error()) { - explain->tracker.on_record_read(); - thd->inc_examined_row_count(1); - if (table->vfield) - (void) table->update_virtual_fields(table->file, - VCOL_UPDATE_FOR_DELETE); - if (!select || select->skip_record(thd) > 0) + if (record_should_be_deleted(thd, table, select, explain)) { - explain->tracker.on_record_after_where(); table->file->position(table->record[0]); if ((error= deltempfile->unique_add((char*) table->file->ref))) { error= 1; goto terminate_delete; } - if (!--tmplimit && using_limit) - { - break; - } + if (!--tmplimit && using_limit) + break; } } end_read_record(&info); - if (deltempfile->get(table) || - table->file->ha_index_or_rnd_end() || - init_read_record(&info, thd, table, NULL , &deltempfile->sort, 0, 1, - FALSE)) + if (deltempfile->get(table) || table->file->ha_index_or_rnd_end() || + init_read_record(&info, thd, table, 0, &deltempfile->sort, 0, 1, false)) { error= 1; goto terminate_delete; } - delete_record= 1; + delete_record= true; } while (!(error=info.read_record(&info)) && !thd->killed && ! thd->is_error()) { - if (delete_while_scanning == 1) - { - explain->tracker.on_record_read(); - thd->inc_examined_row_count(1); - if (table->vfield) - (void) table->update_virtual_fields(table->file, - VCOL_UPDATE_FOR_DELETE); - delete_record=(!select || select->skip_record(thd) > 0) ? 1 : 0; - if (delete_record) - explain->tracker.on_record_after_where(); - } - if (delete_record == 1) + if (delete_while_scanning) + delete_record= record_should_be_deleted(thd, table, select, explain); + if (delete_record) { if (table->triggers && table->triggers->process_triggers(thd, TRG_EVENT_DELETE, @@ -808,13 +804,14 @@ l */ int mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, uint wild_num, List &field_list, Item **conds, - uint &delete_while_scanning) + bool *delete_while_scanning) { Item *fake_conds= 0; SELECT_LEX *select_lex= &thd->lex->select_lex; DBUG_ENTER("mysql_prepare_delete"); List all_fields; + *delete_while_scanning= true; thd->lex->allow_sum_func= 0; if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context, &thd->lex->select_lex.top_join_list, @@ -834,11 +831,9 @@ l my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias, "DELETE"); DBUG_RETURN(TRUE); } - { - TABLE_LIST *duplicate; - if ((duplicate= unique_table(thd, table_list, table_list->next_global, 0))) - delete_while_scanning= 0; - } + + if (unique_table(thd, table_list, table_list->next_global, 0)) + *delete_while_scanning= false; if (select_lex->inner_refs_list.elements && fix_inner_refs(thd, all_fields, select_lex, select_lex->ref_pointer_array)) @@ -1002,7 +997,7 @@ multi_delete::initialize_tables(JOIN *join) DBUG_RETURN(1); table_map tables_to_delete_from=0; - delete_while_scanning= 1; + delete_while_scanning= true; for (walk= delete_tables; walk; walk= walk->next_local) { TABLE_LIST *tbl= walk->correspondent_table->find_table_for_update(); @@ -1015,7 +1010,7 @@ multi_delete::initialize_tables(JOIN *join) in join, we need to defer delete. So the delete doesn't interfers with the scaning of results. */ - delete_while_scanning= 0; + delete_while_scanning= false; } } @@ -1051,7 +1046,7 @@ multi_delete::initialize_tables(JOIN *join) case send_data() shouldn't delete any rows a we may touch the rows in the deleted table many times */ - delete_while_scanning= 0; + delete_while_scanning= false; } } walk= delete_tables; @@ -1064,10 +1059,9 @@ multi_delete::initialize_tables(JOIN *join) for (;walk ;walk= walk->next_local) { TABLE *table=walk->table; - *tempfiles_ptr++= new Unique (refpos_order_cmp, - (void *) table->file, - table->file->ref_length, - MEM_STRIP_BUF_SIZE); + *tempfiles_ptr++= new (thd->mem_root) Unique (refpos_order_cmp, table->file, + table->file->ref_length, + MEM_STRIP_BUF_SIZE); } init_ftfuncs(thd, thd->lex->current_select, 1); DBUG_RETURN(thd->is_fatal_error != 0); diff --git a/sql/sql_delete.h b/sql/sql_delete.h index d49b0114c52..54d6cf146b5 100644 --- a/sql/sql_delete.h +++ b/sql/sql_delete.h @@ -28,7 +28,7 @@ template class SQL_I_List; int mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, uint wild_num, List &field_list, Item **conds, - uint &delete_while_scanning); + bool *delete_while_scanning); bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, SQL_I_List *order, ha_rows rows, ulonglong options, select_result *result); diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc index dc0dcbac43f..1095c6ead99 100644 --- a/sql/sql_prepare.cc +++ b/sql/sql_prepare.cc @@ -1496,7 +1496,7 @@ static bool mysql_test_delete(Prepared_statement *stmt, uint table_count= 0; THD *thd= stmt->thd; LEX *lex= stmt->lex; - uint delete_while_scanning=1; + bool delete_while_scanning; DBUG_ENTER("mysql_test_delete"); if (delete_precheck(thd, table_list) || @@ -1526,7 +1526,7 @@ static bool mysql_test_delete(Prepared_statement *stmt, lex->select_lex.with_wild, lex->select_lex.item_list, &lex->select_lex.where, - delete_while_scanning)); + &delete_while_scanning)); error: DBUG_RETURN(TRUE); } From f3ad96a3a619eb9b0650a3579dac6ab684674043 Mon Sep 17 00:00:00 2001 From: Sergei Golubchik Date: Sat, 8 Jul 2017 01:38:56 +0200 Subject: [PATCH 3/8] fix the bad merge this fixes vcol.upgrade failure --- sql/item.cc | 7 ------- 1 file changed, 7 deletions(-) diff --git a/sql/item.cc b/sql/item.cc index 08ee969e900..d25d1110910 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -1011,13 +1011,6 @@ bool Item_field::check_field_expression_processor(void *arg) Field *org_field= (Field*) arg; if (field->flags & NO_DEFAULT_VALUE_FLAG) return 0; - if (field->flags & AUTO_INCREMENT_FLAG) - { - my_error(ER_EXPRESSION_REFERS_TO_UNINIT_FIELD, - MYF(0), - org_field->field_name.str, field->field_name.str); - return 1; - } if ((field->default_value && field->default_value->flags) || field->vcol_info) { if (field == org_field || From 77ace5dbd1a4ef9df63dd35707178da2d3443a1e Mon Sep 17 00:00:00 2001 From: Alexander Barkov Date: Mon, 10 Jul 2017 10:51:07 +0400 Subject: [PATCH 4/8] (partial) MDEV-12518 Unify sql_yacc.yy and sql_yacc_ora.yy This is a partial patch for MDEV-12518 unifying: - Data types: row_type_body vs field_type_row Fixing sql_yacc_ora.yy: removing field_type_row adding row_type_body instead --- sql/sql_yacc.yy | 2 +- sql/sql_yacc_ora.yy | 59 +++++++++++++++++++++++---------------------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index c2739d1d2fa..52a877c28ec 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -3256,7 +3256,7 @@ row_field_definition_list: ; row_type_body: - '(' row_field_definition_list ')' { $$= $2; } + '(' row_field_definition_list ')' { $$= $2; } ; sp_decl_idents_init_vars: diff --git a/sql/sql_yacc_ora.yy b/sql/sql_yacc_ora.yy index 0ae8c4e11d9..90d7e686c43 100644 --- a/sql/sql_yacc_ora.yy +++ b/sql/sql_yacc_ora.yy @@ -1095,7 +1095,6 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); case_stmt_body opt_bin_mod opt_if_exists_table_element opt_if_not_exists_table_element opt_recursive - type_or_rowtype %type create_or_replace @@ -1353,7 +1352,8 @@ END_OF_INPUT %type view_algorithm view_check_option %type view_suid opt_view_suid -%type sp_decl_idents sp_handler_type sp_hcond_list +%type sp_decl_idents sp_decl_idents_init_vars +%type sp_handler_type sp_hcond_list %type sp_cond sp_hcond sqlstate signal_value opt_signal_value %type sp_decl_body_list opt_sp_decl_body_list %type sp_decl_non_handler sp_decl_non_handler_list @@ -1388,7 +1388,7 @@ END_OF_INPUT %type condition_information; %type row_field_name row_field_definition -%type row_field_definition_list field_type_row +%type row_field_definition_list row_type_body %type opt_window_clause window_def_list window_def window_spec %type window_name @@ -2548,13 +2548,13 @@ sp_param_name_and_type: { Lex->sphead->fill_spvar_using_type_reference($$= $1, $2); } - | sp_param_name field_type_row + | sp_param_name ROW_SYM row_type_body { $$= $1; $$->field_def.field_name= $$->name; Lex->sphead->fill_spvar_definition(thd, &$$->field_def); - Lex->sphead->row_fill_field_definitions(thd, $2); - $$->field_def.set_row_field_definitions($2); + Lex->sphead->row_fill_field_definitions(thd, $3); + $$->field_def.set_row_field_definitions($3); } ; @@ -2580,13 +2580,13 @@ sp_pdparam: { Lex->sphead->fill_spvar_using_type_reference($1, $3); } - | sp_param_name sp_opt_inout field_type_row + | sp_param_name sp_opt_inout ROW_SYM row_type_body { $1->mode= $2; $1->field_def.field_name= $1->name; Lex->sphead->fill_spvar_definition(thd, &$1->field_def); - Lex->sphead->row_fill_field_definitions(thd, $3); - $1->field_def.set_row_field_definitions($3); + Lex->sphead->row_fill_field_definitions(thd, $4); + $1->field_def.set_row_field_definitions($4); } ; @@ -2769,46 +2769,45 @@ row_field_definition_list: } ; -field_type_row: - ROW_SYM '(' row_field_definition_list ')' { $$= $3; } +row_type_body: + '(' row_field_definition_list ')' { $$= $2; } ; -type_or_rowtype: - TYPE_SYM { $$= 0; } - | ROWTYPE_SYM { $$= 1; } - ; - -sp_decl_non_handler: +sp_decl_idents_init_vars: sp_decl_idents { Lex->sp_variable_declarations_init(thd, $1); } + ; + +sp_decl_non_handler: + sp_decl_idents_init_vars type_with_opt_collate sp_opt_default { if (Lex->sp_variable_declarations_finalize(thd, $1, - &Lex->last_field[0], $4)) + &Lex->last_field[0], $3)) MYSQL_YYABORT; $$.init_using_vars($1); } - | sp_decl_idents - { - Lex->sp_variable_declarations_init(thd, $1); - } - optionally_qualified_column_ident '%' type_or_rowtype + | sp_decl_idents_init_vars + optionally_qualified_column_ident '%' TYPE_SYM sp_opt_default { - if ($5 ? - Lex->sp_variable_declarations_rowtype_finalize(thd, $1, $3, $6) : - Lex->sp_variable_declarations_with_ref_finalize(thd, $1, $3, $6)) + if (Lex->sp_variable_declarations_with_ref_finalize(thd, $1, $2, $5)) MYSQL_YYABORT; $$.init_using_vars($1); } - | sp_decl_idents + | sp_decl_idents_init_vars + optionally_qualified_column_ident '%' ROWTYPE_SYM + sp_opt_default { - Lex->sp_variable_declarations_init(thd, $1); + if (Lex->sp_variable_declarations_rowtype_finalize(thd, $1, $2, $5)) + MYSQL_YYABORT; + $$.init_using_vars($1); } - field_type_row + | sp_decl_idents_init_vars + ROW_SYM row_type_body sp_opt_default { if (Lex->sp_variable_declarations_row_finalize(thd, $1, $3, $4)) @@ -4651,9 +4650,11 @@ size_number: case 'g': case 'G': text_shift_number+=10; + /* fall through */ case 'm': case 'M': text_shift_number+=10; + /* fall through */ case 'k': case 'K': text_shift_number+=10; From 42cb3dcb7414b2e7cd9e77f1294abb4cd9ce87dc Mon Sep 17 00:00:00 2001 From: Alexander Barkov Date: Mon, 10 Jul 2017 12:24:58 +0400 Subject: [PATCH 5/8] (partial) MDEV-12518 Unify sql_yacc.yy and sql_yacc_ora.yy Adding keyword_sp_verb_clause into sql_yacc.yy and sql_yacc_ora.yy --- sql/sql_yacc.yy | 38 +++++++++++++++++++++++++------------- sql/sql_yacc_ora.yy | 36 ++++++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 52a877c28ec..70a9299ec3d 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -14548,27 +14548,23 @@ user: user_maybe_role /* Keyword that we allow for identifiers (except SP labels) */ keyword: keyword_sp {} + | keyword_sp_verb_clause{} | ASCII_SYM {} | BACKUP_SYM {} - | BEGIN_SYM {} | BINLOG_SYM {} | BYTE_SYM {} | CACHE_SYM {} | CHARSET {} | CHECKSUM_SYM {} | CHECKPOINT_SYM {} - | CLOSE_SYM {} | COLUMN_ADD_SYM {} | COLUMN_CHECK_SYM {} | COLUMN_CREATE_SYM {} | COLUMN_DELETE_SYM {} | COLUMN_GET_SYM {} | COMMENT_SYM {} - | COMMIT_SYM {} | CONTAINS_SYM {} | DEALLOCATE_SYM {} - | DO_SYM {} - | END {} | EXAMINED_SYM {} | EXCLUDE_SYM {} | EXECUTE_SYM {} @@ -14577,13 +14573,11 @@ keyword: | FOLLOWING_SYM {} | FORMAT_SYM {} | GET_SYM {} - | HANDLER_SYM {} | HELP_SYM {} | HOST_SYM {} | INSTALL_SYM {} | LANGUAGE_SYM {} | NO_SYM {} - | OPEN_SYM {} | OPTION {} | OPTIONS_SYM {} | OTHERS_SYM {} @@ -14594,14 +14588,10 @@ keyword: | PRECEDING_SYM {} | PREPARE_SYM {} | REMOVE_SYM {} - | REPAIR {} | RESET_SYM {} | RESTORE_SYM {} - | ROLLBACK_SYM {} - | SAVEPOINT_SYM {} | SECURITY_SYM {} | SERVER_SYM {} - | SHUTDOWN {} | SIGNED_SYM {} | SOCKET_SYM {} | SLAVE {} @@ -14611,7 +14601,6 @@ keyword: | STOP_SYM {} | STORED_SYM {} | TIES_SYM {} - | TRUNCATE_SYM {} | UNICODE_SYM {} | UNINSTALL_SYM {} | UNBOUNDED_SYM {} @@ -14632,6 +14621,29 @@ keyword_sp: ; +/* + Keywords that start a statement. + Generally allowed as identifiers (e.g. table, column names) + - not allowed as SP label names + - not allowed as variable names in Oracle-style assignments: + xxx:=10 +*/ +keyword_sp_verb_clause: + BEGIN_SYM { /* Compound. Reserved in Oracle */ } + | CLOSE_SYM { /* Verb clause. Reserved in Oracle */ } + | COMMIT_SYM { /* Verb clause. Reserved in Oracle */ } + | DO_SYM { /* Verb clause */ } + | END { /* Compound. Reserved in Oracle */ } + | HANDLER_SYM { /* Verb clause */ } + | OPEN_SYM { /* Verb clause. Reserved in Oracle */ } + | REPAIR { /* Verb clause */ } + | ROLLBACK_SYM { /* Verb clause. Reserved in Oracle */ } + | SAVEPOINT_SYM { /* Verb clause. Reserved in Oracle */ } + | SHUTDOWN { /* Verb clause */ } + | TRUNCATE_SYM { /* Verb clause. Reserved in Oracle */ } + ; + + /* These keywords are generally allowed as identifiers, but not allowed as non-delimited SP variable names in sql_mode=ORACLE. @@ -14647,6 +14659,7 @@ keyword_sp_data_type: | FIXED_SYM {} | GEOMETRYCOLLECTION {} | GEOMETRY_SYM {} + | JSON_SYM {} | LINESTRING {} | MEDIUM_SYM {} | MULTILINESTRING {} @@ -14785,7 +14798,6 @@ keyword_sp_not_data_type: | ISOLATION {} | ISOPEN_SYM {} | ISSUER_SYM {} - | JSON_SYM {} | INSERT_METHOD {} | KEY_BLOCK_SIZE {} | LAST_VALUE {} diff --git a/sql/sql_yacc_ora.yy b/sql/sql_yacc_ora.yy index 90d7e686c43..8aed87cca8a 100644 --- a/sql/sql_yacc_ora.yy +++ b/sql/sql_yacc_ora.yy @@ -14800,20 +14800,9 @@ keyword_directly_not_assignable: | LANGUAGE_SYM { /* SP characteristic */ } | NO_SYM { /* SP characteristic */ } | CHARSET { /* SET CHARSET utf8; */ } - | DO_SYM { /* Verb clause */ } - | REPAIR { /* Verb clause */ } - | HANDLER_SYM { /* Verb clause */ } - | CLOSE_SYM { /* Verb clause. Reserved in Oracle */ } - | OPEN_SYM { /* Verb clause. Reserved in Oracle */ } - | SAVEPOINT_SYM { /* Verb clause. Reserved in Oracle */ } - | TRUNCATE_SYM { /* Verb clause. Reserved in Oracle */ } - | BEGIN_SYM { /* Compound. Reserved in Oracle */ } - | END { /* Compound. Reserved in Oracle */ } | FOLLOWS_SYM { /* Conflicts with assignment in FOR EACH */} | PRECEDES_SYM { /* Conflicts with assignment in FOR EACH */} - | COMMIT_SYM { /* Verb clause. Reserved in Oracle */ } - | ROLLBACK_SYM { /* Verb clause. Reserver in Oracle */ } - | SHUTDOWN { /* Verb clause */ } + | keyword_sp_verb_clause ; /* @@ -14828,6 +14817,29 @@ keyword_sp: ; +/* + Keywords that start a statement. + Generally allowed as identifiers (e.g. table, column names) + - not allowed as SP label names + - not allowed as variable names in Oracle-style assignments: + xxx:=10 +*/ +keyword_sp_verb_clause: + BEGIN_SYM { /* Compound. Reserved in Oracle */ } + | CLOSE_SYM { /* Verb clause. Reserved in Oracle */ } + | COMMIT_SYM { /* Verb clause. Reserved in Oracle */ } + | DO_SYM { /* Verb clause */ } + | END { /* Compound. Reserved in Oracle */ } + | HANDLER_SYM { /* Verb clause */ } + | OPEN_SYM { /* Verb clause. Reserved in Oracle */ } + | REPAIR { /* Verb clause */ } + | ROLLBACK_SYM { /* Verb clause. Reserved in Oracle */ } + | SAVEPOINT_SYM { /* Verb clause. Reserved in Oracle */ } + | SHUTDOWN { /* Verb clause */ } + | TRUNCATE_SYM { /* Verb clause. Reserved in Oracle */ } + ; + + /* These keywords are generally allowed as identifiers, but not allowed as non-delimited SP variable names in sql_mode=ORACLE. From 59350ce07646c9c93ca4504dbe3e976a65e3715b Mon Sep 17 00:00:00 2001 From: Alexander Barkov Date: Tue, 11 Jul 2017 15:10:25 +0400 Subject: [PATCH 6/8] MDEV-13292 Move the code from sp_head::init() to sp_head::sp_head() --- sql/sp_head.cc | 53 +++++++++++++++----------------------------------- sql/sp_head.h | 2 +- sql/sql_lex.cc | 3 +-- 3 files changed, 18 insertions(+), 40 deletions(-) diff --git a/sql/sp_head.cc b/sql/sp_head.cc index c6fd461b9d5..973eeef0165 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -540,15 +540,30 @@ sp_head::operator delete(void *ptr, size_t size) throw() } -sp_head::sp_head() +sp_head::sp_head(stored_procedure_type type) :Query_arena(&main_mem_root, STMT_INITIALIZED_FOR_SP), Database_qualified_name(&null_clex_str, &null_clex_str), + m_type(type), m_flags(0), + m_explicit_name(false), + /* + FIXME: the only use case when name is NULL is events, and it should + be rewritten soon. Remove the else part and replace 'if' with + an assert when this is done. + */ + m_qname(null_clex_str), + m_params(null_clex_str), + m_body(null_clex_str), + m_body_utf8(null_clex_str), + m_defstr(null_clex_str), m_sp_cache_version(0), m_creation_ctx(0), unsafe_flags(0), m_recursion_level(0), m_next_cached_sp(0), + m_param_begin(NULL), + m_param_end(NULL), + m_body_begin(NULL), m_cont_level(0) { m_first_instance= this; @@ -556,12 +571,6 @@ sp_head::sp_head() m_last_cached_sp= this; m_return_field_def.charset = NULL; - /* - FIXME: the only use case when name is NULL is events, and it should - be rewritten soon. Remove the else part and replace 'if' with - an assert when this is done. - */ - m_qname= null_clex_str; DBUG_ENTER("sp_head::sp_head"); @@ -573,9 +582,6 @@ sp_head::sp_head() my_hash_init(&m_sroutines, system_charset_info, 0, 0, 0, sp_sroutine_key, 0, 0); - m_body_utf8.str= NULL; - m_body_utf8.length= 0; - DBUG_VOID_RETURN; } @@ -597,33 +603,6 @@ sp_head::init(LEX *lex) lex->trg_table_fields.empty(); my_init_dynamic_array(&m_instr, sizeof(sp_instr *), 16, 8, MYF(0)); - m_param_begin= NULL; - m_param_end= NULL; - - m_body_begin= NULL ; - - m_qname.str= NULL; - m_qname.length= 0; - - m_explicit_name= false; - - m_db.str= NULL; - m_db.length= 0; - - m_name.str= NULL; - m_name.length= 0; - - m_params.str= NULL; - m_params.length= 0; - - m_body.str= NULL; - m_body.length= 0; - - m_defstr.str= NULL; - m_defstr.length= 0; - - m_return_field_def.charset= NULL; - DBUG_VOID_RETURN; } diff --git a/sql/sp_head.h b/sql/sp_head.h index 9c6b69f0ab8..cf05d47a95c 100644 --- a/sql/sp_head.h +++ b/sql/sp_head.h @@ -303,7 +303,7 @@ public: static void operator delete(void *ptr, size_t size) throw (); - sp_head(); + sp_head(stored_procedure_type type); /// Initialize after we have reset mem_root void diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 07534b7e7a6..20e5fc0b450 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -5825,11 +5825,10 @@ sp_head *LEX::make_sp_head(THD *thd, sp_name *name, sp_head *sp; /* Order is important here: new - reset - init */ - if ((sp= new sp_head())) + if ((sp= new sp_head(type))) { sp->reset_thd_mem_root(thd); sp->init(this); - sp->m_type= type; if (name) sp->init_sp_name(thd, name); sp->m_chistics= &sp_chistics; From 31b35118490357063047755d849ab4c8687ce938 Mon Sep 17 00:00:00 2001 From: Alexander Barkov Date: Tue, 11 Jul 2017 16:16:11 +0400 Subject: [PATCH 7/8] Fixing a type-clash bison warning in keyword_directly_not_assignable The warning was introduced by the patch that added the keyword_sp_verb_clause rule. --- sql/sql_yacc_ora.yy | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sql/sql_yacc_ora.yy b/sql/sql_yacc_ora.yy index 8aed87cca8a..d720cd18faf 100644 --- a/sql/sql_yacc_ora.yy +++ b/sql/sql_yacc_ora.yy @@ -14796,13 +14796,13 @@ keyword_directly_assignable: CREATE TRIGGER .. FOR EACH ROW FOLLOWS tr1 a:= 10; */ keyword_directly_not_assignable: - CONTAINS_SYM { /* SP characteristic */ } - | LANGUAGE_SYM { /* SP characteristic */ } - | NO_SYM { /* SP characteristic */ } - | CHARSET { /* SET CHARSET utf8; */ } - | FOLLOWS_SYM { /* Conflicts with assignment in FOR EACH */} - | PRECEDES_SYM { /* Conflicts with assignment in FOR EACH */} - | keyword_sp_verb_clause + CONTAINS_SYM { /* SP characteristic */ } + | LANGUAGE_SYM { /* SP characteristic */ } + | NO_SYM { /* SP characteristic */ } + | CHARSET { /* SET CHARSET utf8; */ } + | FOLLOWS_SYM { /* Conflicts with assignment in FOR EACH */} + | PRECEDES_SYM { /* Conflicts with assignment in FOR EACH */} + | keyword_sp_verb_clause { } ; /* From 7c3df72d0a627aa6c12af14d57b2664af7f8b380 Mon Sep 17 00:00:00 2001 From: Alexander Barkov Date: Wed, 12 Jul 2017 11:57:47 +0400 Subject: [PATCH 8/8] MDEV-13298 Change sp_head::m_chistics from a pointer to a structure --- sql/event_data_objects.cc | 8 ++++- sql/field.h | 2 ++ sql/item_func.cc | 12 +++---- sql/sp.cc | 72 ++++++++++++++++++--------------------- sql/sp.h | 2 +- sql/sp_head.cc | 30 ++++++++++------ sql/sp_head.h | 19 +++++++++-- sql/sql_lex.cc | 5 ++- sql/sql_lex.h | 8 +++++ sql/sql_parse.cc | 2 +- sql/sql_trigger.cc | 10 +++--- sql/sql_yacc.yy | 7 ++-- sql/sql_yacc_ora.yy | 7 ++-- sql/wsrep_mysqld.cc | 2 +- 14 files changed, 110 insertions(+), 76 deletions(-) diff --git a/sql/event_data_objects.cc b/sql/event_data_objects.cc index 86bdadabc17..2ab06fb34f2 100644 --- a/sql/event_data_objects.cc +++ b/sql/event_data_objects.cc @@ -1426,7 +1426,13 @@ Event_job_data::execute(THD *thd, bool drop) sphead->m_flags|= sp_head::LOG_SLOW_STATEMENTS; sphead->m_flags|= sp_head::LOG_GENERAL_LOG; - sphead->set_info(0, 0, &thd->lex->sp_chistics, sql_mode); + /* + construct_sp_sql() + parse_sql() set suid to SP_IS_NOT_SUID, + because we have the security context already set to the event + definer here. See more comments in construct_sp_sql(). + */ + DBUG_ASSERT(sphead->suid() == SP_IS_NOT_SUID); + sphead->m_sql_mode= sql_mode; sphead->set_creation_ctx(creation_ctx); sphead->optimize(); diff --git a/sql/field.h b/sql/field.h index 92fc9b4cca8..208941b3daa 100644 --- a/sql/field.h +++ b/sql/field.h @@ -818,6 +818,8 @@ public: { return store(ls->str, ls->length, cs); } int store(const LEX_CSTRING *ls, CHARSET_INFO *cs) { return store(ls->str, ls->length, cs); } + int store(const LEX_CSTRING &ls, CHARSET_INFO *cs) + { return store(ls.str, ls.length, cs); } virtual double val_real(void)=0; virtual longlong val_int(void)=0; virtual bool val_bool(void)= 0; diff --git a/sql/item_func.cc b/sql/item_func.cc index f56ede71b40..b44b29c15a5 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -6316,7 +6316,7 @@ Item_func_sp::init_result_field(THD *thd) bool Item_func_sp::is_expensive() { - return !m_sp->m_chistics->detistic || + return !m_sp->detistic() || current_thd->locked_tables_mode < LTM_LOCK_TABLES; } @@ -6390,8 +6390,8 @@ Item_func_sp::execute_impl(THD *thd) Sub_statement_state statement_state; Security_context *save_security_ctx= thd->security_ctx; enum enum_sp_data_access access= - (m_sp->m_chistics->daccess == SP_DEFAULT_ACCESS) ? - SP_DEFAULT_ACCESS_MAPPING : m_sp->m_chistics->daccess; + (m_sp->daccess() == SP_DEFAULT_ACCESS) ? + SP_DEFAULT_ACCESS_MAPPING : m_sp->daccess(); DBUG_ENTER("Item_func_sp::execute_impl"); @@ -6408,7 +6408,7 @@ Item_func_sp::execute_impl(THD *thd) statement-based replication (SBR) is active. */ - if (!m_sp->m_chistics->detistic && !trust_function_creators && + if (!m_sp->detistic() && !trust_function_creators && (access == SP_CONTAINS_SQL || access == SP_MODIFIES_SQL_DATA) && (mysql_bin_log.is_open() && thd->variables.binlog_format == BINLOG_FORMAT_STMT)) @@ -6590,7 +6590,7 @@ Item_func_sp::fix_fields(THD *thd, Item **ref) #endif /* ! NO_EMBEDDED_ACCESS_CHECKS */ } - if (!m_sp->m_chistics->detistic) + if (!m_sp->detistic()) { used_tables_cache |= RAND_TABLE_BIT; const_item_cache= FALSE; @@ -6604,7 +6604,7 @@ void Item_func_sp::update_used_tables() { Item_func::update_used_tables(); - if (!m_sp->m_chistics->detistic) + if (!m_sp->detistic()) { used_tables_cache |= RAND_TABLE_BIT; const_item_cache= FALSE; diff --git a/sql/sp.cc b/sql/sp.cc index 35b52c526a4..a01861b3d61 100644 --- a/sql/sp.cc +++ b/sql/sp.cc @@ -487,9 +487,8 @@ db_find_routine_aux(THD *thd, stored_procedure_type type, const sp_name *name, */ if (name->m_name.length > table->field[1]->field_length) DBUG_RETURN(SP_KEY_NOT_FOUND); - table->field[0]->store(name->m_db.str, name->m_db.length, &my_charset_bin); - table->field[1]->store(name->m_name.str, name->m_name.length, - &my_charset_bin); + table->field[0]->store(name->m_db, &my_charset_bin); + table->field[1]->store(name->m_name, &my_charset_bin); table->field[2]->store((longlong) type, TRUE); key_copy(key, table->record[0], table->key_info, table->key_info->key_length); @@ -533,7 +532,7 @@ db_find_routine(THD *thd, stored_procedure_type type, const sp_name *name, const char *definer; longlong created; longlong modified; - st_sp_chistics chistics; + Sp_chistics chistics; char *ptr; uint length; char buff[65]; @@ -567,7 +566,6 @@ db_find_routine(THD *thd, stored_procedure_type type, const sp_name *name, goto done; } - bzero((char *)&chistics, sizeof(chistics)); if ((ptr= get_field(thd->mem_root, table->field[MYSQL_PROC_FIELD_ACCESS])) == NULL) { @@ -849,7 +847,7 @@ db_load_routine(THD *thd, stored_procedure_type type, params, strlen(params), returns, strlen(returns), body, strlen(body), - &chistics, definer_user_name, definer_host_name, + chistics, definer_user_name, definer_host_name, sql_mode)) { ret= SP_INTERNAL_ERROR; @@ -902,7 +900,7 @@ db_load_routine(THD *thd, stored_procedure_type type, } (*sphp)->set_definer(definer_user_name, definer_host_name); - (*sphp)->set_info(created, modified, &chistics, sql_mode); + (*sphp)->set_info(created, modified, chistics, sql_mode); (*sphp)->set_creation_ctx(creation_ctx); (*sphp)->optimize(); /* @@ -1139,11 +1137,11 @@ sp_create_routine(THD *thd, stored_procedure_type type, sp_head *sp) store_failed= table->field[MYSQL_PROC_FIELD_DB]-> - store(sp->m_db.str, sp->m_db.length, system_charset_info); + store(sp->m_db, system_charset_info); store_failed= store_failed || table->field[MYSQL_PROC_FIELD_NAME]-> - store(sp->m_name.str, sp->m_name.length, system_charset_info); + store(sp->m_name, system_charset_info); store_failed= store_failed || table->field[MYSQL_PROC_MYSQL_TYPE]-> @@ -1151,29 +1149,29 @@ sp_create_routine(THD *thd, stored_procedure_type type, sp_head *sp) store_failed= store_failed || table->field[MYSQL_PROC_FIELD_SPECIFIC_NAME]-> - store(sp->m_name.str, sp->m_name.length, system_charset_info); + store(sp->m_name, system_charset_info); - if (sp->m_chistics->daccess != SP_DEFAULT_ACCESS) + if (sp->daccess() != SP_DEFAULT_ACCESS) { store_failed= store_failed || table->field[MYSQL_PROC_FIELD_ACCESS]-> - store((longlong)sp->m_chistics->daccess, TRUE); + store((longlong)sp->daccess(), TRUE); } store_failed= store_failed || table->field[MYSQL_PROC_FIELD_DETERMINISTIC]-> - store((longlong)(sp->m_chistics->detistic ? 1 : 2), TRUE); + store((longlong)(sp->detistic() ? 1 : 2), TRUE); - if (sp->m_chistics->suid != SP_IS_DEFAULT_SUID) + if (sp->suid() != SP_IS_DEFAULT_SUID) { store_failed= store_failed || table->field[MYSQL_PROC_FIELD_SECURITY_TYPE]-> - store((longlong)sp->m_chistics->suid, TRUE); + store((longlong)sp->suid(), TRUE); } store_failed= store_failed || table->field[MYSQL_PROC_FIELD_PARAM_LIST]-> - store(sp->m_params.str, sp->m_params.length, system_charset_info); + store(sp->m_params, system_charset_info); if (sp->m_type == TYPE_ENUM_FUNCTION) { @@ -1186,11 +1184,11 @@ sp_create_routine(THD *thd, stored_procedure_type type, sp_head *sp) store_failed= store_failed || table->field[MYSQL_PROC_FIELD_BODY]-> - store(sp->m_body.str, sp->m_body.length, system_charset_info); + store(sp->m_body, system_charset_info); store_failed= store_failed || table->field[MYSQL_PROC_FIELD_DEFINER]-> - store(definer.str, definer.length, system_charset_info); + store(definer, system_charset_info); ((Field_timestamp *)table->field[MYSQL_PROC_FIELD_CREATED])->set_time(); ((Field_timestamp *)table->field[MYSQL_PROC_FIELD_MODIFIED])->set_time(); @@ -1199,26 +1197,25 @@ sp_create_routine(THD *thd, stored_procedure_type type, sp_head *sp) table->field[MYSQL_PROC_FIELD_SQL_MODE]-> store((longlong)saved_mode, TRUE); - if (sp->m_chistics->comment.str) + if (sp->comment().str) { store_failed= store_failed || table->field[MYSQL_PROC_FIELD_COMMENT]-> - store(sp->m_chistics->comment.str, sp->m_chistics->comment.length, - system_charset_info); + store(sp->comment(), system_charset_info); } if ((sp->m_type == TYPE_ENUM_FUNCTION) && !trust_function_creators && mysql_bin_log.is_open()) { - if (!sp->m_chistics->detistic) + if (!sp->detistic()) { /* Note that this test is not perfect; one could use a non-deterministic read-only function in an update statement. */ enum enum_sp_data_access access= - (sp->m_chistics->daccess == SP_DEFAULT_ACCESS) ? - SP_DEFAULT_ACCESS_MAPPING : sp->m_chistics->daccess; + (sp->daccess() == SP_DEFAULT_ACCESS) ? + SP_DEFAULT_ACCESS_MAPPING : sp->daccess(); if (access == SP_CONTAINS_SQL || access == SP_MODIFIES_SQL_DATA) { @@ -1255,7 +1252,7 @@ sp_create_routine(THD *thd, stored_procedure_type type, sp_head *sp) table->field[MYSQL_PROC_FIELD_BODY_UTF8]->set_notnull(); store_failed= store_failed || table->field[MYSQL_PROC_FIELD_BODY_UTF8]->store( - sp->m_body_utf8.str, sp->m_body_utf8.length, system_charset_info); + sp->m_body_utf8, system_charset_info); if (store_failed) { @@ -1291,7 +1288,7 @@ log: sp->m_params.str, sp->m_params.length, retstr.ptr(), retstr.length(), sp->m_body.str, sp->m_body.length, - sp->m_chistics, &(thd->lex->definer->user), + sp->chistics(), &(thd->lex->definer->user), &(thd->lex->definer->host), saved_mode)) { @@ -1448,8 +1445,7 @@ sp_update_routine(THD *thd, stored_procedure_type type, const sp_name *name, table->field[MYSQL_PROC_FIELD_ACCESS]-> store((longlong)chistics->daccess, TRUE); if (chistics->comment.str) - table->field[MYSQL_PROC_FIELD_COMMENT]->store(chistics->comment.str, - chistics->comment.length, + table->field[MYSQL_PROC_FIELD_COMMENT]->store(chistics->comment, system_charset_info); if ((ret= table->file->ha_update_row(table->record[1],table->record[0])) && ret != HA_ERR_RECORD_IS_THE_SAME) @@ -1792,7 +1788,7 @@ sp_find_routine(THD *thd, stored_procedure_type type, const sp_name *name, } if (db_load_routine(thd, type, name, &new_sp, sp->m_sql_mode, sp->m_params.str, returns, - sp->m_body.str, *sp->m_chistics, + sp->m_body.str, sp->chistics(), &sp->m_definer.user, &sp->m_definer.host, sp->m_created, sp->m_modified, sp->get_creation_ctx()) == SP_OK) @@ -2201,7 +2197,7 @@ show_create_sp(THD *thd, String *buf, const char *params, ulong paramslen, const char *returns, ulong returnslen, const char *body, ulong bodylen, - const st_sp_chistics *chistics, + const st_sp_chistics &chistics, const LEX_CSTRING *definer_user, const LEX_CSTRING *definer_host, sql_mode_t sql_mode) @@ -2209,7 +2205,7 @@ show_create_sp(THD *thd, String *buf, sql_mode_t old_sql_mode= thd->variables.sql_mode; /* Make some room to begin with */ if (buf->alloc(100 + dblen + 1 + namelen + paramslen + returnslen + bodylen + - chistics->comment.length + 10 /* length of " DEFINER= "*/ + + chistics.comment.length + 10 /* length of " DEFINER= "*/ + USER_HOST_BUFF_SIZE)) return FALSE; @@ -2243,7 +2239,7 @@ show_create_sp(THD *thd, String *buf, buf->append(returns, returnslen); } buf->append('\n'); - switch (chistics->daccess) { + switch (chistics.daccess) { case SP_NO_SQL: buf->append(STRING_WITH_LEN(" NO SQL\n")); break; @@ -2258,14 +2254,14 @@ show_create_sp(THD *thd, String *buf, /* Do nothing */ break; } - if (chistics->detistic) + if (chistics.detistic) buf->append(STRING_WITH_LEN(" DETERMINISTIC\n")); - if (chistics->suid == SP_IS_NOT_SUID) + if (chistics.suid == SP_IS_NOT_SUID) buf->append(STRING_WITH_LEN(" SQL SECURITY INVOKER\n")); - if (chistics->comment.length) + if (chistics.comment.length) { buf->append(STRING_WITH_LEN(" COMMENT ")); - append_unescaped(buf, chistics->comment.str, chistics->comment.length); + append_unescaped(buf, chistics.comment.str, chistics.comment.length); buf->append('\n'); } buf->append(body, bodylen); @@ -2303,7 +2299,6 @@ sp_load_for_information_schema(THD *thd, TABLE *proc_table, String *db, { const char *sp_body; String defstr; - struct st_sp_chistics sp_chistics; const LEX_CSTRING definer_user= {STRING_WITH_LEN("")}; const LEX_CSTRING definer_host= {STRING_WITH_LEN("")}; LEX_CSTRING sp_db_str; @@ -2326,7 +2321,6 @@ sp_load_for_information_schema(THD *thd, TABLE *proc_table, String *db, Stored_program_creation_ctx *creation_ctx= Stored_routine_creation_ctx::load_from_db(thd, &sp_name_obj, proc_table); sp_body= (type == TYPE_ENUM_FUNCTION ? "RETURN NULL" : "BEGIN END"); - bzero((char*) &sp_chistics, sizeof(sp_chistics)); defstr.set_charset(creation_ctx->get_client_cs()); if (!show_create_sp(thd, &defstr, type, sp_db_str.str, sp_db_str.length, @@ -2334,7 +2328,7 @@ sp_load_for_information_schema(THD *thd, TABLE *proc_table, String *db, params, strlen(params), returns, strlen(returns), sp_body, strlen(sp_body), - &sp_chistics, &definer_user, &definer_host, sql_mode)) + Sp_chistics(), &definer_user, &definer_host, sql_mode)) return 0; thd->lex= &newlex; diff --git a/sql/sp.h b/sql/sp.h index d33c767369e..dc93b8b20ee 100644 --- a/sql/sp.h +++ b/sql/sp.h @@ -240,7 +240,7 @@ bool show_create_sp(THD *thd, String *buf, const char *params, ulong paramslen, const char *returns, ulong returnslen, const char *body, ulong bodylen, - const st_sp_chistics *chistics, + const st_sp_chistics &chistics, const LEX_CSTRING *definer_user, const LEX_CSTRING *definer_host, sql_mode_t sql_mode); diff --git a/sql/sp_head.cc b/sql/sp_head.cc index 973eeef0165..bb3275316cc 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -559,6 +559,8 @@ sp_head::sp_head(stored_procedure_type type) m_sp_cache_version(0), m_creation_ctx(0), unsafe_flags(0), + m_created(0), + m_modified(0), m_recursion_level(0), m_next_cached_sp(0), m_param_begin(NULL), @@ -1408,7 +1410,7 @@ set_routine_security_ctx(THD *thd, sp_head *sp, bool is_proc, Security_context **save_ctx) { *save_ctx= 0; - if (sp->m_chistics->suid != SP_IS_NOT_SUID && + if (sp->suid() != SP_IS_NOT_SUID && sp->m_security_ctx.change_security_context(thd, &sp->m_definer.user, &sp->m_definer.host, &sp->m_db, @@ -1520,7 +1522,7 @@ sp_head::execute_trigger(THD *thd, Security_context *save_ctx= NULL; - if (m_chistics->suid != SP_IS_NOT_SUID && + if (suid() != SP_IS_NOT_SUID && m_security_ctx.change_security_context(thd, &m_definer.user, &m_definer.host, @@ -2432,20 +2434,26 @@ sp_head::sp_add_instr_cpush_for_cursors(THD *thd, sp_pcontext *pcontext) } +void +sp_head::set_chistics(const st_sp_chistics &chistics) +{ + m_chistics.set(chistics); + if (m_chistics.comment.length == 0) + m_chistics.comment.str= 0; + else + m_chistics.comment.str= strmake_root(mem_root, + m_chistics.comment.str, + m_chistics.comment.length); +} + + void sp_head::set_info(longlong created, longlong modified, - const st_sp_chistics *chistics, sql_mode_t sql_mode) + const st_sp_chistics &chistics, sql_mode_t sql_mode) { m_created= created; m_modified= modified; - m_chistics= (st_sp_chistics *) memdup_root(mem_root, (char*) chistics, - sizeof(*chistics)); - if (m_chistics->comment.length == 0) - m_chistics->comment.str= 0; - else - m_chistics->comment.str= strmake_root(mem_root, - m_chistics->comment.str, - m_chistics->comment.length); + set_chistics(chistics); m_sql_mode= sql_mode; } diff --git a/sql/sp_head.h b/sql/sp_head.h index cf05d47a95c..1435bb460c3 100644 --- a/sql/sp_head.h +++ b/sql/sp_head.h @@ -179,7 +179,15 @@ public: Column_definition m_return_field_def; /**< This is used for FUNCTIONs only. */ const char *m_tmp_query; ///< Temporary pointer to sub query string - st_sp_chistics *m_chistics; +private: + /* + Private to guarantee that m_chistics.comment is properly set to: + - a string which is alloced on this->mem_root + - or (NULL,0) + set_chistics() makes sure this. + */ + Sp_chistics m_chistics; +public: sql_mode_t m_sql_mode; ///< For SHOW CREATE and execution bool m_explicit_name; /**< Prepend the db name? */ LEX_CSTRING m_qname; ///< db.name @@ -189,6 +197,12 @@ public: LEX_CSTRING m_defstr; AUTHID m_definer; + const st_sp_chistics &chistics() const { return m_chistics; } + const LEX_CSTRING &comment() const { return m_chistics.comment; } + void set_suid(enum_sp_suid_behaviour suid) { m_chistics.suid= suid; } + enum_sp_suid_behaviour suid() const { return m_chistics.suid; } + bool detistic() const { return m_chistics.detistic; } + enum_sp_data_access daccess() const { return m_chistics.daccess; } /** Is this routine being executed? */ @@ -671,8 +685,9 @@ public: m_flags|= sp_head::HAS_COLUMN_TYPE_REFS; } + void set_chistics(const st_sp_chistics &chistics); void set_info(longlong created, longlong modified, - const st_sp_chistics *chistics, sql_mode_t sql_mode); + const st_sp_chistics &chistics, sql_mode_t sql_mode); void set_definer(const char *definer, uint definerlen); void set_definer(const LEX_CSTRING *user_name, const LEX_CSTRING *host_name) diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 20e5fc0b450..112ce36b1bf 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -5831,10 +5831,9 @@ sp_head *LEX::make_sp_head(THD *thd, sp_name *name, sp->init(this); if (name) sp->init_sp_name(thd, name); - sp->m_chistics= &sp_chistics; sphead= sp; } - bzero(&sp_chistics, sizeof(sp_chistics)); + sp_chistics.init(); return sp; } @@ -6124,7 +6123,7 @@ bool LEX::maybe_start_compound_statement(THD *thd) { if (!make_sp_head(thd, NULL, TYPE_ENUM_PROCEDURE)) return true; - sp_chistics.suid= SP_IS_NOT_SUID; + sphead->set_suid(SP_IS_NOT_SUID); sphead->set_body_start(thd, thd->m_parser_state->m_lip.get_cpp_ptr()); } return false; diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 695a397c25f..3c7c99dff3d 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -1285,9 +1285,17 @@ struct st_sp_chistics enum enum_sp_suid_behaviour suid; bool detistic; enum enum_sp_data_access daccess; + void init() { bzero(this, sizeof(*this)); } + void set(const st_sp_chistics &other) { *this= other; } }; +class Sp_chistics: public st_sp_chistics +{ +public: + Sp_chistics() { init(); } +}; + struct st_trg_chistics: public st_trg_execution_order { diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 50e8d57e75d..95af02f6ab3 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -2710,7 +2710,7 @@ bool sp_process_definer(THD *thd) DBUG_RETURN(TRUE); if (thd->slave_thread && lex->sphead) - lex->sphead->m_chistics->suid= SP_IS_NOT_SUID; + lex->sphead->set_suid(SP_IS_NOT_SUID); } else { diff --git a/sql/sql_trigger.cc b/sql/sql_trigger.cc index e0980711afe..291d55d61a2 100644 --- a/sql/sql_trigger.cc +++ b/sql/sql_trigger.cc @@ -663,7 +663,7 @@ static void build_trig_stmt_query(THD *thd, TABLE_LIST *tables, if (lex->create_info.or_replace()) stmt_query->append(STRING_WITH_LEN("OR REPLACE ")); - if (lex->sphead->m_chistics->suid != SP_IS_NOT_SUID) + if (lex->sphead->suid() != SP_IS_NOT_SUID) { /* SUID trigger */ lex->definer->set_lex_string(trg_definer, trg_definer_holder); @@ -1431,7 +1431,7 @@ bool Table_triggers_list::check_n_load(THD *thd, const char *db, lex.set_trg_event_type_for_tables(); if (lex.sphead) - lex.sphead->set_info(0, 0, &lex.sp_chistics, sql_mode); + lex.sphead->m_sql_mode= sql_mode; if (unlikely(!(trigger= (new (&table->mem_root) Trigger(trigger_list, lex.sphead))))) @@ -1489,7 +1489,7 @@ bool Table_triggers_list::check_n_load(THD *thd, const char *db, continue; } - sp->set_info(0, 0, &lex.sp_chistics, sql_mode); + sp->m_sql_mode= sql_mode; sp->set_creation_ctx(creation_ctx); if (!trg_definer || !trg_definer->length) @@ -1519,7 +1519,7 @@ bool Table_triggers_list::check_n_load(THD *thd, const char *db, authorization of the invoker. */ - sp->m_chistics->suid= SP_IS_NOT_SUID; + sp->set_suid(SP_IS_NOT_SUID); } else { @@ -1691,7 +1691,7 @@ void Trigger::get_trigger_info(LEX_CSTRING *trigger_stmt, } *trigger_body= body->m_body_utf8; - if (body->m_chistics->suid == SP_IS_NOT_SUID) + if (body->suid() == SP_IS_NOT_SUID) { *definer= empty_lex_str; } diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 70a9299ec3d..17f8b3849d3 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -2950,7 +2950,6 @@ ev_sql_stmt: TYPE_ENUM_PROCEDURE)) MYSQL_YYABORT; - lex->sp_chistics.suid= SP_IS_SUID; //always the definer! lex->sphead->set_body_start(thd, lip->get_cpp_ptr()); } sp_proc_stmt @@ -7230,7 +7229,7 @@ alter: if (lex->sphead) my_yyabort_error((ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE")); - bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics)); + lex->sp_chistics.init(); } sp_a_chistics { @@ -7245,7 +7244,7 @@ alter: if (lex->sphead) my_yyabort_error((ER_SP_NO_DROP_SP, MYF(0), "FUNCTION")); - bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics)); + lex->sp_chistics.init(); } sp_a_chistics { @@ -16674,6 +16673,7 @@ sf_tail: LEX *lex= thd->lex; Lex_input_stream *lip= YYLIP; + lex->sphead->set_chistics(lex->sp_chistics); lex->sphead->set_body_start(thd, lip->get_cpp_tok_start()); } sp_proc_stmt_in_returns_clause @@ -16705,6 +16705,7 @@ sp_tail: sp_parenthesized_pdparam_list sp_c_chistics { + Lex->sphead->set_chistics(Lex->sp_chistics); Lex->sphead->set_body_start(thd, YYLIP->get_cpp_tok_start()); } sp_proc_stmt diff --git a/sql/sql_yacc_ora.yy b/sql/sql_yacc_ora.yy index d720cd18faf..e177ed1cb48 100644 --- a/sql/sql_yacc_ora.yy +++ b/sql/sql_yacc_ora.yy @@ -2391,7 +2391,6 @@ ev_sql_stmt: TYPE_ENUM_PROCEDURE)) MYSQL_YYABORT; - lex->sp_chistics.suid= SP_IS_SUID; //always the definer! lex->sphead->set_body_start(thd, lip->get_cpp_ptr()); } sp_proc_stmt @@ -7211,7 +7210,7 @@ alter: if (lex->sphead) my_yyabort_error((ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE")); - bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics)); + lex->sp_chistics.init(); } sp_a_chistics { @@ -7226,7 +7225,7 @@ alter: if (lex->sphead) my_yyabort_error((ER_SP_NO_DROP_SP, MYF(0), "FUNCTION")); - bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics)); + lex->sp_chistics.init(); } sp_a_chistics { @@ -16913,6 +16912,7 @@ sf_tail: LEX *lex= thd->lex; Lex_input_stream *lip= YYLIP; + lex->sphead->set_chistics(lex->sp_chistics); lex->sphead->set_body_start(thd, lip->get_cpp_tok_start()); } sp_tail_is @@ -16947,6 +16947,7 @@ sp_tail: opt_sp_parenthesized_pdparam_list sp_c_chistics { + Lex->sphead->set_chistics(Lex->sp_chistics); Lex->sphead->set_body_start(thd, YYLIP->get_cpp_tok_start()); } sp_tail_is diff --git a/sql/wsrep_mysqld.cc b/sql/wsrep_mysqld.cc index 40a0e4f00a6..87eb97e4fec 100644 --- a/sql/wsrep_mysqld.cc +++ b/sql/wsrep_mysqld.cc @@ -2253,7 +2253,7 @@ static int wsrep_create_sp(THD *thd, uchar** buf, size_t* buf_len) sp->m_params.str, sp->m_params.length, retstr.c_ptr(), retstr.length(), sp->m_body.str, sp->m_body.length, - sp->m_chistics, &(thd->lex->definer->user), + sp->chistics(), &(thd->lex->definer->user), &(thd->lex->definer->host), saved_mode)) {