Commit c22f7e8e authored by Igor Babaev's avatar Igor Babaev

MDEV-29428 Incorrect result for delete with "order by" clause

ORDER BY clause without LIMIT clause can be removed from DELETE statements.
parent ee495b22
......@@ -564,3 +564,50 @@ having t3.a > any (select t2.b from t2
where t2.b*10 < sum(t3.b)));
drop table t1,t2,t3;
End of 10.4 tests
#
# MDEV-29428: DELETE with ORDER BY without LIMIT clause
#
create table t1 (c1 integer, c2 integer, c3 integer);
insert into t1 values
(1,1,1), (1,2,2), (1,3,3), (2,1,4), (2,2,5), (2,3,6), (2,4,7), (2,5,8);
create temporary table t select * from t1;
explain delete from t1 order by c2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL 8 Deleting all rows
delete from t1 order by c2;
select *from t1;
c1 c2 c3
delete from t1;
insert into t1 select * from t;
explain delete from t1
where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
delete from t1
where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
select *from t1;
c1 c2 c3
1 1 1
1 2 2
2 1 4
2 2 5
delete from t1;
insert into t1 select * from t;
explain delete from t1
where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3
order by c2;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
delete from t1
where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3
order by c2;
select *from t1;
c1 c2 c3
1 1 1
1 2 2
2 1 4
2 2 5
drop table t1;
End of 11.1 tests
......@@ -624,3 +624,45 @@ update t1 set t1.a=t1.a+10
drop table t1,t2,t3;
--echo End of 10.4 tests
--echo #
--echo # MDEV-29428: DELETE with ORDER BY without LIMIT clause
--echo #
create table t1 (c1 integer, c2 integer, c3 integer);
insert into t1 values
(1,1,1), (1,2,2), (1,3,3), (2,1,4), (2,2,5), (2,3,6), (2,4,7), (2,5,8);
create temporary table t select * from t1;
let $q1=
delete from t1 order by c2;
eval explain $q1;
eval $q1;
select *from t1;
delete from t1;
insert into t1 select * from t;
let $q2=
delete from t1
where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
eval explain $q2;
eval $q2;
select *from t1;
delete from t1;
insert into t1 select * from t;
let $q3=
delete from t1
where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3
order by c2;
eval explain $q3;
eval $q3;
select *from t1;
drop table t1;
--echo End of 11.1 tests
......@@ -1453,6 +1453,19 @@ bool multi_delete::send_eof()
}
/**
@brief Remove ORDER BY from DELETE if it's used without limit clause
*/
void Sql_cmd_delete::remove_order_by_without_limit(THD *thd)
{
SELECT_LEX *const select_lex = thd->lex->first_select_lex();
if (select_lex->order_list.elements &&
!select_lex->limit_params.select_limit)
select_lex->order_list.empty();
}
/**
@brief Check whether processing to multi-table delete is prohibited
......@@ -1594,7 +1607,10 @@ bool Sql_cmd_delete::prepare_inner(THD *thd)
DBUG_ASSERT(update_source_table || table_list->view != 0);
if (!table_list->is_multitable() &&
!processing_as_multitable_delete_prohibited(thd))
{
multitable= true;
remove_order_by_without_limit(thd);
}
}
}
......
......@@ -64,6 +64,8 @@ class Sql_cmd_delete final : public Sql_cmd_dml
void set_as_multitable() { multitable= true; }
void remove_order_by_without_limit(THD *thd);
protected:
/**
@brief Perform precheck of table privileges for delete statements
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment