diff --git a/mysql-test/r/subselect4.result b/mysql-test/r/subselect4.result index e8b43758753..2fd5a20c319 100644 --- a/mysql-test/r/subselect4.result +++ b/mysql-test/r/subselect4.result @@ -1190,6 +1190,54 @@ set @@optimizer_switch='materialization=off,semijoin=on'; set @@optimizer_switch=@save_optimizer_switch; drop table t1, t2; # +# LP BUG#777691 Wrong result with subqery in select list and subquery cache=off in maria-5.3 +# +CREATE TABLE t1 ( f1 varchar(32)) ; +INSERT INTO t1 VALUES ('b'),('x'),('c'),('x'); +CREATE TABLE t2 ( f2 int, f3 varchar(32)) ; +INSERT INTO t2 VALUES (1,'x'); +set @save_optimizer_switch=@@optimizer_switch; +set @@optimizer_switch='materialization=off,in_to_exists=on,subquery_cache=off'; +EXPLAIN +SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 4 +2 DEPENDENT SUBQUERY t2 system NULL NULL NULL NULL 1 +SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1; +f1 max_f2 +b NULL +x 1 +c NULL +x 1 +set @@optimizer_switch='materialization=on,in_to_exists=off,subquery_cache=off'; +EXPLAIN +SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 4 +2 DEPENDENT SUBQUERY t2 system NULL NULL NULL NULL 1 +SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1; +f1 max_f2 +b NULL +x 1 +c NULL +x 1 +set @@optimizer_switch='materialization=off,in_to_exists=on,subquery_cache=off'; +Even when t2 is not constant table, the result must be the same. +INSERT INTO t2 VALUES (2,'y'); +EXPLAIN +SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 4 +2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where +SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1; +f1 max_f2 +b NULL +x 1 +c NULL +x 1 +set @@optimizer_switch=@save_optimizer_switch; +drop table t1, t2; +# # LP BUG#641203 Query returns rows where no result is expected (impossible WHERE) # CREATE TABLE t1 (c1 varchar(1) DEFAULT NULL); diff --git a/mysql-test/t/subselect4.test b/mysql-test/t/subselect4.test index f2dca9d3a4f..cfaddd58685 100644 --- a/mysql-test/t/subselect4.test +++ b/mysql-test/t/subselect4.test @@ -964,6 +964,40 @@ WHERE (t2.pk = t1.pk) AND t2.pk IN (SELECT f2 FROM t1); -- echo set @@optimizer_switch='materialization=off,semijoin=on'; +set @@optimizer_switch=@save_optimizer_switch; + +drop table t1, t2; + + +--echo # +--echo # LP BUG#777691 Wrong result with subqery in select list and subquery cache=off in maria-5.3 +--echo # + +CREATE TABLE t1 ( f1 varchar(32)) ; +INSERT INTO t1 VALUES ('b'),('x'),('c'),('x'); + +CREATE TABLE t2 ( f2 int, f3 varchar(32)) ; +INSERT INTO t2 VALUES (1,'x'); + +set @save_optimizer_switch=@@optimizer_switch; +set @@optimizer_switch='materialization=off,in_to_exists=on,subquery_cache=off'; + +EXPLAIN +SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1; +SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1; + +set @@optimizer_switch='materialization=on,in_to_exists=off,subquery_cache=off'; +EXPLAIN +SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1; +SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1; + +set @@optimizer_switch='materialization=off,in_to_exists=on,subquery_cache=off'; +--echo Even when t2 is not constant table, the result must be the same. +INSERT INTO t2 VALUES (2,'y'); +EXPLAIN +SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1; +SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1; + set @@optimizer_switch=@save_optimizer_switch; drop table t1, t2; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index b323cae468d..4be9d534fa7 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -14651,14 +14651,6 @@ do_select(JOIN *join,List *fields,TABLE *table,Procedure *procedure) { List *columns_list= (procedure ? &join->procedure_fields_list : fields); - /* - With implicit grouping all fields of special row produced for an - empty result are NULL. See return_zero_rows() for the same behavior. - */ - TABLE_LIST *table; - List_iterator_fast li(join->select_lex->leaf_tables); - while ((table= li++)) - mark_as_null_row(table->table); rc= join->result->send_data(*columns_list) > 0; } }