Skip to content

Commit

Permalink
MDEV-26470 "No database" selected when using CTE in a subquery of DEL…
Browse files Browse the repository at this point in the history
…ETE statement

This bug led to reporting bogus messages "No database selected" for DELETE
statements if they used subqueries in their WHERE conditions and these
subqueries contained references to CTEs.
The bug happened because the grammar rule for DELETE statement did not
call the function LEX::check_cte_dependencies_and_resolve_references() and
as a result of it references to CTEs were not identified as such.

Approved by Oleksandr Byelkin <sanja@mariadb.com>
  • Loading branch information
igorbabaev committed Nov 21, 2021
1 parent 0dae416 commit 114e18b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
22 changes: 22 additions & 0 deletions mysql-test/r/cte_nonrecursive.result
Original file line number Diff line number Diff line change
Expand Up @@ -2162,4 +2162,26 @@ a
1
3
drop table t1,t2;
#
# MDEV-26470: CTE in WITH clause of subquery used in DELETE
#
create table t1 (a int);
insert into t1 values (3), (7), (1), (5);
create table t2 (b int);
insert into t2 values (4), (1), (3), (2);
delete from t1
where a in (with cte(a) as (select * from t2 where b <=2) select a from cte);
select * from t1;
a
3
7
5
insert into t1 values (1), (3);
delete t1 from t1, t2
where t1.a=t2.b or
t1.a in (with cte(a) as (select b+1 from t2) select * from cte);
select * from t1;
a
7
drop table t1,t2;
# End of 10.2 tests
23 changes: 23 additions & 0 deletions mysql-test/t/cte_nonrecursive.test
Original file line number Diff line number Diff line change
Expand Up @@ -1601,4 +1601,27 @@ select * from t2;

drop table t1,t2;

--echo #
--echo # MDEV-26470: CTE in WITH clause of subquery used in DELETE
--echo #

create table t1 (a int);
insert into t1 values (3), (7), (1), (5);

create table t2 (b int);
insert into t2 values (4), (1), (3), (2);

delete from t1
where a in (with cte(a) as (select * from t2 where b <=2) select a from cte);
select * from t1;

insert into t1 values (1), (3);

delete t1 from t1, t2
where t1.a=t2.b or
t1.a in (with cte(a) as (select b+1 from t2) select * from cte);
select * from t1;

drop table t1,t2;

--echo # End of 10.2 tests
4 changes: 4 additions & 0 deletions sql/sql_yacc.yy
Original file line number Diff line number Diff line change
Expand Up @@ -12668,6 +12668,10 @@ delete:
lex->select_lex.init_order();
}
opt_delete_options single_multi
{
if (Lex->check_cte_dependencies_and_resolve_references())
MYSQL_YYABORT;
}
;

single_multi:
Expand Down

0 comments on commit 114e18b

Please sign in to comment.