Skip to content

Commit f90040f

Browse files
committed
MDEV-19429: Wrong query result with EXISTS and LIMIT 0
Check EXISTS LIMIT before rewriting.
1 parent cc86a0b commit f90040f

12 files changed

+228
-43
lines changed

mysql-test/r/derived_view.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
12881288
3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 100.00 Using where
12891289
Warnings:
12901290
Note 1276 Field or reference 't.a' of SELECT #3 was resolved in SELECT #1
1291-
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists(select 28 from `test`.`t3` where ('j' < `test`.`t1`.`a`)))
1291+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists(select 28 from `test`.`t3` where ('j' < `test`.`t1`.`a`) limit 1))
12921292
SELECT * FROM (SELECT * FROM t1) AS t
12931293
WHERE EXISTS (SELECT t2.a FROM t3 RIGHT JOIN t2 ON (t3.a = t2.a)
12941294
WHERE t2.b < t.a);

mysql-test/r/subselect.result

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
341341
2 DEPENDENT SUBQUERY t7 eq_ref PRIMARY PRIMARY 4 test.t6.clinic_uq 1 100.00 Using index
342342
Warnings:
343343
Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1
344-
Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <expr_cache><`test`.`t6`.`clinic_uq`>(exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`)))
344+
Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <expr_cache><`test`.`t6`.`clinic_uq`>(exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`) limit 1))
345345
select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
346346
ERROR 23000: Column 'a' in field list is ambiguous
347347
drop table t1,t2,t3;
@@ -1887,7 +1887,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
18871887
2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index
18881888
Warnings:
18891889
Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1
1890-
Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(<expr_cache><`test`.`tt`.`id`>(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null)))))
1890+
Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(<expr_cache><`test`.`tt`.`id`>(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null) limit 1))))
18911891
insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
18921892
create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
18931893
insert into t2 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text1'), (12, 'text2'), (13, 'text3'), (14, 'text4'), (15, 'text5'), (16, 'text6'), (17, 'text7'), (18, 'text8'), (19, 'text9'), (20, 'text10'),(21, 'text1'), (22, 'text2'), (23, 'text3'), (24, 'text4'), (25, 'text5'), (26, 'text6'), (27, 'text7'), (28, 'text8'), (29, 'text9'), (30, 'text10'), (31, 'text1'), (32, 'text2'), (33, 'text3'), (34, 'text4'), (35, 'text5'), (36, 'text6'), (37, 'text7'), (38, 'text8'), (39, 'text9'), (40, 'text10'), (41, 'text1'), (42, 'text2'), (43, 'text3'), (44, 'text4'), (45, 'text5'), (46, 'text6'), (47, 'text7'), (48, 'text8'), (49, 'text9'), (50, 'text10');
@@ -2423,7 +2423,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
24232423
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
24242424
Warnings:
24252425
Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1
2426-
Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <expr_cache><`test`.`up`.`a`>(exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`)))
2426+
Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <expr_cache><`test`.`up`.`a`>(exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`) limit 1))
24272427
drop table t1;
24282428
CREATE TABLE t1 (t1_a int);
24292429
INSERT INTO t1 VALUES (1);
@@ -4432,7 +4432,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
44324432
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
44334433
Warnings:
44344434
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
4435-
Note 1003 select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)))
4435+
Note 1003 select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`) limit 1))
44364436
EXPLAIN EXTENDED
44374437
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION
44384438
(SELECT 1 FROM t2 WHERE t1.a = t2.a));
@@ -4444,7 +4444,7 @@ NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
44444444
Warnings:
44454445
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
44464446
Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1
4447-
Note 1003 select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`))))
4447+
Note 1003 select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) limit 1))
44484448
DROP TABLE t1,t2;
44494449
create table t0(a int);
44504450
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
@@ -7187,4 +7187,35 @@ id select_type table type possible_keys key key_len ref rows Extra
71877187
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
71887188
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
71897189
drop table t1,t2;
7190+
#
7191+
# MDEV-19429: Wrong query result with EXISTS and LIMIT 0
7192+
#
7193+
create table t10 (a int);
7194+
insert into t10 values (1),(2),(3);
7195+
create table t12 (a int);
7196+
insert into t12 values (1),(2),(3);
7197+
select * from t10 where exists (select * from t12 order by a limit 0);
7198+
a
7199+
explain select * from t10 where exists (select * from t12 order by a limit 0);
7200+
id select_type table type possible_keys key key_len ref rows Extra
7201+
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
7202+
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Zero limit
7203+
prepare stmt1 from "select * from t10 where exists (select * from t12 order by a limit ?)";
7204+
set @l=1;
7205+
execute stmt1 using @l;
7206+
a
7207+
1
7208+
2
7209+
3
7210+
set @l=2;
7211+
execute stmt1 using @l;
7212+
a
7213+
1
7214+
2
7215+
3
7216+
set @l=0;
7217+
execute stmt1 using @l;
7218+
a
7219+
deallocate prepare stmt1;
7220+
drop table t10, t12;
71907221
End of 5.5 tests

mysql-test/r/subselect_mat.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2219,7 +2219,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
22192219
3 MATERIALIZED t3 ALL NULL NULL NULL NULL 8 100.00
22202220
Warnings:
22212221
Note 1276 Field or reference 'test.t1.f1' of SELECT #2 was resolved in SELECT #1
2222-
Note 1003 select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where <expr_cache><`test`.`t1`.`f1`>(exists(select 1 from `test`.`t2` semi join (`test`.`t3`) join `test`.`t3` where ((`test`.`t3`.`i3` = `test`.`t2`.`i2`) and (`test`.`t1`.`f1` = `test`.`t3`.`f3`))))
2222+
Note 1003 select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where <expr_cache><`test`.`t1`.`f1`>(exists(select 1 from `test`.`t2` semi join (`test`.`t3`) join `test`.`t3` where ((`test`.`t3`.`i3` = `test`.`t2`.`i2`) and (`test`.`t1`.`f1` = `test`.`t3`.`f3`)) limit 1))
22232223
# this checks the result set above
22242224
set @save_optimizer_switch= @@optimizer_switch;
22252225
set optimizer_switch= 'materialization=off,semijoin=off';

mysql-test/r/subselect_no_mat.result

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
348348
2 DEPENDENT SUBQUERY t7 eq_ref PRIMARY PRIMARY 4 test.t6.clinic_uq 1 100.00 Using index
349349
Warnings:
350350
Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1
351-
Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <expr_cache><`test`.`t6`.`clinic_uq`>(exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`)))
351+
Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <expr_cache><`test`.`t6`.`clinic_uq`>(exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`) limit 1))
352352
select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
353353
ERROR 23000: Column 'a' in field list is ambiguous
354354
drop table t1,t2,t3;
@@ -1894,7 +1894,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
18941894
2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index
18951895
Warnings:
18961896
Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1
1897-
Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(<expr_cache><`test`.`tt`.`id`>(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null)))))
1897+
Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(<expr_cache><`test`.`tt`.`id`>(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null) limit 1))))
18981898
insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
18991899
create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
19001900
insert into t2 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text1'), (12, 'text2'), (13, 'text3'), (14, 'text4'), (15, 'text5'), (16, 'text6'), (17, 'text7'), (18, 'text8'), (19, 'text9'), (20, 'text10'),(21, 'text1'), (22, 'text2'), (23, 'text3'), (24, 'text4'), (25, 'text5'), (26, 'text6'), (27, 'text7'), (28, 'text8'), (29, 'text9'), (30, 'text10'), (31, 'text1'), (32, 'text2'), (33, 'text3'), (34, 'text4'), (35, 'text5'), (36, 'text6'), (37, 'text7'), (38, 'text8'), (39, 'text9'), (40, 'text10'), (41, 'text1'), (42, 'text2'), (43, 'text3'), (44, 'text4'), (45, 'text5'), (46, 'text6'), (47, 'text7'), (48, 'text8'), (49, 'text9'), (50, 'text10');
@@ -2430,7 +2430,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
24302430
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
24312431
Warnings:
24322432
Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1
2433-
Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <expr_cache><`test`.`up`.`a`>(exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`)))
2433+
Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <expr_cache><`test`.`up`.`a`>(exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`) limit 1))
24342434
drop table t1;
24352435
CREATE TABLE t1 (t1_a int);
24362436
INSERT INTO t1 VALUES (1);
@@ -4436,7 +4436,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
44364436
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
44374437
Warnings:
44384438
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
4439-
Note 1003 select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)))
4439+
Note 1003 select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`) limit 1))
44404440
EXPLAIN EXTENDED
44414441
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION
44424442
(SELECT 1 FROM t2 WHERE t1.a = t2.a));
@@ -4448,7 +4448,7 @@ NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
44484448
Warnings:
44494449
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
44504450
Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1
4451-
Note 1003 select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`))))
4451+
Note 1003 select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) limit 1))
44524452
DROP TABLE t1,t2;
44534453
create table t0(a int);
44544454
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
@@ -7184,6 +7184,37 @@ id select_type table type possible_keys key key_len ref rows Extra
71847184
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
71857185
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
71867186
drop table t1,t2;
7187+
#
7188+
# MDEV-19429: Wrong query result with EXISTS and LIMIT 0
7189+
#
7190+
create table t10 (a int);
7191+
insert into t10 values (1),(2),(3);
7192+
create table t12 (a int);
7193+
insert into t12 values (1),(2),(3);
7194+
select * from t10 where exists (select * from t12 order by a limit 0);
7195+
a
7196+
explain select * from t10 where exists (select * from t12 order by a limit 0);
7197+
id select_type table type possible_keys key key_len ref rows Extra
7198+
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
7199+
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Zero limit
7200+
prepare stmt1 from "select * from t10 where exists (select * from t12 order by a limit ?)";
7201+
set @l=1;
7202+
execute stmt1 using @l;
7203+
a
7204+
1
7205+
2
7206+
3
7207+
set @l=2;
7208+
execute stmt1 using @l;
7209+
a
7210+
1
7211+
2
7212+
3
7213+
set @l=0;
7214+
execute stmt1 using @l;
7215+
a
7216+
deallocate prepare stmt1;
7217+
drop table t10, t12;
71877218
End of 5.5 tests
71887219
set optimizer_switch=default;
71897220
select @@optimizer_switch like '%materialization=on%';

mysql-test/r/subselect_no_opts.result

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
344344
2 DEPENDENT SUBQUERY t7 eq_ref PRIMARY PRIMARY 4 test.t6.clinic_uq 1 100.00 Using index
345345
Warnings:
346346
Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1
347-
Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`))
347+
Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`) limit 1)
348348
select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
349349
ERROR 23000: Column 'a' in field list is ambiguous
350350
drop table t1,t2,t3;
@@ -1890,7 +1890,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
18901890
2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index
18911891
Warnings:
18921892
Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1
1893-
Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null))))
1893+
Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null) limit 1)))
18941894
insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
18951895
create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
18961896
insert into t2 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text1'), (12, 'text2'), (13, 'text3'), (14, 'text4'), (15, 'text5'), (16, 'text6'), (17, 'text7'), (18, 'text8'), (19, 'text9'), (20, 'text10'),(21, 'text1'), (22, 'text2'), (23, 'text3'), (24, 'text4'), (25, 'text5'), (26, 'text6'), (27, 'text7'), (28, 'text8'), (29, 'text9'), (30, 'text10'), (31, 'text1'), (32, 'text2'), (33, 'text3'), (34, 'text4'), (35, 'text5'), (36, 'text6'), (37, 'text7'), (38, 'text8'), (39, 'text9'), (40, 'text10'), (41, 'text1'), (42, 'text2'), (43, 'text3'), (44, 'text4'), (45, 'text5'), (46, 'text6'), (47, 'text7'), (48, 'text8'), (49, 'text9'), (50, 'text10');
@@ -2426,7 +2426,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
24262426
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
24272427
Warnings:
24282428
Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1
2429-
Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`))
2429+
Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`) limit 1)
24302430
drop table t1;
24312431
CREATE TABLE t1 (t1_a int);
24322432
INSERT INTO t1 VALUES (1);
@@ -4432,7 +4432,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
44324432
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
44334433
Warnings:
44344434
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
4435-
Note 1003 select 2 AS `2` from `test`.`t1` where exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`))
4435+
Note 1003 select 2 AS `2` from `test`.`t1` where exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`) limit 1)
44364436
EXPLAIN EXTENDED
44374437
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION
44384438
(SELECT 1 FROM t2 WHERE t1.a = t2.a));
@@ -4444,7 +4444,7 @@ NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
44444444
Warnings:
44454445
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
44464446
Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1
4447-
Note 1003 select 2 AS `2` from `test`.`t1` where exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)))
4447+
Note 1003 select 2 AS `2` from `test`.`t1` where exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) limit 1)
44484448
DROP TABLE t1,t2;
44494449
create table t0(a int);
44504450
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
@@ -7182,5 +7182,36 @@ id select_type table type possible_keys key key_len ref rows Extra
71827182
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
71837183
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
71847184
drop table t1,t2;
7185+
#
7186+
# MDEV-19429: Wrong query result with EXISTS and LIMIT 0
7187+
#
7188+
create table t10 (a int);
7189+
insert into t10 values (1),(2),(3);
7190+
create table t12 (a int);
7191+
insert into t12 values (1),(2),(3);
7192+
select * from t10 where exists (select * from t12 order by a limit 0);
7193+
a
7194+
explain select * from t10 where exists (select * from t12 order by a limit 0);
7195+
id select_type table type possible_keys key key_len ref rows Extra
7196+
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
7197+
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Zero limit
7198+
prepare stmt1 from "select * from t10 where exists (select * from t12 order by a limit ?)";
7199+
set @l=1;
7200+
execute stmt1 using @l;
7201+
a
7202+
1
7203+
2
7204+
3
7205+
set @l=2;
7206+
execute stmt1 using @l;
7207+
a
7208+
1
7209+
2
7210+
3
7211+
set @l=0;
7212+
execute stmt1 using @l;
7213+
a
7214+
deallocate prepare stmt1;
7215+
drop table t10, t12;
71857216
End of 5.5 tests
71867217
set @optimizer_switch_for_subselect_test=null;

0 commit comments

Comments
 (0)