Skip to content

Commit 75d354a

Browse files
committed
MDEV-33988 DELETE single table to support table aliases
Gain MySQL compatibility by allowing table aliases in a single table statement. This now supports the syntax of: DELETE [delete_opts] FROM tbl_name [[AS] tbl_alias] [PARTITION (partition_name [, partition_name] ...)] .... The delete.test is from MySQL commit 1a72b69778a9791be44525501960b08856833b8d / Change-Id: Iac3a2b5ed993f65b7f91acdfd60013c2344db5c0. Co-Author: Gleb Shchepa <gleb.shchepa@oracle.com> (for delete.test) Reviewed by Igor Babaev (igor@mariadb.com)
1 parent 0cd20e3 commit 75d354a

File tree

8 files changed

+241
-12
lines changed

8 files changed

+241
-12
lines changed

mysql-test/main/delete.result

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,21 +259,21 @@ a INT
259259
);
260260
INSERT INTO db2.t1 (a) SELECT * FROM t2;
261261
DELETE FROM t1 alias USING t1, t2 alias WHERE t1.a = alias.a;
262-
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'alias USING t1, t2 alias WHERE t1.a = alias.a' at line 1
262+
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'USING t1, t2 alias WHERE t1.a = alias.a' at line 1
263263
DELETE FROM alias USING t1, t2 alias WHERE t1.a = alias.a;
264264
DELETE FROM t1, alias USING t1, t2 alias WHERE t1.a = alias.a;
265265
DELETE FROM t1, t2 USING t1, t2 alias WHERE t1.a = alias.a;
266266
ERROR 42S02: Unknown table 't2' in MULTI DELETE
267267
DELETE FROM db1.t1 alias USING db1.t1, db2.t1 alias WHERE db1.t1.a = alias.a;
268-
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'alias USING db1.t1, db2.t1 alias WHERE db1.t1.a = alias.a' at line 1
268+
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'USING db1.t1, db2.t1 alias WHERE db1.t1.a = alias.a' at line 1
269269
DELETE FROM alias USING db1.t1, db2.t1 alias WHERE db1.t1.a = alias.a;
270270
DELETE FROM db2.alias USING db1.t1, db2.t1 alias WHERE db1.t1.a = alias.a;
271271
ERROR 42S02: Unknown table 'alias' in MULTI DELETE
272272
DELETE FROM t1 USING t1 WHERE a = 1;
273273
SELECT * FROM t1;
274274
a
275275
DELETE FROM t1 alias USING t1 alias WHERE a = 2;
276-
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'alias USING t1 alias WHERE a = 2' at line 1
276+
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'USING t1 alias WHERE a = 2' at line 1
277277
SELECT * FROM t1;
278278
a
279279
DROP TABLE t1, t2;
@@ -611,3 +611,28 @@ c1 c2 c3
611611
2 2 5
612612
drop table t1;
613613
End of 11.1 tests
614+
#
615+
# MDEV-33988 DELETE to support table aliases
616+
#
617+
CREATE TABLE t1 (c1 INT);
618+
INSERT INTO t1 VALUES (1), (2);
619+
SELECT * FROM t1;
620+
c1
621+
1
622+
2
623+
DELETE FROM t1 AS a1 WHERE a1.c1 = 2;
624+
SELECT * FROM t1;
625+
c1
626+
1
627+
CREATE TABLE t2 (c2 INT);
628+
INSERT INTO t2 VALUES (1), (2);
629+
SELECT * FROM t2;
630+
c2
631+
1
632+
2
633+
DELETE FROM t2 a2 WHERE NOT EXISTS (SELECT * FROM t1 WHERE t1.c1 = a2.c2);
634+
SELECT * FROM t2;
635+
c2
636+
1
637+
DROP TABLE t1, t2;
638+
End of 11.6 tests

mysql-test/main/delete.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,3 +668,26 @@ select *from t1;
668668
drop table t1;
669669

670670
--echo End of 11.1 tests
671+
672+
--echo #
673+
--echo # MDEV-33988 DELETE to support table aliases
674+
--echo #
675+
# Test from MySQL Bug#27455809; DELETE FROM ...WHERE NOT EXISTS WITH TABLE ALIAS CREATES AN ERROR 1064 (42000
676+
677+
CREATE TABLE t1 (c1 INT);
678+
INSERT INTO t1 VALUES (1), (2);
679+
SELECT * FROM t1;
680+
681+
DELETE FROM t1 AS a1 WHERE a1.c1 = 2;
682+
SELECT * FROM t1;
683+
684+
CREATE TABLE t2 (c2 INT);
685+
INSERT INTO t2 VALUES (1), (2);
686+
SELECT * FROM t2;
687+
688+
DELETE FROM t2 a2 WHERE NOT EXISTS (SELECT * FROM t1 WHERE t1.c1 = a2.c2);
689+
SELECT * FROM t2;
690+
691+
DROP TABLE t1, t2;
692+
693+
--echo End of 11.6 tests

mysql-test/main/delete_use_source.result

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,74 @@ c1 c2 c3
334334
2 1 4
335335
deallocate prepare stmt;
336336
drop table t1,t2;
337+
# End of 11.1
338+
#
339+
# MDEV-33988 DELETE (single table) to support table aliases
340+
#
341+
create table t1 (c1 int, c2 int, c3 int);
342+
insert into t1 values
343+
(1,1,1), (1,2,2), (1,3,3), (2,1,4), (2,2,5), (2,3,6), (2,5,7), (3,5,8);
344+
create table t2 (id int auto_increment primary key, a int, key(a));
345+
insert into t2(a) values (3), (5), (-1);
346+
# 1. The alias in delete coincides with the table name in IN subquery.
347+
explain extended
348+
delete from t1 t2 where t2.c1 in (select a from t2);
349+
id select_type table type possible_keys key key_len ref rows filtered Extra
350+
1 PRIMARY t2 ALL NULL NULL NULL NULL 8 100.00 Using where
351+
1 PRIMARY t2 ref a a 5 test.t2.c1 1 100.00 Using index; FirstMatch(t2)
352+
Warnings:
353+
Note 1003 delete from `test`.`t1` `t2` using (`test`.`t2`) where `test`.`t2`.`a` = `test`.`t2`.`c1`
354+
delete from t1 t2 where t2.c1 in (select a from t2);
355+
select * from t1;
356+
c1 c2 c3
357+
1 1 1
358+
1 2 2
359+
1 3 3
360+
2 1 4
361+
2 2 5
362+
2 3 6
363+
2 5 7
364+
# 2. The alias in delete is different from the alias in IN subquery
365+
explain extended
366+
delete from t1 t_x where t_x.c2 IN (select a from t2 as t_y);
367+
id select_type table type possible_keys key key_len ref rows filtered Extra
368+
1 PRIMARY t_x ALL NULL NULL NULL NULL 7 100.00 Using where
369+
1 PRIMARY t_y ref a a 5 test.t_x.c2 1 100.00 Using index; FirstMatch(t_x)
370+
Warnings:
371+
Note 1003 delete from `test`.`t1` `t_x` using (`test`.`t2` `t_y`) where `test`.`t_y`.`a` = `test`.`t_x`.`c2`
372+
delete from t1 t_x where t_x.c2 IN (select a from t2 as t_y);
373+
select * from t1;
374+
c1 c2 c3
375+
1 1 1
376+
1 2 2
377+
2 1 4
378+
2 2 5
379+
# 3. The alias in delete is the same as the alias in IN subquery.
380+
explain extended
381+
delete from t1 as t_x where t_x.c3 IN (select a from t2 t_x);
382+
id select_type table type possible_keys key key_len ref rows filtered Extra
383+
1 PRIMARY t_x ALL NULL NULL NULL NULL 4 100.00 Using where
384+
1 PRIMARY t_x ref a a 5 test.t_x.c3 1 100.00 Using index; FirstMatch(t_x)
385+
Warnings:
386+
Note 1003 delete from `test`.`t1` `t_x` using (`test`.`t2` `t_x`) where `test`.`t_x`.`a` = `test`.`t_x`.`c3`
387+
delete from t1 as t_x where t_x.c3 IN (select a from t2 t_x);
388+
select * from t1;
389+
c1 c2 c3
390+
1 1 1
391+
1 2 2
392+
2 1 4
393+
# 4. The table in delete is the alias in IN subquery
394+
explain extended
395+
delete from t1 t2 where t2.c1 IN (select a -1 from t2 as t1);
396+
id select_type table type possible_keys key key_len ref rows filtered Extra
397+
1 PRIMARY t2 ALL NULL NULL NULL NULL 3 100.00
398+
1 PRIMARY t1 index NULL a 5 NULL 3 33.33 Using where; Using index; FirstMatch(t2)
399+
Warnings:
400+
Note 1003 delete from `test`.`t1` `t2` using (`test`.`t2` `t1`) where `test`.`t2`.`c1` = `test`.`t1`.`a` - 1
401+
delete from t1 t2 where t2.c1 IN (select a -1 from t2 as t1);
402+
select * from t1;
403+
c1 c2 c3
404+
1 1 1
405+
1 2 2
406+
drop table t1, t2;
407+
# End of 11.6

mysql-test/main/delete_use_source.test

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,50 @@ select * from t2;
257257
deallocate prepare stmt;
258258

259259
drop table t1,t2;
260+
261+
--echo # End of 11.1
262+
263+
--echo #
264+
--echo # MDEV-33988 DELETE (single table) to support table aliases
265+
--echo #
266+
267+
create table t1 (c1 int, c2 int, c3 int);
268+
insert into t1 values
269+
(1,1,1), (1,2,2), (1,3,3), (2,1,4), (2,2,5), (2,3,6), (2,5,7), (3,5,8);
270+
create table t2 (id int auto_increment primary key, a int, key(a));
271+
insert into t2(a) values (3), (5), (-1);
272+
273+
--echo # 1. The alias in delete coincides with the table name in IN subquery.
274+
275+
explain extended
276+
delete from t1 t2 where t2.c1 in (select a from t2);
277+
delete from t1 t2 where t2.c1 in (select a from t2);
278+
279+
select * from t1;
280+
281+
--echo # 2. The alias in delete is different from the alias in IN subquery
282+
283+
explain extended
284+
delete from t1 t_x where t_x.c2 IN (select a from t2 as t_y);
285+
delete from t1 t_x where t_x.c2 IN (select a from t2 as t_y);
286+
287+
select * from t1;
288+
289+
--echo # 3. The alias in delete is the same as the alias in IN subquery.
290+
291+
explain extended
292+
delete from t1 as t_x where t_x.c3 IN (select a from t2 t_x);
293+
delete from t1 as t_x where t_x.c3 IN (select a from t2 t_x);
294+
295+
select * from t1;
296+
297+
--echo # 4. The table in delete is the alias in IN subquery
298+
299+
explain extended
300+
delete from t1 t2 where t2.c1 IN (select a -1 from t2 as t1);
301+
delete from t1 t2 where t2.c1 IN (select a -1 from t2 as t1);
302+
select * from t1;
303+
304+
drop table t1, t2;
305+
306+
--echo # End of 11.6

mysql-test/main/partition_explicit_prune.result

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,3 +2006,41 @@ drop table t1;
20062006
#
20072007
# End of 10.4 tests
20082008
#
2009+
#
2010+
# MDEV-33988 DELETE (single table) to support table aliases
2011+
#
2012+
CREATE TABLE `t1` (
2013+
`id` int(11) default NULL
2014+
)
2015+
PARTITION BY RANGE (id) (
2016+
PARTITION p0 VALUES LESS THAN (6),
2017+
PARTITION p1 VALUES LESS THAN (11),
2018+
PARTITION p2 VALUES LESS THAN (16),
2019+
PARTITION p3 VALUES LESS THAN (21));
2020+
INSERT INTO `t1` VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10),
2021+
(11), (12), (13), (14), (15), (16), (17), (18), (19), (20);
2022+
DELETE FROM t1 AS a PARTITION(p0) WHERE a.id % 2 = 0;
2023+
SELECT * FROM t1 ORDER BY id;
2024+
id
2025+
1
2026+
3
2027+
5
2028+
6
2029+
7
2030+
8
2031+
9
2032+
10
2033+
11
2034+
12
2035+
13
2036+
14
2037+
15
2038+
16
2039+
17
2040+
18
2041+
19
2042+
20
2043+
DROP TABLE t1;
2044+
#
2045+
# End of 11.6 tests
2046+
#

mysql-test/main/partition_explicit_prune.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,3 +940,28 @@ drop table t1;
940940
--echo # End of 10.4 tests
941941
--echo #
942942

943+
--echo #
944+
--echo # MDEV-33988 DELETE (single table) to support table aliases
945+
--echo #
946+
947+
CREATE TABLE `t1` (
948+
`id` int(11) default NULL
949+
)
950+
PARTITION BY RANGE (id) (
951+
PARTITION p0 VALUES LESS THAN (6),
952+
PARTITION p1 VALUES LESS THAN (11),
953+
PARTITION p2 VALUES LESS THAN (16),
954+
PARTITION p3 VALUES LESS THAN (21));
955+
956+
INSERT INTO `t1` VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10),
957+
(11), (12), (13), (14), (15), (16), (17), (18), (19), (20);
958+
959+
DELETE FROM t1 AS a PARTITION(p0) WHERE a.id % 2 = 0;
960+
961+
SELECT * FROM t1 ORDER BY id;
962+
963+
DROP TABLE t1;
964+
965+
--echo #
966+
--echo # End of 11.6 tests
967+
--echo #

mysql-test/main/trigger-compat.result

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ FLUSH TABLE t2;
6969
# has broken triggers. The parse error refers to the first broken
7070
# trigger.
7171
CREATE TRIGGER tr16 AFTER UPDATE ON t1 FOR EACH ROW INSERT INTO t1 VALUES (1);
72-
ERROR 42000: Trigger 'tr13' has an error in its body: 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a USING t1 a' at line 1'
72+
ERROR 42000: Trigger 'tr13' has an error in its body: 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'USING t1 a' at line 1'
7373
CREATE TRIGGER tr22 BEFORE INSERT ON t2 FOR EACH ROW DELETE FROM non_existing_table;
7474
ERROR 42000: Unknown trigger has an error in its body: 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Not allowed syntax here, and trigger name cant be extracted either.' at line 1'
7575
SHOW TRIGGERS;
@@ -80,20 +80,20 @@ tr15 UPDATE t1 CREATE DEFINER=`root`@`localhost` TRIGGER tr15 BEFORE UPDATE ON t
8080
tr13 DELETE t1 CREATE DEFINER=`root`@`localhost` TRIGGER tr13 BEFORE DELETE ON t1 FOR EACH ROW DELETE FROM t1 a USING t1 a BEFORE # latin1 latin1_swedish_ci utf8mb4_uca1400_ai_ci
8181
tr14 DELETE t1 DELETE FROM non_existing_table AFTER # root@localhost latin1 latin1_swedish_ci utf8mb4_uca1400_ai_ci
8282
INSERT INTO t1 VALUES (1);
83-
ERROR 42000: Trigger 'tr13' has an error in its body: 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a USING t1 a' at line 1'
83+
ERROR 42000: Trigger 'tr13' has an error in its body: 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'USING t1 a' at line 1'
8484
INSERT INTO t2 VALUES (1);
8585
ERROR 42000: Unknown trigger has an error in its body: 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Not allowed syntax here, and trigger name cant be extracted either.' at line 1'
8686
DELETE FROM t1;
87-
ERROR 42000: Trigger 'tr13' has an error in its body: 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a USING t1 a' at line 1'
87+
ERROR 42000: Trigger 'tr13' has an error in its body: 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'USING t1 a' at line 1'
8888
UPDATE t1 SET a = 1 WHERE a = 1;
89-
ERROR 42000: Trigger 'tr13' has an error in its body: 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a USING t1 a' at line 1'
89+
ERROR 42000: Trigger 'tr13' has an error in its body: 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'USING t1 a' at line 1'
9090
SELECT * FROM t1;
9191
a
9292
1
9393
2
9494
3
9595
RENAME TABLE t1 TO t1_2;
96-
ERROR 42000: Trigger 'tr13' has an error in its body: 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a USING t1 a' at line 1'
96+
ERROR 42000: Trigger 'tr13' has an error in its body: 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'USING t1 a' at line 1'
9797
SHOW TRIGGERS;
9898
Trigger Event Table Statement Timing Created sql_mode Definer character_set_client collation_connection Database Collation
9999
tr11 INSERT t1 DELETE FROM t3 BEFORE # root@localhost latin1 latin1_swedish_ci utf8mb4_uca1400_ai_ci

sql/sql_yacc.yy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13734,10 +13734,10 @@ delete_part2:
1373413734
;
1373513735

1373613736
delete_single_table:
13737-
FROM table_ident opt_use_partition
13737+
FROM table_ident opt_table_alias_clause opt_use_partition
1373813738
{
1373913739
if (unlikely(!Select->
13740-
add_table_to_list(thd, $2, NULL, TL_OPTION_UPDATING,
13740+
add_table_to_list(thd, $2, $3, TL_OPTION_UPDATING,
1374113741
YYPS->m_lock_type,
1374213742
YYPS->m_mdl_type,
1374313743
NULL,
@@ -13750,11 +13750,11 @@ delete_single_table:
1375013750
Lex->query_tables= 0;
1375113751
Lex->query_tables_last= &Lex->query_tables;
1375213752
if (unlikely(!Select->
13753-
add_table_to_list(thd, $2, NULL, TL_OPTION_UPDATING,
13753+
add_table_to_list(thd, $2, $3, TL_OPTION_UPDATING,
1375413754
YYPS->m_lock_type,
1375513755
YYPS->m_mdl_type,
1375613756
NULL,
13757-
$3)))
13757+
$4)))
1375813758
MYSQL_YYABORT;
1375913759
Lex->auxiliary_table_list.first->correspondent_table=
1376013760
Lex->query_tables;

0 commit comments

Comments
 (0)