Commit 0dae4163 authored by Igor Babaev's avatar Igor Babaev

MDEV-27086 "No database selected" when using UNION of CTEs to define 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>
parent e9f171b4
...@@ -2136,4 +2136,30 @@ drop procedure sp1; ...@@ -2136,4 +2136,30 @@ drop procedure sp1;
drop function g; drop function g;
drop function f; drop function f;
drop table t1; 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 # End of 10.2 tests
...@@ -1578,4 +1578,27 @@ drop function g; ...@@ -1578,4 +1578,27 @@ drop function g;
drop function f; drop function f;
drop table t1; 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 --echo # End of 10.2 tests
...@@ -4869,6 +4869,10 @@ create_like: ...@@ -4869,6 +4869,10 @@ create_like:
opt_create_select: opt_create_select:
/* empty */ {} /* empty */ {}
| opt_duplicate opt_as create_select_query_expression | opt_duplicate opt_as create_select_query_expression
{
if (Lex->check_cte_dependencies_and_resolve_references())
MYSQL_YYABORT;
}
; ;
create_select_query_expression: create_select_query_expression:
...@@ -4877,16 +4881,12 @@ create_select_query_expression: ...@@ -4877,16 +4881,12 @@ create_select_query_expression:
{ {
Select->set_braces(0); Select->set_braces(0);
Select->set_with_clause($1); Select->set_with_clause($1);
if (Lex->check_cte_dependencies_and_resolve_references())
MYSQL_YYABORT;
} }
union_clause union_clause
| opt_with_clause SELECT_SYM create_select_part2 | opt_with_clause SELECT_SYM create_select_part2
create_select_part3_union_not_ready create_select_part4 create_select_part3_union_not_ready create_select_part4
{ {
Select->set_with_clause($1); Select->set_with_clause($1);
if (Lex->check_cte_dependencies_and_resolve_references())
MYSQL_YYABORT;
} }
| '(' create_select_query_specification ')' | '(' create_select_query_specification ')'
| '(' create_select_query_specification ')' | '(' create_select_query_specification ')'
......
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