Skip to content

Commit

Permalink
Fixed bug mdev-9931.
Browse files Browse the repository at this point in the history
When the specification of a WITH table referred to a view
that used a based table with the same name as the WITH table
the server went into an infinite loop because it erroneously
resolved the reference to the base table as the reference to
the WITH table.

With tables used in a view cannot be searched for beyond the
scope the view.
  • Loading branch information
igorbabaev committed Apr 19, 2016
1 parent 4b8e54b commit 308cee5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 1 deletion.
13 changes: 13 additions & 0 deletions mysql-test/r/cte_nonrecursive.result
Original file line number Diff line number Diff line change
Expand Up @@ -746,3 +746,16 @@ with t(f1,f1) as (select * from t1 where b >= 'c')
select t1.b from t2,t1 where t1.a = t2.c;
ERROR 42S21: Duplicate column name 'f1'
drop table t1,t2;
#
# Bug mdev-9937: View used in the specification of with table
# refers to the base table with the same name
#
create table t1 (a int);
insert into t1 values (20), (30), (10);
create view v1 as select * from t1 where a > 10;
with t1 as (select * from v1) select * from t1;
a
20
30
drop view v1;
drop table t1;
14 changes: 14 additions & 0 deletions mysql-test/t/cte_nonrecursive.test
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,17 @@ with t(f1,f1) as (select * from t1 where b >= 'c')
select t1.b from t2,t1 where t1.a = t2.c;

drop table t1,t2;

--echo #
--echo # Bug mdev-9937: View used in the specification of with table
--echo # refers to the base table with the same name
--echo #

create table t1 (a int);
insert into t1 values (20), (30), (10);
create view v1 as select * from t1 where a > 10;

with t1 as (select * from v1) select * from t1;

drop view v1;
drop table t1;
5 changes: 4 additions & 1 deletion sql/sql_cte.cc
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,10 @@ With_element *st_select_lex::find_table_def_in_with_clauses(TABLE_LIST *table)
{
With_clause *with_clause=sl->get_with_clause();
if (with_clause && (found= with_clause->find_table_def(table)))
return found;
return found;
/* Do not look for the table's definition beyond the scope of the view */
if (sl->master_unit()->is_view)
break;
}
return found;
}
Expand Down
1 change: 1 addition & 0 deletions sql/sql_lex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2078,6 +2078,7 @@ void st_select_lex_unit::init_query()
found_rows_for_union= 0;
insert_table_with_stored_vcol= 0;
derived= 0;
is_view= false;
with_clause= 0;
with_element= 0;
columns_are_renamed= false;
Expand Down
1 change: 1 addition & 0 deletions sql/sql_lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ class st_select_lex_unit: public st_select_lex_node {
derived tables/views handling.
*/
TABLE_LIST *derived;
bool is_view;
/* With clause attached to this unit (if any) */
With_clause *with_clause;
/* With element where this unit is used as the specification (if any) */
Expand Down
2 changes: 2 additions & 0 deletions sql/sql_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,8 @@ bool mysql_make_view(THD *thd, TABLE_SHARE *share, TABLE_LIST *table,
sl->context.error_processor_data= (void *)table;
}

table->select_lex->master_unit()->is_view= true;

/*
check MERGE algorithm ability
- algorithm is not explicit TEMPORARY TABLE
Expand Down

0 comments on commit 308cee5

Please sign in to comment.