1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-30 16:24:05 +03:00
The flag alias_name_used was not set on for the outer references
in subqueries. It resulted in replacement of any outer reference
resolved against an alias for a full field name when the frm 
representation of a view with a subquery was generated. 
If the subquery and the outer query referenced the same table in
their from lists this replacement effectively changed the meaning
of the view and led to wrong results for selects from this view. 

Modified several functions to ensure setting the right value of
the alias_name_used flag for outer references resolved against
aliases.
 


mysql-test/r/view.result:
  Added a test case for bug #26560.
mysql-test/t/view.test:
  Added a test case for bug #26560.
sql/item.cc:
  Fixed bug #26560.
  Made the function resolve_ref_in_select_and_group analyze the return
  value of the last parameter with the type of the name resolution for
  the submitted reference. If the reference has been resolved against 
  an alias name from select list then its flag alias_name_used is set on.
  Now this value is used in Item_field::fix_outer_field to initialize the flag
  when the item_ref object is created for an outer reference.
  Added a parameter for the second Item_ref::Item_ref constructor to initialize
  properly the flag alias_name_used. The default value of the parameter is FALSE.
  If this flag is set on at the creation of an object by this constructor it
  will never be changed. Corrected appropriately the Item_ref::set_properties
  function.
  The function Item_ref::print now prints alias name for an outer reference
  if the flag alias_name_used is set on.
sql/item.h:
  Fixed bug #26560.
  Added a parameter for the second Item_ref::Item_ref constructor to initialize
  properly the flag alias_name_used. The default value of the parameter is FALSE.
  A similar change has been applied to the first Item_direct_ref::Item_direct_ref
  constructor.
sql/mysql_priv.h:
  Fixed bug #26560.
  Added an an enumeration type enum_resolution_type to return info on
  how the function find_item_in_list has resolved the submitted item.
  The type is used only for this function.
sql/sql_base.cc:
  Fixed bug #26560.
  Made the last parameter of the function find_field_in_tables return
  more detailed information on how the submitted item has been resolved.
  Now it says whether the item has been resolved
    against an alias name,
    or as a field name without alias,
    or as a field name hidden by alias, 
    or was resolved ignoring alias.
sql/sql_select.cc:
  Fixed bug #26560.
  Took into account the new type of the last parameter of the function
  find_item_in_list.
This commit is contained in:
unknown
2007-03-04 19:54:35 -08:00
parent 476f91b50c
commit 6da758c216
7 changed files with 195 additions and 69 deletions

View File

@ -3468,10 +3468,13 @@ find_field_in_tables(THD *thd, Item_ident *item,
return not_found_item, report other errors,
return 0
IGNORE_ERRORS Do not report errors, return 0 if error
unaliased Set to true if item is field which was found
by original field name and not by its alias
in item list. Set to false otherwise.
resolution Set to the resolution type if the item is found
(it says whether the item is resolved
against an alias name,
or as a field name without alias,
or as a field hidden by alias,
or ignoring alias)
RETURN VALUES
0 Item is not found or item is not unique,
error message is reported
@ -3487,7 +3490,8 @@ Item **not_found_item= (Item**) 0x1;
Item **
find_item_in_list(Item *find, List<Item> &items, uint *counter,
find_item_error_report_type report_error, bool *unaliased)
find_item_error_report_type report_error,
enum_resolution_type *resolution)
{
List_iterator<Item> li(items);
Item **found=0, **found_unaliased= 0, *item;
@ -3501,10 +3505,9 @@ find_item_in_list(Item *find, List<Item> &items, uint *counter,
*/
bool is_ref_by_name= 0;
uint unaliased_counter;
LINT_INIT(unaliased_counter); // Dependent on found_unaliased
*unaliased= FALSE;
*resolution= NOT_RESOLVED;
is_ref_by_name= (find->type() == Item::FIELD_ITEM ||
find->type() == Item::REF_ITEM);
@ -3571,63 +3574,77 @@ find_item_in_list(Item *find, List<Item> &items, uint *counter,
}
found_unaliased= li.ref();
unaliased_counter= i;
*resolution= RESOLVED_IGNORING_ALIAS;
if (db_name)
break; // Perfect match
}
}
else if (!my_strcasecmp(system_charset_info, item_field->name,
field_name))
else
{
/*
If table name was not given we should scan through aliases
(or non-aliased fields) first. We are also checking unaliased
name of the field in then next else-if, to be able to find
instantly field (hidden by alias) if no suitable alias (or
non-aliased field) was found.
*/
if (found)
int fname_cmp= my_strcasecmp(system_charset_info,
item_field->field_name,
field_name);
if (!my_strcasecmp(system_charset_info,
item_field->name,field_name))
{
if ((*found)->eq(item, 0))
continue; // Same field twice
if (report_error != IGNORE_ERRORS)
my_error(ER_NON_UNIQ_ERROR, MYF(0),
find->full_name(), current_thd->where);
return (Item**) 0;
/*
If table name was not given we should scan through aliases
and non-aliased fields first. We are also checking unaliased
name of the field in then next else-if, to be able to find
instantly field (hidden by alias) if no suitable alias or
non-aliased field was found.
*/
if (found)
{
if ((*found)->eq(item, 0))
continue; // Same field twice
if (report_error != IGNORE_ERRORS)
my_error(ER_NON_UNIQ_ERROR, MYF(0),
find->full_name(), current_thd->where);
return (Item**) 0;
}
found= li.ref();
*counter= i;
*resolution= fname_cmp ? RESOLVED_AGAINST_ALIAS:
RESOLVED_WITH_NO_ALIAS;
}
found= li.ref();
*counter= i;
}
else if (!my_strcasecmp(system_charset_info, item_field->field_name,
field_name))
{
/*
We will use un-aliased field or react on such ambiguities only if
we won't be able to find aliased field.
Again if we have ambiguity with field outside of select list
we should prefer fields from select list.
*/
if (found_unaliased)
{
if ((*found_unaliased)->eq(item, 0))
continue; // Same field twice
found_unaliased_non_uniq= 1;
}
else
else if (!fname_cmp)
{
/*
We will use non-aliased field or react on such ambiguities only if
we won't be able to find aliased field.
Again if we have ambiguity with field outside of select list
we should prefer fields from select list.
*/
if (found_unaliased)
{
if ((*found_unaliased)->eq(item, 0))
continue; // Same field twice
found_unaliased_non_uniq= 1;
}
found_unaliased= li.ref();
unaliased_counter= i;
}
}
}
else if (!table_name && (find->eq(item,0) ||
is_ref_by_name && find->name && item->name &&
!my_strcasecmp(system_charset_info,
item->name,find->name)))
{
found= li.ref();
*counter= i;
break;
}
else if (!table_name)
{
if (is_ref_by_name && find->name && item->name &&
!my_strcasecmp(system_charset_info,item->name,find->name))
{
found= li.ref();
*counter= i;
*resolution= RESOLVED_AGAINST_ALIAS;
break;
}
else if (find->eq(item,0))
{
found= li.ref();
*counter= i;
*resolution= RESOLVED_IGNORING_ALIAS;
break;
}
}
}
if (!found)
{
@ -3642,7 +3659,7 @@ find_item_in_list(Item *find, List<Item> &items, uint *counter,
{
found= found_unaliased;
*counter= unaliased_counter;
*unaliased= TRUE;
*resolution= RESOLVED_BEHIND_ALIAS;
}
}
if (found)