Commit fa7f6772 authored by Igor Babaev's avatar Igor Babaev

Fixed bug mdev-4355.

This patch almost totally revised the patch for bug mdev-4177.
The latter had too many defects. In particular, it did not
propagate multiple equalities formed when merging a degenerate
disjunct into underlying AND formula.
parent 8a732d5a
......@@ -5155,7 +5155,7 @@ a b c
8 8 8
DROP TABLE t1,t2;
#
# Bug mdev-4413: another manifestations of bug mdev-2474
# Bug mdev-4413: another manifestations of bug mdev-4274
# (valgrind complains)
#
CREATE TABLE t1 (a int, b int) ENGINE=MyISAM;
......@@ -5167,4 +5167,54 @@ WHERE c = a AND
( 0 OR ( b BETWEEN 45 AND 300 OR a > 45 AND a < 100 ) AND b = c );
a b c
DROP TABLE t1, t2;
#
# Bug mdev-4355: equalities from the result of simplification of OR
# are not propagated to lower AND levels
#
CREATE TABLE t1 (a INT, b INT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1,101),(2,102),(3,103),(4,104),(5,11);
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (1 != 1 OR a = 5) AND (b != 1 OR a = 1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where ((`test`.`t1`.`a` = 5) and (`test`.`t1`.`b` <> 1))
SELECT * FROM t1 WHERE (1 != 1 OR a = 5) AND (b != 1 OR a = 1);
a b
5 11
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (1 != 1 OR a = 5);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where ((`test`.`t1`.`a` = 5) and (`test`.`t1`.`b` <> 1))
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (1 != 1 OR a = 5);
a b
5 11
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (a = 5 OR 1 != 1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where ((`test`.`t1`.`a` = 5) and (`test`.`t1`.`b` <> 1))
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (a = 5 OR 1 != 1);
a b
5 11
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b = 1 OR a = 1) AND (b = 5 AND a = 5 OR 1 != 1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where 0
SELECT * FROM t1 WHERE (b = 1 OR a = 1) AND (b = 5 AND a = 5 OR 1 != 1);
a b
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b = 1 OR a = 5) AND (b = 5 AND a = 5 OR 1 != 1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where ((`test`.`t1`.`b` = 5) and (`test`.`t1`.`a` = 5))
SELECT * FROM t1 WHERE (b = 1 OR a = 5) AND (b = 5 AND a = 5 OR 1 != 1);
a b
DROP TABLE t1;
End of 5.3 tests
......@@ -5166,7 +5166,7 @@ a b c
8 8 8
DROP TABLE t1,t2;
#
# Bug mdev-4413: another manifestations of bug mdev-2474
# Bug mdev-4413: another manifestations of bug mdev-4274
# (valgrind complains)
#
CREATE TABLE t1 (a int, b int) ENGINE=MyISAM;
......@@ -5178,6 +5178,56 @@ WHERE c = a AND
( 0 OR ( b BETWEEN 45 AND 300 OR a > 45 AND a < 100 ) AND b = c );
a b c
DROP TABLE t1, t2;
#
# Bug mdev-4355: equalities from the result of simplification of OR
# are not propagated to lower AND levels
#
CREATE TABLE t1 (a INT, b INT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1,101),(2,102),(3,103),(4,104),(5,11);
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (1 != 1 OR a = 5) AND (b != 1 OR a = 1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where ((`test`.`t1`.`a` = 5) and (`test`.`t1`.`b` <> 1))
SELECT * FROM t1 WHERE (1 != 1 OR a = 5) AND (b != 1 OR a = 1);
a b
5 11
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (1 != 1 OR a = 5);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where ((`test`.`t1`.`a` = 5) and (`test`.`t1`.`b` <> 1))
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (1 != 1 OR a = 5);
a b
5 11
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (a = 5 OR 1 != 1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where ((`test`.`t1`.`a` = 5) and (`test`.`t1`.`b` <> 1))
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (a = 5 OR 1 != 1);
a b
5 11
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b = 1 OR a = 1) AND (b = 5 AND a = 5 OR 1 != 1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where 0
SELECT * FROM t1 WHERE (b = 1 OR a = 1) AND (b = 5 AND a = 5 OR 1 != 1);
a b
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b = 1 OR a = 5) AND (b = 5 AND a = 5 OR 1 != 1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where ((`test`.`t1`.`b` = 5) and (`test`.`t1`.`a` = 5))
SELECT * FROM t1 WHERE (b = 1 OR a = 5) AND (b = 5 AND a = 5 OR 1 != 1);
a b
DROP TABLE t1;
End of 5.3 tests
set join_cache_level=default;
show variables like 'join_cache_level';
......
......@@ -5155,7 +5155,7 @@ a b c
8 8 8
DROP TABLE t1,t2;
#
# Bug mdev-4413: another manifestations of bug mdev-2474
# Bug mdev-4413: another manifestations of bug mdev-4274
# (valgrind complains)
#
CREATE TABLE t1 (a int, b int) ENGINE=MyISAM;
......@@ -5167,4 +5167,54 @@ WHERE c = a AND
( 0 OR ( b BETWEEN 45 AND 300 OR a > 45 AND a < 100 ) AND b = c );
a b c
DROP TABLE t1, t2;
#
# Bug mdev-4355: equalities from the result of simplification of OR
# are not propagated to lower AND levels
#
CREATE TABLE t1 (a INT, b INT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1,101),(2,102),(3,103),(4,104),(5,11);
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (1 != 1 OR a = 5) AND (b != 1 OR a = 1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where ((`test`.`t1`.`a` = 5) and (`test`.`t1`.`b` <> 1))
SELECT * FROM t1 WHERE (1 != 1 OR a = 5) AND (b != 1 OR a = 1);
a b
5 11
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (1 != 1 OR a = 5);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where ((`test`.`t1`.`a` = 5) and (`test`.`t1`.`b` <> 1))
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (1 != 1 OR a = 5);
a b
5 11
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (a = 5 OR 1 != 1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where ((`test`.`t1`.`a` = 5) and (`test`.`t1`.`b` <> 1))
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (a = 5 OR 1 != 1);
a b
5 11
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b = 1 OR a = 1) AND (b = 5 AND a = 5 OR 1 != 1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where 0
SELECT * FROM t1 WHERE (b = 1 OR a = 1) AND (b = 5 AND a = 5 OR 1 != 1);
a b
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b = 1 OR a = 5) AND (b = 5 AND a = 5 OR 1 != 1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where ((`test`.`t1`.`b` = 5) and (`test`.`t1`.`a` = 5))
SELECT * FROM t1 WHERE (b = 1 OR a = 5) AND (b = 5 AND a = 5 OR 1 != 1);
a b
DROP TABLE t1;
End of 5.3 tests
......@@ -1380,7 +1380,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 100.00 Using where
2 MATERIALIZED t3 hash_ALL NULL #hash#$hj 5 test.t2.i2 3 100.00 Using where; Using join buffer (flat, BNLH join)
Warnings:
Note 1003 select `test`.`t1`.`i1` AS `i1` from `test`.`t1` semi join (`test`.`t2` join `test`.`t3`) where ((`test`.`t3`.`i3` = `test`.`t2`.`i2`) and (`test`.`t1`.`i1` = `test`.`t2`.`i2`) and (`test`.`t2`.`i2` > 0))
Note 1003 select `test`.`t1`.`i1` AS `i1` from `test`.`t1` semi join (`test`.`t2` join `test`.`t3`) where ((`test`.`t3`.`i3` = `test`.`t2`.`i2`) and (`test`.`t1`.`i1` = `test`.`t2`.`i2`) and (`test`.`t3`.`i3` > 0))
SELECT * FROM t1
WHERE i1 IN (SELECT i3 FROM t2, t3 WHERE i3 > 0 AND i3 = i2 OR 1=2);
i1
......
......@@ -4326,7 +4326,7 @@ SELECT * FROM t1 INNER JOIN t2 ON ( c = a )
DROP TABLE t1,t2;
--echo #
--echo # Bug mdev-4413: another manifestations of bug mdev-2474
--echo # Bug mdev-4413: another manifestations of bug mdev-4274
--echo # (valgrind complains)
--echo #
......@@ -4342,4 +4342,34 @@ SELECT * FROM t1, t2
DROP TABLE t1, t2;
--echo #
--echo # Bug mdev-4355: equalities from the result of simplification of OR
--echo # are not propagated to lower AND levels
--echo #
CREATE TABLE t1 (a INT, b INT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1,101),(2,102),(3,103),(4,104),(5,11);
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (1 != 1 OR a = 5) AND (b != 1 OR a = 1);
SELECT * FROM t1 WHERE (1 != 1 OR a = 5) AND (b != 1 OR a = 1);
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (1 != 1 OR a = 5);
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (1 != 1 OR a = 5);
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (a = 5 OR 1 != 1);
SELECT * FROM t1 WHERE (b != 1 OR a = 1) AND (a = 5 OR 1 != 1);
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b = 1 OR a = 1) AND (b = 5 AND a = 5 OR 1 != 1);
SELECT * FROM t1 WHERE (b = 1 OR a = 1) AND (b = 5 AND a = 5 OR 1 != 1);
EXPLAIN EXTENDED
SELECT * FROM t1 WHERE (b = 1 OR a = 5) AND (b = 5 AND a = 5 OR 1 != 1);
SELECT * FROM t1 WHERE (b = 1 OR a = 5) AND (b = 5 AND a = 5 OR 1 != 1);
DROP TABLE t1;
--echo End of 5.3 tests
......@@ -5608,10 +5608,12 @@ 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
@param item multiple equality whose members are to be merged
@param save_merged keep the list of equalities in 'item' intact
(e.g. for other merges)
@details
If the Item_equal 'item' happened to have some elements of the list
If the Item_equal 'item' happens 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
......@@ -5626,24 +5628,45 @@ void Item_equal::merge(Item_equal *item)
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.
The method 'merge_into_with_check' really merges two lists of equal items if
they have common members.
*/
bool Item_equal::merge_with_check(Item_equal *item)
bool Item_equal::merge_with_check(Item_equal *item, bool save_merged)
{
bool intersected= FALSE;
Item_equal_fields_iterator_slow fi(*this);
Item_equal_fields_iterator_slow fi(*item);
while (fi++)
{
if (item->contains(fi.get_curr_field()))
if (contains(fi.get_curr_field()))
{
fi.remove();
intersected= TRUE;
if (!save_merged)
fi.remove();
}
}
if (intersected)
item->merge(this);
{
if (!save_merged)
merge(item);
else
{
Item *c= item->get_const();
if (c)
add_const(c);
if (!cond_false)
{
Item *item;
fi.rewind();
while ((item= fi++))
{
if (!contains(fi.get_curr_field()))
add(item);
}
}
}
}
return intersected;
}
......@@ -5652,17 +5675,25 @@ bool Item_equal::merge_with_check(Item_equal *item)
@brief
Merge this object into a list of Item_equal objects
@param list the list of Item_equal objects to merge into
@param list the list of Item_equal objects to merge into
@param save_merged keep the list of equalities in 'this' intact
(e.g. for other merges)
@param only_intersected do not merge if there are no common members
in any of Item_equal objects from the list
and this Item_equal
@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 that replaces e1,...,ek in the 'list'. Otherwise, in the case
when the value of the parameter only_if_intersected is false, this
Item_equal is joined to the 'list'.
*/
void Item_equal::merge_into_list(List<Item_equal> *list)
void Item_equal::merge_into_list(List<Item_equal> *list,
bool save_merged,
bool only_intersected)
{
Item_equal *item;
List_iterator<Item_equal> it(*list);
......@@ -5671,16 +5702,16 @@ void Item_equal::merge_into_list(List<Item_equal> *list)
{
if (!merge_into)
{
if (merge_with_check(item))
if (item->merge_with_check(this, save_merged))
merge_into= item;
}
else
{
if (item->merge_with_check(merge_into))
if (merge_into->merge_with_check(item, false))
it.remove();
}
}
if (!merge_into)
if (!only_intersected && !merge_into)
list->push_back(this);
}
......
......@@ -1761,8 +1761,9 @@ class Item_equal: public Item_bool_func
/** 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);
bool merge_with_check(Item_equal *equal_item, bool save_merged);
void merge_into_list(List<Item_equal> *list, bool save_merged,
bool only_intersected);
void update_const();
enum Functype functype() const { return MULT_EQUAL_FUNC; }
longlong val_int();
......
This diff is collapsed.
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