Skip to content

Commit

Permalink
MDEV-29428 Incorrect result for delete with "order by" clause
Browse files Browse the repository at this point in the history
ORDER BY clause without LIMIT clause can be removed from DELETE statements.
  • Loading branch information
igorbabaev committed Mar 16, 2023
1 parent ee495b2 commit c22f7e8
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
47 changes: 47 additions & 0 deletions mysql-test/main/delete.result
Expand Up @@ -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
42 changes: 42 additions & 0 deletions mysql-test/main/delete.test
Expand Up @@ -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
16 changes: 16 additions & 0 deletions sql/sql_delete.cc
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions sql/sql_delete.h
Expand Up @@ -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
Expand Down

0 comments on commit c22f7e8

Please sign in to comment.