Skip to content

Commit 0540b94

Browse files
committed
WL#11864 Implement I_S.VIEW_TABLE_USAGE and I_S.VIEW_ROUTINE_USAGE
This WL implements I_S.VIEW_TABLE_USAGE and I_S.VIEW_ROUTINE_USAGE. Following functional requirements are implemented: FR1: Implement I_S.VIEW_TABLE_USAGE as a system view over mysql.view_table_usage DD table. This I_S view shows the table names that is used by a view. FR2: Implement I_S.VIEW_ROUTINE_USAGE as a system view over mysql.view_routine_usage DD table. This I_S view shows function names that is used by a view. FR3: FR1 and FR2 would show only views that are permitted to the current user executing I_S query. FR4: FR1 should also show the view names that are directly used by the view. FR5: FR1 and FR2 would show dependent table and function names that are permitted to the current user executing I_S query. FR6: Increase the IS_DD_VERSION number as we are adding new I_S tables. - The native function CAN_ACCESS_VIEW() is changed to avoid printing the same warning repeatedly. This is required because optimizer end-up calling the native function to check view access more times than expected. - Due to the bug#28098779, the metadata of I_S view column nullability changes. Recording the result files until the bug is fixed. These changes would be reverted by above bug fix eventually. New test main.dd_is_view_usage is added. Change-Id: Iffc1d1471bd7be8872a7cd77586c66528df4185f
1 parent d02518c commit 0540b94

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+3504
-72
lines changed

mysql-test/r/dd_is_compatibility_ci.result

+2
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ TABLE_PRIVILEGES
185185
TRIGGERS
186186
USER_PRIVILEGES
187187
VIEWS
188+
VIEW_ROUTINE_USAGE
189+
VIEW_TABLE_USAGE
188190
DROP VIEW v1;
189191
# 3. I_S tables/views/columns are not case sensitive, but on trunk, the
190192
# two statements below will have different case of the column heading.

mysql-test/r/dd_is_compatibility_cs.result

+2
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ TABLE_PRIVILEGES
185185
TRIGGERS
186186
USER_PRIVILEGES
187187
VIEWS
188+
VIEW_ROUTINE_USAGE
189+
VIEW_TABLE_USAGE
188190
DROP VIEW v1;
189191
# 3. I_S tables/views/columns are not case sensitive, but on trunk, the
190192
# two statements below will have different case of the column heading.

mysql-test/r/dd_is_view_usage.result

+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
#
2+
# WL#11864 Implement I_S.VIEW_TABLE_USAGE and I_S.VIEW_ROUTINE_USAGE
3+
#
4+
# Basic schema definition used by test cases.
5+
CREATE DATABASE db1;
6+
USE db1;
7+
CREATE TABLE t1 (c1 INT);
8+
CREATE FUNCTION f1() RETURNS INT RETURN 1;
9+
CREATE FUNCTION f2() RETURNS INT RETURN 2;
10+
CREATE VIEW v1 AS SELECT f1() AS f1;
11+
CREATE VIEW v2 AS SELECT f1, f2() AS f2 FROM v1;
12+
CREATE VIEW v3 AS SELECT c1, f2 FROM t1, v2;
13+
# Case 1: Show table and views used by view v2 and v3.
14+
SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
15+
WHERE VIEW_SCHEMA='db1' ORDER BY VIEW_NAME, TABLE_NAME;
16+
VIEW_CATALOG VIEW_SCHEMA VIEW_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
17+
def db1 v2 def db1 v1
18+
def db1 v3 def db1 t1
19+
def db1 v3 def db1 v2
20+
# Case 2: Show routines used by views.
21+
SELECT * FROM INFORMATION_SCHEMA.VIEW_ROUTINE_USAGE
22+
WHERE TABLE_SCHEMA='db1';
23+
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME SPECIFIC_CATALOG SPECIFIC_SCHEMA SPECIFIC_NAME
24+
def db1 v1 def db1 f1
25+
def db1 v2 def db1 f2
26+
# Case 3: Rename the column of t1 and list view dependency.
27+
# The query should throw warning on view v3.
28+
ALTER TABLE t1 RENAME COLUMN c1 TO c2;
29+
SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
30+
WHERE VIEW_SCHEMA='db1' AND VIEW_NAME='v3';
31+
VIEW_CATALOG VIEW_SCHEMA VIEW_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
32+
def db1 v3 def db1 t1
33+
def db1 v3 def db1 v2
34+
Warnings:
35+
Warning 1356 View 'db1.v3' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
36+
ALTER TABLE t1 RENAME COLUMN c2 TO c1;
37+
SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
38+
WHERE VIEW_SCHEMA='db1' AND VIEW_NAME='v3';
39+
VIEW_CATALOG VIEW_SCHEMA VIEW_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
40+
def db1 v3 def db1 t1
41+
def db1 v3 def db1 v2
42+
# Case 4: Drop function f1 and list dependency.
43+
# The query should throw warning on view v1.
44+
DROP FUNCTION f1;
45+
SELECT * FROM INFORMATION_SCHEMA.VIEW_ROUTINE_USAGE
46+
WHERE TABLE_SCHEMA='db1' AND TABLE_NAME='v1';
47+
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME SPECIFIC_CATALOG SPECIFIC_SCHEMA SPECIFIC_NAME
48+
Warnings:
49+
Warning 1356 View 'db1.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
50+
CREATE FUNCTION f1() RETURNS INT return 1;
51+
SELECT * FROM INFORMATION_SCHEMA.VIEW_ROUTINE_USAGE
52+
WHERE TABLE_SCHEMA='db1' AND TABLE_NAME='v1';
53+
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME SPECIFIC_CATALOG SPECIFIC_SCHEMA SPECIFIC_NAME
54+
def db1 v1 def db1 f1
55+
# Case 5: Access checks.
56+
CREATE USER 'testuser'@'localhost';
57+
# 5.1 Grant permission on v2, but not v1. VIEW_TABLE_USAGE will not
58+
# list any entry because user has no permission on v1.
59+
GRANT ALL ON db1.v2 TO 'testuser'@'localhost';
60+
SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
61+
WHERE VIEW_SCHEMA='db1' ORDER BY VIEW_NAME, TABLE_NAME;
62+
VIEW_CATALOG VIEW_SCHEMA VIEW_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
63+
# 5.2 Give permission on v1 and that VIEW_TABLE_USAGE shows v2 entry.
64+
GRANT ALL ON db1.v1 TO 'testuser'@'localhost';
65+
SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
66+
WHERE VIEW_SCHEMA='db1' ORDER BY VIEW_NAME, TABLE_NAME;
67+
VIEW_CATALOG VIEW_SCHEMA VIEW_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
68+
def db1 v2 def db1 v1
69+
# 5.3 VIEW_ROUTINE_USAGE does not list v1, as it have no grant on f1.
70+
SELECT * FROM INFORMATION_SCHEMA.VIEW_ROUTINE_USAGE
71+
WHERE TABLE_SCHEMA='db1';
72+
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME SPECIFIC_CATALOG SPECIFIC_SCHEMA SPECIFIC_NAME
73+
# 5.4 Grant access to f1 and check that VIEW_ROUTINE_USAGE shows it.
74+
GRANT EXECUTE ON FUNCTION db1.f1 TO 'testuser'@'localhost';
75+
SELECT * FROM INFORMATION_SCHEMA.VIEW_ROUTINE_USAGE
76+
WHERE TABLE_SCHEMA='db1';
77+
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME SPECIFIC_CATALOG SPECIFIC_SCHEMA SPECIFIC_NAME
78+
def db1 v1 def db1 f1
79+
# 5.5 Grant access on v3, see that dependency 't1' is not listed.
80+
GRANT ALL ON db1.v3 TO 'testuser'@'localhost';
81+
SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
82+
WHERE VIEW_SCHEMA='db1' ORDER BY VIEW_NAME, TABLE_NAME;
83+
VIEW_CATALOG VIEW_SCHEMA VIEW_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
84+
def db1 v2 def db1 v1
85+
def db1 v3 def db1 v2
86+
# 5.6 Grant access on t1, see that dependency 't1' is listed.
87+
GRANT SELECT on db1.t1 TO 'testuser'@'localhost';
88+
SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
89+
WHERE VIEW_SCHEMA='db1' ORDER BY VIEW_NAME, TABLE_NAME;
90+
VIEW_CATALOG VIEW_SCHEMA VIEW_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
91+
def db1 v2 def db1 v1
92+
def db1 v3 def db1 t1
93+
def db1 v3 def db1 v2
94+
DROP USER 'testuser'@'localhost';
95+
# Cleanup
96+
DROP FUNCTION f1;
97+
DROP FUNCTION f2;
98+
DROP VIEW v1, v2, v3;
99+
DROP TABLE t1;
100+
# Case 6:
101+
# 6.1 Rename table t1 to t2 and list view dependency.
102+
# The query should throw warning on view v1.
103+
CREATE TABLE t1 (c1 INT);
104+
CREATE VIEW v1 AS SELECT c1 FROM t1;
105+
SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
106+
WHERE VIEW_SCHEMA='db1' ORDER BY VIEW_NAME, TABLE_NAME;
107+
VIEW_CATALOG VIEW_SCHEMA VIEW_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
108+
def db1 v1 def db1 t1
109+
RENAME TABLE t1 to t2;
110+
SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
111+
WHERE VIEW_SCHEMA='db1' ORDER BY VIEW_NAME, TABLE_NAME;
112+
VIEW_CATALOG VIEW_SCHEMA VIEW_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
113+
def db1 v1 def db1 t1
114+
Warnings:
115+
Warning 1356 View 'db1.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
116+
RENAME TABLE t2 to t1;
117+
SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
118+
WHERE VIEW_SCHEMA='db1' ORDER BY VIEW_NAME, TABLE_NAME;
119+
VIEW_CATALOG VIEW_SCHEMA VIEW_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
120+
def db1 v1 def db1 t1
121+
# 6.2 Drop table t1 and list view dependency.
122+
DROP TABLE t1;
123+
SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
124+
WHERE VIEW_SCHEMA='db1' ORDER BY VIEW_NAME, TABLE_NAME;
125+
VIEW_CATALOG VIEW_SCHEMA VIEW_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
126+
def db1 v1 def db1 t1
127+
Warnings:
128+
Warning 1356 View 'db1.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
129+
DROP VIEW v1;
130+
# Case 7: List dependent table and function names
131+
# that are permitted to the current user.
132+
CREATE USER testuser1@localhost;
133+
GRANT CREATE ON db1.* TO testuser1@localhost;
134+
GRANT SELECT ON db1.* TO testuser1@localhost;
135+
GRANT CREATE VIEW ON db1.* TO testuser1@localhost;
136+
GRANT CREATE ROUTINE ON db1.* TO testuser1@localhost;
137+
GRANT DROP ON db1.* TO testuser1@localhost;
138+
CREATE USER testuser2@localhost;
139+
GRANT CREATE ON db1.* TO testuser2@localhost;
140+
GRANT SELECT ON db1.* TO testuser2@localhost;
141+
GRANT CREATE VIEW ON db1.* TO testuser2@localhost;
142+
GRANT CREATE ROUTINE ON db1.* TO testuser2@localhost;
143+
GRANT DROP ON db1.* TO testuser2@localhost;
144+
CREATE TABLE t1(c1 INT);
145+
CREATE VIEW v1 AS SELECT c1 FROM t1;
146+
CREATE FUNCTION f1() RETURNS INT RETURN 1;
147+
CREATE VIEW v2 AS SELECT f1();
148+
CREATE TABLE t2(c1 INT);
149+
CREATE VIEW v3 AS SELECT c1 FROM t2;
150+
CREATE FUNCTION f2() RETURNS INT RETURN 1;
151+
CREATE VIEW v4 AS SELECT f2();
152+
SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
153+
WHERE VIEW_SCHEMA='db1' ORDER BY VIEW_NAME, TABLE_NAME;
154+
VIEW_CATALOG VIEW_SCHEMA VIEW_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
155+
def db1 v1 def db1 t1
156+
SELECT * FROM INFORMATION_SCHEMA.VIEW_ROUTINE_USAGE
157+
WHERE TABLE_SCHEMA='db1';
158+
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME SPECIFIC_CATALOG SPECIFIC_SCHEMA SPECIFIC_NAME
159+
def db1 v2 def db1 f1
160+
SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
161+
WHERE VIEW_SCHEMA='db1' ORDER BY VIEW_NAME, TABLE_NAME;
162+
VIEW_CATALOG VIEW_SCHEMA VIEW_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
163+
def db1 v3 def db1 t2
164+
SELECT * FROM INFORMATION_SCHEMA.VIEW_ROUTINE_USAGE
165+
WHERE TABLE_SCHEMA='db1';
166+
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME SPECIFIC_CATALOG SPECIFIC_SCHEMA SPECIFIC_NAME
167+
def db1 v4 def db1 f2
168+
DROP VIEW v1,v2;
169+
DROP FUNCTION f1;
170+
DROP TABLE t1;
171+
DROP VIEW v3,v4;
172+
DROP FUNCTION f2;
173+
DROP TABLE t2;
174+
DROP USER testuser1@localhost;
175+
DROP USER testuser2@localhost;
176+
# Case 8: Revoke view privilege
177+
CREATE USER testuser1@localhost;
178+
CREATE TABLE t1(c1 INT);
179+
CREATE VIEW v1 AS SELECT c1 FROM t1;
180+
GRANT ALL ON db1.v1 TO testuser1@localhost;
181+
GRANT ALL ON db1.t1 TO testuser1@localhost;
182+
SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
183+
WHERE VIEW_SCHEMA='db1' ORDER BY VIEW_NAME, TABLE_NAME;
184+
VIEW_CATALOG VIEW_SCHEMA VIEW_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
185+
def db1 v1 def db1 t1
186+
REVOKE ALL ON db1.v1 FROM testuser1@localhost;
187+
SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
188+
WHERE VIEW_SCHEMA='db1' ORDER BY VIEW_NAME, TABLE_NAME;
189+
VIEW_CATALOG VIEW_SCHEMA VIEW_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
190+
REVOKE ALL ON db1.t1 FROM testuser1@localhost;
191+
DROP USER testuser1@localhost;
192+
DROP VIEW v1;
193+
DROP TABLE t1;
194+
DROP DATABASE db1;

mysql-test/r/information_schema.result

+11-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ TABLE_PRIVILEGES
9292
TRIGGERS
9393
USER_PRIVILEGES
9494
VIEWS
95+
VIEW_ROUTINE_USAGE
96+
VIEW_TABLE_USAGE
9597
columns_priv
9698
component
9799
db
@@ -831,6 +833,8 @@ TABLES TABLE_NAME select
831833
TABLE_CONSTRAINTS TABLE_NAME select
832834
TABLE_PRIVILEGES TABLE_NAME select
833835
VIEWS TABLE_NAME select
836+
VIEW_ROUTINE_USAGE TABLE_NAME select
837+
VIEW_TABLE_USAGE TABLE_NAME select
834838
delete from mysql.user where user='mysqltest_4';
835839
delete from mysql.db where user='mysqltest_4';
836840
flush privileges;
@@ -839,7 +843,7 @@ table_schema IN ('mysql', 'information_schema', 'test', 'mysqltest')
839843
AND table_name not like 'ndb%' AND table_name COLLATE utf8_general_ci not like 'innodb_%'
840844
GROUP BY TABLE_SCHEMA;
841845
TABLE_SCHEMA count(*)
842-
information_schema 32
846+
information_schema 34
843847
mysql 31
844848
create table t1 (i int, j int);
845849
create trigger trg1 before insert on t1 for each row
@@ -1312,6 +1316,8 @@ TABLE_PRIVILEGES information_schema.TABLE_PRIVILEGES 1
13121316
TRIGGERS information_schema.TRIGGERS 1
13131317
USER_PRIVILEGES information_schema.USER_PRIVILEGES 1
13141318
VIEWS information_schema.VIEWS 1
1319+
VIEW_ROUTINE_USAGE information_schema.VIEW_ROUTINE_USAGE 1
1320+
VIEW_TABLE_USAGE information_schema.VIEW_TABLE_USAGE 1
13151321
create table t1(f1 int);
13161322
create view v1 as select f1+1 as a from t1;
13171323
create table t2 (f1 int, f2 int);
@@ -2440,6 +2446,8 @@ TABLE_PRIVILEGES TABLE_SCHEMA
24402446
TRIGGERS TRIGGER_SCHEMA
24412447
USER_PRIVILEGES GRANTEE
24422448
VIEWS TABLE_SCHEMA
2449+
VIEW_ROUTINE_USAGE TABLE_SCHEMA
2450+
VIEW_TABLE_USAGE VIEW_SCHEMA
24432451
SELECT t.table_name, c1.column_name
24442452
FROM information_schema.tables t
24452453
INNER JOIN
@@ -2491,6 +2499,8 @@ TABLE_PRIVILEGES TABLE_SCHEMA
24912499
TRIGGERS TRIGGER_SCHEMA
24922500
USER_PRIVILEGES GRANTEE
24932501
VIEWS TABLE_SCHEMA
2502+
VIEW_ROUTINE_USAGE TABLE_SCHEMA
2503+
VIEW_TABLE_USAGE VIEW_SCHEMA
24942504
#
24952505
# BUG#24679166: TABLE CREATION WITH BINARY TYPE COLUMN IS RESULTING IN
24962506
# ASSERT CONDITION FAILURE.

mysql-test/r/information_schema_ci.result

+11-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ TABLE_PRIVILEGES
9292
TRIGGERS
9393
USER_PRIVILEGES
9494
VIEWS
95+
VIEW_ROUTINE_USAGE
96+
VIEW_TABLE_USAGE
9597
columns_priv
9698
component
9799
db
@@ -831,6 +833,8 @@ TABLES TABLE_NAME select
831833
TABLE_CONSTRAINTS TABLE_NAME select
832834
TABLE_PRIVILEGES TABLE_NAME select
833835
VIEWS TABLE_NAME select
836+
VIEW_ROUTINE_USAGE TABLE_NAME select
837+
VIEW_TABLE_USAGE TABLE_NAME select
834838
delete from mysql.user where user='mysqltest_4';
835839
delete from mysql.db where user='mysqltest_4';
836840
flush privileges;
@@ -839,7 +843,7 @@ table_schema IN ('mysql', 'information_schema', 'test', 'mysqltest')
839843
AND table_name not like 'ndb%' AND table_name COLLATE utf8_general_ci not like 'innodb_%'
840844
GROUP BY TABLE_SCHEMA;
841845
TABLE_SCHEMA count(*)
842-
information_schema 32
846+
information_schema 34
843847
mysql 31
844848
create table t1 (i int, j int);
845849
create trigger trg1 before insert on t1 for each row
@@ -1312,6 +1316,8 @@ TABLE_PRIVILEGES information_schema.TABLE_PRIVILEGES 1
13121316
TRIGGERS information_schema.TRIGGERS 1
13131317
USER_PRIVILEGES information_schema.USER_PRIVILEGES 1
13141318
VIEWS information_schema.VIEWS 1
1319+
VIEW_ROUTINE_USAGE information_schema.VIEW_ROUTINE_USAGE 1
1320+
VIEW_TABLE_USAGE information_schema.VIEW_TABLE_USAGE 1
13151321
create table t1(f1 int);
13161322
create view v1 as select f1+1 as a from t1;
13171323
create table t2 (f1 int, f2 int);
@@ -2440,6 +2446,8 @@ TABLE_PRIVILEGES TABLE_SCHEMA
24402446
TRIGGERS TRIGGER_SCHEMA
24412447
USER_PRIVILEGES GRANTEE
24422448
VIEWS TABLE_SCHEMA
2449+
VIEW_ROUTINE_USAGE TABLE_SCHEMA
2450+
VIEW_TABLE_USAGE VIEW_SCHEMA
24432451
SELECT t.table_name, c1.column_name
24442452
FROM information_schema.tables t
24452453
INNER JOIN
@@ -2491,6 +2499,8 @@ TABLE_PRIVILEGES TABLE_SCHEMA
24912499
TRIGGERS TRIGGER_SCHEMA
24922500
USER_PRIVILEGES GRANTEE
24932501
VIEWS TABLE_SCHEMA
2502+
VIEW_ROUTINE_USAGE TABLE_SCHEMA
2503+
VIEW_TABLE_USAGE VIEW_SCHEMA
24942504
#
24952505
# BUG#24679166: TABLE CREATION WITH BINARY TYPE COLUMN IS RESULTING IN
24962506
# ASSERT CONDITION FAILURE.

mysql-test/r/information_schema_db.result

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ TABLE_PRIVILEGES
3838
TRIGGERS
3939
USER_PRIVILEGES
4040
VIEWS
41+
VIEW_ROUTINE_USAGE
42+
VIEW_TABLE_USAGE
4143
show tables from INFORMATION_SCHEMA like 'T%';
4244
Tables_in_information_schema (T%)
4345
TABLES

0 commit comments

Comments
 (0)