Back to home page

OSCL-LXR

 
 

    


0001 -- Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
0002 --
0003 -- CREATE VIEW
0004 -- https://github.com/postgres/postgres/blob/REL_12_STABLE/src/test/regress/sql/create_view.sql
0005 
0006 -- [SPARK-27764] Support geometric types
0007 -- CREATE VIEW street AS
0008 --    SELECT r.name, r.thepath, c.cname AS cname
0009 --    FROM ONLY road r, real_city c
0010 --    WHERE c.outline ## r.thepath;
0011 
0012 -- [SPARK-27764] Support geometric types
0013 -- CREATE VIEW iexit AS
0014 --    SELECT ih.name, ih.thepath,
0015 --      interpt_pp(ih.thepath, r.thepath) AS exit
0016 --    FROM ihighway ih, ramp r
0017 --    WHERE ih.thepath ## r.thepath;
0018 
0019 CREATE TABLE emp (
0020   name string,
0021   age int,
0022   -- [SPARK-27764] Support geometric types
0023   -- location point
0024   salary int,
0025   manager string
0026 ) USING parquet;
0027 
0028 CREATE VIEW toyemp AS
0029    SELECT name, age, /* location ,*/ 12*salary AS annualsal
0030    FROM emp;
0031 
0032 -- [SPARK-29659] Support COMMENT ON syntax
0033 -- Test comments
0034 -- COMMENT ON VIEW noview IS 'no view';
0035 -- COMMENT ON VIEW toyemp IS 'is a view';
0036 -- COMMENT ON VIEW toyemp IS NULL;
0037 
0038 DROP VIEW toyemp;
0039 DROP TABLE emp;
0040 
0041 -- These views are left around mainly to exercise special cases in pg_dump.
0042 
0043 -- [SPARK-19842] Informational Referential Integrity Constraints Support in Spark
0044 CREATE TABLE view_base_table (key int /* PRIMARY KEY */, data varchar(20)) USING PARQUET;
0045 --
0046 CREATE VIEW key_dependent_view AS
0047    SELECT * FROM view_base_table GROUP BY key;
0048 --
0049 -- [SPARK-19842] Informational Referential Integrity Constraints Support in Spark
0050 -- ALTER TABLE view_base_table DROP CONSTRAINT view_base_table_pkey;  -- fails
0051 
0052 CREATE VIEW key_dependent_view_no_cols AS
0053    SELECT FROM view_base_table GROUP BY key HAVING length(data) > 0;
0054 
0055 --
0056 -- CREATE OR REPLACE VIEW
0057 --
0058 
0059 CREATE TABLE viewtest_tbl (a int, b int) using parquet;
0060 -- [SPARK-29386] Copy data between a file and a table
0061 -- COPY viewtest_tbl FROM stdin;
0062 -- 5    10
0063 -- 10   15
0064 -- 15   20
0065 -- 20   25
0066 -- \.
0067 INSERT INTO viewtest_tbl VALUES (5, 10), (10, 15), (15, 20), (20, 25);
0068 
0069 CREATE OR REPLACE VIEW viewtest AS
0070         SELECT * FROM viewtest_tbl;
0071 
0072 CREATE OR REPLACE VIEW viewtest AS
0073         SELECT * FROM viewtest_tbl WHERE a > 10;
0074 
0075 SELECT * FROM viewtest;
0076 
0077 CREATE OR REPLACE VIEW viewtest AS
0078         SELECT a, b FROM viewtest_tbl WHERE a > 5 ORDER BY b DESC;
0079 
0080 SELECT * FROM viewtest;
0081 
0082 -- should fail
0083 -- [SPARK-29660] Dropping columns and changing column names/types are prohibited in VIEW definition
0084 CREATE OR REPLACE VIEW viewtest AS
0085         SELECT a FROM viewtest_tbl WHERE a <> 20;
0086 
0087 -- should fail
0088 -- [SPARK-29660] Dropping columns and changing column names/types are prohibited in VIEW definition
0089 CREATE OR REPLACE VIEW viewtest AS
0090         SELECT 1, * FROM viewtest_tbl;
0091 
0092 -- should fail
0093 -- [SPARK-29660] Dropping columns and changing column names/types are prohibited in VIEW definition
0094 CREATE OR REPLACE VIEW viewtest AS
0095         SELECT a, decimal(b) FROM viewtest_tbl;
0096 
0097 -- should work
0098 CREATE OR REPLACE VIEW viewtest AS
0099         SELECT a, b, 0 AS c FROM viewtest_tbl;
0100 
0101 DROP VIEW viewtest;
0102 DROP TABLE viewtest_tbl;
0103 
0104 -- tests for temporary views
0105 
0106 -- [SPARK-29661] Support cascaded syntax in CREATE SCHEMA
0107 -- CREATE SCHEMA temp_view_test
0108 --     CREATE TABLE base_table (a int, id int) using parquet
0109 --     CREATE TABLE base_table2 (a int, id int) using parquet;
0110 CREATE SCHEMA temp_view_test;
0111 CREATE TABLE temp_view_test.base_table (a int, id int) using parquet;
0112 CREATE TABLE temp_view_test.base_table2 (a int, id int) using parquet;
0113 
0114 -- Replace SET with USE
0115 -- SET search_path TO temp_view_test, public;
0116 USE temp_view_test;
0117 
0118 -- Since Spark doesn't support CREATE TEMPORARY TABLE, we used CREATE TEMPORARY VIEW instead
0119 -- CREATE TEMPORARY TABLE temp_table (a int, id int);
0120 CREATE TEMPORARY VIEW temp_table AS SELECT * FROM VALUES
0121   (1, 1) as temp_table(a, id);
0122 
0123 -- should be created in temp_view_test schema
0124 CREATE VIEW v1 AS SELECT * FROM base_table;
0125 DESC TABLE EXTENDED v1;
0126 -- should be created in temp object schema
0127 -- [SPARK-29628] Forcibly create a temporary view in CREATE VIEW if referencing a temporary view
0128 CREATE VIEW v1_temp AS SELECT * FROM temp_table;
0129 -- should be created in temp object schema
0130 CREATE TEMP VIEW v2_temp AS SELECT * FROM base_table;
0131 DESC TABLE EXTENDED v2_temp;
0132 -- should be created in temp_views schema
0133 CREATE VIEW temp_view_test.v2 AS SELECT * FROM base_table;
0134 DESC TABLE EXTENDED temp_view_test.v2;
0135 -- should fail
0136 -- [SPARK-29628] Forcibly create a temporary view in CREATE VIEW if referencing a temporary view
0137 CREATE VIEW temp_view_test.v3_temp AS SELECT * FROM temp_table;
0138 -- should fail
0139 -- [SPARK-29661] Support cascaded syntax in CREATE SCHEMA
0140 -- CREATE SCHEMA test_view_schema
0141 --     CREATE TEMP VIEW testview AS SELECT 1;
0142 
0143 -- joins: if any of the join relations are temporary, the view
0144 -- should also be temporary
0145 
0146 -- should be non-temp
0147 CREATE VIEW v3 AS
0148     SELECT t1.a AS t1_a, t2.a AS t2_a
0149     FROM base_table t1, base_table2 t2
0150     WHERE t1.id = t2.id;
0151 DESC TABLE EXTENDED v3;
0152 -- should be temp (one join rel is temp)
0153 -- [SPARK-29628] Forcibly create a temporary view in CREATE VIEW if referencing a temporary view
0154 CREATE VIEW v4_temp AS
0155     SELECT t1.a AS t1_a, t2.a AS t2_a
0156     FROM base_table t1, temp_table t2
0157     WHERE t1.id = t2.id;
0158 -- should be temp
0159 -- [SPARK-29628] Forcibly create a temporary view in CREATE VIEW if referencing a temporary view
0160 CREATE VIEW v5_temp AS
0161     SELECT t1.a AS t1_a, t2.a AS t2_a, t3.a AS t3_a
0162     FROM base_table t1, base_table2 t2, temp_table t3
0163     WHERE t1.id = t2.id and t2.id = t3.id;
0164 
0165 -- subqueries
0166 CREATE VIEW v4 AS SELECT * FROM base_table WHERE id IN (SELECT id FROM base_table2);
0167 DESC TABLE EXTENDED v4;
0168 CREATE VIEW v5 AS SELECT t1.id, t2.a FROM base_table t1, (SELECT * FROM base_table2) t2;
0169 DESC TABLE EXTENDED v5;
0170 CREATE VIEW v6 AS SELECT * FROM base_table WHERE EXISTS (SELECT 1 FROM base_table2);
0171 DESC TABLE EXTENDED v6;
0172 CREATE VIEW v7 AS SELECT * FROM base_table WHERE NOT EXISTS (SELECT 1 FROM base_table2);
0173 DESC TABLE EXTENDED v7;
0174 CREATE VIEW v8 AS SELECT * FROM base_table WHERE EXISTS (SELECT 1);
0175 DESC TABLE EXTENDED v8;
0176 
0177 -- [SPARK-29628] Forcibly create a temporary view in CREATE VIEW if referencing a temporary view
0178 CREATE VIEW v6_temp AS SELECT * FROM base_table WHERE id IN (SELECT id FROM temp_table);
0179 CREATE VIEW v7_temp AS SELECT t1.id, t2.a FROM base_table t1, (SELECT * FROM temp_table) t2;
0180 CREATE VIEW v8_temp AS SELECT * FROM base_table WHERE EXISTS (SELECT 1 FROM temp_table);
0181 CREATE VIEW v9_temp AS SELECT * FROM base_table WHERE NOT EXISTS (SELECT 1 FROM temp_table);
0182 
0183 -- a view should also be temporary if it references a temporary view
0184 -- [SPARK-29628] Forcibly create a temporary view in CREATE VIEW if referencing a temporary view
0185 CREATE VIEW v10_temp AS SELECT * FROM v7_temp;
0186 CREATE VIEW v11_temp AS SELECT t1.id, t2.a FROM base_table t1, v10_temp t2;
0187 CREATE VIEW v12_temp AS SELECT true FROM v11_temp;
0188 
0189 -- [SPARK-27764] Support ANSI SQL CREATE SEQUENCE
0190 -- a view should also be temporary if it references a temporary sequence
0191 -- CREATE SEQUENCE seq1;
0192 -- CREATE TEMPORARY SEQUENCE seq1_temp;
0193 -- CREATE VIEW v9 AS SELECT seq1.is_called FROM seq1;
0194 -- CREATE VIEW v13_temp AS SELECT seq1_temp.is_called FROM seq1_temp;
0195 
0196 -- Skip the tests below because of PostgreSQL specific cases
0197 -- SELECT relname FROM pg_class
0198 --     WHERE relname LIKE 'v_'
0199 --     AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'temp_view_test')
0200 --     ORDER BY relname;
0201 -- SELECT relname FROM pg_class
0202 --     WHERE relname LIKE 'v%'
0203 --     AND relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname LIKE 'pg_temp%')
0204 --     ORDER BY relname;
0205 
0206 CREATE SCHEMA testviewschm2;
0207 -- Replace SET with USE
0208 -- SET search_path TO testviewschm2, public;
0209 USE testviewschm2;
0210 
0211 CREATE TABLE t1 (num int, name string) using parquet;
0212 CREATE TABLE t2 (num2 int, value string) using parquet;
0213 -- Since Spark doesn't support CREATE TEMPORARY TABLE, we used CREATE TEMPORARY VIEW instead
0214 -- CREATE TEMP TABLE tt (num2 int, value string);
0215 CREATE TEMP VIEW tt AS SELECT * FROM VALUES
0216   (1, 'a') AS tt(num2, value);
0217 
0218 CREATE VIEW nontemp1 AS SELECT * FROM t1 CROSS JOIN t2;
0219 DESC TABLE EXTENDED nontemp1;
0220 -- [SPARK-29628] Forcibly create a temporary view in CREATE VIEW if referencing a temporary view
0221 CREATE VIEW temporal1 AS SELECT * FROM t1 CROSS JOIN tt;
0222 CREATE VIEW nontemp2 AS SELECT * FROM t1 INNER JOIN t2 ON t1.num = t2.num2;
0223 DESC TABLE EXTENDED nontemp2;
0224 -- [SPARK-29628] Forcibly create a temporary view in CREATE VIEW if referencing a temporary view
0225 CREATE VIEW temporal2 AS SELECT * FROM t1 INNER JOIN tt ON t1.num = tt.num2;
0226 CREATE VIEW nontemp3 AS SELECT * FROM t1 LEFT JOIN t2 ON t1.num = t2.num2;
0227 DESC TABLE EXTENDED nontemp3;
0228 -- [SPARK-29628] Forcibly create a temporary view in CREATE VIEW if referencing a temporary view
0229 CREATE VIEW temporal3 AS SELECT * FROM t1 LEFT JOIN tt ON t1.num = tt.num2;
0230 CREATE VIEW nontemp4 AS SELECT * FROM t1 LEFT JOIN t2 ON t1.num = t2.num2 AND t2.value = 'xxx';
0231 DESC TABLE EXTENDED nontemp4;
0232 -- [SPARK-29628] Forcibly create a temporary view in CREATE VIEW if referencing a temporary view
0233 CREATE VIEW temporal4 AS SELECT * FROM t1 LEFT JOIN tt ON t1.num = tt.num2 AND tt.value = 'xxx';
0234 CREATE VIEW temporal5 AS SELECT * FROM t1 WHERE num IN (SELECT num FROM t1 WHERE EXISTS (SELECT 1 FROM tt));
0235 
0236 -- Skip the tests below because of PostgreSQL specific cases
0237 -- SELECT relname FROM pg_class
0238 --     WHERE relname LIKE 'nontemp%'
0239 --     AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'testviewschm2')
0240 --     ORDER BY relname;
0241 -- SELECT relname FROM pg_class
0242 --     WHERE relname LIKE 'temporal%'
0243 --     AND relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname LIKE 'pg_temp%')
0244 --     ORDER BY relname;
0245 
0246 CREATE TABLE tbl1 ( a int, b int) using parquet;
0247 CREATE TABLE tbl2 (c int, d int) using parquet;
0248 CREATE TABLE tbl3 (e int, f int) using parquet;
0249 CREATE TABLE tbl4 (g int, h int) using parquet;
0250 -- Since Spark doesn't support CREATE TEMPORARY TABLE, we used CREATE TABLE instead
0251 -- CREATE TEMP TABLE tmptbl (i int, j int);
0252 CREATE TABLE tmptbl (i int, j int) using parquet;
0253 INSERT INTO tmptbl VALUES (1, 1);
0254 
0255 --Should be in testviewschm2
0256 CREATE   VIEW  pubview AS SELECT * FROM tbl1 WHERE tbl1.a
0257 BETWEEN (SELECT d FROM tbl2 WHERE c = 1) AND (SELECT e FROM tbl3 WHERE f = 2)
0258 AND EXISTS (SELECT g FROM tbl4 LEFT JOIN tbl3 ON tbl4.h = tbl3.f);
0259 DESC TABLE EXTENDED pubview;
0260 
0261 -- Skip the test below because of PostgreSQL specific cases
0262 -- SELECT count(*) FROM pg_class where relname = 'pubview'
0263 -- AND relnamespace IN (SELECT OID FROM pg_namespace WHERE nspname = 'testviewschm2');
0264 
0265 --Should be in temp object schema
0266 CREATE   VIEW  mytempview AS SELECT * FROM tbl1 WHERE tbl1.a
0267 BETWEEN (SELECT d FROM tbl2 WHERE c = 1) AND (SELECT e FROM tbl3 WHERE f = 2)
0268 AND EXISTS (SELECT g FROM tbl4 LEFT JOIN tbl3 ON tbl4.h = tbl3.f)
0269 AND NOT EXISTS (SELECT g FROM tbl4 LEFT JOIN tmptbl ON tbl4.h = tmptbl.j);
0270 DESC TABLE EXTENDED mytempview;
0271 
0272 -- Skip the test below because of PostgreSQL specific cases
0273 -- SELECT count(*) FROM pg_class where relname LIKE 'mytempview'
0274 -- And relnamespace IN (SELECT OID FROM pg_namespace WHERE nspname LIKE 'pg_temp%');
0275 
0276 --
0277 -- CREATE VIEW and WITH(...) clause
0278 -- CREATE VIEW mysecview1
0279 --        AS SELECT * FROM tbl1 WHERE a = 0;
0280 --
0281 -- Skip the tests below because Spark doesn't support `WITH options`
0282 -- CREATE VIEW mysecview2 WITH (security_barrier=true)
0283 --        AS SELECT * FROM tbl1 WHERE a > 0;
0284 -- CREATE VIEW mysecview3 WITH (security_barrier=false)
0285 --        AS SELECT * FROM tbl1 WHERE a < 0;
0286 -- CREATE VIEW mysecview4 WITH (security_barrier)
0287 --        AS SELECT * FROM tbl1 WHERE a <> 0;
0288 -- Spark cannot support options in WITH clause
0289 -- CREATE VIEW mysecview5 WITH (security_barrier=100)   -- Error
0290 --        AS SELECT * FROM tbl1 WHERE a > 100;
0291 -- CREATE VIEW mysecview6 WITH (invalid_option)         -- Error
0292 --        AS SELECT * FROM tbl1 WHERE a < 100;
0293 -- Skip the test below because of PostgreSQL specific cases
0294 -- SELECT relname, relkind, reloptions FROM pg_class
0295 --        WHERE oid in ('mysecview1'::regclass, 'mysecview2'::regclass,
0296 --                      'mysecview3'::regclass, 'mysecview4'::regclass)
0297 --        ORDER BY relname;
0298 
0299 -- CREATE OR REPLACE VIEW mysecview1
0300 --        AS SELECT * FROM tbl1 WHERE a = 256;
0301 -- CREATE OR REPLACE VIEW mysecview2
0302 --        AS SELECT * FROM tbl1 WHERE a > 256;
0303 -- CREATE OR REPLACE VIEW mysecview3 WITH (security_barrier=true)
0304 --        AS SELECT * FROM tbl1 WHERE a < 256;
0305 -- CREATE OR REPLACE VIEW mysecview4 WITH (security_barrier=false)
0306 --        AS SELECT * FROM tbl1 WHERE a <> 256;
0307 -- Skip the test below because of PostgreSQL specific cases
0308 -- SELECT relname, relkind, reloptions FROM pg_class
0309 --        WHERE oid in ('mysecview1'::regclass, 'mysecview2'::regclass,
0310 --                      'mysecview3'::regclass, 'mysecview4'::regclass)
0311 --        ORDER BY relname;
0312 
0313 -- Check that unknown literals are converted to "text" in CREATE VIEW,
0314 -- so that we don't end up with unknown-type columns.
0315 
0316 -- Skip the tests below because of PostgreSQL specific cases
0317 -- CREATE VIEW unspecified_types AS
0318 --   SELECT 42 as i, 42.5 as num, 'foo' as u, 'foo'::unknown as u2, null as n;
0319 -- \d+ unspecified_types
0320 -- SELECT * FROM unspecified_types;
0321 
0322 -- This test checks that proper typmods are assigned in a multi-row VALUES
0323 
0324 CREATE VIEW tt1 AS
0325   SELECT * FROM (
0326     VALUES
0327        ('abc', '0123456789', 42, 'abcd'),
0328        ('0123456789', 'abc', 42.12, 'abc')
0329   ) vv(a,b,c,d);
0330 -- Replace the PostgreSQL meta command `\d` with `DESC`
0331 -- \d+ tt1
0332 SELECT * FROM tt1;
0333 SELECT string(a) FROM tt1;
0334 DROP VIEW tt1;
0335 
0336 -- Test view decompilation in the face of relation renaming conflicts
0337 
0338 CREATE TABLE tt1 (f1 int, f2 int, f3 string) using parquet;
0339 CREATE TABLE tx1 (x1 int, x2 int, x3 string) using parquet;
0340 CREATE TABLE temp_view_test.tt1 (y1 int, f2 int, f3 string) using parquet;
0341 
0342 CREATE VIEW aliased_view_1 AS
0343   select * from tt1
0344     where exists (select 1 from tx1 where tt1.f1 = tx1.x1);
0345 CREATE VIEW aliased_view_2 AS
0346   select * from tt1 a1
0347     where exists (select 1 from tx1 where a1.f1 = tx1.x1);
0348 CREATE VIEW aliased_view_3 AS
0349   select * from tt1
0350     where exists (select 1 from tx1 a2 where tt1.f1 = a2.x1);
0351 CREATE VIEW aliased_view_4 AS
0352   select * from temp_view_test.tt1
0353     where exists (select 1 from tt1 where temp_view_test.tt1.y1 = tt1.f1);
0354 
0355 -- Replace the PostgreSQL meta command `\d` with `DESC`
0356 -- \d+ aliased_view_1
0357 DESC TABLE aliased_view_1;
0358 -- \d+ aliased_view_2
0359 DESC TABLE aliased_view_2;
0360 -- \d+ aliased_view_3
0361 DESC TABLE aliased_view_3;
0362 -- \d+ aliased_view_4
0363 DESC TABLE aliased_view_4;
0364 
0365 ALTER TABLE tx1 RENAME TO a1;
0366 
0367 -- Replace the PostgreSQL meta command `\d` with `DESC`
0368 -- \d+ aliased_view_1
0369 DESC TABLE aliased_view_1;
0370 -- \d+ aliased_view_2
0371 DESC TABLE aliased_view_2;
0372 -- \d+ aliased_view_3
0373 DESC TABLE aliased_view_3;
0374 -- \d+ aliased_view_4
0375 DESC TABLE aliased_view_4;
0376 
0377 ALTER TABLE tt1 RENAME TO a2;
0378 
0379 -- Replace the PostgreSQL meta command `\d` with `DESC`
0380 -- \d+ aliased_view_1
0381 DESC TABLE aliased_view_1;
0382 -- \d+ aliased_view_2
0383 DESC TABLE aliased_view_2;
0384 -- \d+ aliased_view_3
0385 DESC TABLE aliased_view_3;
0386 -- \d+ aliased_view_4
0387 DESC TABLE aliased_view_4;
0388 
0389 ALTER TABLE a1 RENAME TO tt1;
0390 
0391 -- Replace the PostgreSQL meta command `\d` with `DESC`
0392 -- \d+ aliased_view_1
0393 DESC TABLE aliased_view_1;
0394 -- \d+ aliased_view_2
0395 DESC TABLE aliased_view_2;
0396 -- \d+ aliased_view_3
0397 DESC TABLE aliased_view_3;
0398 -- \d+ aliased_view_4
0399 DESC TABLE aliased_view_4;
0400 
0401 ALTER TABLE a2 RENAME TO tx1;
0402 -- [SPARK-29632] Support ALTER TABLE [relname] SET SCHEMA [dbname]
0403 -- ALTER TABLE tx1 SET SCHEMA temp_view_test;
0404 
0405 -- \d+ aliased_view_1
0406 -- \d+ aliased_view_2
0407 -- \d+ aliased_view_3
0408 -- \d+ aliased_view_4
0409 
0410 -- [SPARK-29632] Support ALTER TABLE [relname] SET SCHEMA [dbname]
0411 -- ALTER TABLE temp_view_test.tt1 RENAME TO tmp1;
0412 -- ALTER TABLE temp_view_test.tmp1 SET SCHEMA testviewschm2;
0413 -- ALTER TABLE tmp1 RENAME TO tx1;
0414 
0415 -- Replace the PostgreSQL meta command `\d` with `DESC`
0416 -- \d+ aliased_view_1
0417 -- \d+ aliased_view_2
0418 -- \d+ aliased_view_3
0419 -- \d+ aliased_view_4
0420 
0421 -- Test aliasing of joins
0422 
0423 create view view_of_joins as
0424 select * from
0425   (select * from (tbl1 cross join tbl2) same) ss,
0426   (tbl3 cross join tbl4) same;
0427 
0428 -- Replace the PostgreSQL meta command `\d` with `DESC`
0429 -- \d+ view_of_joins
0430 
0431 -- Test view decompilation in the face of column addition/deletion/renaming
0432 
0433 create table tt2 (a int, b int, c int) using parquet;
0434 create table tt3 (ax bigint, b short, c decimal) using parquet;
0435 create table tt4 (ay int, b int, q int) using parquet;
0436 
0437 create view v1 as select * from tt2 natural join tt3;
0438 create view v1a as select * from (tt2 natural join tt3) j;
0439 create view v2 as select * from tt2 join tt3 using (b,c) join tt4 using (b);
0440 create view v2a as select * from (tt2 join tt3 using (b,c) join tt4 using (b)) j;
0441 create view v3 as select * from tt2 join tt3 using (b,c) full join tt4 using (b);
0442 
0443 -- Replace `pg_get_viewdef` with `DESC`
0444 -- select pg_get_viewdef('v1', true);
0445 DESC TABLE v1;
0446 -- select pg_get_viewdef('v1a', true);
0447 DESC TABLE v1a;
0448 -- select pg_get_viewdef('v2', true);
0449 DESC TABLE v2;
0450 -- select pg_get_viewdef('v2a', true);
0451 DESC TABLE v2a;
0452 -- select pg_get_viewdef('v3', true);
0453 DESC TABLE v3;
0454 
0455 alter table tt2 add column d int;
0456 alter table tt2 add column e int;
0457 
0458 -- Replace `pg_get_viewdef` with `DESC`
0459 -- select pg_get_viewdef('v1', true);
0460 DESC TABLE v1;
0461 -- select pg_get_viewdef('v1a', true);
0462 DESC TABLE v1a;
0463 -- select pg_get_viewdef('v2', true);
0464 DESC TABLE v2;
0465 -- select pg_get_viewdef('v2a', true);
0466 DESC TABLE v2a;
0467 -- select pg_get_viewdef('v3', true);
0468 DESC TABLE v3;
0469 
0470 -- [SPARK-27764] Make COLUMN optional in ALTER TABLE
0471 -- [SPARK-27589] Spark file source V2 (For supporting RENAME COLUMN in ALTER TABLE)
0472 -- alter table tt3 rename c to d;
0473 drop table tt3;
0474 create table tt3 (ax bigint, b short, d decimal) using parquet;
0475 
0476 -- select pg_get_viewdef('v1', true);
0477 -- select pg_get_viewdef('v1a', true);
0478 -- select pg_get_viewdef('v2', true);
0479 -- select pg_get_viewdef('v2a', true);
0480 -- select pg_get_viewdef('v3', true);
0481 
0482 alter table tt3 add column c int;
0483 alter table tt3 add column e int;
0484 
0485 -- Replace `pg_get_viewdef` with `DESC`
0486 -- select pg_get_viewdef('v1', true);
0487 DESC TABLE v1;
0488 -- select pg_get_viewdef('v1a', true);
0489 DESC TABLE v1a;
0490 -- select pg_get_viewdef('v2', true);
0491 DESC TABLE v2;
0492 -- select pg_get_viewdef('v2a', true);
0493 DESC TABLE v2a;
0494 -- select pg_get_viewdef('v3', true);
0495 DESC TABLE v3;
0496 
0497 -- [SPARK-27589] Spark file source V2 (For supporting DROP COLUMN in ALTER TABLE)
0498 -- alter table tt2 drop column d;
0499 
0500 -- select pg_get_viewdef('v1', true);
0501 -- select pg_get_viewdef('v1a', true);
0502 -- select pg_get_viewdef('v2', true);
0503 -- select pg_get_viewdef('v2a', true);
0504 -- select pg_get_viewdef('v3', true);
0505 
0506 create table tt5 (a int, b int) using parquet;
0507 create table tt6 (c int, d int) using parquet;
0508 create view vv1 as select * from (tt5 cross join tt6) j(aa,bb,cc,dd);
0509 -- Replace `pg_get_viewdef` with `DESC`
0510 -- select pg_get_viewdef('vv1', true);
0511 DESC TABLE vv1;
0512 alter table tt5 add column c int;
0513 -- select pg_get_viewdef('vv1', true);
0514 DESC TABLE vv1;
0515 alter table tt5 add column cc int;
0516 -- select pg_get_viewdef('vv1', true);
0517 DESC TABLE vv1;
0518 -- [SPARK-27589] Spark file source V2 (For supporting DROP COLUMN in ALTER TABLE)
0519 -- alter table tt5 drop column c;
0520 -- select pg_get_viewdef('vv1', true);
0521 
0522 -- Unnamed FULL JOIN USING is lots of fun too
0523 
0524 -- [SPARK-27589] Spark file source V2 (For supporting DROP COLUMN in ALTER TABLE)
0525 create table tt7 (x int, /* xx int, */ y int) using parquet;
0526 -- alter table tt7 drop column xx;
0527 create table tt8 (x int, z int) using parquet;
0528 
0529 create view vv2 as
0530 select * from (values(1,2,3,4,5)) v(a,b,c,d,e)
0531 union all
0532 select * from tt7 full join tt8 using (x), tt8 tt8x;
0533 
0534 -- Replace `pg_get_viewdef` with `DESC`
0535 -- select pg_get_viewdef('vv2', true);
0536 DESC TABLE vv2;
0537 
0538 create view vv3 as
0539 select * from (values(1,2,3,4,5,6)) v(a,b,c,x,e,f)
0540 union all
0541 select * from
0542   tt7 full join tt8 using (x),
0543   tt7 tt7x full join tt8 tt8x using (x);
0544 
0545 -- Replace `pg_get_viewdef` with `DESC`
0546 -- select pg_get_viewdef('vv3', true);
0547 DESC TABLE vv3;
0548 
0549 create view vv4 as
0550 select * from (values(1,2,3,4,5,6,7)) v(a,b,c,x,e,f,g)
0551 union all
0552 select * from
0553   tt7 full join tt8 using (x),
0554   tt7 tt7x full join tt8 tt8x using (x) full join tt8 tt8y using (x);
0555 
0556 -- Replace `pg_get_viewdef` with `DESC`
0557 -- select pg_get_viewdef('vv4', true);
0558 DESC TABLE vv4;
0559 
0560 alter table tt7 add column zz int;
0561 alter table tt7 add column z int;
0562 -- [SPARK-27589] Spark file source V2 (For supporting DROP COLUMN in ALTER TABLE)
0563 -- alter table tt7 drop column zz;
0564 alter table tt8 add column z2 int;
0565 
0566 -- Replace `pg_get_viewdef` with `DESC`
0567 -- select pg_get_viewdef('vv2', true);
0568 DESC TABLE vv2;
0569 -- select pg_get_viewdef('vv3', true);
0570 DESC TABLE vv3;
0571 -- select pg_get_viewdef('vv4', true);
0572 DESC TABLE vv4;
0573 
0574 -- Implicit coercions in a JOIN USING create issues similar to FULL JOIN
0575 
0576 -- [SPARK-27589] Spark file source V2 (For supporting DROP COLUMN in ALTER TABLE)
0577 create table tt7a (x date, /* xx int, */ y int) using parquet;
0578 -- alter table tt7a drop column xx;
0579 create table tt8a (x timestamp, z int) using parquet;
0580 
0581 -- To pass the query, added exact column names in the select stmt
0582 create view vv2a as
0583 select * from (values(now(),2,3,now(),5)) v(a,b,c,d,e)
0584 union all
0585 select * from tt7a left join tt8a using (x), tt8a tt8ax;
0586 
0587 -- Replace `pg_get_viewdef` with `DESC`
0588 -- select pg_get_viewdef('vv4', true);
0589 DESC TABLE vv4;
0590 -- select pg_get_viewdef('vv2a', true);
0591 DESC TABLE vv2a;
0592 
0593 --
0594 -- Also check dropping a column that existed when the view was made
0595 --
0596 
0597 create table tt9 (x int, xx int, y int) using parquet;
0598 create table tt10 (x int, z int) using parquet;
0599 
0600 create view vv5 as select x,y,z from tt9 join tt10 using(x);
0601 
0602 -- Replace `pg_get_viewdef` with `DESC`
0603 -- select pg_get_viewdef('vv5', true);
0604 DESC TABLE vv5;
0605 
0606 -- [SPARK-27589] Spark file source V2 (For supporting DROP COLUMN in ALTER TABLE)
0607 -- alter table tt9 drop column xx;
0608 
0609 -- Replace `pg_get_viewdef` with `DESC`
0610 -- select pg_get_viewdef('vv5', true);
0611 DESC TABLE vv5;
0612 
0613 --
0614 -- Another corner case is that we might add a column to a table below a
0615 -- JOIN USING, and thereby make the USING column name ambiguous
0616 --
0617 
0618 create table tt11 (x int, y int) using parquet;
0619 create table tt12 (x int, z int) using parquet;
0620 create table tt13 (z int, q int) using parquet;
0621 
0622 create view vv6 as select x,y,z,q from
0623   (tt11 join tt12 using(x)) join tt13 using(z);
0624 
0625 -- Replace `pg_get_viewdef` with `DESC`
0626 -- select pg_get_viewdef('vv6', true);
0627 DESC TABLE vv6;
0628 
0629 alter table tt11 add column z int;
0630 
0631 -- Replace `pg_get_viewdef` with `DESC`
0632 -- select pg_get_viewdef('vv6', true);
0633 DESC TABLE vv6;
0634 
0635 --
0636 -- Check cases involving dropped/altered columns in a function's rowtype result
0637 --
0638 
0639 -- Skip the tests below because Spark does't support PostgreSQL-specific UDFs/transactions
0640 -- create table tt14t (f1 text, f2 text, f3 text, f4 text);
0641 -- insert into tt14t values('foo', 'bar', 'baz', '42');
0642 --
0643 -- alter table tt14t drop column f2;
0644 --
0645 -- create function tt14f() returns setof tt14t as
0646 -- $$
0647 -- declare
0648 --     rec1 record;
0649 -- begin
0650 --     for rec1 in select * from tt14t
0651 --     loop
0652 --         return next rec1;
0653 --     end loop;
0654 -- end;
0655 -- $$
0656 -- language plpgsql;
0657 --
0658 -- create view tt14v as select t.* from tt14f() t;
0659 --
0660 -- select pg_get_viewdef('tt14v', true);
0661 -- select * from tt14v;
0662 --
0663 -- begin;
0664 --
0665 -- -- this perhaps should be rejected, but it isn't:
0666 -- alter table tt14t drop column f3;
0667 --
0668 -- -- f3 is still in the view ...
0669 -- select pg_get_viewdef('tt14v', true);
0670 -- -- but will fail at execution
0671 -- select f1, f4 from tt14v;
0672 -- select * from tt14v;
0673 --
0674 -- rollback;
0675 --
0676 -- begin;
0677 --
0678 -- -- this perhaps should be rejected, but it isn't:
0679 -- alter table tt14t alter column f4 type integer using f4::integer;
0680 --
0681 -- -- f4 is still in the view ...
0682 -- select pg_get_viewdef('tt14v', true);
0683 -- -- but will fail at execution
0684 -- select f1, f3 from tt14v;
0685 -- select * from tt14v;
0686 --
0687 -- rollback;
0688 
0689 -- check display of whole-row variables in some corner cases
0690 
0691 -- Skip the tests below because we do not support creating types
0692 -- create type nestedcomposite as (x int8_tbl);
0693 -- create view tt15v as select row(i)::nestedcomposite from int8_tbl i;
0694 -- select * from tt15v;
0695 -- select pg_get_viewdef('tt15v', true);
0696 -- select row(i.*::int8_tbl)::nestedcomposite from int8_tbl i;
0697 --
0698 -- create view tt16v as select * from int8_tbl i, lateral(values(i)) ss;
0699 -- select * from tt16v;
0700 -- select pg_get_viewdef('tt16v', true);
0701 -- select * from int8_tbl i, lateral(values(i.*::int8_tbl)) ss;
0702 --
0703 -- create view tt17v as select * from int8_tbl i where i in (values(i));
0704 -- select * from tt17v;
0705 -- select pg_get_viewdef('tt17v', true);
0706 -- select * from int8_tbl i where i.* in (values(i.*::int8_tbl));
0707 
0708 -- check unique-ification of overlength names
0709 
0710 CREATE TABLE int8_tbl (q1 int, q2 int) USING parquet;
0711 
0712 create view tt18v as
0713   select * from int8_tbl xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy
0714   union all
0715   select * from int8_tbl xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz;
0716 -- Replace `pg_get_viewdef` with `DESC`
0717 -- select pg_get_viewdef('tt18v', true);
0718 DESC TABLE tt18v;
0719 -- explain (costs off) select * from tt18v;
0720 
0721 -- check display of ScalarArrayOp with a sub-select
0722 
0723 -- Skip the tests below because of PostgreSQL specific cases
0724 -- select 'foo'::text = any(array['abc','def','foo']::text[]);
0725 -- select 'foo'::text = any((select array['abc','def','foo']::text[]));  -- fail
0726 -- select 'foo'::text = any((select array['abc','def','foo']::text[])::text[]);
0727 --
0728 -- create view tt19v as
0729 -- select 'foo'::text = any(array['abc','def','foo']::text[]) c1,
0730 --        'foo'::text = any((select array['abc','def','foo']::text[])::text[]) c2;
0731 -- select pg_get_viewdef('tt19v', true);
0732 
0733 -- check display of assorted RTE_FUNCTION expressions
0734 
0735 -- [SPARK-28682] ANSI SQL: Collation Support
0736 -- create view tt20v as
0737 -- select * from
0738 --   coalesce(1,2) as c,
0739 --   collation for ('x'::text) col,
0740 --   current_date as d,
0741 --   localtimestamp(3) as t,
0742 --   cast(1+2 as int4) as i4,
0743 --   cast(1+2 as int8) as i8;
0744 -- select pg_get_viewdef('tt20v', true);
0745 
0746 -- corner cases with empty join conditions
0747 
0748 create view tt21v as
0749 select * from tt5 natural inner join tt6;
0750 -- Replace `pg_get_viewdef` with `DESC`
0751 -- select pg_get_viewdef('tt21v', true);
0752 DESC TABLE tt21v;
0753 
0754 create view tt22v as
0755 select * from tt5 natural left join tt6;
0756 -- Replace `pg_get_viewdef` with `DESC`
0757 -- select pg_get_viewdef('tt22v', true);
0758 DESC TABLE tt22v;
0759 
0760 -- check handling of views with immediately-renamed columns
0761 
0762 create view tt23v (col_a, col_b) as
0763 select q1 as other_name1, q2 as other_name2 from int8_tbl
0764 union
0765 select 42, 43;
0766 
0767 -- Replace `pg_get_viewdef` with `DESC`
0768 -- select pg_get_viewdef('tt23v', true);
0769 DESC TABLE tt23v;
0770 -- Skip the test below because of PostgreSQL specific cases
0771 -- select pg_get_ruledef(oid, true) from pg_rewrite
0772 --   where ev_class = 'tt23v'::regclass and ev_type = '1';
0773 
0774 -- clean up all the random objects we made above
0775 DROP SCHEMA temp_view_test CASCADE;
0776 DROP SCHEMA testviewschm2 CASCADE;
0777 
0778 DROP VIEW temp_table;
0779 DROP VIEW tt;