diff --git a/mysql-test/r/update_innodb.result b/mysql-test/r/update_innodb.result index 88c86c50625ea..9ba6699a47308 100644 --- a/mysql-test/r/update_innodb.result +++ b/mysql-test/r/update_innodb.result @@ -29,3 +29,29 @@ CREATE ALGORITHM=UNDEFINED VIEW `v1` AS select `t4`.`c1` AS `c1`,`t4`.`c2` AS `c UPDATE t1 a JOIN t2 b ON a.c1 = b.c1 JOIN v1 vw ON b.c2 = vw.c1 JOIN t3 del ON vw.c2 = del.c2 SET a.c2 = ( SELECT max(t.c1) FROM t3 t, v1 i WHERE del.c2 = t.c2 AND vw.c3 = i.c3 AND t.c3 = 4 ) WHERE a.c2 IS NULL OR a.c2 < '2011-05-01'; drop view v1; drop table t1,t2,t3,t4; +# +# MDEV-10232 Scalar result of subquery changes after adding an outer select stmt +# +CREATE TABLE t1 ( +a_id INT(20) UNSIGNED NOT NULL AUTO_INCREMENT, +b_id INT(20) UNSIGNED NULL DEFAULT NULL, +c_id VARCHAR(255) NULL DEFAULT NULL, +PRIMARY KEY (a_id))COLLATE = 'utf8_general_ci' ENGINE = InnoDB; +CREATE TABLE t2 ( +b_id INT(20) UNSIGNED NOT NULL AUTO_INCREMENT, +c_id VARCHAR(255) NULL DEFAULT NULL, +PRIMARY KEY (b_id), +INDEX idx_c_id (c_id))COLLATE = 'utf8_general_ci' ENGINE = InnoDB; +INSERT INTO t1 (b_id, c_id) VALUES (NULL, NULL); +INSERT INTO t2 (c_id) VALUES (NULL); +INSERT INTO t2 (c_id) VALUES (NULL); +SELECT * FROM T1; +a_id b_id c_id +1 NULL NULL +SELECT t2.b_id FROM t1,t2 WHERE t2.c_id = t1.c_id; +b_id +UPDATE t1 SET b_id = (SELECT t2.b_id FROM t2 t2 WHERE t2.c_id = t1.c_id); +SELECT * FROM T1; +a_id b_id c_id +1 NULL NULL +drop table t1,t2; diff --git a/mysql-test/t/update_innodb.test b/mysql-test/t/update_innodb.test index 67c356c4e2e1d..d4fb8b772122b 100644 --- a/mysql-test/t/update_innodb.test +++ b/mysql-test/t/update_innodb.test @@ -37,3 +37,29 @@ UPDATE t1 a JOIN t2 b ON a.c1 = b.c1 JOIN v1 vw ON b.c2 = vw.c1 JOIN t3 del ON v drop view v1; drop table t1,t2,t3,t4; + +--echo # +--echo # MDEV-10232 Scalar result of subquery changes after adding an outer select stmt +--echo # + +CREATE TABLE t1 ( + a_id INT(20) UNSIGNED NOT NULL AUTO_INCREMENT, + b_id INT(20) UNSIGNED NULL DEFAULT NULL, + c_id VARCHAR(255) NULL DEFAULT NULL, + PRIMARY KEY (a_id))COLLATE = 'utf8_general_ci' ENGINE = InnoDB; + +CREATE TABLE t2 ( + b_id INT(20) UNSIGNED NOT NULL AUTO_INCREMENT, + c_id VARCHAR(255) NULL DEFAULT NULL, + PRIMARY KEY (b_id), + INDEX idx_c_id (c_id))COLLATE = 'utf8_general_ci' ENGINE = InnoDB; + +INSERT INTO t1 (b_id, c_id) VALUES (NULL, NULL); +INSERT INTO t2 (c_id) VALUES (NULL); +INSERT INTO t2 (c_id) VALUES (NULL); + +SELECT * FROM T1; +SELECT t2.b_id FROM t1,t2 WHERE t2.c_id = t1.c_id; +UPDATE t1 SET b_id = (SELECT t2.b_id FROM t2 t2 WHERE t2.c_id = t1.c_id); +SELECT * FROM T1; +drop table t1,t2; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index e37c1b8c2809d..cc07067bc8d2b 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -9367,8 +9367,6 @@ static void add_not_null_conds(JOIN *join) UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1); not_null_item is the t1.f1, but it's referred_tab is 0. */ - if (!referred_tab) - continue; if (!(notnull= new (join->thd->mem_root) Item_func_isnotnull(join->thd, item))) DBUG_VOID_RETURN; @@ -9380,16 +9378,19 @@ static void add_not_null_conds(JOIN *join) */ if (notnull->fix_fields(join->thd, ¬null)) DBUG_VOID_RETURN; + DBUG_EXECUTE("where",print_where(notnull, - referred_tab->table->alias.c_ptr(), - QT_ORDINARY);); + (referred_tab ? + referred_tab->table->alias.c_ptr() : + "outer_ref_cond"), + QT_ORDINARY);); if (!tab->first_inner) - { - COND *new_cond= referred_tab->join == join ? + { + COND *new_cond= (referred_tab && referred_tab->join == join) ? referred_tab->select_cond : join->outer_ref_cond; add_cond_and_fix(join->thd, &new_cond, notnull); - if (referred_tab->join == join) + if (referred_tab && referred_tab->join == join) referred_tab->set_select_cond(new_cond, __LINE__); else join->outer_ref_cond= new_cond;