You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
sql: support referencing a routine from a view query
This commit adds support for invoking a UDF from a view (materialized or
otherwise). This requires resolving routine references while planning the
`CREATE VIEW` statement and passing them to the schema changer.
Fixes#146123
Release note (sql change): Added support for invoking a UDF from a view
query. Renaming or setting the schema on the routine is currently not
allowed if it is referenced by a view.
Copy file name to clipboardExpand all lines: pkg/sql/logictest/testdata/logic_test/drop_function
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -301,7 +301,7 @@ CREATE SCHEMA altSchema;
301
301
CREATE FUNCTION altSchema.f_called_by_b() RETURNS INT LANGUAGE SQL AS $$ SELECT 1 $$;
302
302
CREATE TABLE t1_with_b_2_ref(j int default altSchema.f_called_by_b() CHECK (altSchema.f_called_by_b() > 0));
303
303
304
-
statement error pgcode 0A000 cannot set schema for function \"f_called_by_b\" because other functions \(\[test.public.f_called_by_b2\]\) still depend on it
304
+
statement error pgcode 0A000 cannot set schema for function \"f_called_by_b\" because other functions or views \(\[test.public.f_called_by_b2\]\) still depend on it
305
305
ALTER FUNCTION f_called_by_b SET SCHEMA altSchema;
Copy file name to clipboardExpand all lines: pkg/sql/logictest/testdata/logic_test/udf
+145Lines changed: 145 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1046,3 +1046,148 @@ select * from pg_catalog.pg_attrdef;
1046
1046
3466299041 175 2 unique_rowid() unique_rowid()
1047
1047
1048
1048
subtest end
1049
+
1050
+
subtest view
1051
+
1052
+
statement ok
1053
+
CREATE TABLE xy (x INT, y INT);
1054
+
INSERT INTO xy VALUES (1, 2), (3, 4), (5, 6);
1055
+
1056
+
statement ok
1057
+
CREATE FUNCTION f_scalar() RETURNS INT LANGUAGE SQL AS $$ SELECT count(*) FROM xy $$;
1058
+
1059
+
statement ok
1060
+
CREATE FUNCTION f_setof() RETURNS SETOF xy LANGUAGE SQL AS $$ SELECT * FROM xy $$;
1061
+
1062
+
statement ok
1063
+
CREATE VIEW v AS SELECT x, y, f_scalar() FROM f_setof();
1064
+
1065
+
statement ok
1066
+
CREATE MATERIALIZED VIEW mv AS SELECT x, y, f_scalar() FROM f_setof();
1067
+
1068
+
query III
1069
+
SELECT * FROM v ORDER BY x, y;
1070
+
----
1071
+
1 2 3
1072
+
3 4 3
1073
+
5 6 3
1074
+
1075
+
query III
1076
+
SELECT * FROM mv ORDER BY x, y;
1077
+
----
1078
+
1 2 3
1079
+
3 4 3
1080
+
5 6 3
1081
+
1082
+
statement ok
1083
+
REFRESH MATERIALIZED VIEW mv;
1084
+
1085
+
query III
1086
+
SELECT * FROM mv ORDER BY x, y;
1087
+
----
1088
+
1 2 3
1089
+
3 4 3
1090
+
5 6 3
1091
+
1092
+
statement ok
1093
+
INSERT INTO xy VALUES (7, 8);
1094
+
1095
+
query III
1096
+
SELECT * FROM v ORDER BY x, y;
1097
+
----
1098
+
1 2 4
1099
+
3 4 4
1100
+
5 6 4
1101
+
7 8 4
1102
+
1103
+
query III
1104
+
SELECT * FROM mv ORDER BY x, y;
1105
+
----
1106
+
1 2 3
1107
+
3 4 3
1108
+
5 6 3
1109
+
1110
+
statement ok
1111
+
REFRESH MATERIALIZED VIEW mv;
1112
+
1113
+
query III
1114
+
SELECT * FROM mv ORDER BY x, y;
1115
+
----
1116
+
1 2 4
1117
+
3 4 4
1118
+
5 6 4
1119
+
7 8 4
1120
+
1121
+
statement ok
1122
+
CREATE ROLE bob;
1123
+
1124
+
statement ok
1125
+
GRANT ALL ON SCHEMA public TO bob;
1126
+
1127
+
statement ok
1128
+
GRANT ALL ON v TO bob;
1129
+
1130
+
statement ok
1131
+
GRANT ALL ON mv TO bob;
1132
+
1133
+
statement ok
1134
+
REVOKE EXECUTE ON FUNCTION f_scalar() FROM PUBLIC;
1135
+
1136
+
statement ok
1137
+
REVOKE EXECUTE ON FUNCTION f_scalar() FROM bob;
1138
+
1139
+
statement ok
1140
+
SET ROLE bob;
1141
+
1142
+
statement error pgcode 42501 pq: user bob does not have EXECUTE privilege on function f_scalar
1143
+
SELECT f_scalar();
1144
+
1145
+
statement error pgcode 42501 pq: user bob does not have EXECUTE privilege on function f_scalar
1146
+
SELECT * FROM v;
1147
+
1148
+
statement ok
1149
+
SELECT * FROM mv;
1150
+
1151
+
statement ok
1152
+
SET ROLE root;
1153
+
1154
+
statement error pgcode 0A000 cannot rename function "f_scalar" because other functions or views \(\[test.public.v, test.public.mv\]\) still depend on it
1155
+
ALTER FUNCTION f_scalar RENAME TO f_scalar_renamed;
1156
+
1157
+
statement error pgcode 0A000 cannot rename function "f_setof" because other functions or views \(\[test.public.v, test.public.mv\]\) still depend on it
1158
+
ALTER FUNCTION f_setof RENAME TO f_setof_renamed;
1159
+
1160
+
statement error pgcode 2BP01 pq: cannot drop function "f_scalar" because other objects \(\[test.public.v, test.public.mv\]\) still depend on it
1161
+
DROP FUNCTION f_scalar;
1162
+
1163
+
statement error pgcode 2BP01 pq: cannot drop function "f_setof" because other objects \(\[test.public.v, test.public.mv\]\) still depend on it
1164
+
DROP FUNCTION f_setof;
1165
+
1166
+
statement ok
1167
+
DROP VIEW v;
1168
+
1169
+
statement ok
1170
+
DROP MATERIALIZED VIEW mv;
1171
+
1172
+
statement ok
1173
+
DROP FUNCTION f_scalar;
1174
+
1175
+
statement ok
1176
+
DROP FUNCTION f_setof;
1177
+
1178
+
# Test a view referencing a builtin function.
1179
+
statement ok
1180
+
CREATE VIEW v_builtin AS SELECT * FROM generate_series(1, 4);
CREATE FUNCTION recursion_check() RETURNS STRING LANGUAGE SQL AS $$ SELECT recursion_check() $$;
56
56
57
57
# Validate that function renaming is blocked.
58
-
statement error pgcode 0A000 cannot rename function \"lower_hello\" because other functions \(\[test.public.upper_hello, test.public.concat_hello\]\) still depend on it
58
+
statement error pgcode 0A000 cannot rename function \"lower_hello\" because other functions or views \(\[test.public.upper_hello, test.public.concat_hello\]\) still depend on it
59
59
ALTER FUNCTION lower_hello rename to lower_hello_new
60
60
61
61
statement ok
62
62
CREATE SCHEMA sc2;
63
63
64
64
# Validate that function schema changes are blocked.
65
-
statement error pgcode 0A000 cannot set schema for function \"lower_hello\" because other functions \(\[test.public.upper_hello, test.public.concat_hello\]\) still depend on it
65
+
statement error pgcode 0A000 cannot set schema for function \"lower_hello\" because other functions or views \(\[test.public.upper_hello, test.public.concat_hello\]\) still depend on it
66
66
ALTER FUNCTION lower_hello SET SCHEMA sc2;
67
67
68
-
69
68
statement ok
70
69
CREATE FUNCTION f() RETURNS INT LANGUAGE SQL AS $$
Copy file name to clipboardExpand all lines: pkg/sql/logictest/testdata/logic_test/udf_unsupported
-29Lines changed: 0 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -69,35 +69,6 @@ CREATE INDEX idx_b ON test_tbl_t (test_tbl_f());
69
69
subtest end
70
70
71
71
72
-
subtest disallow_udf_in_views
73
-
74
-
statement ok
75
-
CREATE FUNCTION test_vf_f() RETURNS STRING LANGUAGE SQL AS $$ SELECT lower('hello') $$;
76
-
77
-
78
-
statement error pgcode 42883 pq: unknown function: test_vf_f\(\)\nHINT:.*intention was to use a user-defined function in the view query, which is currently not supported.
79
-
CREATE VIEW v AS SELECT test_vf_f();
80
-
81
-
statement ok
82
-
CREATE VIEW v AS SELECT lower('hello');
83
-
84
-
query T
85
-
SELECT create_statement FROM [SHOW CREATE FUNCTION test_vf_f];
0 commit comments