summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorunknown <igor@rurik.mysql.com>2004-12-25 19:17:57 -0800
committerunknown <igor@rurik.mysql.com>2004-12-25 19:17:57 -0800
commita4d840a09c09ca0c17bcc6776dda3d8f4ebec07d (patch)
tree5fd3149194f671afa2f117c7d5fff0f9720577f2
parent1548c6b765464ef05c12fd0147819e1b35791bf3 (diff)
downloadmariadb-git-a4d840a09c09ca0c17bcc6776dda3d8f4ebec07d.tar.gz
subselect.result, subselect.test:
Added test cases for bug #7351. item_cmpfunc.cc: Fixed bug #7351: incorrect result for a query with a subquery returning empty set. If in the predicate v IN (SELECT a FROM t WHERE cond) v is null, then the result of the predicate is either INKNOWN or FALSE. It is FALSE if the subquery returns an empty set. item_subselect.cc: Fixed bug #7351: incorrect result for a query with a subquery returning empty set. The problem was due to not a quite legal transformation for 'IN' subqueries. A subquery containing a predicate of the form v IN (SELECT a FROM t WHERE cond) was transformed into EXISTS(SELECT a FROM t WHERE cond AND (a=v OR a IS NULL)). Yet, this transformation is valid only if v is not null. If v is null, then, in the case when (SELECT a FROM t WHERE cond) returns an empty set the value of the predicate is FALSE, otherwise the result of the predicate is INKNOWN. The fix resolves this problem by changing the result of the transformation to EXISTS(SELECT a FROM t WHERE cond AND (v IS NULL OR (a=v OR a IS NULL))) in the case when v is nullable. The new transformation prevents applying the lookup optimization for IN subqueries. To make it still applicable we have to introduce guarded access methods. sql/item_subselect.cc: Fixed bug #7351: incorrect result for a query with a subquery returning empty set. The problem was due to not a quite legal transformation for 'IN' subqueries. A subquery containing a predicate of the form v IN (SELECT a FROM t WHERE cond) was transformed into EXISTS(SELECT a FROM t WHERE cond AND (a=v OR a IS NULL)). Yet, this transformation is valid only if v is not null. If v is null, then, in the case when (SELECT a FROM t WHERE cond) returns an empty set the value of the predicate is FALSE, otherwise the result of the predicate is INKNOWN. The fix resolves this problem by changing the result of the transformation to EXISTS(SELECT a FROM t WHERE cond AND (v IS NULL OR (a=v OR a IS NULL))) in the case when v is nullable. The new transformation prevents applying the lookup optimization for IN subqueries. To make it still applicable we have to introduce guarded access methods. sql/item_cmpfunc.cc: Fixed bug #7351: incorrect result for a query with a subquery returning empty set. If in the predicate v IN (SELECT a FROM t WHERE cond) v is null, then the result of the predicate is either INKNOWN or FALSE. It is FALSE if the subquery returns an empty set. mysql-test/t/subselect.test: Added test cases for bug #7351. mysql-test/r/subselect.result: Added test cases for bug #7351.
-rw-r--r--mysql-test/r/subselect.result25
-rw-r--r--mysql-test/t/subselect.test21
-rw-r--r--sql/item_cmpfunc.cc5
-rw-r--r--sql/item_subselect.cc13
4 files changed, 52 insertions, 12 deletions
diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result
index 0735f133e6f..b264018866c 100644
--- a/mysql-test/r/subselect.result
+++ b/mysql-test/r/subselect.result
@@ -1425,7 +1425,7 @@ Note 1003 (select test.t1.s1 AS `s1` from test.t1)
s1
tttt
drop table t1;
-create table t1 (s1 char(5), index s1(s1));
+create table t1 (s1 char(5) not null, index s1(s1));
create table t2 (s1 char(5), index s1(s1));
insert into t1 values ('a1'),('a2'),('a3');
insert into t2 values ('a1'),('a2');
@@ -1451,25 +1451,25 @@ a2 1
a3 1
explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY t1 index NULL s1 6 NULL 3 Using index
+1 PRIMARY t1 index NULL s1 5 NULL 3 Using index
2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 Using index
Warnings:
Note 1003 select test.t1.s1 AS `s1`,not(<in_optimizer>(test.t1.s1,<exists>(<index_lookup>(<cache>(test.t1.s1) in t2 on s1 chicking NULL)))) AS `s1 NOT IN (SELECT s1 FROM t2)` from test.t1
explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY t1 index NULL s1 6 NULL 3 Using index
+1 PRIMARY t1 index NULL s1 5 NULL 3 Using index
2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 Using index
Warnings:
Note 1003 select test.t1.s1 AS `s1`,<in_optimizer>(test.t1.s1,<exists>(<index_lookup>(<cache>(test.t1.s1) in t2 on s1 chicking NULL))) AS `s1 = ANY (SELECT s1 FROM t2)` from test.t1
explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY t1 index NULL s1 6 NULL 3 Using index
+1 PRIMARY t1 index NULL s1 5 NULL 3 Using index
2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 Using index
Warnings:
Note 1003 select test.t1.s1 AS `s1`,not(<in_optimizer>(test.t1.s1,<exists>(<index_lookup>(<cache>(test.t1.s1) in t2 on s1 chicking NULL)))) AS `s1 <> ALL (SELECT s1 FROM t2)` from test.t1
explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY t1 index NULL s1 6 NULL 3 Using index
+1 PRIMARY t1 index NULL s1 5 NULL 3 Using index
2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 1 Using index; Using where
Warnings:
Note 1003 select test.t1.s1 AS `s1`,not(<in_optimizer>(test.t1.s1,<exists>(<index_lookup>(<cache>(test.t1.s1) in t2 on s1 chicking NULL where (test.t2.s1 < _latin1'a2'))))) AS `s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')` from test.t1
@@ -2125,3 +2125,18 @@ SELECT DISTINCT Continent AS c FROM t1 WHERE Code <> SOME ( SELECT Code FROM t1
c
Oceania
drop table t1;
+CREATE TABLE t1 ( f1 BIGINT );
+INSERT INTO t1 SET f1= NULL;
+INSERT INTO t1 SET f1= 1;
+CREATE TABLE t2 ( f1 BIGINT );
+SELECT f1 FROM t1
+WHERE f1 <> ALL ( SELECT f1 FROM t2 );
+f1
+NULL
+1
+INSERT INTO t2 VALUES (1), (2);
+SELECT f1 FROM t1
+WHERE f1 <> ALL ( SELECT f1 FROM t2 WHERE f1 > 2 );
+f1
+NULL
+1
diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test
index f59851fa722..6f7daa66897 100644
--- a/mysql-test/t/subselect.test
+++ b/mysql-test/t/subselect.test
@@ -889,7 +889,7 @@ drop table t1;
#
# IN optimisation test results
#
-create table t1 (s1 char(5), index s1(s1));
+create table t1 (s1 char(5) not null, index s1(s1));
create table t2 (s1 char(5), index s1(s1));
insert into t1 values ('a1'),('a2'),('a3');
insert into t2 values ('a1'),('a2');
@@ -1386,3 +1386,22 @@ INSERT INTO t1 VALUES ('UMI','United States Minor Outlying Islands','Oceania','M
/*!40000 ALTER TABLE t1 ENABLE KEYS */;
SELECT DISTINCT Continent AS c FROM t1 WHERE Code <> SOME ( SELECT Code FROM t1 WHERE Continent = c AND Population < 200);
drop table t1;
+
+#
+# Test cases for bug #7351:
+# quantified predicate with subquery returning empty result set
+#
+
+CREATE TABLE t1 ( f1 BIGINT );
+INSERT INTO t1 SET f1= NULL;
+INSERT INTO t1 SET f1= 1;
+CREATE TABLE t2 ( f1 BIGINT );
+
+SELECT f1 FROM t1
+ WHERE f1 <> ALL ( SELECT f1 FROM t2 );
+
+INSERT INTO t2 VALUES (1), (2);
+
+SELECT f1 FROM t1
+ WHERE f1 <> ALL ( SELECT f1 FROM t2 WHERE f1 > 2 );
+
diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc
index a135f08ae45..3d79c16b5d0 100644
--- a/sql/item_cmpfunc.cc
+++ b/sql/item_cmpfunc.cc
@@ -636,12 +636,13 @@ longlong Item_in_optimizer::val_int()
{
DBUG_ASSERT(fixed == 1);
cache->store(args[0]);
+ longlong tmp= args[1]->val_int_result();
if (cache->null_value)
{
- null_value= 1;
+ if (tmp)
+ null_value= 1;
return 0;
}
- longlong tmp= args[1]->val_int_result();
null_value= args[1]->null_value;
return tmp;
}
diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc
index 1d0f46fd196..e1a80941a52 100644
--- a/sql/item_subselect.cc
+++ b/sql/item_subselect.cc
@@ -824,6 +824,8 @@ Item_in_subselect::single_value_transformer(JOIN *join,
select_lex->ref_pointer_array,
(char *)"<ref>",
this->full_name()));
+ if (!abort_on_null && left_expr->maybe_null)
+ item= new Item_cond_or(new Item_func_isnull(left_expr), item);
/*
AND and comparison functions can't be changed during fix_fields()
we can assign select_lex->having here, and pass 0 as last
@@ -869,6 +871,8 @@ Item_in_subselect::single_value_transformer(JOIN *join,
select_lex->having_fix_field= 0;
item= new Item_cond_or(item,
new Item_func_isnull(orig_item));
+ if (left_expr->maybe_null)
+ item= new Item_cond_or(new Item_func_isnull(left_expr), item);
}
item->name= (char *)in_additional_cond;
/*
@@ -889,12 +893,13 @@ Item_in_subselect::single_value_transformer(JOIN *join,
we can assign select_lex->having here, and pass 0 as last
argument (reference) to fix_fields()
*/
- select_lex->having=
- join->having=
- func->create(expr,
- new Item_null_helper(this, item,
+ item= func->create(expr,
+ new Item_null_helper(this, item,
(char *)"<no matter>",
(char *)"<result>"));
+ if (!abort_on_null && left_expr->maybe_null)
+ item= new Item_cond_or(new Item_func_isnull(left_expr), item);
+ select_lex->having= join->having= item;
select_lex->having_fix_field= 1;
if (join->having->fix_fields(thd, join->tables_list,
0))