Commit 3630a00e authored by Igor Babaev's avatar Igor Babaev

Fixed bug mdev-10782.

This bug in the code of Item_ref::build_clone could
cause corruption of items in where conditions.
Also made sure that equality predicates extracted
from multiple equality items to be pushed into
materialized views were cloned.
parent 9810d5ea
...@@ -6982,3 +6982,28 @@ drop view v1,v2,v3,v4; ...@@ -6982,3 +6982,28 @@ drop view v1,v2,v3,v4;
drop view v_union,v2_union,v3_union,v4_union; drop view v_union,v2_union,v3_union,v4_union;
drop view v_double,v_char,v_decimal; drop view v_double,v_char,v_decimal;
drop table t1,t2,t1_double,t2_double,t1_char,t2_char,t1_decimal,t2_decimal; drop table t1,t2,t1_double,t2_double,t1_char,t2_char,t1_decimal,t2_decimal;
#
# MDEV-10782: condition extracted from a multiple equality
# pushed into HAVING
#
CREATE TABLE t1 (i int);
INSERT INTO t1 VALUES (1),(2);
EXPLAIN EXTENDED
SELECT *
FROM ( SELECT * FROM ( SELECT MIN(i) as f FROM t1 ) sq1 ) AS sq2
WHERE f = 8;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <derived3> ALL NULL NULL NULL NULL 2 100.00 Using where
3 DERIVED t1 ALL NULL NULL NULL NULL 2 100.00
Warnings:
Note 1003 select `sq1`.`f` AS `f` from (select min(`test`.`t1`.`i`) AS `f` from `test`.`t1` having (`f` = 8)) `sq1` where (`sq1`.`f` = 8)
SELECT *
FROM ( SELECT * FROM ( SELECT MIN(i) as f FROM t1 ) sq1 ) AS sq2
WHERE f = 8;
f
SELECT *
FROM ( SELECT * FROM ( SELECT MIN(i) as f FROM t1 ) sq1 ) AS sq2
WHERE f = 1;
f
1
DROP TABLE t1;
...@@ -877,4 +877,26 @@ eval explain format=json $query; ...@@ -877,4 +877,26 @@ eval explain format=json $query;
drop view v1,v2,v3,v4; drop view v1,v2,v3,v4;
drop view v_union,v2_union,v3_union,v4_union; drop view v_union,v2_union,v3_union,v4_union;
drop view v_double,v_char,v_decimal; drop view v_double,v_char,v_decimal;
drop table t1,t2,t1_double,t2_double,t1_char,t2_char,t1_decimal,t2_decimal; drop table t1,t2,t1_double,t2_double,t1_char,t2_char,t1_decimal,t2_decimal;
\ No newline at end of file
--echo #
--echo # MDEV-10782: condition extracted from a multiple equality
--echo # pushed into HAVING
--echo #
CREATE TABLE t1 (i int);
INSERT INTO t1 VALUES (1),(2);
EXPLAIN EXTENDED
SELECT *
FROM ( SELECT * FROM ( SELECT MIN(i) as f FROM t1 ) sq1 ) AS sq2
WHERE f = 8;
SELECT *
FROM ( SELECT * FROM ( SELECT MIN(i) as f FROM t1 ) sq1 ) AS sq2
WHERE f = 8;
SELECT *
FROM ( SELECT * FROM ( SELECT MIN(i) as f FROM t1 ) sq1 ) AS sq2
WHERE f = 1;
DROP TABLE t1;
\ No newline at end of file
...@@ -2296,10 +2296,16 @@ Item* Item_func_or_sum::build_clone(THD *thd, MEM_ROOT *mem_root) ...@@ -2296,10 +2296,16 @@ Item* Item_func_or_sum::build_clone(THD *thd, MEM_ROOT *mem_root)
if (!copy) if (!copy)
return 0; return 0;
if (arg_count > 2) if (arg_count > 2)
{
copy->args= copy->args=
(Item**) alloc_root(mem_root, sizeof(Item*) * arg_count); (Item**) alloc_root(mem_root, sizeof(Item*) * arg_count);
if (!copy->args)
return 0;
}
else if (arg_count > 0) else if (arg_count > 0)
copy->args= copy->tmp_arg; copy->args= copy->tmp_arg;
for (uint i= 0; i < arg_count; i++) for (uint i= 0; i < arg_count; i++)
{ {
Item *arg_clone= args[i]->build_clone(thd, mem_root); Item *arg_clone= args[i]->build_clone(thd, mem_root);
...@@ -2332,6 +2338,10 @@ Item* Item_ref::build_clone(THD *thd, MEM_ROOT *mem_root) ...@@ -2332,6 +2338,10 @@ Item* Item_ref::build_clone(THD *thd, MEM_ROOT *mem_root)
Item_ref *copy= (Item_ref *) get_copy(thd, mem_root); Item_ref *copy= (Item_ref *) get_copy(thd, mem_root);
if (!copy) if (!copy)
return 0; return 0;
copy->ref=
(Item**) alloc_root(mem_root, sizeof(Item*));
if (!copy->ref)
return 0;
Item *item_clone= (* ref)->build_clone(thd, mem_root); Item *item_clone= (* ref)->build_clone(thd, mem_root);
if (!item_clone) if (!item_clone)
return 0; return 0;
......
...@@ -8057,8 +8057,12 @@ Item* TABLE_LIST::build_pushable_cond_for_table(THD *thd, Item *cond) ...@@ -8057,8 +8057,12 @@ Item* TABLE_LIST::build_pushable_cond_for_table(THD *thd, Item *cond)
{ {
if (!(item->used_tables() == tab_map)) if (!(item->used_tables() == tab_map))
continue; continue;
Item_func_eq *eq= Item_func_eq *eq= 0;
new (thd->mem_root) Item_func_eq(thd, item, left_item); Item *left_item_clone= left_item->build_clone(thd, thd->mem_root);
Item *right_item_clone= item->build_clone(thd, thd->mem_root);
if (left_item_clone && right_item_clone)
eq= new (thd->mem_root) Item_func_eq(thd, right_item_clone,
left_item_clone);
if (eq) if (eq)
{ {
i++; i++;
...@@ -8071,10 +8075,13 @@ Item* TABLE_LIST::build_pushable_cond_for_table(THD *thd, Item *cond) ...@@ -8071,10 +8075,13 @@ Item* TABLE_LIST::build_pushable_cond_for_table(THD *thd, Item *cond)
new_cond= new (thd->mem_root) Item_cond_and(thd, new_cond, eq); new_cond= new (thd->mem_root) Item_cond_and(thd, new_cond, eq);
break; break;
default: default:
((Item_cond_and*)new_cond)->argument_list()->push_back(eq, thd->mem_root); ((Item_cond_and*)new_cond)->argument_list()->push_back(eq,
thd->mem_root);
} }
} }
} }
if (new_cond)
new_cond->fix_fields(thd, &new_cond);
return new_cond; return new_cond;
} }
else if (cond->get_extraction_flag() != NO_EXTRACTION_FL) else if (cond->get_extraction_flag() != NO_EXTRACTION_FL)
......
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