Debian Codegraph @

mysql-test/suite/gcol/inc/gcol_view.inc

raw
1 ################################################################################
2 # inc/gcol_view.inc #
3 # #
4 # Purpose: #
5 # Testing views defined on tables with generated columns. #
6 # #
7 # #
8 # #
9 #------------------------------------------------------------------------------#
10 # Original Author: Andrey Zhakov #
11 # Original Date: 2008-09-04 #
12 # Change Author: #
13 # Change Date: #
14 # Change: #
15 ################################################################################
16
17
18
19 create table t1 (a int not null,
20 b int generated always as (-a) virtual,
21 c int generated always as (-a) stored);
22 insert into t1 (a) values (1), (1), (2), (2), (3);
23 analyze table t1;
24
25 # simple view
26 create view v1 (d,e) as select abs(b), abs(c) from t1;
27 --sorted_result
28 select d,e from v1;
29 select is_updatable from information_schema.views where table_name='v1';
30
31 # view with different algorithms (explain output differs)
32 --replace_column 10 X
33 explain select d,e from v1;
34 create algorithm=temptable view v2 (d,e) as select abs(b), abs(c) from t1;
35 show create view v2;
36 --sorted_result
37 select d,e from v2;
38 --replace_column 10 X
39 explain select d,e from v2;
40
41 # VIEW on VIEW test
42 create view v3 (d,e) as select d*2, e*2 from v1;
43 --sorted_result
44 select * from v3;
45 --replace_column 10 X
46 explain select * from v3;
47
48 drop view v1,v2,v3;
49 drop table t1;
50
51 #
52 # DISTINCT option for VIEW
53 #
54 create table t1 (a int not null,
55 b int generated always as (-a) virtual,
56 c int generated always as (-a) stored);
57 insert into t1 (a) values (1), (2), (3), (1), (2), (3);
58 create view v1 as select distinct b from t1;
59 --sorted_result
60 select * from v1;
61 --replace_column 10 X
62 explain select * from v1;
63 --sorted_result
64 select * from t1;
65 drop view v1;
66 create view v1 as select distinct c from t1;
67 --sorted_result
68 select * from v1;
69 --replace_column 10 X
70 explain select * from v1;
71 --sorted_result
72 select * from t1;
73 drop view v1;
74 drop table t1;
75
76 #
77 # LIMIT clause test
78 #
79 create table t1 (a int not null,
80 b int generated always as (-a) virtual,
81 c int generated always as (-a) stored);
82 insert into t1 (a) values (1), (2), (3), (4);
83 create view v1 as select b+1 from t1 order by 1 desc limit 2;
84 select * from v1;
85 --replace_column 10 X
86 explain select * from v1;
87 drop view v1;
88 create view v1 as select c+1 from t1 order by 1 desc limit 2;
89 --sorted_result
90 select * from v1;
91 --replace_column 10 X
92 explain select * from v1;
93 drop view v1;
94 drop table t1;
95
96 #
97 # simple view + simple update, insert and delete
98 #
99 create table t1 (a int,
100 b int,
101 c int generated always as (-a) virtual,
102 d int generated always as (-a) stored,
103 primary key(a));
104 insert into t1 (a,b) values (10,2), (20,3), (30,4), (40,5), (50,10);
105 create view v1 (a,e,f,g) as select a, b+1,c+1,d+1 from t1;
106 # updatable field of updateable view
107 update v1 set a=a+e;
108 select * from v1 order by a;
109 select * from t1 order by a;
110 delete from v1;
111 select * from v1;
112 select * from t1;
113 --error ER_NONUPDATEABLE_COLUMN,
114 insert into v1 (a,e) values (60,15);
115 drop table t1;
116 drop view v1;
117
118 #
119 # outer join based on VIEW with WHERE clause
120 #
121 create table t1 (a int,
122 b int generated always as (-a) virtual,
123 c int generated always as (-a) stored,
124 primary key(a));
125 insert into t1 (a) values (1), (2), (3);
126 create view v1 (x,y,z) as select a,b,c from t1 where b < -1;
127 --sorted_result
128 select t1.a, v1.x, v1.y, v1.z from t1 left join v1 on (t1.b= v1.y);
129 drop view v1;
130 create view v1 (x,y,z) as select a,b,c from t1 where c < -1;
131 --sorted_result
132 select t1.a, v1.x, v1.y, v1.z from t1 left join v1 on (t1.c= v1.z);
133 drop view v1;
134 drop table t1;
135
136 #
137 # VIEW built over UNION
138 #
139 create table t1 (a1 int,
140 b1 int generated always as (-a1) virtual,
141 c1 int generated always as (-a1) stored);
142 create table t2 (a2 int,
143 b2 int generated always as (-a2) virtual,
144 c2 int generated always as (-a2) stored);
145 insert into t1 (a1) values (1), (2);
146 insert into t2 (a2) values (2), (3);
147 create view v1 as select * from t1,t2 union all select * from t1,t2;
148 --sorted_result
149 select * from v1;
150 drop view v1;
151 drop table t1, t2;
152
153 #
154 # Showing VIEW with VIEWs in subquery
155 #
156 create table t1 (a int,
157 b int generated always as (-a) virtual,
158 c int generated always as (-a) stored);
159 create table t2 like t1;
160 create view v1 as select a,b,c from t1;
161 create view v2 as select a,b,c from t2 where b in (select b from v1);
162 show create view v2;
163 drop view v2, v1;
164 drop table t1, t2;
165
166 #
167 # TODO: VIEW with full text
168 #
169 #CREATE TABLE t1 (c1 int not null auto_increment primary key, c2 varchar(20), fulltext(c2));
170 #insert into t1 (c2) VALUES ('real Beer'),('Water'),('Kossu'),('Coca-Cola'),('Vodka'),('Wine'),('almost real Beer');
171 #select * from t1 WHERE match (c2) against ('Beer');
172 #CREATE VIEW v1 AS SELECT * from t1 WHERE match (c2) against ('Beer');
173 #select * from v1;
174 #drop view v1;
175 #drop table t1;
176
177 #
178 # distinct in temporary table with a VIEW
179 #
180 create table t1 (a int,
181 b int generated always as (-a) virtual,
182 c int generated always as (-a) stored);
183 insert into t1 (a) values (1),(1),(2),(2),(3),(3);
184 create view v1 as select b from t1;
185 --sorted_result
186 select distinct b from v1;
187 select distinct b from v1 order by b limit 2;
188 select distinct b from t1 order by b limit 2;
189 prepare stmt1 from "select distinct b from v1 order by b limit 2";
190 execute stmt1;
191 execute stmt1;
192 deallocate prepare stmt1;
193 drop view v1;
194 create view v1 as select c from t1;
195 --sorted_result
196 select distinct c from v1;
197 select distinct c from v1 order by c limit 2;
198 select distinct c from t1 order by c limit 2;
199 prepare stmt1 from "select distinct c from v1 order by c limit 2";
200 execute stmt1;
201 execute stmt1;
202 deallocate prepare stmt1;
203 drop view v1;
204 drop table t1;
205
206 #
207 # WITH CHECK OPTION insert/update test
208 #
209 create table t1 (a int,
210 b int generated always as (-a) virtual,
211 c int generated always as (-a) stored);
212 create view v1 as select * from t1 where b > -2 && c >-2 with check option;
213 # simple insert
214 insert into v1 (a) values (1);
215 -- error 1369
216 insert into v1 (a) values (3);
217 # simple insert with ignore
218 insert ignore into v1 (a) values (2),(3),(0);
219 --sorted_result
220 select * from t1;
221 drop view v1;
222 drop table t1;
223