File: delete.test

package info (click to toggle)
mariadb 1%3A11.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 772,520 kB
  • sloc: ansic: 2,414,714; cpp: 1,791,394; asm: 381,336; perl: 62,905; sh: 49,647; pascal: 40,897; java: 39,363; python: 20,791; yacc: 20,432; sql: 17,907; xml: 12,344; ruby: 8,544; cs: 6,542; makefile: 6,145; ada: 1,879; lex: 1,193; javascript: 996; objc: 80; tcl: 73; awk: 46; php: 22
file content (830 lines) | stat: -rw-r--r-- 21,055 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
#
# Check for problems with delete
#

call mtr.add_suppression("Sort aborted.*");

--disable_warnings
drop table if exists t1,t2,t3,t11,t12;
--enable_warnings
CREATE TABLE t1 (a tinyint(3), b tinyint(5));
INSERT INTO t1 VALUES (1,1);
INSERT LOW_PRIORITY INTO t1 VALUES (1,2);
INSERT INTO t1 VALUES (1,3);
DELETE from t1 where a=1 limit 1;
DELETE LOW_PRIORITY from t1 where a=1;

INSERT INTO t1 VALUES (1,1);
DELETE from t1;
LOCK TABLE t1 write;
INSERT INTO t1 VALUES (1,2);
DELETE from t1;
UNLOCK TABLES;
INSERT INTO t1 VALUES (1,2);
SET AUTOCOMMIT=0;
DELETE from t1;
SET AUTOCOMMIT=1;
drop table t1;

#
# Test of delete when the delete will cause a node to disappear and reappear
# (This assumes a block size of 1024)
#

create table t1 (
	a bigint not null,
	b bigint not null default 0,
	c bigint not null default 0,
	d bigint not null default 0,
	e bigint not null default 0,
	f bigint not null default 0,
	g bigint not null default 0,
	h bigint not null default 0,
	i bigint not null default 0,
	j bigint not null default 0,
	primary key (a,b,c,d,e,f,g,h,i,j));
insert into t1 (a) values (2),(4),(6),(8),(10),(12),(14),(16),(18),(20),(22),(24),(26),(23);
delete from t1 where a=26;
drop table t1;
create table t1 (
	a bigint not null,
	b bigint not null default 0,
	c bigint not null default 0,
	d bigint not null default 0,
	e bigint not null default 0,
	f bigint not null default 0,
	g bigint not null default 0,
	h bigint not null default 0,
	i bigint not null default 0,
	j bigint not null default 0,
	primary key (a,b,c,d,e,f,g,h,i,j));
insert into t1 (a) values (2),(4),(6),(8),(10),(12),(14),(16),(18),(20),(22),(24),(26),(23),(27);
delete from t1 where a=27;
drop table t1;

CREATE TABLE `t1` (
  `i` int(10) NOT NULL default '0',
  `i2` int(10) NOT NULL default '0',
  PRIMARY KEY  (`i`)
);
-- error 1054
DELETE FROM t1 USING t1 WHERE post='1';
drop table t1;

#
# CHAR(0) bug - not actually DELETE bug, but anyway...
#

CREATE TABLE t1 (
  bool     char(0) default NULL,
  not_null varchar(20) binary NOT NULL default '',
  misc     integer not null,
  PRIMARY KEY  (not_null)
) ENGINE=MyISAM;

INSERT INTO t1 VALUES (NULL,'a',4), (NULL,'b',5), (NULL,'c',6), (NULL,'d',7);

select * from t1 where misc > 5 and bool is null;
delete   from t1 where misc > 5 and bool is null;
select * from t1 where misc > 5 and bool is null;

select count(*) from t1;
delete from t1 where 1 > 2;
select count(*) from t1;
delete from t1 where 3 > 2;
select count(*) from t1;

drop table t1;
#
# Bug #5733: Table handler error with self-join multi-table DELETE
#

create table t1 (a int not null auto_increment primary key, b char(32));
insert into t1 (b) values ('apple'), ('apple');
select * from t1;
delete t1 from t1, t1 as t2 where t1.b = t2.b and t1.a > t2.a;
select * from t1;
drop table t1;

--echo #
--echo # IGNORE option
--echo #
create table t11 (a int NOT NULL, b int, primary key (a));
create table t12 (a int NOT NULL, b int, primary key (a));
create table t2 (a int NOT NULL, b int, primary key (a));
insert into t11 values (0, 10),(1, 11),(2, 12);
insert into t12 values (33, 10),(0, 11),(2, 12);
insert into t2 values (1, 21),(2, 12),(3, 23);
select * from t11;
select * from t12;
select * from t2;
-- error 1242
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b <> (select b from t2 where t11.a < t2.a);
select * from t11;
select * from t12;
explain delete ignore t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b <> (select b from t2 where t11.a < t2.a);
delete ignore t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b <> (select b from t2 where t11.a < t2.a);
select * from t11;
select * from t12;
insert into t11 values (2, 12);
-- error 1242
delete from t11 where t11.b <> (select b from t2 where t11.a < t2.a);
select * from t11;
delete ignore from t11 where t11.b <> (select b from t2 where t11.a < t2.a);
select * from t11;
drop table t11, t12, t2;

#
# Bug #4198: deletion and KEYREAD
#

create table t1 (a int, b int, unique key (a), key (b));
insert into t1 values (3, 3), (7, 7);
delete t1 from t1 where a = 3;
check table t1;
select * from t1;
drop table t1;

#
# Bug #8392: delete with ORDER BY containing a direct reference to the table 
#
 
CREATE TABLE t1 ( a int PRIMARY KEY );
DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a;
INSERT INTO t1 VALUES (0),(1),(2);
DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a LIMIT 1;
SELECT * FROM t1;
DROP TABLE t1;

#
# Bug #21392: multi-table delete with alias table name fails with 
# 1003: Incorrect table name
#

create table t1 (a int);
delete `4.t1` from t1 as `4.t1` where `4.t1`.a = 5;
delete FROM `4.t1` USING t1 as `4.t1` where `4.t1`.a = 5;
drop table t1;

#
# Bug#17711: DELETE doesn't use index when ORDER BY, LIMIT and
#            non-restricting WHERE is present.
#
create table t1(f1 int primary key);
insert into t1 values (4),(3),(1),(2);
delete from t1 where (@a:= f1) order by f1 limit 1;
select @a;
drop table t1;

# BUG#30385 "Server crash when deleting with order by and limit"
CREATE TABLE t1 (
  `date` date ,
  `time` time ,
  `seq` int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (`seq`),
  KEY `seq` (`seq`),
  KEY `time` (`time`),
  KEY `date` (`date`)
);
DELETE FROM t1 ORDER BY date ASC, time ASC LIMIT 1;
drop table t1;

--echo End of 4.1 tests

#
# Test of multi-delete where we are not scanning the first table
#

CREATE TABLE t1 (a int not null,b int not null);
CREATE TABLE t2 (a int not null, b int not null, primary key (a,b));
CREATE TABLE t3 (a int not null, b int not null, primary key (a,b));
insert into t1 values (1,1),(2,1),(1,3);
insert into t2 values (1,1),(2,2),(3,3);
insert into t3 values (1,1),(2,1),(1,3);
select * from t1,t2,t3 where t1.a=t2.a AND t2.b=t3.a and t1.b=t3.b;
explain select * from t1,t2,t3 where t1.a=t2.a AND t2.b=t3.a and t1.b=t3.b;
delete t2.*,t3.* from t1,t2,t3 where t1.a=t2.a AND t2.b=t3.a and t1.b=t3.b;
# This should be empty
select * from t3;
drop table t1,t2,t3;

#
# Bug #8143: deleting '0000-00-00' values using IS NULL
#

create table t1(a date not null);
insert into t1 values (0);
select * from t1 where a is null;
delete from t1 where a is null;
select count(*) from t1;
drop table t1;

#
# Bug #26186: delete order by, sometimes accept unknown column
#
CREATE TABLE t1 (a INT); INSERT INTO t1 VALUES (1);

--error ER_BAD_FIELD_ERROR
DELETE FROM t1 ORDER BY x;

# even columns from a table not used in query (and not even existing)
--error ER_BAD_FIELD_ERROR
DELETE FROM t1 ORDER BY t2.x;

# subquery (as long as the subquery from is valid or DUAL)
--error ER_BAD_FIELD_ERROR
DELETE FROM t1 ORDER BY (SELECT x);

DROP TABLE t1;

#
# Bug #30234: Unexpected behavior using DELETE with AS and USING
# '
CREATE TABLE t1 (
  a INT
);

CREATE TABLE t2 (
  a INT
);

CREATE DATABASE db1;
CREATE TABLE db1.t1 (
  a INT
);
INSERT INTO db1.t1 (a) SELECT * FROM t1;

CREATE DATABASE db2;
CREATE TABLE db2.t1 (
  a INT
);
INSERT INTO db2.t1 (a) SELECT * FROM t2;

--error ER_PARSE_ERROR
DELETE FROM t1 alias USING t1, t2 alias WHERE t1.a = alias.a;
DELETE FROM alias USING t1, t2 alias WHERE t1.a = alias.a;
DELETE FROM t1, alias USING t1, t2 alias WHERE t1.a = alias.a;
--error ER_UNKNOWN_TABLE
DELETE FROM t1, t2 USING t1, t2 alias WHERE t1.a = alias.a;
--error ER_PARSE_ERROR
DELETE FROM db1.t1 alias USING db1.t1, db2.t1 alias WHERE db1.t1.a = alias.a;
DELETE FROM alias USING db1.t1, db2.t1 alias WHERE db1.t1.a = alias.a;
--error ER_UNKNOWN_TABLE
DELETE FROM db2.alias USING db1.t1, db2.t1 alias WHERE db1.t1.a = alias.a;
DELETE FROM t1 USING t1 WHERE a = 1;
SELECT * FROM t1;
--error ER_PARSE_ERROR
DELETE FROM t1 alias USING t1 alias WHERE a = 2;
SELECT * FROM t1;

DROP TABLE t1, t2;
DROP DATABASE db1;
DROP DATABASE db2;

#
# Bug 31742: delete from ... order by function call that causes an error, 
#            asserts server
#

CREATE FUNCTION f1() RETURNS INT RETURN 1;
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (0);
--error 1318
DELETE FROM t1 ORDER BY (f1(10)) LIMIT 1;
DROP TABLE t1;
DROP FUNCTION f1;


--echo #
--echo # Bug #49552 : sql_buffer_result cause crash + not found records 
--echo #   in multitable delete/subquery
--echo #

CREATE TABLE t1(a INT);
INSERT INTO t1 VALUES (1),(2),(3);
SET SESSION SQL_BUFFER_RESULT=1;
DELETE t1 FROM (SELECT SUM(a) a FROM t1) x,t1;

SET SESSION SQL_BUFFER_RESULT=DEFAULT;
SELECT * FROM t1;
DROP TABLE t1;

--echo End of 5.0 tests

#
# Bug#27525: table not found when using multi-table-deletes with aliases over
#            several databas
# Bug#21148: MULTI-DELETE fails to resolve a table by alias if it's from a
#            different database
#

--disable_warnings
DROP DATABASE IF EXISTS db1;
DROP DATABASE IF EXISTS db2;
DROP DATABASE IF EXISTS db3;
DROP DATABASE IF EXISTS db4;
DROP TABLE IF EXISTS t1, t2;
DROP PROCEDURE IF EXISTS count;
--enable_warnings
USE test;
CREATE DATABASE db1;
CREATE DATABASE db2;

CREATE TABLE db1.t1 (a INT, b INT);
INSERT INTO db1.t1 VALUES (1,1),(2,2),(3,3);
CREATE TABLE db1.t2 AS SELECT * FROM db1.t1;
CREATE TABLE db2.t1 AS SELECT * FROM db1.t2;
CREATE TABLE db2.t2 AS SELECT * FROM db2.t1;
CREATE TABLE t1 AS SELECT * FROM db2.t2;
CREATE TABLE t2 AS SELECT * FROM t1;

delimiter |;
CREATE PROCEDURE count_rows()
BEGIN
  SELECT COUNT(*) AS "COUNT(db1.t1)" FROM db1.t1;
  SELECT COUNT(*) AS "COUNT(db1.t2)" FROM db1.t2;
  SELECT COUNT(*) AS "COUNT(db2.t1)" FROM db2.t1;
  SELECT COUNT(*) AS "COUNT(db2.t2)" FROM db2.t2;
  SELECT COUNT(*) AS "COUNT(test.t1)" FROM test.t1;
  SELECT COUNT(*) AS "COUNT(test.t2)" FROM test.t2;
END|
delimiter ;|

#
# Testing without a selected database
#

CREATE DATABASE db3;
USE db3;
DROP DATABASE db3;
--error ER_NO_DB_ERROR
SELECT * FROM t1;

# Detect missing table references

--error ER_NO_DB_ERROR
DELETE a1,a2 FROM db1.t1, db2.t2;
--error ER_NO_DB_ERROR
DELETE a1,a2 FROM db1.t1, db2.t2;
--error ER_NO_DB_ERROR
DELETE a1,a2 FROM db1.t1 AS a1, db2.t2;
--error ER_NO_DB_ERROR
DELETE a1,a2 FROM db1.t1, db2.t2 AS a2;
--error ER_NO_DB_ERROR
DELETE a1,a2 FROM db3.t1 AS a1, db4.t2 AS a2;
--error ER_NO_DB_ERROR
DELETE a1,a2 FROM db3.t1 AS a1, db4.t2 AS a2;

--error ER_NO_DB_ERROR
DELETE FROM a1,a2 USING db1.t1, db2.t2;
--error ER_NO_DB_ERROR
DELETE FROM a1,a2 USING db1.t1, db2.t2;
--error ER_NO_DB_ERROR
DELETE FROM a1,a2 USING db1.t1 AS a1, db2.t2;
--error ER_NO_DB_ERROR
DELETE FROM a1,a2 USING db1.t1, db2.t2 AS a2;
--error ER_NO_DB_ERROR
DELETE FROM a1,a2 USING db3.t1 AS a1, db4.t2 AS a2;
--error ER_NO_DB_ERROR
DELETE FROM a1,a2 USING db3.t1 AS a1, db4.t2 AS a2;

# Ambiguous table references

--error ER_NO_DB_ERROR
DELETE a1 FROM db1.t1 AS a1, db2.t2 AS a1;
--error ER_NO_DB_ERROR
DELETE a1 FROM db1.a1, db2.t2 AS a1;
--error ER_NO_DB_ERROR
DELETE a1 FROM a1, db1.t1 AS a1;
--error ER_NO_DB_ERROR
DELETE t1 FROM db1.t1, db2.t1 AS a1;
--error ER_NO_DB_ERROR
DELETE t1 FROM db1.t1 AS a1, db2.t1 AS a2;
--error ER_NO_DB_ERROR
DELETE t1 FROM db1.t1, db2.t1;

# Test all again, now with a selected database

USE test;

# Detect missing table references

--error ER_UNKNOWN_TABLE
DELETE a1,a2 FROM db1.t1, db2.t2;
--error ER_UNKNOWN_TABLE
DELETE a1,a2 FROM db1.t1, db2.t2;
--error ER_UNKNOWN_TABLE
DELETE a1,a2 FROM db1.t1 AS a1, db2.t2;
--error ER_UNKNOWN_TABLE
DELETE a1,a2 FROM db1.t1, db2.t2 AS a2;
--error ER_NO_SUCH_TABLE
DELETE a1,a2 FROM db3.t1 AS a1, db4.t2 AS a2;
--error ER_NO_SUCH_TABLE
DELETE a1,a2 FROM db3.t1 AS a1, db4.t2 AS a2;

--error ER_UNKNOWN_TABLE
DELETE FROM a1,a2 USING db1.t1, db2.t2;
--error ER_UNKNOWN_TABLE
DELETE FROM a1,a2 USING db1.t1, db2.t2;
--error ER_UNKNOWN_TABLE
DELETE FROM a1,a2 USING db1.t1 AS a1, db2.t2;
--error ER_UNKNOWN_TABLE
DELETE FROM a1,a2 USING db1.t1, db2.t2 AS a2;
--error ER_NO_SUCH_TABLE
DELETE FROM a1,a2 USING db3.t1 AS a1, db4.t2 AS a2;
--error ER_NO_SUCH_TABLE
DELETE FROM a1,a2 USING db3.t1 AS a1, db4.t2 AS a2;

# Ambiguous table references

--error ER_NONUNIQ_TABLE
DELETE a1 FROM db1.t1 AS a1, db2.t2 AS a1;
--error ER_NO_SUCH_TABLE
DELETE a1 FROM db1.a1, db2.t2 AS a1;
--error ER_NONUNIQ_TABLE
DELETE a1 FROM a1, db1.t1 AS a1;
--error ER_UNKNOWN_TABLE
DELETE t1 FROM db1.t1, db2.t1 AS a1;
--error ER_UNKNOWN_TABLE
DELETE t1 FROM db1.t1 AS a1, db2.t1 AS a2;
--error ER_UNKNOWN_TABLE
DELETE t1 FROM db1.t1, db2.t1;

# Test multiple-table cross database deletes

--disable_ps2_protocol
DELETE t1 FROM db1.t2 AS t1, db2.t2 AS t2 WHERE t2.a = 1 AND t1.a = t2.a;
SELECT ROW_COUNT();
CALL count_rows();
DELETE a1, a2 FROM db2.t1 AS a1, t2 AS a2 WHERE a1.a = 2 AND a2.a = 2;
SELECT ROW_COUNT();
CALL count_rows();

DROP DATABASE db1;
DROP DATABASE db2;
DROP PROCEDURE count_rows;
DROP TABLE t1, t2;
--enable_ps2_protocol

--echo #
--echo # Bug#46958: Assertion in Diagnostics_area::set_ok_status, trigger, 
--echo # merge table
--echo #
CREATE TABLE t1 ( a INT );
CREATE TABLE t2 ( a INT );
CREATE TABLE t3 ( a INT );

INSERT INTO t1 VALUES (1), (2);
INSERT INTO t2 VALUES (1), (2);
INSERT INTO t3 VALUES (1), (2);

CREATE TRIGGER tr1 BEFORE DELETE ON t2
FOR EACH ROW INSERT INTO no_such_table VALUES (1);

--error ER_NO_SUCH_TABLE
DELETE t1, t2, t3 FROM t1, t2, t3;

SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;

DROP TABLE t1, t2, t3;

CREATE TABLE t1 ( a INT );
CREATE TABLE t2 ( a INT );
CREATE TABLE t3 ( a INT );

INSERT INTO t1 VALUES (1), (2);
INSERT INTO t2 VALUES (1), (2);
INSERT INTO t3 VALUES (1), (2);

CREATE TRIGGER tr1 AFTER DELETE ON t2
FOR EACH ROW INSERT INTO no_such_table VALUES (1);

--error ER_NO_SUCH_TABLE
DELETE t1, t2, t3 FROM t1, t2, t3;

SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;

DROP TABLE t1, t2, t3;

--echo #
--echo # Bug #46425 crash in Diagnostics_area::set_ok_status, 
--echo #            empty statement, DELETE IGNORE
--echo #

CREATE table t1 (i INTEGER);

INSERT INTO t1 VALUES (1);

--delimiter |

CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW 
BEGIN 
  INSERT INTO t1 SELECT * FROM t1 AS A;
END |

--delimiter ;
--error ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG
DELETE IGNORE FROM t1;

DROP TABLE t1;

--echo #
--echo # Bug #53450: Crash/assertion
--echo #    "virtual int ha_myisam::index_first(uchar*)") at assert.c:81
--echo #

CREATE TABLE t1 (a INT,    b INT,    c INT,
                 INDEX(a), INDEX(b), INDEX(c));
INSERT INTO t1 VALUES (1,2,3), (4,5,6), (7,8,9);

DELETE FROM t1 WHERE a = 10 OR b = 20 ORDER BY c LIMIT 1;

DROP TABLE t1;

--echo #
--echo # Bug #53034: Multiple-table DELETE statements not accepting
--echo #             "Access compatibility" syntax
--echo #

CREATE TABLE t1 (id INT);
CREATE TABLE t2 LIKE t1;
CREATE TABLE t3 LIKE t1;

DELETE FROM t1.*, test.t2.*, a.* USING t1, t2, t3 AS a;

DROP TABLE t1, t2, t3;

--echo End of 5.1 tests


--echo #
--echo # Bug#51099 Assertion in mysql_multi_delete_prepare()
--echo #

--disable_warnings
DROP TABLE IF EXISTS t1, t2;
DROP VIEW IF EXISTS v1, v2;
--enable_warnings

CREATE TABLE t1(a INT);
CREATE TABLE t2(b INT);
CREATE VIEW v1 AS SELECT a, b FROM t1, t2;
CREATE VIEW v2 AS SELECT a FROM v1;

# This is a normal delete
--error ER_VIEW_DELETE_MERGE_VIEW
DELETE FROM v2;
# This is a multi table delete, check that we get the same error
# This caused the assertion.
--error ER_VIEW_DELETE_MERGE_VIEW
DELETE v2 FROM v2;

DROP VIEW v2, v1;
DROP TABLE t1, t2;

--echo End of 5.5 tests

--echo #
--echo # MDEV-30586: DELETE with WHERE containing nested subquery
--echo #             with set function aggregated in outer subquery
--echo #

create table t1 (a int);
insert into t1 values (3), (7), (1);

create table t2 (b int);
insert into t2 values (2), (1), (4), (7);

create table t3 (a int, b int);
insert into t3 values (2,10), (7,30), (2,30), (1,10), (7,40);

let $c=
        t1.a in (select t3.a from t3 group by t3.a
                   having t3.a > any (select t2.b from t2
                                        where t2.b*10 < sum(t3.b)));

eval
select * from t1
  where $c;

eval
delete from t1
  where $c;

eval
select * from t1
  where $c;

eval
update t1 set t1.a=t1.a+10
  where $c;

drop table t1,t2,t3;

--echo End of 10.4 tests

--echo #
--echo # MDEV-26459: Assertion `block_size <= 0xFFFFFFFFL' failed
--echo # in calculate_block_sizes for 10.7 only
--echo #

SET @sort_buffer_size_save= @@sort_buffer_size;
--disable_warnings
SET sort_buffer_size=1125899906842624;
--enable_warnings
CREATE TABLE t1 (a INT,b CHAR,KEY(a,b));
DELETE a1 FROM t1 AS a1,t1 AS a2 WHERE a1.a=a2.a;
DROP TABLE t1;
SET sort_buffer_size= @sort_buffer_size_save;

--echo # End of 10.11 tests

--echo #
--echo # MDEV-29428: DELETE with ORDER BY without LIMIT clause
--echo #

create table t1 (c1 integer, c2 integer, c3 integer);

insert into t1 values
  (1,1,1), (1,2,2), (1,3,3), (2,1,4), (2,2,5), (2,3,6), (2,4,7), (2,5,8);

create temporary table t select * from t1;

let $q1=
delete from t1 order by c2;
eval explain $q1;
eval $q1;
select *from t1;

delete from t1;
insert into t1 select * from t;

let $q2=
delete from t1
  where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
eval explain $q2;
eval $q2;
select *from t1;

delete from t1;
insert into t1 select * from t;

let $q3=
delete from t1
  where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3
    order by c2;
eval explain $q3;
eval $q3;
select *from t1;

drop table t1;

--echo End of 11.1 tests

--echo #
--echo # MDEV-33988 DELETE to support table aliases
--echo #
# Test from MySQL Bug#27455809; DELETE FROM ...WHERE NOT EXISTS WITH TABLE ALIAS CREATES AN ERROR 1064 (42000

CREATE TABLE t1 (c1 INT);
INSERT INTO t1 VALUES (1), (2);
SELECT * FROM t1;

DELETE FROM t1 AS a1 WHERE a1.c1 = 2;
SELECT * FROM t1;

CREATE TABLE t2 (c2 INT);
INSERT INTO t2 VALUES (1), (2);
SELECT * FROM t2;

DELETE FROM t2 a2 WHERE NOT EXISTS (SELECT * FROM t1 WHERE t1.c1 = a2.c2);
SELECT * FROM t2;

DROP TABLE t1, t2;

--echo End of 11.6 tests

--echo #
--echo # MDEV-25008: Delete query gets stuck on mariadb, same query works
--echo #             on MySQL 8.0.21
--echo #

--source include/have_sequence.inc
--source include/have_innodb.inc

# original case
CREATE TABLE t1 (
  id int NOT NULL PRIMARY KEY,
  item_id varchar(100),
  seller_name varchar(400),
  variant varchar(400),
  FULLTEXT KEY t1_serial_IDX (item_id,seller_name,variant)
)engine=innodb;

insert into t1 select seq,seq,seq,seq from seq_1_to_10000;

# Masking the `rows` column as the value might vary a bit
--replace_column 9 #
explain
DELETE FROM t1 WHERE id NOT IN
  (SELECT m FROM (SELECT max(id) m FROM t1 GROUP BY item_id, seller_name, variant) AS innertable);

drop table t1;

# example of not full table
create table t1 (a int primary key, b int, c int, key(b));
insert into t1 select seq, seq, seq from seq_1_to_20000;
create table t2 as select * from t1;
explain delete from t1  where    b <= 2 and a not in (select b from t2);
explain delete from t1  where    b <= 3 and a not in (select b from t2);
drop table t1, t2;

--echo # End of 11.7 tests

--echo #
--echo # MDEV-30469: Add support of ORDER BY and LIMIT to multidelete query.
--echo #

--echo # Check that limits work with hints

create table t2 (id int, index xid(id));
insert into t2 values (1),(10),(2),(9),(3),(8);

DELETE t2.* FROM t2 use index(xid) ORDER BY (id) LIMIT 2;
select * from t2 ORDER BY (id);
DELETE t2.* FROM t2 use index(xid) ORDER BY (id) DESC LIMIT 3;
select * from t2;
--echo # Check some useles syntax
DELETE t2.* FROM t2 FORCE INDEX FOR GROUP BY (xid) ORDER BY (id) LIMIT 1;

drop table t2;


--echo # Check that hints work with limit

create table t2 (id int primary key, index xid(id));
insert into t2 values (1),(10),(2),(9),(3),(8);

--echo # default primary index
explain
DELETE t2.* FROM t2 ORDER BY (id) LIMIT 2;
--echo # make it use other undex
explain
DELETE t2.* FROM t2 use index(xid) ORDER BY (id) LIMIT 2;
explain
DELETE t2.* FROM t2 force index(xid) ORDER BY (id) LIMIT 2;
--echo # prohibit primary index
explain
DELETE t2.* FROM t2 ignore index(primary) ORDER BY (id) LIMIT 2;

drop table t2;

--echo # Check that limits work with hints & PS protocol

create table t2 (id int, index xid(id));
insert into t2 values (1),(10),(2),(9),(3),(8);

prepare stmt from
"DELETE t2.* FROM t2 use index(xid) ORDER BY (id) LIMIT ?";
set @lim= 2;
execute stmt using @lim;
select * from t2 ORDER BY (id);
set @lim= 1;
execute stmt using @lim;
select * from t2 ORDER BY (id);
set @lim= 3;
execute stmt using @lim;
select * from t2 ORDER BY (id);

drop table t2;

--echo # Check that hints work with limit in normal DELETE syntax

create table t2 (id int primary key, index xid(id));
insert into t2 values (1),(10),(2),(9),(3),(8);

--echo # default primary index
explain
DELETE FROM t2 ORDER BY (id) LIMIT 2;
--echo # make it use other undex
explain
DELETE FROM t2 use index(xid) ORDER BY (id) LIMIT 2;
explain
DELETE FROM t2 force index(xid) ORDER BY (id) LIMIT 2;
--echo # prohibit primary index
explain
DELETE FROM t2 ignore index(primary) ORDER BY (id) LIMIT 2;

--echo # should issue warnings becaouse we can not switch it internally
--echo # to multiupdate due to RETURNING
DELETE FROM t2 ignore index(primary) ORDER BY (id) LIMIT 2 RETURNING id;

drop table t2;

--echo #
--echo # End of 11.4 test
--echo #