--- mariadb-10.0.14/sql/sql_view.cc 2014-09-25 00:29:47.000000000 +0200 +++ mariadb-10.0.14-patched/sql/sql_view.cc 2014-10-30 12:30:17.344598704 +0100 @@ -159,31 +159,65 @@ } + /** Check if auto generated column names are conforming and possibly generate a conforming name for them if not. - @param item_list List of Items which should be checked -*/ + @param lex LEX for this thread. -static void make_valid_column_names(List &item_list) + @retval false Operation was a success. + @retval true An error occurred. +*/ +static bool make_valid_column_names(SELECT_LEX *select_lex) { Item *item; - uint name_len; - List_iterator_fast it(item_list); + size_t name_len; char buff[NAME_LEN]; + uint column_no= 1; DBUG_ENTER("make_valid_column_names"); - for (uint column_no= 1; (item= it++); column_no++) + for (SELECT_LEX *sl= select_lex; sl; sl= sl->next_select()) { - if (!item->is_autogenerated_name || !check_column_name(item->name)) - continue; - name_len= my_snprintf(buff, NAME_LEN, "Name_exp_%u", column_no); - item->orig_name= item->name; - item->set_name(buff, name_len, system_charset_info); + for (List_iterator_fast it(sl->item_list); (item= it++); column_no++) + { + if (!item->is_autogenerated_name || !check_column_name(item->name)) + continue; + name_len= my_snprintf(buff, NAME_LEN, "Name_exp_%u", column_no); + item->orig_name= item->name; + item->set_name(buff, name_len, system_charset_info); + } + + /* + There is a possibility of generating same name for column in more than + one SELECT_LEX. For Example: + + CREATE TABLE t1 (Name_exp_1 INT, Name_exp_2 INT, Name_exp_3 INT); + CREATE TABLE t2 (Name_exp_1 INT, Name_exp_2 INT, Name_exp_3 INT); + + CREATE VIEW v1 AS SELECT '', t1.Name_exp_2 AS Name_exp_2 FROM t1 + UNION + SELECT '', t2.Name_exp_1 AS Name_exp_1 from t2; + + But, column names of the first SELECT_LEX is considered + for the output. + + mysql> SELECT * FROM v1; + +------------+------------+ + | Name_exp_1 | Name_exp_2 | + +------------+------------+ + | | 2 | + | | 3 | + +------------+------------+ + + So, checking for duplicate names in only "sl", current + SELECT_LEX. + */ + if (check_duplicate_names(sl->item_list, 1)) + DBUG_RETURN(true); } - DBUG_VOID_RETURN; + DBUG_RETURN(false); } @@ -545,10 +579,7 @@ } /* Check if the auto generated column names are conforming. */ - for (sl= select_lex; sl; sl= sl->next_select()) - make_valid_column_names(sl->item_list); - - if (check_duplicate_names(select_lex->item_list, 1)) + if (make_valid_column_names(select_lex)) { res= TRUE; goto err;