Skip to content

Commit

Permalink
MDEV-27086 "No database selected" when using UNION of CTEs to define …
Browse files Browse the repository at this point in the history
…table

This bug concerned only CREATE TABLE statements of the form
  CREATE TABLE <table name> AS <with clause> <union>.
For such a statement not all references to CTE used in <union> were resolved.
As a result a bogus message was reported for the first unresolved reference.
This happened because for such statements the function resolving references
to CTEs LEX::check_cte_dependencies_and_resolve_references() was called
prematurely in the parser.

Approved by Oleksandr Byelkin <sanja@mariadb.com>
  • Loading branch information
igorbabaev committed Nov 20, 2021
1 parent e9f171b commit 0dae416
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
26 changes: 26 additions & 0 deletions mysql-test/r/cte_nonrecursive.result
Original file line number Diff line number Diff line change
Expand Up @@ -2136,4 +2136,30 @@ drop procedure sp1;
drop function g;
drop function f;
drop table t1;
#
# MDEV-27086: union using CTEs in CREATE TABLE
#
create or replace temporary table tmp as
with cte1 as (select 1 as a), cte2 as (select 2 as a)
select * from cte1 union select * from cte2;
select * from tmp;
a
1
2
create table t1 as
with cte1 as (select 1 as a), cte2 as (select 2 as a)
select * from cte1 union select * from cte2;
select * from t1;
a
1
2
insert into t1 values (3);
create table t2 as
with cte1 as (select * from t1 where a <2), cte2 as (select * from t1 where a > 2)
select * from cte1 union select * from cte2;
select * from t2;
a
1
3
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 @@ -1578,4 +1578,27 @@ drop function g;
drop function f;
drop table t1;

--echo #
--echo # MDEV-27086: union using CTEs in CREATE TABLE
--echo #

create or replace temporary table tmp as
with cte1 as (select 1 as a), cte2 as (select 2 as a)
select * from cte1 union select * from cte2;
select * from tmp;

create table t1 as
with cte1 as (select 1 as a), cte2 as (select 2 as a)
select * from cte1 union select * from cte2;
select * from t1;

insert into t1 values (3);

create table t2 as
with cte1 as (select * from t1 where a <2), cte2 as (select * from t1 where a > 2)
select * from cte1 union select * from cte2;
select * from t2;

drop table t1,t2;

--echo # End of 10.2 tests
8 changes: 4 additions & 4 deletions sql/sql_yacc.yy
Original file line number Diff line number Diff line change
Expand Up @@ -4869,6 +4869,10 @@ create_like:
opt_create_select:
/* empty */ {}
| opt_duplicate opt_as create_select_query_expression
{
if (Lex->check_cte_dependencies_and_resolve_references())
MYSQL_YYABORT;
}
;

create_select_query_expression:
Expand All @@ -4877,16 +4881,12 @@ create_select_query_expression:
{
Select->set_braces(0);
Select->set_with_clause($1);
if (Lex->check_cte_dependencies_and_resolve_references())
MYSQL_YYABORT;
}
union_clause
| opt_with_clause SELECT_SYM create_select_part2
create_select_part3_union_not_ready create_select_part4
{
Select->set_with_clause($1);
if (Lex->check_cte_dependencies_and_resolve_references())
MYSQL_YYABORT;
}
| '(' create_select_query_specification ')'
| '(' create_select_query_specification ')'
Expand Down

0 comments on commit 0dae416

Please sign in to comment.