diff options
-rw-r--r-- | mysql-test/r/select.result | 10 | ||||
-rw-r--r-- | mysql-test/t/select.test | 11 | ||||
-rw-r--r-- | sql/item.cc | 15 | ||||
-rw-r--r-- | sql/sql_lex.cc | 2 | ||||
-rw-r--r-- | sql/sql_lex.h | 1 | ||||
-rw-r--r-- | sql/sql_select.cc | 5 |
6 files changed, 44 insertions, 0 deletions
diff --git a/mysql-test/r/select.result b/mysql-test/r/select.result index 4effb8d173c..df0dd3af616 100644 --- a/mysql-test/r/select.result +++ b/mysql-test/r/select.result @@ -2617,6 +2617,16 @@ select found_rows(); found_rows() 1 DROP TABLE t1; +CREATE TABLE t1 (a INT, b INT); +(SELECT a, b AS c FROM t1) ORDER BY c+1; +a c +(SELECT a, b AS c FROM t1) ORDER BY b+1; +a c +SELECT a, b AS c FROM t1 ORDER BY c+1; +a c +SELECT a, b AS c FROM t1 ORDER BY b+1; +a c +drop table t1; create table t1(f1 int, f2 int); create table t2(f3 int); select f1 from t1,t2 where f1=f2 and (f1,f2) = ((1,1)); diff --git a/mysql-test/t/select.test b/mysql-test/t/select.test index e6f91389d7b..7dc017d149f 100644 --- a/mysql-test/t/select.test +++ b/mysql-test/t/select.test @@ -2053,6 +2053,7 @@ AND FK_firma_id = 2; drop table t1; # +# # Test for Bug#8009, SELECT failed on bigint unsigned when using HEX # @@ -2165,6 +2166,16 @@ select found_rows(); DROP TABLE t1; # +# Bug 7672 Unknown column error in order clause +# +CREATE TABLE t1 (a INT, b INT); +(SELECT a, b AS c FROM t1) ORDER BY c+1; +(SELECT a, b AS c FROM t1) ORDER BY b+1; +SELECT a, b AS c FROM t1 ORDER BY c+1; +SELECT a, b AS c FROM t1 ORDER BY b+1; +drop table t1; + +# # Bug #13356 assertion failed in resolve_const_item() # create table t1(f1 int, f2 int); diff --git a/sql/item.cc b/sql/item.cc index 7419bc173d5..d284af2b9f1 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -1760,6 +1760,21 @@ bool Item_field::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) if ((tmp= find_field_in_tables(thd, this, tables, &where, 0)) == not_found_field) { + /* Look up in current select's item_list to find aliased fields */ + if (thd->lex->current_select->is_item_list_lookup) + { + uint counter; + bool not_used; + Item** res= find_item_in_list(this, thd->lex->current_select->item_list, + &counter, REPORT_EXCEPT_NOT_FOUND, + ¬_used); + if (res != not_found_item && (*res)->type() == Item::FIELD_ITEM) + { + set_field((*((Item_field**)res))->field); + return 0; + } + } + /* We can't find table field in table list of current select, consequently we have to find it in outer subselect(s). diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 8636b6542fc..16641ad6dd5 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -1083,6 +1083,7 @@ void st_select_lex::init_query() prep_where= 0; subquery_in_having= explicit_limit= 0; parsing_place= NO_MATTER; + is_item_list_lookup= 0; } void st_select_lex::init_select() @@ -1109,6 +1110,7 @@ void st_select_lex::init_select() select_limit= HA_POS_ERROR; offset_limit= 0; with_sum_func= 0; + } /* diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 8d919f12563..47908ba8685 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -426,6 +426,7 @@ public: List<Item> item_list; /* list of fields & expressions */ List<String> interval_list, use_index, *use_index_ptr, ignore_index, *ignore_index_ptr; + bool is_item_list_lookup; /* Usualy it is pointer to ftfunc_list_alloc, but in union used to create fake select_lex for calling mysql_select under results of union diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 0d9cab6a36b..f72d897e22d 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -8348,11 +8348,16 @@ find_order_in_list(THD *thd, Item **ref_pointer_array, 'it' reassigned in if condition because fix_field can change it. */ + thd->lex->current_select->is_item_list_lookup= 1; if (!it->fixed && (it->fix_fields(thd, tables, order->item) || (it= *order->item)->check_cols(1) || thd->is_fatal_error)) + { + thd->lex->current_select->is_item_list_lookup= 0; return 1; // Wrong field + } + thd->lex->current_select->is_item_list_lookup= 0; uint el= all_fields.elements; all_fields.push_front(it); // Add new field to field list ref_pointer_array[el]= it; |