-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
views
1500 lines (1109 loc) · 34.2 KB
/
views
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# knob-opt: sync-event-log
statement ok
SET CLUSTER SETTING sql.cross_db_views.enabled = TRUE
statement ok
CREATE TABLE t (a INT PRIMARY KEY, b INT)
let $t_id
SELECT id FROM system.namespace WHERE name='t'
statement ok
INSERT INTO t VALUES (1, 99), (2, 98), (3, 97)
statement ok
CREATE VIEW v1 AS SELECT a, b FROM t
statement error pgcode 42P07 relation \"test.public.v1\" already exists
CREATE VIEW v1 AS SELECT a, b FROM t
statement error pgcode 42P07 relation \"test.public.t\" already exists
CREATE VIEW t AS SELECT a, b FROM t
# view statement ignored if other way around.
statement ok
CREATE VIEW IF NOT EXISTS v1 AS SELECT b, a FROM v1
statement ok
CREATE VIEW IF NOT EXISTS v2 (x, y) AS SELECT a, b FROM t
statement error pgcode 42601 CREATE VIEW specifies 1 column name, but data source has 2 columns
CREATE VIEW v3 (x) AS SELECT a, b FROM t
statement error pgcode 42601 CREATE VIEW specifies 3 column names, but data source has 2 columns
CREATE VIEW v4 (x, y, z) AS SELECT a, b FROM t
statement error pgcode 42P01 relation "dne" does not exist
CREATE VIEW v5 AS SELECT a, b FROM dne
statement ok
CREATE VIEW v6 (x, y) AS SELECT a, b FROM v1
statement ok
CREATE VIEW v7 (x, y) AS SELECT a, b FROM v1 ORDER BY a DESC LIMIT 2
statement error pgcode 42703 column \"j\" does not exist
CREATE VIEW err AS SELECT j FROM t
statement error pgcode 42703 column \"j\" does not exist
CREATE VIEW err AS SELECT a FROM t WHERE a = j
query II colnames,rowsort
SELECT * FROM v1
----
a b
1 99
2 98
3 97
query II colnames,rowsort
SELECT * FROM v2
----
x y
1 99
2 98
3 97
query II colnames,rowsort
SELECT * FROM v6
----
x y
1 99
2 98
3 97
query II colnames
SELECT * FROM v7
----
x y
3 97
2 98
query II colnames
SELECT * FROM v7 ORDER BY x LIMIT 1
----
x y
2 98
query II
SELECT * FROM v2 ORDER BY x DESC LIMIT 1
----
3 97
query I rowsort
SELECT x FROM v2
----
1
2
3
query I rowsort
SELECT y FROM v2
----
99
98
97
query I
SELECT x FROM v7
----
3
2
query I
SELECT x FROM v7 ORDER BY x LIMIT 1
----
2
query I
SELECT y FROM v7
----
97
98
query I
SELECT y FROM v7 ORDER BY x LIMIT 1
----
98
query IIII rowsort
SELECT * FROM v1 AS v1 INNER JOIN v2 AS v2 ON v1.a = v2.x
----
1 99 1 99
2 98 2 98
3 97 3 97
statement ok
CREATE DATABASE test2
statement ok
SET DATABASE = test2
query II colnames,rowsort
SELECT * FROM test.v1
----
a b
1 99
2 98
3 97
query II colnames,rowsort
SELECT * FROM test.v2
----
x y
1 99
2 98
3 97
query II colnames,rowsort
SELECT * FROM test.v6
----
x y
1 99
2 98
3 97
query II colnames
SELECT * FROM test.v7
----
x y
3 97
2 98
query II colnames
SELECT * FROM test.v7 ORDER BY x LIMIT 1
----
x y
2 98
statement ok
CREATE VIEW v1 AS SELECT x, y FROM test.v2
statement ok
SET DATABASE = test
query II colnames,rowsort
SELECT * FROM test2.v1
----
x y
1 99
2 98
3 97
query TT
SHOW CREATE VIEW v1
----
v1 CREATE VIEW public.v1 (
a,
b
) AS SELECT a, b FROM test.public.t
query TT
SHOW CREATE VIEW v2
----
v2 CREATE VIEW public.v2 (
x,
y
) AS SELECT a, b FROM test.public.t
query TT
SHOW CREATE VIEW v6
----
v6 CREATE VIEW public.v6 (
x,
y
) AS SELECT a, b FROM test.public.v1
query TT
SHOW CREATE VIEW v7
----
v7 CREATE VIEW public.v7 (
x,
y
) AS SELECT a, b FROM test.public.v1 ORDER BY a DESC LIMIT 2
query TT
SHOW CREATE VIEW test2.v1
----
test2.public.v1 CREATE VIEW public.v1 (
x,
y
) AS SELECT x, y FROM test.public.v2
statement ok
GRANT SELECT ON t TO testuser
user testuser
query II rowsort
SELECT * FROM t
----
1 99
2 98
3 97
query error user testuser does not have SELECT privilege on relation v1
SELECT * FROM v1
query error user testuser does not have SELECT privilege on relation v6
SELECT * FROM v6
query error user testuser has no privileges on relation v1
SHOW CREATE VIEW v1
user root
statement ok
REVOKE SELECT ON t FROM testuser
statement ok
GRANT SELECT ON v1 TO testuser
user testuser
query error user testuser does not have SELECT privilege on relation t
SELECT * FROM t
query II rowsort
SELECT * FROM v1
----
1 99
2 98
3 97
query error user testuser does not have SELECT privilege on relation v6
SELECT * FROM v6
query TT
SHOW CREATE VIEW v1
----
v1 CREATE VIEW public.v1 (
a,
b
) AS SELECT a, b FROM test.public.t
user root
statement ok
REVOKE SELECT ON v1 FROM testuser
statement ok
GRANT SELECT ON v6 TO testuser
user testuser
query error user testuser does not have SELECT privilege on relation t
SELECT * FROM t
query error user testuser does not have SELECT privilege on relation v1
SELECT * FROM v1
query II rowsort
SELECT * FROM v6
----
1 99
2 98
3 97
user root
# Ensure that views work over tables identified by numeric reference.
statement ok
CREATE VIEW num_ref_view AS SELECT a, b FROM [$t_id AS t]
statement ok
GRANT SELECT ON num_ref_view TO testuser
user testuser
query II rowsort
SELECT * FROM num_ref_view
----
1 99
2 98
3 97
user root
statement ok
DROP VIEW num_ref_view
statement error pgcode 42809 "v1" is not a table
DROP TABLE v1
statement error pgcode 42809 "t" is not a view
DROP VIEW t
skipif config local-legacy-schema-changer
statement error cannot drop relation "v1" because view "v7" depends on it
DROP VIEW v1
# Legacy schema changer orders dependent views differently, but
# semantically the same error.
onlyif config local-legacy-schema-changer
statement error cannot drop relation "v1" because view "v6" depends on it
DROP VIEW v1
statement error cannot drop relation "v2" because view "test2.public.v1" depends on it
DROP VIEW v2
statement ok
DROP VIEW test2.v1
statement ok
DROP VIEW v7
statement ok
DROP VIEW v6
statement ok
DROP VIEW v2
statement ok
DROP VIEW v1
statement error pgcode 42P01 relation "v1" does not exist
DROP VIEW v1
# Verify that we can depend on virtual tables.
statement ok
CREATE VIEW virt1 AS SELECT table_schema FROM information_schema.columns
statement ok
DROP VIEW virt1
# Verify that we can depend on virtual views.
statement ok
CREATE VIEW virt2 AS SELECT range_id, lease_holder FROM crdb_internal.ranges
statement ok
DROP VIEW virt2
# Verify correct rejection of star expressions
# TODO(a-robinson): Support star expressions as soon as we can (#10028)
statement error views do not currently support \* expressions
create view s1 AS SELECT * FROM t
statement error views do not currently support \* expressions
create view s1 AS SELECT t.* FROM t
statement error views do not currently support \* expressions
create view s1 AS SELECT a FROM t ORDER BY t.*
statement error views do not currently support \* expressions
create view s1 AS SELECT count(1) FROM t GROUP BY t.*
statement error views do not currently support \* expressions
create view s1 AS SELECT alias.* FROM t AS alias
statement error views do not currently support \* expressions
create view s1 AS TABLE t
statement error views do not currently support \* expressions
create view s1 AS SELECT a FROM (SELECT * FROM t)
statement error views do not currently support \* expressions
create view s1 AS SELECT a FROM t WHERE NOT a IN (SELECT a FROM (SELECT * FROM t))
statement error views do not currently support \* expressions
create view s1 AS SELECT a FROM t GROUP BY a HAVING a IN (SELECT a FROM (SELECT * FROM t))
statement error views do not currently support \* expressions
create view s1 AS SELECT t1.*, t2.a FROM t AS t1 JOIN t AS t2 ON t1.a = t2.a
statement error views do not currently support \* expressions
create view s1 AS SELECT t1.a, t2.* FROM t AS t1 JOIN t AS t2 ON t1.a = t2.a
statement error views do not currently support \* expressions
create view s1 AS SELECT t1.*, t2.* FROM t AS t1 JOIN t AS t2 ON t1.a = t2.a
statement error views do not currently support \* expressions
create view s1 AS SELECT * FROM t AS t1 JOIN t AS t2 ON t1.a = t2.a
statement error views do not currently support \* expressions
create view s1 AS SELECT t1.a, t2.a FROM (SELECT * FROM t) AS t1 JOIN t AS t2 ON t1.a = t2.a
statement error views do not currently support \* expressions
create view s1 AS SELECT t1.a, t2.a FROM t AS t1 JOIN (SELECT * FROM t) AS t2 ON t1.a = t2.a
statement error views do not currently support \* expressions
create view s1 AS SELECT t1.a, t2.a FROM t AS t1 JOIN t AS t2 ON t1.a IN (SELECT a FROM (SELECT * FROM t))
statement ok
create view s1 AS SELECT count(*) FROM t
statement ok
create view s2 AS SELECT a FROM t WHERE a IN (SELECT count(*) FROM t)
statement ok
create view s3 AS SELECT a, count(*) FROM t GROUP BY a
statement ok
create view s4 AS SELECT a, count(*) FROM t GROUP BY a HAVING a > (SELECT count(*) FROM t)
statement ok
DROP VIEW s4
statement ok
DROP VIEW s3
statement ok
DROP VIEW s2
statement ok
DROP VIEW s1
statement ok
DROP TABLE t
# Check for memory leak (#10466)
statement ok
CREATE VIEW foo AS SELECT catalog_name, schema_name, sql_path FROM information_schema.schemata
statement error pq: relation "test.public.foo" already exists
CREATE VIEW foo AS SELECT catalog_name, schema_name, sql_path FROM information_schema.schemata
# Ensure views work with dates/timestamps (#12420)
statement ok
CREATE TABLE t (d DATE, t TIMESTAMP)
statement ok
CREATE VIEW dt AS SELECT d, t FROM t WHERE d > DATE '1988-11-12' AND t < TIMESTAMP '2017-01-01'
statement ok
SELECT * FROM dt
statement ok
CREATE VIEW dt2 AS SELECT d, t FROM t WHERE d > d + INTERVAL '10h'
statement ok
SELECT * FROM dt2
# Ensure that creating a view doesn't leak any session-level settings that
# could affect subsequent AS OF SYSTEM TIME queries (#13547).
statement ok
CREATE VIEW v AS SELECT d, t FROM t
statement error pq: AS OF SYSTEM TIME must be provided on a top-level statement
CREATE TABLE t2 AS SELECT d, t FROM t AS OF SYSTEM TIME '2017-02-13 21:30:00'
statement ok
DROP TABLE t CASCADE
statement ok
CREATE TABLE t (a INT[])
statement ok
INSERT INTO t VALUES (array[1,2,3])
statement ok
CREATE VIEW b AS SELECT a[1] FROM t
query I
SELECT * FROM b
----
1
statement ok
DROP TABLE t CASCADE
statement ok
CREATE VIEW arr(a) AS SELECT ARRAY[3]
query TI
SELECT *, a[1] FROM arr
----
{3} 3
# Regression for #15951
statement ok
CREATE TABLE t15951 (a int, b int)
statement ok
CREATE VIEW Caps15951 AS SELECT a, b FROM t15951
statement ok
INSERT INTO t15951 VALUES (1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)
query R
SELECT sum (Caps15951. a) FROM Caps15951 GROUP BY b ORDER BY b
----
1
3
6
query R
SELECT sum ("caps15951". a) FROM "caps15951" GROUP BY b ORDER BY b
----
1
3
6
statement ok
CREATE VIEW "QuotedCaps15951" AS SELECT a, b FROM t15951
query R
SELECT sum ("QuotedCaps15951". a) FROM "QuotedCaps15951" GROUP BY b ORDER BY b
----
1
3
6
# Regression tests for #23833
statement ok
CREATE VIEW w AS WITH a AS (SELECT 1 AS x) SELECT x FROM a
query T
SELECT create_statement FROM [SHOW CREATE w]
----
CREATE VIEW public.w (
x
) AS WITH a AS (SELECT 1 AS x) SELECT x FROM a
statement ok
CREATE VIEW w2 AS WITH t AS (SELECT x FROM w) SELECT x FROM t
query T
SELECT create_statement FROM [SHOW CREATE w2]
----
CREATE VIEW public.w2 (
x
) AS WITH t AS (SELECT x FROM test.public.w) SELECT x FROM t
statement ok
CREATE VIEW w3 AS (WITH t AS (SELECT x FROM w) SELECT x FROM t)
query T
SELECT create_statement FROM [SHOW CREATE w3]
----
CREATE VIEW public.w3 (
x
) AS (WITH t AS (SELECT x FROM test.public.w) SELECT x FROM t)
statement ok
CREATE TABLE ab (a INT PRIMARY KEY, b INT)
statement error INSERT cannot be used inside a view definition
CREATE VIEW crud_view AS SELECT a, b FROM [INSERT INTO ab VALUES (100, 100) RETURNING a, b]
statement ok
CREATE TABLE cd (c INT PRIMARY KEY, b INT)
statement ok
INSERT INTO ab VALUES (1, 1), (2, 2), (3, 3)
statement ok
INSERT INTO cd VALUES (2, 2), (3, 3), (4, 4)
# View containing a correlated subquery.
statement ok
CREATE VIEW v1 AS SELECT a, b, EXISTS(SELECT c FROM cd WHERE cd.c=ab.a) FROM ab;
query IIB rowsort
SELECT * FROM v1
----
1 1 false
2 2 true
3 3 true
# Regression test for #47704: the columns inside PARITION BY and ORDER BY were
# losing their qualification.
statement ok
CREATE TABLE a47704 (foo UUID);
CREATE TABLE b47704 (foo UUID)
statement ok
CREATE VIEW v47704 AS
SELECT first_value(a47704.foo) OVER (PARTITION BY a47704.foo ORDER BY a47704.foo)
FROM a47704 JOIN b47704 ON a47704.foo = b47704.foo
# Verify that the descriptor did not "lose" the column qualification inside
# PARITION BY and ORDER BY.
query T
SELECT create_statement FROM [ SHOW CREATE VIEW v47704 ]
----
CREATE VIEW public.v47704 (
first_value
) AS SELECT
first_value(a47704.foo) OVER (PARTITION BY a47704.foo ORDER BY a47704.foo)
FROM
test.public.a47704 JOIN test.public.b47704 ON a47704.foo = b47704.foo
statement ok
SELECT * FROM v47704
subtest create_or_replace
user root
statement ok
DROP TABLE IF EXISTS t, t2;
CREATE TABLE t (x INT);
INSERT INTO t VALUES (1), (2);
CREATE TABLE t2 (x INT);
INSERT INTO t2 VALUES (3), (4);
# Test some error cases.
statement error pq: \"t\" is not a view
CREATE OR REPLACE VIEW t AS VALUES (1)
statement ok
CREATE OR REPLACE VIEW tview AS SELECT x AS x, x+1 AS x1, x+2 AS x2 FROM t
# Test cases where new columns don't line up.
statement error pq: cannot drop columns from view
CREATE OR REPLACE VIEW tview AS SELECT x AS x, x+1 AS x1 FROM t
statement error pq: cannot change name of view column \"x\" to \"xy\"
CREATE OR REPLACE VIEW tview AS SELECT x AS xy, x+1 AS x1, x+2 AS x2 FROM t
statement error pq: cannot change type of view column "x1" from int to string
CREATE OR REPLACE VIEW tview AS SELECT x AS x, (x+1)::STRING AS x1, x+2 AS x2 FROM t
statement ok
CREATE OR REPLACE VIEW tview AS SELECT x AS x, x+1 AS x1, x+2 AS x2, x+3 AS x3 FROM t
query IIII rowsort
SELECT * FROM tview
----
1 2 3 4
2 3 4 5
# Test cases where back references get updated.
statement ok
CREATE OR REPLACE VIEW tview AS SELECT x AS x, x+1 AS x1, x+2 AS x2, x+3 AS x3 FROM t2
query IIII rowsort
SELECT * FROM tview
----
3 4 5 6
4 5 6 7
# After remaking tview, it no longer depends on t.
statement ok
DROP TABLE t
# However, we now depend on t2.
statement error cannot drop relation "t2" because view "tview" depends on it
DROP TABLE t2
# Test that if we add a reference to something in t2 and use it when replacing
# the view that we now reference that object as well.
statement ok
CREATE INDEX i ON t2 (x);
CREATE INDEX i2 ON t2 (x);
statement ok
CREATE OR REPLACE VIEW tview AS SELECT x AS x, x+1 AS x1, x+2 AS x2, x+3 AS x3 FROM t2@i
statement error pq: cannot drop index \"i\" because view \"tview\" depends on it
DROP INDEX t2@i
# However, if we change the view, we should be able to drop i.
statement ok
CREATE OR REPLACE VIEW tview AS SELECT x AS x, x+1 AS x1, x+2 AS x2, x+3 AS x3 FROM t2@i2;
DROP INDEX t2@i
# ... and not i2.
statement error pq: cannot drop index \"i2\" because view \"tview\" depends on it
DROP INDEX t2@i2
# Ensure that users can't replace views they don't have privilege to.
statement ok
GRANT CREATE ON DATABASE test TO testuser;
GRANT CREATE, SELECT ON TABLE tview, t2 TO testuser
user testuser
statement error pq: user testuser does not have DROP privilege on relation tview
CREATE OR REPLACE VIEW tview AS SELECT x AS x, x+1 AS x1, x+2 AS x2, x+3 AS x3 FROM t2
# Give privilege now.
user root
statement ok
GRANT DROP ON TABLE tview TO testuser
user testuser
statement ok
CREATE OR REPLACE VIEW tview AS SELECT x AS x, x+1 AS x1, x+2 AS x2, x+3 AS x3 FROM t2
user root
# Ensure a view that contains a table that is referenced multiple times with
# different column sets depends on the correct columns.
# Depended on columns should not be droppable.
# Only column a should be depended on in this case.
statement ok
DROP TABLE ab CASCADE;
statement ok
CREATE TABLE ab (a INT, b INT);
CREATE VIEW vab (x) AS SELECT ab.a FROM ab, ab AS ab2
statement ok
ALTER TABLE ab DROP COLUMN b
statement error pq: cannot drop column "a" because view "vab" depends on it
ALTER TABLE ab DROP COLUMN a
statement ok
CREATE TABLE abc (a INT, b INT, c INT);
CREATE VIEW vabc AS SELECT abc.a, abc2.b, abc3.c FROM abc, abc AS abc2, abc AS abc3
# All three columns a,b,c should not be droppable.
statement error pq: cannot drop column "a" because view "vabc" depends on it
ALTER TABLE abc DROP COLUMN a
statement error pq: cannot drop column "b" because view "vabc" depends on it
ALTER TABLE abc DROP COLUMN b
statement error pq: cannot drop column "c" because view "vabc" depends on it
ALTER TABLE abc DROP COLUMN c
# RegClass objects used in expressions should be added to view dependencies.
statement ok
CREATE TABLE toreg();
CREATE VIEW vregclass AS SELECT 'toreg'::regclass
statement error pq: cannot drop relation "toreg" because view "vregclass" depends on it
DROP TABLE toreg;
statement ok
DROP VIEW vregclass;
statement ok
CREATE VIEW vregclass AS SELECT x FROM (SELECT CAST('toreg' AS regclass) AS x)
statement error pq: cannot drop relation "toreg" because view "vregclass" depends on it
DROP TABLE toreg;
statement ok
DROP VIEW vregclass;
statement ok
CREATE SEQUENCE s_reg;
CREATE VIEW vregclass AS SELECT x FROM [SELECT 's_reg'::REGCLASS AS x]
statement error pq: cannot drop sequence s_reg because other objects depend on it
DROP SEQUENCE s_reg
# RegClass dependencies should not be added if the RegClass expression contains
# a variable.
statement ok
DROP VIEW vregclass;
statement ok
CREATE VIEW vregclass AS SELECT x::regclass FROM (SELECT 's_reg' AS x);
DROP SEQUENCE s_reg;
statement ok
DROP VIEW vregclass;
statement ok
CREATE VIEW vregclass AS SELECT x::regclass FROM (SELECT 'does_not_exist' AS x);
statement error pq: relation "does_not_exist" does not exist
SELECT * FROM vregclass
statement ok
DROP VIEW vregclass;
statement ok
CREATE table tregclass();
statement ok
CREATE VIEW vregclass AS SELECT 1 FROM (SELECT 1) AS foo WHERE 'tregclass'::regclass = 'tregclass'::regclass;
statement error pq: cannot drop relation "tregclass" because view "vregclass" depends on it
DROP TABLE tregclass
# Test that we disallow cross-database references from views, depending on the
# cluster setting.
subtest cross_db_views
statement ok
SET CLUSTER SETTING sql.cross_db_views.enabled = FALSE
statement ok
CREATE DATABASE db1
statement ok
CREATE DATABASE db2
statement ok
USE db1
statement ok
CREATE TABLE ab (a INT, b INT)
statement ok
CREATE VIEW v1 AS SELECT a+b FROM ab
statement ok
CREATE VIEW db1.public.v2 AS SELECT a+b FROM db1.public.ab
statement error the view cannot refer to other databases
CREATE VIEW db2.v AS SELECT a+b FROM db1.public.ab
statement ok
CREATE VIEW db2.replace AS SELECT 1
statement error the view cannot refer to other databases
CREATE OR REPLACE VIEW db2.replace AS SELECT a+b FROM db1.public.ab
statement ok
CREATE SEQUENCE db2.seq
statement error the view cannot refer to other databases
CREATE VIEW v2 AS SELECT last_value FROM db2.seq
# Verify that cross-schema views are allowed.
statement ok
CREATE SCHEMA sc2
statement ok
CREATE VIEW db1.sc2.v AS SELECT a+b FROM db1.public.ab
statement ok
CREATE TABLE db1.sc2.cd (c INT, d INT)
statement ok
CREATE VIEW db1.public.v3 AS SELECT a+b+c+d FROM db1.public.ab, db1.sc2.cd
# Verify that views involving system tables are allowed.
statement ok
CREATE VIEW sys AS SELECT id FROM system.descriptor
statement ok
CREATE VIEW sys2 AS SELECT id, a+b FROM system.descriptor, ab
statement ok
USE db2
statement ok
CREATE TABLE cd (c INT, d INT)
statement error the view cannot refer to other databases
CREATE VIEW v AS SELECT a+b+c+d FROM cd, db1.public.ab
statement ok
SET CLUSTER SETTING sql.cross_db_views.enabled = TRUE
statement ok
CREATE VIEW db2.v1 AS SELECT a+b FROM db1.public.ab
statement ok
CREATE VIEW db2.v2 AS SELECT a+b+c+d FROM cd, db1.public.ab
# Validate that cross DB views are detected by internal tables
statement ok
USE DB1;
statement ok
CREATE SEQUENCE SQ1;
statement ok
CREATE TYPE status AS ENUM ('open', 'closed', 'inactive');
statement ok
CREATE TABLE tval (val int primary key);
statement ok
CREATE VIEW rv as select val from tval;
statement ok;
USE DB2;
# Cross-database type references are not allowed.
statement error cross database type references are not supported: db1.public.status
CREATE VIEW vm as (select cast('open' as db1.status) from db1.tval as t)
# If the view is being created in the same database as the type, that is allowed.
statement ok
CREATE VIEW db1.vm as (select cast('open' as db1.status) from db1.tval as t)
statement ok
CREATE TYPE db2.status AS ENUM ('open', 'closed', 'inactive');
statement ok
CREATE VIEW vm as (select s.last_value, t.val as a, v.val as b, cast('open' as db2.status) from db1.tval as t, db1.sq1 as s, db1.rv as v)
query TTTTTTT
select * from "".crdb_internal.cross_db_references order by object_database, object_schema, object_name, referenced_object_database, referenced_object_schema, referenced_object_name desc;
----
db1 public sys system public descriptor view references table
db1 public sys2 system public descriptor view references table
db2 public v1 db1 public ab view references table
db2 public v2 db1 public ab view references table
db2 public vm db1 public tval view references table
db2 public vm db1 public sq1 view references table
db2 public vm db1 public rv view references table
# Test that user defined types used in views are tracked.
subtest view_user_defined_types
statement ok
CREATE TYPE typ AS ENUM('a')
statement ok
CREATE VIEW v AS (SELECT 'a'::typ::string AS k)
statement error cannot drop type "typ" because other objects \(\[db2.public.v\]\) still depend on it
DROP TYPE typ
statement ok
DROP VIEW v
statement ok
CREATE VIEW v AS (WITH r AS (SELECT 'a'::typ < 'a'::typ AS k) SELECT k FROM r)
statement error cannot drop type "typ" because other objects \(\[db2.public.v\]\) still depend on it
DROP TYPE typ
statement ok
DROP VIEW v
statement ok
CREATE TABLE t (i INT, k STRING AS ('a'::typ::string) STORED)
statement ok
CREATE VIEW v AS (SELECT i FROM t)
# Note that v does not depend on typ since it does not use column k.
statement error cannot drop type "typ" because other objects \(\[db2.public.t\]\) still depend on it
DROP TYPE typ
statement ok
CREATE VIEW v_dep AS (SELECT k FROM t)
# Since v_dep depends on t.k which uses type typ, v_dep has a dependency to typ.
statement error cannot drop type "typ" because other objects \(\[db2.public.t db2.public.v_dep\]\) still depend on it
DROP TYPE typ
statement ok
CREATE TYPE typ2 AS ENUM('a')
statement ok
CREATE VIEW v3 AS (SELECT 'a'::typ2::string AS k)