From d848c0612667159f805c141678652d903093e72c Mon Sep 17 00:00:00 2001 From: totaj Date: Sat, 16 Sep 2023 15:22:06 +0800 Subject: [PATCH] Support ALTER TABLE ADD COLUMN(col_name type), other cmd. --- contrib/dolphin/expected/test_alter_table.out | 41 +++++++++++++ contrib/dolphin/plugin_parser/gram.y | 60 +++++++++++-------- contrib/dolphin/sql/test_alter_table.sql | 15 +++++ 3 files changed, 91 insertions(+), 25 deletions(-) diff --git a/contrib/dolphin/expected/test_alter_table.out b/contrib/dolphin/expected/test_alter_table.out index 105c24c91..781ae631e 100644 --- a/contrib/dolphin/expected/test_alter_table.out +++ b/contrib/dolphin/expected/test_alter_table.out @@ -270,5 +270,46 @@ Has OIDs: no Options: orientation=row, compression=no drop table test_primary; +--new add column grammar +create table add_col_test(a int); +ALTER TABLE add_col_test ADD KEY f2k(f2), ADD COLUMN(f1 INT, f3 int), ADD COLUMN (f2 INT); +ALTER TABLE if EXISTS add_col_test ADD COLUMN (f11 INT, f31 int), ADD COLUMN (f21 INT), ADD KEY f1k(f1); +ALTER TABLE if EXISTS add_col_test ADD (f12 INT, f32 int), ADD COLUMN (f22 INT); +ALTER TABLE if EXISTS add_col_test ADD COLUMN (f13 INT, f33 int), ADD COLUMN (f23 INT); +ALTER TABLE if EXISTS does_not_exists_table_test ADD COLUMN (f1 INT, f3 int), ADD COLUMN (f2 INT), ADD KEY f2k(f2); +NOTICE: relation "does_not_exists_table_test" does not exist, skipping +--original grammar +ALTER TABLE add_col_test ADD (f14 INT, f34 int); +ALTER TABLE if EXISTS add_col_test ADD (f15 INT, f35 int); +ALTER TABLE if EXISTS does_not_exists_table_test ADD (f13 INT, f33 int); +NOTICE: relation "does_not_exists_table_test" does not exist, skipping +\d+ add_col_test + Table "db_alter_table.add_col_test" + Column | Type | Modifiers | Storage | Stats target | Description +--------+---------+-----------+---------+--------------+------------- + a | integer | | plain | | + f1 | integer | | plain | | + f3 | integer | | plain | | + f2 | integer | | plain | | + f11 | integer | | plain | | + f31 | integer | | plain | | + f21 | integer | | plain | | + f12 | integer | | plain | | + f32 | integer | | plain | | + f22 | integer | | plain | | + f13 | integer | | plain | | + f33 | integer | | plain | | + f23 | integer | | plain | | + f14 | integer | | plain | | + f34 | integer | | plain | | + f15 | integer | | plain | | + f35 | integer | | plain | | +Indexes: + "f1k" btree (f1) TABLESPACE pg_default + "f2k" btree (f2) TABLESPACE pg_default +Has OIDs: no +Options: orientation=row, compression=no + +drop table add_col_test; drop schema db_alter_table cascade; reset current_schema; diff --git a/contrib/dolphin/plugin_parser/gram.y b/contrib/dolphin/plugin_parser/gram.y index f48ae9399..baa6bbe5f 100644 --- a/contrib/dolphin/plugin_parser/gram.y +++ b/contrib/dolphin/plugin_parser/gram.y @@ -4501,16 +4501,6 @@ AlterTableStmt: n->need_rewrite_sql = false; $$ = (Node *)n; } - | ALTER TABLE relation_expr ADD_P '(' add_column_cmds ')' - { - AlterTableStmt *n = makeNode(AlterTableStmt); - n->relation = $3; - n->cmds = $6; - n->relkind = OBJECT_TABLE; - n->missing_ok = false; - n->need_rewrite_sql = false; - $$ = (Node *)n; - } /* REDISANYVALUE key value only used in tsdb redis command, it is used in OM code */ | ALTER TABLE relation_expr REDISANYVALUE { @@ -4535,19 +4525,6 @@ AlterTableStmt: n->need_rewrite_sql = false; $$ = (Node *)n; } - /* - * ALTER TABLE IF_P EXISTS ADD_P '(' add_column_cmds ')' - */ - | ALTER TABLE IF_P EXISTS relation_expr ADD_P '(' add_column_cmds ')' - { - AlterTableStmt *n = makeNode(AlterTableStmt); - n->relation = $5; - n->cmds = $8; - n->relkind = OBJECT_TABLE; - n->missing_ok = true; - n->need_rewrite_sql = false; - $$ = (Node *)n; - } | ALTER TABLE relation_expr alter_table_or_partition { if ($4->length == 1 && ((AlterTableCmd*)lfirst($4->head))->subtype == AT_RebuildAllIndexOnPartition) @@ -4889,8 +4866,27 @@ alter_table_or_partition: /* ALTER TABLE sql clauses for ordinary table */ alter_table_cmds: - alter_table_cmd { $$ = list_make1($1); } - | alter_table_cmds ',' alter_table_cmd { $$ = lappend($1, $3); } + alter_table_cmd + { + /* + * alter_table_cmd is defined as Node* actually, but it may be a list, check ADD_P '(' add_column_cmds ')'. + * we use a simple way to check nodetag by IsA, otherwise we need to change alter_table_cmd's type to List, + * and change all return values in alter_table_cmd as a list, which lead to many code change + */ + if (!IsA($1, List)) { + $$ = list_make1($1); + } else { + $$ = (List*)$1; + } + } + | alter_table_cmds ',' alter_table_cmd + { + if (!IsA($3, List)) { + $$ = lappend($1, $3); + } else { + $$ = list_concat($1, (List*)$3); + } + } ; /* ALTER TABLE PARTITION sql clauses */ @@ -5343,6 +5339,10 @@ reset_partition_cmd: } ; +/* + * Note: alter_table_cmd defined as a Node, but it may return a list, check ADD_P '(' add_column_cmds ')'. + * so please check the return value's nodetag before using it. + */ alter_table_cmd: /*ALTER INDEX index_name UNUSABLE*/ UNUSABLE @@ -5411,6 +5411,16 @@ alter_table_cmd: n->def = $3; $$ = (Node *)n; } + /* ALTER TABLE ADD , add column with bracket does not support first/after */ + | ADD_P '(' add_column_cmds ')' + { + $$ = (Node *)$3; + } + /* ALTER TABLE ADD COLUMN , add column with bracket does not support first/after */ + | ADD_P COLUMN '(' add_column_cmds ')' + { + $$ = (Node *)$4; + } | ADD_P TABLE dolphin_qualified_name { AlterTableCmd *n = makeNode(AlterTableCmd); diff --git a/contrib/dolphin/sql/test_alter_table.sql b/contrib/dolphin/sql/test_alter_table.sql index b989c584e..fd112386d 100644 --- a/contrib/dolphin/sql/test_alter_table.sql +++ b/contrib/dolphin/sql/test_alter_table.sql @@ -134,5 +134,20 @@ create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_p \d+ test_primary drop table test_primary; +--new add column grammar +create table add_col_test(a int); +ALTER TABLE add_col_test ADD KEY f2k(f2), ADD COLUMN(f1 INT, f3 int), ADD COLUMN (f2 INT); +ALTER TABLE if EXISTS add_col_test ADD COLUMN (f11 INT, f31 int), ADD COLUMN (f21 INT), ADD KEY f1k(f1); +ALTER TABLE if EXISTS add_col_test ADD (f12 INT, f32 int), ADD COLUMN (f22 INT); +ALTER TABLE if EXISTS add_col_test ADD COLUMN (f13 INT, f33 int), ADD COLUMN (f23 INT); +ALTER TABLE if EXISTS does_not_exists_table_test ADD COLUMN (f1 INT, f3 int), ADD COLUMN (f2 INT), ADD KEY f2k(f2); + +--original grammar +ALTER TABLE add_col_test ADD (f14 INT, f34 int); +ALTER TABLE if EXISTS add_col_test ADD (f15 INT, f35 int); +ALTER TABLE if EXISTS does_not_exists_table_test ADD (f13 INT, f33 int); +\d+ add_col_test + +drop table add_col_test; drop schema db_alter_table cascade; reset current_schema; -- Gitee