From dc5949b33639d232df3cc66c80dde4c1bf4881bb Mon Sep 17 00:00:00 2001 From: "evgen@moonbone.local" <> Date: Tue, 12 Jul 2005 20:45:34 +0400 Subject: [PATCH] Fix bug #11399 Column alias was lost during view preraration. New item created in find_field_in_table() to fix view's item, was created without taking into account original item's alias. This patch checks if alias is set to the original item and if so sets it to newly created item. --- mysql-test/r/view.result | 8 ++++++++ mysql-test/t/view.test | 10 ++++++++++ sql/sql_base.cc | 22 ++++++++++++++++------ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index cad86dc94b8..d11e91a7ba4 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -1863,3 +1863,11 @@ ERROR HY000: Field of view 'test.v2' underlying table doesn't have a default val set sql_mode=default; drop view v2,v1; drop table t1; +create table t1 (f1 int); +insert into t1 values (1); +create view v1 as select f1 from t1; +select f1 as alias from v1; +alias +1 +drop view v1; +drop table t1; diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index 78d992163fe..fcde8bde4dd 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -1714,3 +1714,13 @@ INSERT INTO v2 (vcol1) VALUES(12); set sql_mode=default; drop view v2,v1; drop table t1; + +# +# Bug#11399 Use an alias in a select statement on a view +# +create table t1 (f1 int); +insert into t1 values (1); +create view v1 as select f1 from t1; +select f1 as alias from v1; +drop view v1; +drop table t1; diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 383adcadc6a..26c4e6f8605 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -2407,9 +2407,11 @@ Field *view_ref_found= (Field*) 0x2; name name of field item_name name of item if it will be created (VIEW) length length of name - ref expression substituted in VIEW should be + ref [in/out] expression substituted in VIEW should be passed using this reference (return view_ref_found) + (*ref != NULL) only if *ref contains + the item that we need to replace. check_grants_table do check columns grants for table? check_grants_view do check columns grants for view? allow_rowid do allow finding of "_rowid" field? @@ -2447,11 +2449,6 @@ find_field_in_table(THD *thd, TABLE_LIST *table_list, { if (!my_strcasecmp(system_charset_info, field_it.name(), name)) { - Item *item= field_it.create_item(thd); - if (!item) - { - DBUG_RETURN(0); - } if (table_list->schema_table_reformed) { /* @@ -2470,6 +2467,19 @@ find_field_in_table(THD *thd, TABLE_LIST *table_list, name, length)) DBUG_RETURN(WRONG_GRANT); #endif + Item *item= field_it.create_item(thd); + if (!item) + { + DBUG_RETURN(0); + } + /* + *ref != NULL means that *ref contains the item that we need to + replace. If the item was aliased by the user, set the alias to + the replacing item. + */ + if (*ref && !(*ref)->is_autogenerated_name) + item->set_name((*ref)->name, (*ref)->name_length, + system_charset_info); if (register_tree_change) thd->change_item_tree(ref, item); else