Skip to content

Commit c2b217e

Browse files
committed
MDEV-10731: Wrong NULL match results in "Subquery returns more than 1 row" (error code 1242)
NOT NULL predicate was not added to tables in case of an update query having a subquery.
1 parent 99b2de9 commit c2b217e

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

mysql-test/r/update_innodb.result

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,29 @@ CREATE ALGORITHM=UNDEFINED VIEW `v1` AS select `t4`.`c1` AS `c1`,`t4`.`c2` AS `c
2929
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';
3030
drop view v1;
3131
drop table t1,t2,t3,t4;
32+
#
33+
# MDEV-10232 Scalar result of subquery changes after adding an outer select stmt
34+
#
35+
CREATE TABLE t1 (
36+
a_id INT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
37+
b_id INT(20) UNSIGNED NULL DEFAULT NULL,
38+
c_id VARCHAR(255) NULL DEFAULT NULL,
39+
PRIMARY KEY (a_id))COLLATE = 'utf8_general_ci' ENGINE = InnoDB;
40+
CREATE TABLE t2 (
41+
b_id INT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
42+
c_id VARCHAR(255) NULL DEFAULT NULL,
43+
PRIMARY KEY (b_id),
44+
INDEX idx_c_id (c_id))COLLATE = 'utf8_general_ci' ENGINE = InnoDB;
45+
INSERT INTO t1 (b_id, c_id) VALUES (NULL, NULL);
46+
INSERT INTO t2 (c_id) VALUES (NULL);
47+
INSERT INTO t2 (c_id) VALUES (NULL);
48+
SELECT * FROM T1;
49+
a_id b_id c_id
50+
1 NULL NULL
51+
SELECT t2.b_id FROM t1,t2 WHERE t2.c_id = t1.c_id;
52+
b_id
53+
UPDATE t1 SET b_id = (SELECT t2.b_id FROM t2 t2 WHERE t2.c_id = t1.c_id);
54+
SELECT * FROM T1;
55+
a_id b_id c_id
56+
1 NULL NULL
57+
drop table t1,t2;

mysql-test/t/update_innodb.test

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3737

3838
drop view v1;
3939
drop table t1,t2,t3,t4;
40+
41+
--echo #
42+
--echo # MDEV-10232 Scalar result of subquery changes after adding an outer select stmt
43+
--echo #
44+
45+
CREATE TABLE t1 (
46+
a_id INT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
47+
b_id INT(20) UNSIGNED NULL DEFAULT NULL,
48+
c_id VARCHAR(255) NULL DEFAULT NULL,
49+
PRIMARY KEY (a_id))COLLATE = 'utf8_general_ci' ENGINE = InnoDB;
50+
51+
CREATE TABLE t2 (
52+
b_id INT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
53+
c_id VARCHAR(255) NULL DEFAULT NULL,
54+
PRIMARY KEY (b_id),
55+
INDEX idx_c_id (c_id))COLLATE = 'utf8_general_ci' ENGINE = InnoDB;
56+
57+
INSERT INTO t1 (b_id, c_id) VALUES (NULL, NULL);
58+
INSERT INTO t2 (c_id) VALUES (NULL);
59+
INSERT INTO t2 (c_id) VALUES (NULL);
60+
61+
SELECT * FROM T1;
62+
SELECT t2.b_id FROM t1,t2 WHERE t2.c_id = t1.c_id;
63+
UPDATE t1 SET b_id = (SELECT t2.b_id FROM t2 t2 WHERE t2.c_id = t1.c_id);
64+
SELECT * FROM T1;
65+
drop table t1,t2;

sql/sql_select.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9367,8 +9367,6 @@ static void add_not_null_conds(JOIN *join)
93679367
UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
93689368
not_null_item is the t1.f1, but it's referred_tab is 0.
93699369
*/
9370-
if (!referred_tab)
9371-
continue;
93729370
if (!(notnull= new (join->thd->mem_root)
93739371
Item_func_isnotnull(join->thd, item)))
93749372
DBUG_VOID_RETURN;
@@ -9380,16 +9378,19 @@ static void add_not_null_conds(JOIN *join)
93809378
*/
93819379
if (notnull->fix_fields(join->thd, &notnull))
93829380
DBUG_VOID_RETURN;
9381+
93839382
DBUG_EXECUTE("where",print_where(notnull,
9384-
referred_tab->table->alias.c_ptr(),
9385-
QT_ORDINARY););
9383+
(referred_tab ?
9384+
referred_tab->table->alias.c_ptr() :
9385+
"outer_ref_cond"),
9386+
QT_ORDINARY););
93869387
if (!tab->first_inner)
9387-
{
9388-
COND *new_cond= referred_tab->join == join ?
9388+
{
9389+
COND *new_cond= (referred_tab && referred_tab->join == join) ?
93899390
referred_tab->select_cond :
93909391
join->outer_ref_cond;
93919392
add_cond_and_fix(join->thd, &new_cond, notnull);
9392-
if (referred_tab->join == join)
9393+
if (referred_tab && referred_tab->join == join)
93939394
referred_tab->set_select_cond(new_cond, __LINE__);
93949395
else
93959396
join->outer_ref_cond= new_cond;

0 commit comments

Comments
 (0)