Skip to content

Commit 44395ac

Browse files
author
Guilhem Bichot
committed
Bug#20407961 VIEW'S CHECK OPTION SOMETIMES NOT HONOURED IF INCLUDED IN A TOP VIEW
Problem: when there is a view v2 nested inside another view v1, the check option of v2 is not tested if v1 has LOCAL CHECK OPTION or no check option. The fix: rewrite prep_check_option(); starting from the top view, it recurses into underlying views as before, but with changed rules: if a view has CASCADED or its parent had it, the underlying views are said to have it too; if a view has nothing, its children CHECK OPTIONs are unaffected. effective_with_check is not needed anymore: - it's tested in INSERT, but we must always honour the inserted table's check option - same for LOAD DATA - same for non-SELECT statements using setup_conds (just need to limit ourselves to merged views, for efficiency; which limitation was previously implemented by setting effective_with_check in merge_derived()) Changes to result files show the new behaviour. In funcs_1: - the massive result changes towards the end, are just logs of the failed statements with their error codes. - Added printout of v3's CREATE for more clarity. - for myisam_views-big some other lines are changed, just like it had been done for innodb_views a few days ago.
1 parent 72fd90c commit 44395ac

File tree

14 files changed

+1585
-1247
lines changed

14 files changed

+1585
-1247
lines changed

mysql-test/r/view.result

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,13 +1196,13 @@ ERROR HY000: CHECK OPTION failed 'test.v2'
11961196
insert into v3 values (0);
11971197
ERROR HY000: CHECK OPTION failed 'test.v3'
11981198
insert into v2 values (2);
1199+
ERROR HY000: CHECK OPTION failed 'test.v2'
11991200
insert into v3 values (2);
12001201
ERROR HY000: CHECK OPTION failed 'test.v3'
12011202
select * from t1;
12021203
a
12031204
1
12041205
1
1205-
2
12061206
drop view v3,v2,v1;
12071207
drop table t1;
12081208
create table t1 (a int, primary key (a));
@@ -5941,3 +5941,149 @@ ON alias2.col_varchar_key = alias1.col_varchar_key;
59415941
pk
59425942
DROP VIEW v2;
59435943
DROP TABLE t1, t2, t3;
5944+
#
5945+
# Bug#20407961 VIEW'S CHECK OPTION SOMETIMES NOT HONOURED IF INCLUDED IN A TOP VIEW
5946+
#
5947+
create table t1 (a varchar(100));
5948+
# n/c/l letter suffix means: no/cascaded/local check option
5949+
create view v1n as select * from t1 where a like '%v1n%';
5950+
create view v2c as select * from t1 where a like '%v2c%'
5951+
with check option;
5952+
show create view v2c;
5953+
View Create View character_set_client collation_connection
5954+
v2c CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2c` AS select `t1`.`a` AS `a` from `t1` where (`t1`.`a` like '%v2c%') WITH CASCADED CHECK OPTION latin1 latin1_swedish_ci
5955+
create view v3l as select * from t1 where a like '%v3l%'
5956+
with local check option;
5957+
show create view v3l;
5958+
View Create View character_set_client collation_connection
5959+
v3l CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v3l` AS select `t1`.`a` AS `a` from `t1` where (`t1`.`a` like '%v3l%') WITH LOCAL CHECK OPTION latin1 latin1_swedish_ci
5960+
# The basic, single-view feature works:
5961+
insert into t1 values('');
5962+
insert into v1n values('');
5963+
insert into v2c values('');
5964+
ERROR HY000: CHECK OPTION failed 'test.v2c'
5965+
insert into v2c values('v2c');
5966+
insert into v3l values('');
5967+
ERROR HY000: CHECK OPTION failed 'test.v3l'
5968+
insert into v3l values('v3l');
5969+
# We also test UPDATE of the successfully inserted row
5970+
update v1n set a='_';
5971+
# if this update to v2c was allowed, it would be produce a row in t1
5972+
# not matching v2c's filter, which is against the goal of CHECK
5973+
# OPTION.
5974+
update v2c set a='';
5975+
ERROR HY000: CHECK OPTION failed 'test.v2c'
5976+
update v3l set a='';
5977+
ERROR HY000: CHECK OPTION failed 'test.v3l'
5978+
# Top view without no CHECK OPTION, still CHECK OPTION conditions
5979+
# of underlying views are checked.
5980+
create view v4n as select * from v2c where a like '%v4n%';
5981+
create view v5n as select * from v3l where a like '%v5n%';
5982+
# The view's nesting structure is:
5983+
# v4n -> v2c -> t1
5984+
# We present the query with needed tags, verify that it passes,
5985+
# then we take each tag away and verify that it fails.
5986+
insert into v4n values('v2c');
5987+
insert into v4n values('');
5988+
ERROR HY000: CHECK OPTION failed 'test.v4n'
5989+
# To check UPDATE on v4n, we need to update a row originally
5990+
# visible in v4n, so we create it:
5991+
insert into v4n values('v4n v2c');
5992+
# This row can legally disappear from v4n:
5993+
update v4n set a='v2c';
5994+
insert into v4n values('v4n v2c');
5995+
# But this row cannot disappear from v2c:
5996+
update v4n set a='v4n';
5997+
ERROR HY000: CHECK OPTION failed 'test.v4n'
5998+
# Lest we delete it:
5999+
delete from v4n;
6000+
# v5n -> v3l -> t1
6001+
insert into v5n values('v3l');
6002+
insert into v5n values('');
6003+
ERROR HY000: CHECK OPTION failed 'test.v5n'
6004+
# Top view with LOCAL CHECK OPTION, still CHECK OPTION conditions
6005+
# of underlying views are checked.
6006+
create view v4l as select * from v2c where a like '%v4l%'
6007+
with local check option;
6008+
create view v5l as select * from v3l where a like '%v5l%'
6009+
with local check option;
6010+
# v4l -> v2c -> t1
6011+
insert into v4l values('v4l v2c');
6012+
insert into v4l values('v2c');
6013+
ERROR HY000: CHECK OPTION failed 'test.v4l'
6014+
update v4l set a='v2c';
6015+
ERROR HY000: CHECK OPTION failed 'test.v4l'
6016+
insert into v4l values('v4l');
6017+
ERROR HY000: CHECK OPTION failed 'test.v4l'
6018+
update v4l set a='v4l';
6019+
ERROR HY000: CHECK OPTION failed 'test.v4l'
6020+
# v5l -> v3l -> t1
6021+
insert into v5l values('v5l v3l');
6022+
insert into v5l values('v3l');
6023+
ERROR HY000: CHECK OPTION failed 'test.v5l'
6024+
update v5l set a='v3l';
6025+
ERROR HY000: CHECK OPTION failed 'test.v5l'
6026+
insert into v5l values('v5l');
6027+
ERROR HY000: CHECK OPTION failed 'test.v5l'
6028+
update v5l set a='v5l';
6029+
ERROR HY000: CHECK OPTION failed 'test.v5l'
6030+
# CASCADED makes all filtering conditions a requirement
6031+
create view v6c as select * from v5n where a like '%v6c%'
6032+
with cascaded check option;
6033+
# v6c -> v5n -> v3l -> t1
6034+
insert into v6c values('v6c v5n v3l');
6035+
insert into v6c values('v5n v3l');
6036+
ERROR HY000: CHECK OPTION failed 'test.v6c'
6037+
update v6c set a='v5n v3l';
6038+
ERROR HY000: CHECK OPTION failed 'test.v6c'
6039+
insert into v6c values('v6c v3l');
6040+
ERROR HY000: CHECK OPTION failed 'test.v6c'
6041+
update v6c set a='v6c v3l';
6042+
ERROR HY000: CHECK OPTION failed 'test.v6c'
6043+
insert into v6c values('v6c v5n');
6044+
ERROR HY000: CHECK OPTION failed 'test.v6c'
6045+
update v6c set a='v6c v5n';
6046+
ERROR HY000: CHECK OPTION failed 'test.v6c'
6047+
# Also true if top view has no check option:
6048+
create view v7n as select * from v6c where a like '%v7n%';
6049+
# v7n -> v6c -> v5n -> v3l -> t1
6050+
insert into v7n values('v6c v5n v3l');
6051+
insert into v7n values('v5n v3l');
6052+
ERROR HY000: CHECK OPTION failed 'test.v7n'
6053+
insert into v7n values('v6c v3l');
6054+
ERROR HY000: CHECK OPTION failed 'test.v7n'
6055+
insert into v7n values('v6c v5n');
6056+
ERROR HY000: CHECK OPTION failed 'test.v7n'
6057+
# Make a visible row to update:
6058+
insert into v7n values('v7n v6c v5n v3l');
6059+
update v7n set a='v7n v5n v3l';
6060+
ERROR HY000: CHECK OPTION failed 'test.v7n'
6061+
update v7n set a='v7n v6c v3l';
6062+
ERROR HY000: CHECK OPTION failed 'test.v7n'
6063+
update v7n set a='v7n v6c v5nà';
6064+
ERROR HY000: CHECK OPTION failed 'test.v7n'
6065+
# Also true if top view has LOCAL:
6066+
create view v8l as select * from v7n where a like '%v8l%'
6067+
with local check option;
6068+
# v8l -> v7n -> v6c -> v5n -> v3l -> t1
6069+
insert into v8l values('v8l v6c v5n v3l');
6070+
insert into v8l values('v6c v5n v3l');
6071+
ERROR HY000: CHECK OPTION failed 'test.v8l'
6072+
insert into v8l values('v8l v5n v3l');
6073+
ERROR HY000: CHECK OPTION failed 'test.v8l'
6074+
insert into v8l values('v8l v6c v3l');
6075+
ERROR HY000: CHECK OPTION failed 'test.v8l'
6076+
insert into v8l values('v8l v6c v5n');
6077+
ERROR HY000: CHECK OPTION failed 'test.v8l'
6078+
# Make a visible row (=> satisfy v7n's WHERE clause):
6079+
insert into v8l values('v8l v7n v6c v5n v3l');
6080+
update v8l set a='v7n v6c v5n v3l';
6081+
ERROR HY000: CHECK OPTION failed 'test.v8l'
6082+
update v8l set a='v8l v7n v5n v3l';
6083+
ERROR HY000: CHECK OPTION failed 'test.v8l'
6084+
update v8l set a='v8l v7n v6c v3l';
6085+
ERROR HY000: CHECK OPTION failed 'test.v8l'
6086+
update v8l set a='v8l v7n v6c v5n';
6087+
ERROR HY000: CHECK OPTION failed 'test.v8l'
6088+
drop view v1n,v2c,v3l,v4n,v5n,v4l,v5l,v6c,v7n,v8l;
6089+
drop table t1;

0 commit comments

Comments
 (0)