summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mysql-test/r/view.result20
-rw-r--r--mysql-test/t/view.test16
-rw-r--r--sql/table.cc17
3 files changed, 51 insertions, 2 deletions
diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result
index c4391781e9c..2e4826c2ee0 100644
--- a/mysql-test/r/view.result
+++ b/mysql-test/r/view.result
@@ -1724,3 +1724,23 @@ a b
301 0
drop view v3;
drop tables t1,t2;
+create table t1(c1 int);
+create table t2(c2 int);
+insert into t1 values (1),(2),(3);
+insert into t2 values (1);
+SELECT c1 FROM t1 WHERE c1 IN (SELECT c2 FROM t2);
+c1
+1
+SELECT c1 FROM t1 WHERE EXISTS (SELECT c2 FROM t2 WHERE c2 = c1);
+c1
+1
+create view v1 as SELECT c1 FROM t1 WHERE c1 IN (SELECT c2 FROM t2);
+create view v2 as SELECT c1 FROM t1 WHERE EXISTS (SELECT c2 FROM t2 WHERE c2 = c1);
+select * from v1;
+c1
+1
+select * from v2;
+c1
+1
+drop view v2, v1;
+drop table t1, t2;
diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test
index 77f0f65323e..be61431dffa 100644
--- a/mysql-test/t/view.test
+++ b/mysql-test/t/view.test
@@ -1654,3 +1654,19 @@ select * from v3;
drop view v3;
drop tables t1,t2;
+
+#
+# Resolving view fields in subqueries in VIEW (Bug #6394)
+#
+create table t1(c1 int);
+create table t2(c2 int);
+insert into t1 values (1),(2),(3);
+insert into t2 values (1);
+SELECT c1 FROM t1 WHERE c1 IN (SELECT c2 FROM t2);
+SELECT c1 FROM t1 WHERE EXISTS (SELECT c2 FROM t2 WHERE c2 = c1);
+create view v1 as SELECT c1 FROM t1 WHERE c1 IN (SELECT c2 FROM t2);
+create view v2 as SELECT c1 FROM t1 WHERE EXISTS (SELECT c2 FROM t2 WHERE c2 = c1);
+select * from v1;
+select * from v2;
+drop view v2, v1;
+drop table t1, t2;
diff --git a/sql/table.cc b/sql/table.cc
index 71198993009..1fb40f5eb3a 100644
--- a/sql/table.cc
+++ b/sql/table.cc
@@ -1687,6 +1687,7 @@ bool st_table_list::setup_ancestor(THD *thd, Item **conds,
Field_translator *transl;
SELECT_LEX *select= &view->select_lex;
SELECT_LEX *current_select_save= thd->lex->current_select;
+ byte *main_table_list_save= thd->lex->select_lex.table_list.first;
Item *item;
TABLE_LIST *tbl;
List_iterator_fast<Item> it(select->item_list);
@@ -1709,8 +1710,13 @@ bool st_table_list::setup_ancestor(THD *thd, Item **conds,
if (field_translation)
{
DBUG_PRINT("info", ("there are already translation table"));
- /* prevent look up in SELECTs tree */
+ /*
+ prevent look up in SELECTs tree, and emulate main table list by
+ ancestor table list for subquery processing
+ */
thd->lex->current_select= &thd->lex->select_lex;
+ thd->lex->select_lex.table_list.first= (byte *)ancestor;
+
thd->lex->select_lex.no_wrap_view_item= 1;
thd->set_query_id= 1;
/* this view was prepared already on previous PS/SP execution */
@@ -1755,8 +1761,13 @@ bool st_table_list::setup_ancestor(THD *thd, Item **conds,
DBUG_RETURN(1);
}
- /* prevent look up in SELECTs tree */
+ /*
+ prevent look up in SELECTs tree, and emulate main table list by ancestor
+ table list for subquery processing
+ */
thd->lex->current_select= &thd->lex->select_lex;
+ thd->lex->select_lex.table_list.first= (byte *)ancestor;
+
thd->lex->select_lex.no_wrap_view_item= 1;
/*
@@ -1901,6 +1912,7 @@ bool st_table_list::setup_ancestor(THD *thd, Item **conds,
ok:
thd->lex->select_lex.no_wrap_view_item= save_wrapper;
thd->lex->current_select= current_select_save;
+ thd->lex->select_lex.table_list.first= main_table_list_save;
thd->set_query_id= save_set_query_id;
thd->allow_sum_func= save_allow_sum_func;
DBUG_RETURN(0);
@@ -1915,6 +1927,7 @@ err:
}
thd->lex->select_lex.no_wrap_view_item= save_wrapper;
thd->lex->current_select= current_select_save;
+ thd->lex->select_lex.table_list.first= main_table_list_save;
thd->set_query_id= save_set_query_id;
thd->allow_sum_func= save_allow_sum_func;
DBUG_RETURN(1);