Fixed bug mdev-4177

The function remove_eq_cond removes the parts of a disjunction
for which it has been proved that they are always true. In the
result of this removal the disjunction may be converted into a 
formula without OR that must be merged into the the AND formula
that contains the disjunction.
The merging of two AND conditions must take into account the
multiple equalities that may be part of each of them.
These multiple equality must be merged and become part of the
and object built as the result of the merge of the AND conditions.
Erroneously the function remove_eq_cond lacked the code that 
would merge multiple equalities of the merged AND conditions.
This could lead to confusing situations when at the same AND 
level there were two multiple equalities with common members
and the list of equal items contained only some of these 
multiple equalities.
This, in its turn, could lead to an incorrect work of the
function substitute_for_best_equal_field when it tried to optimize
ref accesses. This resulted in forming invalid TABLE_REF objects
that were used to build look-up keys when materialized subqueries
were exploited.
This commit is contained in:
Igor Babaev 2013-02-24 19:16:11 -08:00
commit d434d79acf
7 changed files with 290 additions and 17 deletions

View file

@ -5491,7 +5491,7 @@ Item_equal::Item_equal(Item_equal *item_equal)
}
/*
/**
@brief
Add a constant item to the Item_equal object
@ -5537,6 +5537,7 @@ void Item_equal::add_const(Item *c, Item *f)
const_item_cache= 1;
}
/**
@brief
Check whether a field is referred to in the multiple equality
@ -5603,6 +5604,87 @@ void Item_equal::merge(Item_equal *item)
}
/**
@brief
Merge members of another Item_equal object into this one
@param item multiple equality whose members are to be merged
@details
If the Item_equal 'item' happened to have some elements of the list
of equal items belonging to 'this' object then the function merges
the equal items from 'item' into this list.
If both lists contains constants and they are different then
the value of the cond_false flag is set to TRUE.
@retval
1 the lists of equal items in 'item' and 'this' contain common elements
@retval
0 otherwise
@notes
The method 'merge' just joins the list of equal items belonging to 'item'
to the list of equal items belonging to this object assuming that the lists
are disjoint. It would be more correct to call the method 'join'.
The method 'merge_with_check' really merges two lists of equal items if they
have common members.
*/
bool Item_equal::merge_with_check(Item_equal *item)
{
bool intersected= FALSE;
Item_equal_fields_iterator_slow fi(*this);
while (fi++)
{
if (item->contains(fi.get_curr_field()))
{
fi.remove();
intersected= TRUE;
}
}
if (intersected)
item->merge(this);
return intersected;
}
/**
@brief
Merge this object into a list of Item_equal objects
@param list the list of Item_equal objects to merge into
@details
If the list of equal items from 'this' object contains common members
with the lists of equal items belonging to Item_equal objects from 'list'
then all involved Item_equal objects e1,...,ek are merged into one
Item equal that replaces e1,...,ek in the 'list'. Otherwise this
Item_equal is joined to the 'list'.
*/
void Item_equal::merge_into_list(List<Item_equal> *list)
{
Item_equal *item;
List_iterator<Item_equal> it(*list);
Item_equal *merge_into= NULL;
while((item= it++))
{
if (!merge_into)
{
if (merge_with_check(item))
merge_into= item;
}
else
{
if (item->merge_with_check(merge_into))
it.remove();
}
}
if (!merge_into)
list->push_back(this);
}
/**
@brief
Order equal items of the multiple equality according to a sorting criteria

View file

@ -1612,6 +1612,7 @@ public:
bool eval_not_null_tables(uchar *opt_arg);
};
template <template<class> class LI, class T> class Item_equal_iterator;
/*
The class Item_equal is used to represent conjunctions of equality
@ -1760,6 +1761,8 @@ public:
/** Get number of field items / references to field items in this object */
uint n_field_items() { return equal_items.elements-test(with_const); }
void merge(Item_equal *item);
bool merge_with_check(Item_equal *equal_item);
void merge_into_list(List<Item_equal> *list);
void update_const();
enum Functype functype() const { return MULT_EQUAL_FUNC; }
longlong val_int();
@ -1775,7 +1778,8 @@ public:
CHARSET_INFO *compare_collation();
void set_context_field(Item_field *ctx_field) { context_field= ctx_field; }
friend class Item_equal_fields_iterator;
friend class Item_equal_iterator<List_iterator_fast,Item>;
friend class Item_equal_iterator<List_iterator,Item>;
friend Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
Item_equal *item_equal);
friend bool setup_sj_materialization_part1(struct st_join_table *tab);
@ -1798,39 +1802,42 @@ public:
/*
The class Item_equal_fields_iterator is used to iterate over references
to table/view columns from a list of equal items.
The template Item_equal_iterator is used to define classes
Item_equal_fields_iterator and Item_equal_fields_iterator_slow.
These are helper classes for the class Item equal
Both classes are used to iterate over references to table/view columns
from the list of equal items that included in an Item_equal object.
The second class supports the operation of removal of the current member
from the list when performing an iteration.
*/
class Item_equal_fields_iterator : public List_iterator_fast<Item>
template <template<class> class LI, class T> class Item_equal_iterator
: public LI<T>
{
protected:
Item_equal *item_equal;
Item *curr_item;
public:
Item_equal_fields_iterator(Item_equal &item_eq)
:List_iterator_fast<Item> (item_eq.equal_items)
Item_equal_iterator<LI,T>(Item_equal &item_eq)
:LI<T> (item_eq.equal_items)
{
curr_item= NULL;
item_equal= &item_eq;
if (item_eq.with_const)
{
List_iterator_fast<Item> *list_it= this;
LI<T> *list_it= this;
curr_item= (*list_it)++;
}
}
Item* operator++(int)
{
List_iterator_fast<Item> *list_it= this;
LI<T> *list_it= this;
curr_item= (*list_it)++;
return curr_item;
}
Item ** ref()
{
return List_iterator_fast<Item>::ref();
}
void rewind(void)
{
List_iterator_fast<Item> *list_it= this;
LI<T> *list_it= this;
list_it->rewind();
if (item_equal->with_const)
curr_item= (*list_it)++;
@ -1843,6 +1850,34 @@ public:
};
class Item_equal_fields_iterator
:public Item_equal_iterator<List_iterator_fast,Item >
{
public:
Item_equal_fields_iterator(Item_equal &item_eq)
:Item_equal_iterator(item_eq)
{ }
Item ** ref()
{
return List_iterator_fast<Item>::ref();
}
};
class Item_equal_fields_iterator_slow
:public Item_equal_iterator<List_iterator,Item >
{
public:
Item_equal_fields_iterator_slow(Item_equal &item_eq)
:Item_equal_iterator(item_eq)
{ }
void remove()
{
List_iterator<Item>::remove();
}
};
class Item_cond_and :public Item_cond
{
public:

View file

@ -13159,7 +13159,61 @@ remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value)
li.remove();
else if (item != new_item)
{
VOID(li.replace(new_item));
if (and_level)
{
/*
Take a special care of multiple equality predicates
that may be part of 'cond' and 'new_item'.
Those multiple equalities that have common members
must be merged.
*/
Item_cond_and *cond_and= (Item_cond_and *) cond;
List<Item_equal> *cond_equal_items=
&cond_and->cond_equal.current_level;
List<Item> *cond_and_list= cond_and->argument_list();
if (new_item->type() == Item::COND_ITEM &&
((Item_cond*) new_item)->functype() == Item_func::COND_AND_FUNC)
{
Item_cond_and *new_item_and= (Item_cond_and *) new_item;
List<Item_equal> *new_item_equal_items=
&new_item_and->cond_equal.current_level;
List<Item> *new_item_and_list= new_item_and->argument_list();
cond_and_list->disjoin((List<Item>*) cond_equal_items);
new_item_and_list->disjoin((List<Item>*) new_item_equal_items);
Item_equal *equal_item;
List_iterator<Item_equal> it(*new_item_equal_items);
while ((equal_item= it++))
{
equal_item->merge_into_list(cond_equal_items);
}
if (new_item_and_list->is_empty())
li.remove();
else
li.replace(*new_item_and_list);
cond_and_list->concat((List<Item>*) cond_equal_items);
}
else if (new_item->type() == Item::FUNC_ITEM &&
((Item_cond*) new_item)->functype() ==
Item_func::MULT_EQUAL_FUNC)
{
cond_and_list->disjoin((List<Item>*) cond_equal_items);
((Item_equal *) new_item)->merge_into_list(cond_equal_items);
li.remove();
cond_and_list->concat((List<Item>*) cond_equal_items);
}
else
li.replace(new_item);
}
else
{
if (new_item->type() == Item::COND_ITEM &&
((Item_cond*) new_item)->functype() ==
((Item_cond*) cond)->functype())
li.replace(*((Item_cond*) new_item)->argument_list());
else
li.replace(new_item);
}
should_fix_fields=1;
}
if (*cond_value == Item::COND_UNDEF)