Skip to content

Commit 0c8d6fd

Browse files
committed
MDEV-15364 FOREIGN CASCADE operations in system versioned referenced tables
Merge pull request #667
2 parents 5a9e7bc + 7477b97 commit 0c8d6fd

File tree

14 files changed

+447
-170
lines changed

14 files changed

+447
-170
lines changed

mysql-test/suite/versioning/common.inc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,26 @@ if ($MTR_COMBINATION_TRX_ID)
7777
let $sys_datatype_expl_uc= BIGINT(20) UNSIGNED;
7878
let $sys_datatype_max= 18446744073709551615;
7979
}
80+
81+
eval create or replace function current_row(sys_trx_end $sys_datatype_expl)
82+
returns int
83+
deterministic
84+
return sys_trx_end = $sys_datatype_max;
85+
86+
delimiter ~~;
87+
eval create or replace function check_row(row_start $sys_datatype_expl, row_end $sys_datatype_expl)
88+
returns varchar(255)
89+
deterministic
90+
begin
91+
if row_end < row_start then
92+
return "ERROR: row_end < row_start";
93+
elseif row_end = row_start then
94+
return "ERROR: row_end == row_start";
95+
elseif current_row(row_end) then
96+
return "CURRENT ROW";
97+
end if;
98+
return "HISTORICAL ROW";
99+
end~~
100+
delimiter ;~~
101+
80102
--enable_query_log

mysql-test/suite/versioning/common_finish.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ drop procedure verify_vtq_dummy;
44
drop function sys_commit_ts;
55
drop procedure concat_exec2;
66
drop procedure concat_exec3;
7+
drop function current_row;
8+
drop function check_row;
79
--enable_query_log

mysql-test/suite/versioning/r/foreign.result

Lines changed: 133 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ id int unique key
66
) engine innodb;
77
create table child(
88
parent_id int,
9-
sys_start timestamp(6) as row start invisible,
10-
sys_end timestamp(6) as row end invisible,
9+
sys_start SYS_DATATYPE as row start invisible,
10+
sys_end SYS_DATATYPE as row end invisible,
1111
period for system_time(sys_start, sys_end),
1212
foreign key(parent_id) references parent(id)
1313
on delete restrict
@@ -25,7 +25,7 @@ update parent set id=id+1;
2525
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`))
2626
delete from child;
2727
update parent set id=id+1;
28-
select * from child for system_time from timestamp 0 to timestamp now(6);
28+
select * from child for system_time all;
2929
parent_id
3030
1
3131
1
@@ -39,8 +39,8 @@ id int(10) unsigned unique key
3939
) engine innodb;
4040
create table child(
4141
parent_id int(10) unsigned primary key,
42-
sys_start timestamp(6) as row start invisible,
43-
sys_end timestamp(6) as row end invisible,
42+
sys_start SYS_DATATYPE as row start invisible,
43+
sys_end SYS_DATATYPE as row end invisible,
4444
period for system_time(sys_start, sys_end),
4545
foreign key(parent_id) references parent(id)
4646
) engine innodb with system versioning;
@@ -58,19 +58,38 @@ id int unique key
5858
) engine innodb;
5959
create table child(
6060
parent_id int,
61-
sys_start timestamp(6) as row start invisible,
62-
sys_end timestamp(6) as row end invisible,
61+
sys_start SYS_DATATYPE as row start invisible,
62+
sys_end SYS_DATATYPE as row end invisible,
6363
period for system_time(sys_start, sys_end),
6464
foreign key(parent_id) references parent(id)
6565
on delete cascade
6666
on update cascade
6767
) engine innodb with system versioning;
68-
ERROR HY000: CASCADE is not supported for TIMESTAMP(6) AS ROW START/END system-versioned tables
68+
insert into parent values(1);
69+
insert into child values(1);
70+
delete from parent where id = 1;
71+
select * from child;
72+
parent_id
73+
select * from child for system_time all;
74+
parent_id
75+
1
76+
insert into parent values(1);
77+
insert into child values(1);
78+
update parent set id = id + 1;
79+
select * from child;
80+
parent_id
81+
2
82+
select * from child for system_time all;
83+
parent_id
84+
1
85+
1
86+
2
87+
drop table child;
6988
drop table parent;
7089
create or replace table parent (
7190
id int primary key,
72-
sys_start timestamp(6) as row start invisible,
73-
sys_end timestamp(6) as row end invisible,
91+
sys_start SYS_DATATYPE as row start invisible,
92+
sys_end SYS_DATATYPE as row end invisible,
7493
period for system_time(sys_start, sys_end)
7594
) with system versioning
7695
engine innodb;
@@ -97,41 +116,76 @@ engine innodb;
97116
create or replace table child (
98117
id int primary key,
99118
parent_id int not null,
100-
row_start timestamp(6) as row start invisible,
101-
row_end timestamp(6) as row end invisible,
119+
row_start SYS_DATATYPE as row start invisible,
120+
row_end SYS_DATATYPE as row end invisible,
102121
period for system_time(row_start, row_end),
103122
constraint `parent-fk`
104123
foreign key (parent_id) references parent (id)
105124
on delete cascade
106125
on update restrict
107126
) with system versioning
108127
engine innodb;
109-
ERROR HY000: CASCADE is not supported for TIMESTAMP(6) AS ROW START/END system-versioned tables
128+
insert into parent (id) values (3);
129+
insert into child (id, parent_id) values (3, 3);
130+
delete from parent;
131+
select * from child;
132+
id parent_id
133+
select *, check_row(row_start, row_end) from child for system_time all;
134+
id parent_id check_row(row_start, row_end)
135+
3 3 HISTORICAL ROW
136+
drop table child;
110137
drop table parent;
111138
#################
112139
# Test SET NULL #
113140
#################
114-
create table parent(
141+
create or replace table parent(
115142
id int unique key
116143
) engine innodb;
117-
create table child(
144+
create or replace table child(
118145
parent_id int,
119-
sys_start timestamp(6) as row start invisible,
120-
sys_end timestamp(6) as row end invisible,
146+
sys_start SYS_DATATYPE as row start invisible,
147+
sys_end SYS_DATATYPE as row end invisible,
121148
period for system_time(sys_start, sys_end),
122149
foreign key(parent_id) references parent(id)
123150
on delete set null
124151
on update set null
125152
) engine innodb with system versioning;
126-
ERROR HY000: SET NULL is not supported for TIMESTAMP(6) AS ROW START/END system-versioned tables
153+
insert into parent values(1);
154+
insert into child values(1);
155+
delete from child;
156+
insert into child values(1);
157+
delete from parent where id = 1;
158+
select * from child;
159+
parent_id
160+
NULL
161+
select *, current_row(sys_end) as current_row from child for system_time all order by sys_end;
162+
parent_id current_row
163+
1 0
164+
1 0
165+
NULL 1
166+
delete from child;
167+
insert into parent values(1);
168+
insert into child values(1);
169+
update parent set id= id + 1;
170+
select * from child;
171+
parent_id
172+
NULL
173+
select *, current_row(sys_end) as current_row from child for system_time all order by sys_end;
174+
parent_id current_row
175+
1 0
176+
1 0
177+
NULL 0
178+
1 0
179+
NULL 1
180+
drop table child;
127181
drop table parent;
128182
###########################
129183
# Parent table is foreign #
130184
###########################
131185
create or replace table parent(
132186
id int unique key,
133-
sys_start timestamp(6) as row start invisible,
134-
sys_end timestamp(6) as row end invisible,
187+
sys_start SYS_DATATYPE as row start invisible,
188+
sys_end SYS_DATATYPE as row end invisible,
135189
period for system_time(sys_start, sys_end)
136190
) engine innodb with system versioning;
137191
create or replace table child(
@@ -162,16 +216,16 @@ drop table parent;
162216
create or replace table a (
163217
cola int(10) primary key,
164218
v_cola int(10) as (cola mod 10) virtual,
165-
sys_start timestamp(6) as row start invisible,
166-
sys_end timestamp(6) as row end invisible,
219+
sys_start SYS_DATATYPE as row start invisible,
220+
sys_end SYS_DATATYPE as row end invisible,
167221
period for system_time(sys_start, sys_end)
168222
) engine=innodb with system versioning;
169223
create index v_cola on a (v_cola);
170224
create or replace table b(
171225
cola int(10),
172226
v_cola int(10),
173-
sys_start timestamp(6) as row start invisible,
174-
sys_end timestamp(6) as row end invisible,
227+
sys_start SYS_DATATYPE as row start invisible,
228+
sys_end SYS_DATATYPE as row end invisible,
175229
period for system_time(sys_start, sys_end)
176230
) engine=innodb with system versioning;
177231
alter table b add constraint `v_cola_fk`
@@ -181,3 +235,58 @@ insert into b(cola, v_cola) values (10,2);
181235
delete from a;
182236
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`b`, CONSTRAINT `v_cola_fk` FOREIGN KEY (`v_cola`) REFERENCES `a` (`v_cola`))
183237
drop table b, a;
238+
###############################################
239+
# CASCADE UPDATE foreign not system versioned #
240+
###############################################
241+
create or replace table parent (
242+
id smallint unsigned not null auto_increment,
243+
value int unsigned not null,
244+
primary key (id, value)
245+
) engine = innodb;
246+
create or replace table child (
247+
id mediumint unsigned not null auto_increment primary key,
248+
parent_id smallint unsigned not null,
249+
parent_value int unsigned not null,
250+
sys_start SYS_DATATYPE as row start invisible,
251+
sys_end SYS_DATATYPE as row end invisible,
252+
period for system_time(sys_start, sys_end),
253+
constraint `fk_child_parent`
254+
foreign key (parent_id, parent_value) references parent (id, value)
255+
on delete cascade
256+
on update cascade
257+
) engine = innodb with system versioning;
258+
create or replace table subchild (
259+
id int not null auto_increment primary key,
260+
parent_id smallint unsigned not null,
261+
parent_value int unsigned not null,
262+
constraint `fk_subchild_child_parent`
263+
foreign key (parent_id, parent_value) references child (parent_id, parent_value)
264+
on delete cascade
265+
on update cascade
266+
) engine=innodb;
267+
insert into parent (value) values (23);
268+
select id, value from parent into @id, @value;
269+
insert into child values (default, @id, @value);
270+
insert into subchild values (default, @id, @value);
271+
select parent_id from subchild;
272+
parent_id
273+
1
274+
update parent set id = 11, value = value + 1;
275+
select parent_id from subchild;
276+
parent_id
277+
11
278+
select * from child;
279+
id parent_id parent_value
280+
1 11 24
281+
delete from parent;
282+
select count(*) from child;
283+
count(*)
284+
0
285+
select * from child for system_time all;
286+
id parent_id parent_value
287+
1 1 23
288+
1 11 24
289+
select count(*) from subchild;
290+
count(*)
291+
0
292+
drop table subchild, child, parent;

0 commit comments

Comments
 (0)