Commit 6bfc2d4b authored by evgen@moonbone.local's avatar evgen@moonbone.local

Fix bug#7672 Unknown column error in order clause

When fixing Item_func_plus in ORDER BY clause field c is searched in all
opened tables, but because c is an alias it wasn't found there.

This patch adds a flag to select_lex which allows Item_field::fix_fields() 
to look up in select's item_list to find aliased fields.
parent a8bdf632
...@@ -2431,3 +2431,13 @@ AND FK_firma_id = 2; ...@@ -2431,3 +2431,13 @@ AND FK_firma_id = 2;
COUNT(*) COUNT(*)
0 0
drop table t1; 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;
...@@ -1983,3 +1983,12 @@ AND FK_firma_id = 2; ...@@ -1983,3 +1983,12 @@ AND FK_firma_id = 2;
drop table t1; 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;
...@@ -348,7 +348,18 @@ bool Item_field::fix_fields(THD *thd,TABLE_LIST *tables) ...@@ -348,7 +348,18 @@ bool Item_field::fix_fields(THD *thd,TABLE_LIST *tables)
{ {
Field *tmp; Field *tmp;
if (!(tmp=find_field_in_tables(thd,this,tables))) if (!(tmp=find_field_in_tables(thd,this,tables)))
{
if (thd->lex.select_lex.is_item_list_lookup)
{
Item** res= find_item_in_list(this, thd->lex.select_lex.item_list);
if (res && *res && (*res)->type() == Item::FIELD_ITEM)
{
set_field((*((Item_field**)res))->field);
return 0;
}
}
return 1; return 1;
}
set_field(tmp); set_field(tmp);
} }
else if (thd && thd->set_query_id && field->query_id != thd->query_id) else if (thd && thd->set_query_id && field->query_id != thd->query_id)
......
...@@ -154,6 +154,7 @@ LEX *lex_start(THD *thd, uchar *buf,uint length) ...@@ -154,6 +154,7 @@ LEX *lex_start(THD *thd, uchar *buf,uint length)
lex->slave_thd_opt=0; lex->slave_thd_opt=0;
lex->sql_command=SQLCOM_END; lex->sql_command=SQLCOM_END;
bzero((char *)&lex->mi,sizeof(lex->mi)); bzero((char *)&lex->mi,sizeof(lex->mi));
lex->select_lex.is_item_list_lookup= 0;
return lex; return lex;
} }
......
...@@ -121,7 +121,7 @@ typedef struct st_select_lex ...@@ -121,7 +121,7 @@ typedef struct st_select_lex
ignore_index, *ignore_index_ptr; ignore_index, *ignore_index_ptr;
List<Item_func_match> ftfunc_list; List<Item_func_match> ftfunc_list;
uint in_sum_expr, sort_default; uint in_sum_expr, sort_default;
bool create_refs, braces; bool create_refs, braces, is_item_list_lookup;
st_select_lex *next; st_select_lex *next;
} SELECT_LEX; } SELECT_LEX;
......
...@@ -6845,8 +6845,14 @@ find_order_in_list(THD *thd,TABLE_LIST *tables,ORDER *order,List<Item> &fields, ...@@ -6845,8 +6845,14 @@ find_order_in_list(THD *thd,TABLE_LIST *tables,ORDER *order,List<Item> &fields,
return 0; return 0;
} }
order->in_field_list=0; order->in_field_list=0;
/* Allow lookup in select's item_list to find aliased fields */
thd->lex.select_lex.is_item_list_lookup= 1;
if ((*order->item)->fix_fields(thd,tables) || thd->fatal_error) if ((*order->item)->fix_fields(thd,tables) || thd->fatal_error)
{
thd->lex.select_lex.is_item_list_lookup= 0;
return 1; // Wrong field return 1; // Wrong field
}
thd->lex.select_lex.is_item_list_lookup= 0;
all_fields.push_front(*order->item); // Add new field to field list all_fields.push_front(*order->item); // Add new field to field list
order->item=(Item**) all_fields.head_ref(); order->item=(Item**) all_fields.head_ref();
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment