Skip to content

Commit

Permalink
[#23011] YSQL: Enable ALTER TABLE IF EXISTS t RENAME c1 TO c2
Browse files Browse the repository at this point in the history
Summary:
Enabled YSQL `ALTER TABLE IF EXISTS ... RENAME ... TO ...` syntax.
In fact the needed internal functionality was already implemented in the functions: `renameatt` and `YBCRename`,
but the command was disabled at the grammar level.

The diff also adds several test-cases for the syntax:
* `ALTER TABLE IF EXISTS ...` (new enabled functionality)
* `ALTER TABLE ... RENAME COLUMN ...`
* Plus minor changes for `ALTER TABLE ... RENAME ...`
Jira: DB-11940

Test Plan:
./yb_build.sh --java-test org.yb.pgsql.TestPgRegressFeature#testPgRegressFeature
./yb_build.sh --java-test org.yb.pgsql.TestPgRegressTable#testPgRegressTable

Reviewers: mihnea, fizaa, jason

Reviewed By: jason

Subscribers: jason, yql

Differential Revision: https://phorge.dev.yugabyte.com/D36390
  • Loading branch information
OlegLoginov committed Jul 22, 2024
1 parent 1b9be2e commit 50422f8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/postgres/src/backend/parser/gram.y
Original file line number Diff line number Diff line change
Expand Up @@ -9665,7 +9665,6 @@ RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name
}
| ALTER TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
{
parser_ybc_not_support(@1, "ALTER TABLE IF EXISTS");
RenameStmt *n = makeNode(RenameStmt);
n->renameType = OBJECT_COLUMN;
n->relationType = OBJECT_TABLE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,14 @@ ERROR: constraint "checkb2" for table "atacc1" does not exist
alter table atacc1 drop constraint if exists checkb3;
delete from atacc1 where b = 5;
-- test rename
alter table atacc1 rename b to e;
alter table atacc1 rename b to d; -- should fail: d already exists
ERROR: column "d" of relation "atacc1" already exists
alter table atacc1 rename b to f;
alter table atacc1 rename column f to e;
alter table if exists doesnt_exist_tab rename b to f;
NOTICE: relation "doesnt_exist_tab" does not exist, skipping
alter table if exists doesnt_exist_tab rename column f to e;
NOTICE: relation "doesnt_exist_tab" does not exist, skipping
select * from atacc1;
e | c | d
----+----+----
Expand Down
27 changes: 27 additions & 0 deletions src/postgres/src/test/regress/expected/yb_pg_alter_table.out
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,33 @@ Check constraints:

DROP TABLE constraint_rename_cache;
DROP TABLE like_constraint_rename_cache;
-- test inheritance
create table renameColumn (a int);
create table renameColumnChild (b int) inherits (renameColumn);
ERROR: INHERITS not supported yet
LINE 1: create table renameColumnChild (b int) inherits (renameColum...
^
HINT: See https://github.com/yugabyte/yugabyte-db/issues/1129. React with thumbs up to raise its priority
/* YB: uncomment when INHERITS is supported
create table renameColumnAnother (c int) inherits (renameColumnChild);

-- these three should fail
alter table renameColumnChild rename column a to d;
alter table only renameColumnChild rename column a to d;
alter table only renameColumn rename column a to d;
*/ -- YB
-- these should work
alter table renameColumn rename column a to d;
/* YB: uncomment when INHERITS is supported
alter table renameColumnChild rename column b to a;
*/ -- YB
-- these should work
alter table if exists doesnt_exist_tab rename column a to d;
NOTICE: relation "doesnt_exist_tab" does not exist, skipping
alter table if exists doesnt_exist_tab rename column b to a;
NOTICE: relation "doesnt_exist_tab" does not exist, skipping
-- this should work
alter table renameColumn add column w int;
--
-- lock levels
--
Expand Down
8 changes: 7 additions & 1 deletion src/postgres/src/test/regress/sql/yb_feature_alter_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ alter table atacc1 drop constraint if exists checkb3;
delete from atacc1 where b = 5;

-- test rename
alter table atacc1 rename b to e;
alter table atacc1 rename b to d; -- should fail: d already exists
alter table atacc1 rename b to f;
alter table atacc1 rename column f to e;

alter table if exists doesnt_exist_tab rename b to f;
alter table if exists doesnt_exist_tab rename column f to e;

select * from atacc1;

-- try dropping all columns
Expand Down
27 changes: 27 additions & 0 deletions src/postgres/src/test/regress/sql/yb_pg_alter_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,33 @@ CREATE TABLE like_constraint_rename_cache
DROP TABLE constraint_rename_cache;
DROP TABLE like_constraint_rename_cache;

-- test inheritance

create table renameColumn (a int);
create table renameColumnChild (b int) inherits (renameColumn);
/* YB: uncomment when INHERITS is supported
create table renameColumnAnother (c int) inherits (renameColumnChild);
-- these three should fail
alter table renameColumnChild rename column a to d;
alter table only renameColumnChild rename column a to d;
alter table only renameColumn rename column a to d;
*/ -- YB

-- these should work
alter table renameColumn rename column a to d;
/* YB: uncomment when INHERITS is supported
alter table renameColumnChild rename column b to a;
*/ -- YB

-- these should work
alter table if exists doesnt_exist_tab rename column a to d;
alter table if exists doesnt_exist_tab rename column b to a;

-- this should work
alter table renameColumn add column w int;


--
-- lock levels
--
Expand Down

0 comments on commit 50422f8

Please sign in to comment.