summaryrefslogtreecommitdiffstats
path: root/mysql-test/suite/period/t/delete.test
blob: 676fa256f95ed5513583e9a3cb2a2dc766d6bf97 (plain)
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
source suite/period/engines.inc;
source include/have_log_bin.inc;
source include/have_partition.inc;

create table t (id int, s date, e date, period for apptime(s,e));

insert into t values(1, '1999-01-01', '2018-12-12');
insert into t values(1, '1999-01-01', '2017-01-01');
insert into t values(1, '2017-01-01', '2019-01-01');
insert into t values(2, '1998-01-01', '2018-12-12');
insert into t values(3, '1997-01-01', '2015-01-01');
insert into t values(4, '2016-01-01', '2020-01-01');
insert into t values(5, '2010-01-01', '2015-01-01');

create or replace table t1 (id int, s date, e date, period for apptime(s,e));
insert t1 select * from t;
create or replace table t2 (id int, s date, e date, period for apptime(s,e));
insert t2 select * from t;
create or replace table t3 (id int, s date, e date, period for apptime(s,e));
insert t3 select * from t;

--let $trig_cols=id, s, e
--let $trig_table=t1
--source suite/period/create_triggers.inc

delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
delete from t1 for portion of APPTIME from '2000-01-01' to '2018-01-01';
--sorted_result
select * from t;
--sorted_result
select * from t1;
select * from log_tbl order by id;

--echo # INSERT trigger only also works
--let $trig_cols=id, s, e
--let $trig_table=t2
--source suite/period/create_triggers.inc
drop trigger tr1del_t2;
drop trigger tr2del_t2;
delete from t2 for portion of APPTIME from '2000-01-01' to '2018-01-01';
select * from log_tbl order by id;

--echo # removing BEFORE INSERT trigger enables internal substitution
--echo # DELETE+INSERT -> UPDATE, but without any side effects.
--echo # The optimization is disabled for non-transactional engines
--let $trig_table=t3
--source suite/period/create_triggers.inc
drop trigger tr1ins_t3;
delete from t3 for portion of APPTIME from '2000-01-01' to '2018-01-01';
select * from log_tbl order by id;

--echo # multi-table DELETE is not possible
--error ER_PARSE_ERROR
delete t, t1 from t1, t for portion of apptime from '2000-01-01' to '2018-01-01';

--error ER_PARSE_ERROR
delete t for portion of apptime from '2000-01-01' to '2018-01-01', t1 from t, t1;

--echo # Here another check fails before parsing ends
--error ER_UNKNOWN_TABLE
delete t, t1 from t for portion of apptime from '2000-01-01' to '2018-01-01', t1;

--error ER_PARSE_ERROR
delete history from t2 for portion of apptime from '2000-01-01' to '2018-01-01';

--error ER_PERIOD_NOT_FOUND
delete from t for portion of othertime from '2000-01-01' to '2018-01-01';
--error ER_PARSE_ERROR
delete from t for portion of system_time from '2000-01-01' to '2018-01-01';

--echo # MDEV-22596 DELETE FOR PORTION does not obey
--echo # "Expression in FOR PORTION OF must be constant" limitation, data can be easily lost
--error ER_NOT_CONSTANT_EXPRESSION
delete from t for portion of apptime from s to e;

create or replace table t (id int, str text, s date, e date,
    period for apptime(s,e));

insert into t values(1, 'data', '1999-01-01', '2018-12-12');
insert into t values(1, 'other data', '1999-01-01', '2018-12-12');
insert into t values(1, 'deleted', '2000-01-01', '2018-01-01');
delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
show warnings;
--sorted_result
select * from t;

drop table t1;

--echo # SQL16, Part 2, 15.7 <Effect of deleting rows from base tables>,
--echo #        General rules, 8)b)i)
--echo # If the column descriptor that corresponds to the i-th field of BR
--echo # describes an identity column, a generated column, a system-time period
--echo # start column, or a system-time period end column, then let V i be
--echo # DEFAULT.

--echo # auto_increment field is updated
create or replace table t (id int primary key auto_increment, s date, e date,
                           period for apptime(s, e));
insert into t values (default, '1999-01-01', '2018-12-12');
select * from t;
delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
--sorted_result
select * from t;
truncate t;
--echo # same for trigger case
insert into t values (default, '1999-01-01', '2018-12-12');
--let $trig_table=t
--source suite/period/create_triggers.inc

delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
--sorted_result
select * from t;
select * from log_tbl order by id;

--echo # generated columns are updated
create or replace table t (s date, e date,
                           xs date as (s) stored, xe date as (e) stored,
                           period for apptime(s, e));
insert into t values('1999-01-01', '2018-12-12', default, default);
--sorted_result
select * from t;
delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
--sorted_result
select * from t;
truncate t;
--echo # same for trigger case
insert into t values('1999-01-01', '2018-12-12', default, default);
--let $trig_table=t
--source suite/period/create_triggers.inc

delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
--sorted_result
select * from t;
select * from log_tbl order by id;

--echo # View can't be used
create or replace view v as select * from t;
--error ER_IT_IS_A_VIEW
delete from v for portion of p from '2000-01-01' to '2018-01-01';

--echo # View can't be used
create or replace view v as select t.* from t, t as t1;
--error ER_VIEW_DELETE_MERGE_VIEW
delete from v for portion of p from '2000-01-01' to '2018-01-01';

--echo # auto_increment field overflow
create or replace table t (id tinyint auto_increment primary key,
                           s date, e date, period for apptime(s,e));

insert into t values(127, '1999-01-01', '2018-12-12');

--error HA_ERR_AUTOINC_ERANGE
delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
select * from t;

--echo # same for trigger case
--let $trig_table=t
--source suite/period/create_triggers.inc
--echo # negotiate side effects of non-transactional MyISAM engine
replace into t values(127, '1999-01-01', '2018-12-12');
select * from t;
truncate table log_tbl;

--error HA_ERR_AUTOINC_ERANGE
delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
select * from t;
select * from log_tbl order by id;

--echo # custom constraint for period fields
create or replace table t(id int, s date, e date, period for apptime(s,e),
                          constraint dist2days check (datediff(e, s) >= 2));
insert into t values(1, '1999-01-01', '2018-12-12'),
                    (2, '1999-01-01', '1999-12-12');
--error ER_CONSTRAINT_FAILED
delete from t for portion of apptime from '1999-01-02' to '2018-12-12';
--echo # negotiate side effects of non-transactional MyISAM engine
truncate t;
insert into t values(1, '1999-01-01', '2018-12-12'),
                    (2, '1999-01-01', '1999-12-12');
--error ER_CONSTRAINT_FAILED
delete from t for portion of apptime from '1999-01-01' to '2018-12-11';
truncate t;
insert into t values(1, '1999-01-01', '2018-12-12'),
                    (2, '1999-01-01', '1999-12-12');

delete from t for portion of apptime from '1999-01-03' to '2018-12-10';
--sorted_result
select *, datediff(e, s) from t;

--echo #
--echo # MDEV-18929 2nd execution of SP does not detect ER_VERS_NOT_VERSIONED
--echo #
create or replace table t1 (id int, s date, e date, period for apptime(s,e));
create or replace procedure sp()
delete from t1 for portion of othertime from '2000-01-01' to '2018-01-01';
--error ER_PERIOD_NOT_FOUND
call sp;
--error ER_PERIOD_NOT_FOUND
call sp;
drop table t1;
drop procedure sp;

drop table t,t2,t3,log_tbl;
drop view v;
drop procedure log;

--echo # MDEV-19130 Assertion
--echo # `next_insert_id >= auto_inc_interval_for_cur_row.minimum()'
--echo # failed in handler::update_auto_increment after error 167

create or replace table t (f tinyint auto_increment null,
                           s timestamp, e timestamp,
                           period for app(s,e), key(f, s));
insert into t (s,e) values
  ('2021-08-22 10:28:43', '2023-09-17 00:00:00'),
  ('2019-05-09 21:45:24', '2020-04-22 14:38:49');
insert into t (s,e) select s,e from t;
insert into t (s,e) select s,e from t;
insert into t (s,e) select s,e from t;
insert into t (s,e) values ('2015-07-07 00:00:00','2020-03-11 08:48:52');
insert into t (s,e) select s,e from t;
insert into t (s,e) select s,e from t;

--replace_regex /row \d+/row ROW/
--error HA_ERR_AUTOINC_ERANGE
insert into t select * from t;

--disable_warnings
delete ignore from t
       for portion of app from '2015-07-07 00:00:00' to '2020-03-11 08:48:52';
--enable_warnings

drop table t;

#
# MDEV-22424 Server crashes in handler::check_duplicate_long_entry_key or Assertion `inited == NONE || lookup_handler != this' failed upon DELETE FOR PORTION on table with long unique key
#
create table t1 (pk int, s date, e date,  period for se (s,e), primary key (pk,se without overlaps));
insert into t1 values (1,'2020-01-01','2020-02-20');
delete from t1 for portion of se from '2020-01-30' to '2020-01-31';
drop table t1;

--echo # End of 10.5 tests
--echo #
--echo # MDEV-19190 Assertion `part_share->auto_inc_initialized` failed in
--echo # ha_partition::get_auto_increment
--echo #
create table t1 (id int, s date, e date, period for app(s,e))
                partition by key(id);
insert into t1 (s,e) values ('2023-07-21','2024-06-07');
alter table t1 modify id int auto_increment key;
delete from t1 for portion of app from '2023-07-20' to '2024-05-23';
select * from t1;
drop table t1;

create table t1 (id int, s date, e date, period for app(s,e))
                partition by key(id);
insert into t1 (s,e) values ('2023-07-21','2024-06-07');
alter table t1 modify id int auto_increment key;
--let $trig_table=t1
--let $trig_cols=id, s, e
--disable_query_log
--source suite/period/create_triggers.inc
--enable_query_log

delete from t1 for portion of app from '2023-07-20' to '2024-05-23';
select * from t1;
drop table t1;
drop table log_tbl;
drop procedure log;