From 5a45f3aa5690e01e7beabd2b7b67cabbc2ab30d7 Mon Sep 17 00:00:00 2001
From: reshke kirill <reshke@double.cloud>
Date: Tue, 10 Dec 2024 08:39:50 +0000
Subject: [PATCH v8 1/2] Add more regression tests to various DDL patterns
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The main goal of this patch is to increase test coverage of CREATE
DOMAIN, ALTER TABLE ... etc DDL commands.

This is prelimitary patch for enhansing error position reporting feature.

Author: Jian He <jian.universality@gmail.com>
Author: Kirill Reshke <reshkekirill@gmail.com>
Reviewed-By: Michaël Paquier <michael@paquier.xyz>
Reviewed-By: Álvaro Herrera <alvherre@alvh.no-ip.org>

discussion: https://postgr.es/m/CALdSSPhqfvKbDwqJaY=yEePi_aq61GmMpW88i6ZH7CMG_2Z4Cg@mail.gmail.com
---
 src/test/regress/expected/alter_table.out | 11 +++++++
 src/test/regress/expected/domain.out      | 38 +++++++++++++++++++++++
 src/test/regress/expected/float8.out      |  3 ++
 src/test/regress/expected/identity.out    |  2 ++
 src/test/regress/sql/alter_table.sql      |  7 +++++
 src/test/regress/sql/domain.sql           | 21 +++++++++++++
 src/test/regress/sql/float8.sql           |  2 ++
 src/test/regress/sql/identity.sql         |  1 +
 8 files changed, 85 insertions(+)

diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out
index 2212c8dbb59..17802f02e87 100644
--- a/src/test/regress/expected/alter_table.out
+++ b/src/test/regress/expected/alter_table.out
@@ -3112,6 +3112,8 @@ ERROR:  cannot alter type "test_type1" because column "test_tbl1_idx.row" uses i
 DROP TABLE test_tbl1;
 DROP TYPE test_type1;
 CREATE TYPE test_type2 AS (a int, b text);
+CREATE TABLE test_tbl2 OF xx;
+ERROR:  type "xx" does not exist
 CREATE TABLE test_tbl2 OF test_type2;
 CREATE TABLE test_tbl2_subclass () INHERITS (test_tbl2);
 \d test_type2
@@ -3263,6 +3265,8 @@ CREATE TABLE tt5 (x int, y numeric(8,2), z int);	-- too few columns
 CREATE TABLE tt6 () INHERITS (tt0);					-- can't have a parent
 CREATE TABLE tt7 (x int, q text, y numeric(8,2));
 ALTER TABLE tt7 DROP q;								-- OK
+ALTER TABLE tt0 OF tt_t_noexist;
+ERROR:  type "tt_t_noexist" does not exist
 ALTER TABLE tt0 OF tt_t0;
 ALTER TABLE tt1 OF tt_t0;
 ERROR:  table "tt1" has different type for column "y"
@@ -3413,6 +3417,13 @@ ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE int;
 ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE text;
 ALTER TABLE comment_test ALTER COLUMN positive_col SET DATA TYPE int;
 ALTER TABLE comment_test ALTER COLUMN positive_col SET DATA TYPE bigint;
+--test error report position
+ALTER TABLE comment_test ALTER COLUMN xmin SET DATA TYPE x;
+ERROR:  cannot alter system column "xmin"
+ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE x;
+ERROR:  type "x" does not exist
+ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE int collate "C";
+ERROR:  collations are not supported by type integer
 -- Check that the comments are intact.
 SELECT col_description('comment_test'::regclass, 1) as comment;
            comment           
diff --git a/src/test/regress/expected/domain.out b/src/test/regress/expected/domain.out
index 42b6559f9c8..c5cc41cacdc 100644
--- a/src/test/regress/expected/domain.out
+++ b/src/test/regress/expected/domain.out
@@ -15,6 +15,44 @@ NOTICE:  drop cascades to type dependenttypetest
 -- this should fail because already gone
 drop domain domaindroptest cascade;
 ERROR:  type "domaindroptest" does not exist
+-- Test syntax.
+-- create domain error case.
+create domain d_fail as x;
+ERROR:  type "x" does not exist
+create domain d_fail as int constraint cc REFERENCES this_table_not_exists(i);
+ERROR:  foreign key constraints not possible for domains
+create domain d_fail as int4 not null no inherit;
+ERROR:  not-null constraints for domains cannot be marked NO INHERIT
+create domain d_fail as int4 not null null;
+ERROR:  conflicting NULL/NOT NULL constraints
+create domain d_fail as int4 not null default 3 default 3;
+ERROR:  multiple default expressions
+create domain d_fail int4 DEFAULT 3 + 'h';
+ERROR:  invalid input syntax for type integer: "h"
+create domain d_fail int4 collate "C";
+ERROR:  collations are not supported by type integer
+create domain d_fail as anyelement;
+ERROR:  "anyelement" is not a valid base type for a domain
+create domain d_fail as int4 unique;
+ERROR:  unique constraints not possible for domains
+create domain d_fail as int4 PRIMARY key;
+ERROR:  primary key constraints not possible for domains
+create domain d_fail as int4 constraint cc generated by default as identity;
+ERROR:  specifying GENERATED not supported for domains
+create domain d_fail as int4 constraint cc generated always as (2) stored;
+ERROR:  specifying GENERATED not supported for domains
+create domain d_fail as int4 constraint cc check(values > 1) no inherit;
+ERROR:  check constraints for domains cannot be marked NO INHERIT
+create domain d_fail as int4 constraint cc check(values > 1) deferrable;
+ERROR:  specifying constraint deferrability not supported for domains
+create domain d_fail as int4 constraint cc check(values > 1) not deferrable;
+ERROR:  specifying constraint deferrability not supported for domains
+create domain d_fail as int4 constraint cc check (value > 1) initially deferred;
+ERROR:  specifying constraint deferrability not supported for domains
+create domain d_fail as int4 constraint cc check(values > 1) initially immediate;
+ERROR:  specifying constraint deferrability not supported for domains
+create domain d_fail as int4 constraint cc check(values > 1) deferrable not deferrable ;
+ERROR:  specifying constraint deferrability not supported for domains
 -- Test domain input.
 -- Note: the point of checking both INSERT and COPY FROM is that INSERT
 -- exercises CoerceToDomain while COPY exercises domain_in.
diff --git a/src/test/regress/expected/float8.out b/src/test/regress/expected/float8.out
index de56998f5cd..1befc471850 100644
--- a/src/test/regress/expected/float8.out
+++ b/src/test/regress/expected/float8.out
@@ -1024,6 +1024,9 @@ create function xfloat8out(xfloat8) returns cstring immutable strict
 NOTICE:  argument type xfloat8 is only a shell
 LINE 1: create function xfloat8out(xfloat8) returns cstring immutabl...
                                    ^
+--should fail, since type x does not exists.
+create type xfloat8 (input = xfloat8in, output = xfloat8out, like = x);
+ERROR:  type "x" does not exist
 create type xfloat8 (input = xfloat8in, output = xfloat8out, like = float8);
 create cast (xfloat8 as float8) without function;
 create cast (float8 as xfloat8) without function;
diff --git a/src/test/regress/expected/identity.out b/src/test/regress/expected/identity.out
index 2a2b777c89b..0398a19484f 100644
--- a/src/test/regress/expected/identity.out
+++ b/src/test/regress/expected/identity.out
@@ -43,6 +43,8 @@ CREATE TABLE itest4 (a int, b text);
 ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY;  -- error, requires NOT NULL
 ERROR:  column "a" of relation "itest4" must be declared NOT NULL before identity can be added
 ALTER TABLE itest4 ALTER COLUMN a SET NOT NULL;
+ALTER TABLE itest4 ALTER COLUMN c ADD GENERATED ALWAYS AS IDENTITY;  -- error, column c does not exist
+ERROR:  column "c" of relation "itest4" does not exist
 ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY;  -- ok
 ALTER TABLE itest4 ALTER COLUMN a DROP NOT NULL;  -- error, disallowed
 ERROR:  column "a" of relation "itest4" is an identity column
diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/alter_table.sql
index 637e3dac389..67c2f89a47c 100644
--- a/src/test/regress/sql/alter_table.sql
+++ b/src/test/regress/sql/alter_table.sql
@@ -1985,6 +1985,7 @@ DROP TABLE test_tbl1;
 DROP TYPE test_type1;
 
 CREATE TYPE test_type2 AS (a int, b text);
+CREATE TABLE test_tbl2 OF xx;
 CREATE TABLE test_tbl2 OF test_type2;
 CREATE TABLE test_tbl2_subclass () INHERITS (test_tbl2);
 \d test_type2
@@ -2048,6 +2049,7 @@ CREATE TABLE tt6 () INHERITS (tt0);					-- can't have a parent
 CREATE TABLE tt7 (x int, q text, y numeric(8,2));
 ALTER TABLE tt7 DROP q;								-- OK
 
+ALTER TABLE tt0 OF tt_t_noexist;
 ALTER TABLE tt0 OF tt_t0;
 ALTER TABLE tt1 OF tt_t0;
 ALTER TABLE tt2 OF tt_t0;
@@ -2145,6 +2147,11 @@ ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE text;
 ALTER TABLE comment_test ALTER COLUMN positive_col SET DATA TYPE int;
 ALTER TABLE comment_test ALTER COLUMN positive_col SET DATA TYPE bigint;
 
+--test error report position
+ALTER TABLE comment_test ALTER COLUMN xmin SET DATA TYPE x;
+ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE x;
+ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE int collate "C";
+
 -- Check that the comments are intact.
 SELECT col_description('comment_test'::regclass, 1) as comment;
 SELECT indexrelid::regclass::text as index, obj_description(indexrelid, 'pg_class') as comment FROM pg_index where indrelid = 'comment_test'::regclass ORDER BY 1, 2;
diff --git a/src/test/regress/sql/domain.sql b/src/test/regress/sql/domain.sql
index ee07b03174e..5faa56b31e4 100644
--- a/src/test/regress/sql/domain.sql
+++ b/src/test/regress/sql/domain.sql
@@ -16,6 +16,27 @@ drop domain domaindroptest cascade;
 -- this should fail because already gone
 drop domain domaindroptest cascade;
 
+-- Test syntax.
+-- create domain error case.
+create domain d_fail as x;
+create domain d_fail as int constraint cc REFERENCES this_table_not_exists(i);
+create domain d_fail as int4 not null no inherit;
+create domain d_fail as int4 not null null;
+create domain d_fail as int4 not null default 3 default 3;
+create domain d_fail int4 DEFAULT 3 + 'h';
+create domain d_fail int4 collate "C";
+create domain d_fail as anyelement;
+
+create domain d_fail as int4 unique;
+create domain d_fail as int4 PRIMARY key;
+create domain d_fail as int4 constraint cc generated by default as identity;
+create domain d_fail as int4 constraint cc generated always as (2) stored;
+create domain d_fail as int4 constraint cc check(values > 1) no inherit;
+create domain d_fail as int4 constraint cc check(values > 1) deferrable;
+create domain d_fail as int4 constraint cc check(values > 1) not deferrable;
+create domain d_fail as int4 constraint cc check (value > 1) initially deferred;
+create domain d_fail as int4 constraint cc check(values > 1) initially immediate;
+create domain d_fail as int4 constraint cc check(values > 1) deferrable not deferrable ;
 
 -- Test domain input.
 
diff --git a/src/test/regress/sql/float8.sql b/src/test/regress/sql/float8.sql
index 98e9926c9e0..d78cbf5f60d 100644
--- a/src/test/regress/sql/float8.sql
+++ b/src/test/regress/sql/float8.sql
@@ -328,6 +328,8 @@ create function xfloat8in(cstring) returns xfloat8 immutable strict
   language internal as 'int8in';
 create function xfloat8out(xfloat8) returns cstring immutable strict
   language internal as 'int8out';
+--should fail, since type x does not exists.
+create type xfloat8 (input = xfloat8in, output = xfloat8out, like = x);
 create type xfloat8 (input = xfloat8in, output = xfloat8out, like = float8);
 create cast (xfloat8 as float8) without function;
 create cast (float8 as xfloat8) without function;
diff --git a/src/test/regress/sql/identity.sql b/src/test/regress/sql/identity.sql
index cb0e05a2f11..45992a3d894 100644
--- a/src/test/regress/sql/identity.sql
+++ b/src/test/regress/sql/identity.sql
@@ -19,6 +19,7 @@ SELECT pg_get_serial_sequence('itest1', 'a');
 CREATE TABLE itest4 (a int, b text);
 ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY;  -- error, requires NOT NULL
 ALTER TABLE itest4 ALTER COLUMN a SET NOT NULL;
+ALTER TABLE itest4 ALTER COLUMN c ADD GENERATED ALWAYS AS IDENTITY;  -- error, column c does not exist
 ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY;  -- ok
 ALTER TABLE itest4 ALTER COLUMN a DROP NOT NULL;  -- error, disallowed
 ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY;  -- error, already set
-- 
2.34.1

