From 36959a919f4f58eef45588a91a1da80039b129a1 Mon Sep 17 00:00:00 2001 From: yuhuanhuan <1500773557@qq.com> Date: Thu, 22 Sep 2022 11:30:07 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90fixbug=E3=80=91=E5=94=AF=E4=B8=80?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=E7=9A=84=E7=B4=A2=E5=BC=95=E5=90=8D=E4=B8=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81key=E5=85=B3=E9=94=AE=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/backend/parser/gram.y | 9 ++++ src/test/regress/expected/alter_table_000.out | 36 +++++++++++++++ src/test/regress/expected/create_table.out | 46 +++++++++++++++++++ src/test/regress/sql/alter_table_000.sql | 26 ++++++++++- src/test/regress/sql/create_table.sql | 38 +++++++++++++++ 5 files changed, 154 insertions(+), 1 deletion(-) diff --git a/src/common/backend/parser/gram.y b/src/common/backend/parser/gram.y index 0f472b798..132c6c085 100644 --- a/src/common/backend/parser/gram.y +++ b/src/common/backend/parser/gram.y @@ -7298,6 +7298,15 @@ ConstraintElem: errmsg("UNIQUE name is not yet supported in distributed database."))); #endif if (u_sess->attr.attr_sql.sql_compatibility == B_FORMAT) { + if (strcasecmp($2, "index") == 0 || strcasecmp($2, "key") == 0) { + const char* message = "index/key cannot be used as unique name."; + InsertErrorMessage(message, u_sess->plsql_cxt.plpgsql_yylloc); + ereport(errstate, + (errmodule(MOD_PARSER), + errcode(ERRCODE_SYNTAX_ERROR), + errmsg("index/key cannot be used as unique name."), + parser_errposition(@1))); + } Constraint *n = makeNode(Constraint); n->contype = CONSTR_UNIQUE; n->location = @1; diff --git a/src/test/regress/expected/alter_table_000.out b/src/test/regress/expected/alter_table_000.out index 08b7f7aef..3774ae619 100644 --- a/src/test/regress/expected/alter_table_000.out +++ b/src/test/regress/expected/alter_table_000.out @@ -2018,6 +2018,18 @@ Indexes: Has OIDs: no Options: orientation=row, compression=no +drop table test_unique; +-- test unreserved_keyword index and key +create table test_unique(f31 int, f32 varchar(20)); +-- error +alter table test_unique add constraint con_t_unique unique key using btree(f31); +ERROR: index/key cannot be used as unique name. +LINE 1: ...ter table test_unique add constraint con_t_unique unique key... + ^ +alter table test_unique add constraint con_t_unique unique index using btree(f31); +ERROR: index/key cannot be used as unique name. +LINE 1: ...ter table test_unique add constraint con_t_unique unique ind... + ^ drop table test_unique; -- test primary key is only supported in B mode -- alter table @@ -2718,5 +2730,29 @@ Number of partitions: 4 (View pg_partition to check each partition range.) Has OIDs: no Options: orientation=row, compression=no +drop table test_p_unique; +-- test unreserved_keyword index and key +CREATE TABLE test_p_unique +( + f1 INTEGER, + f2 INTEGER, + f3 INTEGER +) +PARTITION BY RANGE(f1) +( + PARTITION P1 VALUES LESS THAN(2450815), + PARTITION P2 VALUES LESS THAN(2451179), + PARTITION P3 VALUES LESS THAN(2451544), + PARTITION P4 VALUES LESS THAN(MAXVALUE) +); +-- error +alter table test_p_unique add constraint con_t_unique unique key using btree(f1); +ERROR: index/key cannot be used as unique name. +LINE 1: ...r table test_p_unique add constraint con_t_unique unique key... + ^ +alter table test_p_unique add constraint con_t_unique unique index using btree(f1); +ERROR: index/key cannot be used as unique name. +LINE 1: ...r table test_p_unique add constraint con_t_unique unique ind... + ^ drop table test_p_unique; \c postgres diff --git a/src/test/regress/expected/create_table.out b/src/test/regress/expected/create_table.out index cc46af915..ecefa1f10 100644 --- a/src/test/regress/expected/create_table.out +++ b/src/test/regress/expected/create_table.out @@ -1427,6 +1427,16 @@ Has OIDs: no Options: orientation=row, compression=no drop table test_unique; +-- test unreserved_keyword index and key +-- error +create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique key using btree(f31)); +ERROR: index/key cannot be used as unique name. +LINE 1: ...f31 int, f32 varchar(20), constraint con_t_unique unique key... + ^ +create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique index using btree(f31)); +ERROR: index/key cannot be used as unique name. +LINE 1: ...f31 int, f32 varchar(20), constraint con_t_unique unique ind... + ^ -- partition table -- test primary key in M mode -- test [index_type] @@ -2057,4 +2067,40 @@ ERROR: partition table does not support expression index \d+ test_p_unique drop table test_p_unique; ERROR: table "test_p_unique" does not exist +-- test unreserved_keyword index and key +-- error +CREATE TABLE test_p_unique +( + f1 INTEGER, + f2 INTEGER, + f3 INTEGER, + constraint con_t_unique unique key using btree(f1) +) +PARTITION BY RANGE(f1) +( + PARTITION P1 VALUES LESS THAN(2450815), + PARTITION P2 VALUES LESS THAN(2451179), + PARTITION P3 VALUES LESS THAN(2451544), + PARTITION P4 VALUES LESS THAN(MAXVALUE) +); +ERROR: index/key cannot be used as unique name. +LINE 6: constraint con_t_unique unique key using btree(f1) + ^ +CREATE TABLE test_p_unique +( + f1 INTEGER, + f2 INTEGER, + f3 INTEGER, + constraint con_t_unique unique index using btree(f1) +) +PARTITION BY RANGE(f1) +( + PARTITION P1 VALUES LESS THAN(2450815), + PARTITION P2 VALUES LESS THAN(2451179), + PARTITION P3 VALUES LESS THAN(2451544), + PARTITION P4 VALUES LESS THAN(MAXVALUE) +); +ERROR: index/key cannot be used as unique name. +LINE 6: constraint con_t_unique unique index using btree(f1) + ^ \c postgres diff --git a/src/test/regress/sql/alter_table_000.sql b/src/test/regress/sql/alter_table_000.sql index f3ac6d76a..ad9aa85e1 100644 --- a/src/test/regress/sql/alter_table_000.sql +++ b/src/test/regress/sql/alter_table_000.sql @@ -890,6 +890,13 @@ alter table test_unique add unique using index idx_unique; \d+ test_unique drop table test_unique; +-- test unreserved_keyword index and key +create table test_unique(f31 int, f32 varchar(20)); +-- error +alter table test_unique add constraint con_t_unique unique key using btree(f31); +alter table test_unique add constraint con_t_unique unique index using btree(f31); +drop table test_unique; + -- test primary key is only supported in B mode -- alter table CREATE TABLE test_p_primary @@ -1302,7 +1309,6 @@ alter table test_p_unique add unique using index idx_unique; \d+ test_p_unique drop table test_p_unique; - CREATE TABLE test_p_unique ( f1 INTEGER, @@ -1321,5 +1327,23 @@ alter table test_p_unique add unique using index idx_unique; \d+ test_p_unique drop table test_p_unique; +-- test unreserved_keyword index and key +CREATE TABLE test_p_unique +( + f1 INTEGER, + f2 INTEGER, + f3 INTEGER +) +PARTITION BY RANGE(f1) +( + PARTITION P1 VALUES LESS THAN(2450815), + PARTITION P2 VALUES LESS THAN(2451179), + PARTITION P3 VALUES LESS THAN(2451544), + PARTITION P4 VALUES LESS THAN(MAXVALUE) +); +-- error +alter table test_p_unique add constraint con_t_unique unique key using btree(f1); +alter table test_p_unique add constraint con_t_unique unique index using btree(f1); +drop table test_p_unique; \c postgres \ No newline at end of file diff --git a/src/test/regress/sql/create_table.sql b/src/test/regress/sql/create_table.sql index e705a0c14..5089c53b2 100644 --- a/src/test/regress/sql/create_table.sql +++ b/src/test/regress/sql/create_table.sql @@ -998,6 +998,11 @@ create table test_unique(f31 int, f32 varchar(20), unique ((f31 * 2 + 1) desc, ( \d+ test_unique drop table test_unique; +-- test unreserved_keyword index and key +-- error +create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique key using btree(f31)); +create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique index using btree(f31)); + -- partition table -- test primary key in M mode -- test [index_type] @@ -1408,4 +1413,37 @@ PARTITION BY RANGE(f1) \d+ test_p_unique drop table test_p_unique; + +-- test unreserved_keyword index and key +-- error +CREATE TABLE test_p_unique +( + f1 INTEGER, + f2 INTEGER, + f3 INTEGER, + constraint con_t_unique unique key using btree(f1) +) +PARTITION BY RANGE(f1) +( + PARTITION P1 VALUES LESS THAN(2450815), + PARTITION P2 VALUES LESS THAN(2451179), + PARTITION P3 VALUES LESS THAN(2451544), + PARTITION P4 VALUES LESS THAN(MAXVALUE) +); + +CREATE TABLE test_p_unique +( + f1 INTEGER, + f2 INTEGER, + f3 INTEGER, + constraint con_t_unique unique index using btree(f1) +) +PARTITION BY RANGE(f1) +( + PARTITION P1 VALUES LESS THAN(2450815), + PARTITION P2 VALUES LESS THAN(2451179), + PARTITION P3 VALUES LESS THAN(2451544), + PARTITION P4 VALUES LESS THAN(MAXVALUE) +); + \c postgres