Skip to content

Commit

Permalink
Fixed the bug mdev-12564.
Browse files Browse the repository at this point in the history
Here's  what started happening after the patch that fixed
the bug mdev-10454 with query reported for the bug
SELECT * FROM t t1 right JOIN t t2 ON (t2.pk = t1.pk)
  WHERE (t2.i, t2.pk) NOT IN ( SELECT t3.i, t3.i FROM t t3, t t4 )
        AND t1.c = 'foo';
The patch added an implementation of propagate_equal_fields() for
the class Item_row and thus opened the possibility of equal fields
substitutions.
At the prepare stage after setup_conds() called for WHERE condition
had completed the flag of maybe_null of the Item_row object created
for  (t2.i, t2.pk) was set to false, because the maybe_null flags of
both elements were set to false. However the flag of maybe_null for
t1.pk from the ON condition were set to true, because t1 was an inner
table of an outer join.
At the optimization stage the outer join was converted to inner join,
but the maybe_null flags were not corrected and remained the same.
So after the substitution t2.pk/t1.pk. the maybe_null flag for the
row remained false while the maybe_flag for the second element of
the row was true. As a result, when the in-to_exists transformation
was performed for the NOT IN predicate the guards variables were
not created for the elements of the row, but a guard object for
the second element was created. The object were not valid because
it referred to NULL as a guard variable. This ultimately caused
a crash when the expression with the guard was evaluated at the
execution stage.

The patch made sure that the guard objects are not created without
guard variables.

Yet it does not resolve the problem of inconsistent maybe_null flags.
and it might be that the problem will pop op in other pieces of code.
The resolution of this problem is not easy, but the problem should
be resolved in future versions.
  • Loading branch information
igorbabaev committed Apr 24, 2017
1 parent 44b1fb3 commit 0906dc4
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 4 deletions.
14 changes: 14 additions & 0 deletions mysql-test/r/subselect.result
Original file line number Diff line number Diff line change
Expand Up @@ -7204,3 +7204,17 @@ f1 f2 f3
DROP TABLE t1, t2;
SET NAMES default;
End of 10.1 tests
#
# MDEV-12564: IN TO EXISTS transformation for rows after
# conversion an outer join to inner join
#
CREATE TABLE t (
pk int PRIMARY KEY, i int NOT NULL, c varchar(8), KEY(c)
) ENGINE=MyISAM;
INSERT INTO t VALUES (1,10,'foo'),(2,20,'bar');
SELECT * FROM t t1 RIGHT JOIN t t2 ON (t2.pk = t1.pk)
WHERE (t2.i, t2.pk) NOT IN ( SELECT t3.i, t3.i FROM t t3, t t4 ) AND t1.c = 'foo';
pk i c pk i c
1 10 foo 1 10 foo
DROP TABLE t;
End of 10.2 tests
14 changes: 14 additions & 0 deletions mysql-test/r/subselect_no_exists_to_in.result
Original file line number Diff line number Diff line change
Expand Up @@ -7204,6 +7204,20 @@ f1 f2 f3
DROP TABLE t1, t2;
SET NAMES default;
End of 10.1 tests
#
# MDEV-12564: IN TO EXISTS transformation for rows after
# conversion an outer join to inner join
#
CREATE TABLE t (
pk int PRIMARY KEY, i int NOT NULL, c varchar(8), KEY(c)
) ENGINE=MyISAM;
INSERT INTO t VALUES (1,10,'foo'),(2,20,'bar');
SELECT * FROM t t1 RIGHT JOIN t t2 ON (t2.pk = t1.pk)
WHERE (t2.i, t2.pk) NOT IN ( SELECT t3.i, t3.i FROM t t3, t t4 ) AND t1.c = 'foo';
pk i c pk i c
1 10 foo 1 10 foo
DROP TABLE t;
End of 10.2 tests
set optimizer_switch=default;
select @@optimizer_switch like '%exists_to_in=off%';
@@optimizer_switch like '%exists_to_in=off%'
Expand Down
14 changes: 14 additions & 0 deletions mysql-test/r/subselect_no_mat.result
Original file line number Diff line number Diff line change
Expand Up @@ -7197,6 +7197,20 @@ f1 f2 f3
DROP TABLE t1, t2;
SET NAMES default;
End of 10.1 tests
#
# MDEV-12564: IN TO EXISTS transformation for rows after
# conversion an outer join to inner join
#
CREATE TABLE t (
pk int PRIMARY KEY, i int NOT NULL, c varchar(8), KEY(c)
) ENGINE=MyISAM;
INSERT INTO t VALUES (1,10,'foo'),(2,20,'bar');
SELECT * FROM t t1 RIGHT JOIN t t2 ON (t2.pk = t1.pk)
WHERE (t2.i, t2.pk) NOT IN ( SELECT t3.i, t3.i FROM t t3, t t4 ) AND t1.c = 'foo';
pk i c pk i c
1 10 foo 1 10 foo
DROP TABLE t;
End of 10.2 tests
set optimizer_switch=default;
select @@optimizer_switch like '%materialization=on%';
@@optimizer_switch like '%materialization=on%'
Expand Down
14 changes: 14 additions & 0 deletions mysql-test/r/subselect_no_opts.result
Original file line number Diff line number Diff line change
Expand Up @@ -7195,4 +7195,18 @@ f1 f2 f3
DROP TABLE t1, t2;
SET NAMES default;
End of 10.1 tests
#
# MDEV-12564: IN TO EXISTS transformation for rows after
# conversion an outer join to inner join
#
CREATE TABLE t (
pk int PRIMARY KEY, i int NOT NULL, c varchar(8), KEY(c)
) ENGINE=MyISAM;
INSERT INTO t VALUES (1,10,'foo'),(2,20,'bar');
SELECT * FROM t t1 RIGHT JOIN t t2 ON (t2.pk = t1.pk)
WHERE (t2.i, t2.pk) NOT IN ( SELECT t3.i, t3.i FROM t t3, t t4 ) AND t1.c = 'foo';
pk i c pk i c
1 10 foo 1 10 foo
DROP TABLE t;
End of 10.2 tests
set @optimizer_switch_for_subselect_test=null;
14 changes: 14 additions & 0 deletions mysql-test/r/subselect_no_scache.result
Original file line number Diff line number Diff line change
Expand Up @@ -7210,6 +7210,20 @@ f1 f2 f3
DROP TABLE t1, t2;
SET NAMES default;
End of 10.1 tests
#
# MDEV-12564: IN TO EXISTS transformation for rows after
# conversion an outer join to inner join
#
CREATE TABLE t (
pk int PRIMARY KEY, i int NOT NULL, c varchar(8), KEY(c)
) ENGINE=MyISAM;
INSERT INTO t VALUES (1,10,'foo'),(2,20,'bar');
SELECT * FROM t t1 RIGHT JOIN t t2 ON (t2.pk = t1.pk)
WHERE (t2.i, t2.pk) NOT IN ( SELECT t3.i, t3.i FROM t t3, t t4 ) AND t1.c = 'foo';
pk i c pk i c
1 10 foo 1 10 foo
DROP TABLE t;
End of 10.2 tests
set optimizer_switch=default;
select @@optimizer_switch like '%subquery_cache=on%';
@@optimizer_switch like '%subquery_cache=on%'
Expand Down
14 changes: 14 additions & 0 deletions mysql-test/r/subselect_no_semijoin.result
Original file line number Diff line number Diff line change
Expand Up @@ -7195,5 +7195,19 @@ f1 f2 f3
DROP TABLE t1, t2;
SET NAMES default;
End of 10.1 tests
#
# MDEV-12564: IN TO EXISTS transformation for rows after
# conversion an outer join to inner join
#
CREATE TABLE t (
pk int PRIMARY KEY, i int NOT NULL, c varchar(8), KEY(c)
) ENGINE=MyISAM;
INSERT INTO t VALUES (1,10,'foo'),(2,20,'bar');
SELECT * FROM t t1 RIGHT JOIN t t2 ON (t2.pk = t1.pk)
WHERE (t2.i, t2.pk) NOT IN ( SELECT t3.i, t3.i FROM t t3, t t4 ) AND t1.c = 'foo';
pk i c pk i c
1 10 foo 1 10 foo
DROP TABLE t;
End of 10.2 tests
set @optimizer_switch_for_subselect_test=null;
set @join_cache_level_for_subselect_test=NULL;
17 changes: 17 additions & 0 deletions mysql-test/t/subselect.test
Original file line number Diff line number Diff line change
Expand Up @@ -6056,3 +6056,20 @@ DROP TABLE t1, t2;
SET NAMES default;

--echo End of 10.1 tests

--echo #
--echo # MDEV-12564: IN TO EXISTS transformation for rows after
--echo # conversion an outer join to inner join
--echo #

CREATE TABLE t (
pk int PRIMARY KEY, i int NOT NULL, c varchar(8), KEY(c)
) ENGINE=MyISAM;
INSERT INTO t VALUES (1,10,'foo'),(2,20,'bar');

SELECT * FROM t t1 RIGHT JOIN t t2 ON (t2.pk = t1.pk)
WHERE (t2.i, t2.pk) NOT IN ( SELECT t3.i, t3.i FROM t t3, t t4 ) AND t1.c = 'foo';

DROP TABLE t;

--echo End of 10.2 tests
11 changes: 7 additions & 4 deletions sql/item_subselect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2388,7 +2388,8 @@ Item_in_subselect::create_row_in_to_exists_cond(JOIN * join,
(char *)"<list ref>"));
Item *col_item= new (thd->mem_root)
Item_cond_or(thd, item_eq, item_isnull);
if (!abort_on_null && left_expr->element_index(i)->maybe_null)
if (!abort_on_null && left_expr->element_index(i)->maybe_null &&
get_cond_guard(i))
{
disable_cond_guard_for_const_null_left_expr(i);
if (!(col_item= new (thd->mem_root)
Expand All @@ -2406,7 +2407,8 @@ Item_in_subselect::create_row_in_to_exists_cond(JOIN * join,
ref_pointer_array[i],
(char *)"<no matter>",
(char *)"<list ref>"));
if (!abort_on_null && left_expr->element_index(i)->maybe_null)
if (!abort_on_null && left_expr->element_index(i)->maybe_null &&
get_cond_guard(i) )
{
disable_cond_guard_for_const_null_left_expr(i);
if (!(item_nnull_test=
Expand Down Expand Up @@ -2467,7 +2469,7 @@ Item_in_subselect::create_row_in_to_exists_cond(JOIN * join,
(char *)"<no matter>",
(char *)"<list ref>"));
item= new (thd->mem_root) Item_cond_or(thd, item, item_isnull);
if (left_expr->element_index(i)->maybe_null)
if (left_expr->element_index(i)->maybe_null && get_cond_guard(i))
{
disable_cond_guard_for_const_null_left_expr(i);
if (!(item= new (thd->mem_root)
Expand All @@ -2479,7 +2481,8 @@ Item_in_subselect::create_row_in_to_exists_cond(JOIN * join,
}
*having_item= and_items(thd, *having_item, having_col_item);
}
if (!abort_on_null && left_expr->element_index(i)->maybe_null)
if (!abort_on_null && left_expr->element_index(i)->maybe_null &&
get_cond_guard(i))
{
if (!(item= new (thd->mem_root)
Item_func_trig_cond(thd, item, get_cond_guard(i))))
Expand Down

0 comments on commit 0906dc4

Please sign in to comment.