Skip to content

Commit

Permalink
[#10512] YSQL: Adding support for ALTER MATERIALIZED VIEW
Browse files Browse the repository at this point in the history
Summary:
- Alter a MV’s owner:
     `ALTER MATERIALIZED VIEW OWNER TO { new_owner | CURRENT_USER | SESSION_USER }`

- Rename MV:
     `ALTER MATERIALIZED VIEW [ IF EXISTS ] name RENAME TO new_name`

- Rename MV’s column:
     `ALTER MATERIALIZED VIEW [ IF EXISTS ] name RENAME [ COLUMN ] column_name TO new_column_name`

Test Plan: `TestPgRegressMatview`

Reviewers: fizaa, plee

Reviewed By: fizaa, plee

Subscribers: jenkins-bot, yql

Differential Revision: https://phabricator.dev.yugabyte.com/D19508
  • Loading branch information
Rachel Phan committed Sep 29, 2022
1 parent 9cd5390 commit ec9b36b
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 13 deletions.
20 changes: 15 additions & 5 deletions src/postgres/src/backend/commands/ybccmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ YBCPrepareAlterTableCmd(AlterTableCmd* cmd, Relation rel, List *handles,
return handles;
}
}
Oid relationId = RelationGetRelid(rel);
Oid relationId = YbGetStorageRelid(rel);
switch (cmd->subtype)
{
case AT_AddColumn:
Expand Down Expand Up @@ -1252,7 +1252,7 @@ YBCPrepareAlterTableCmd(AlterTableCmd* cmd, Relation rel, List *handles,
HandleYBStatus(
YBCPgNewAlterTable(
YBCGetDatabaseOidByRelid(relationId),
relationId,
YbGetStorageRelid(dependent_rel),
&alter_cmd_handle));
HandleYBStatus(
YBCPgAlterTableIncrementSchemaVersion(alter_cmd_handle));
Expand Down Expand Up @@ -1290,7 +1290,7 @@ YBCPrepareAlterTable(List** subcmds,
List *handles = NIL;
YBCPgStatement db_handle = NULL;
HandleYBStatus(YBCPgNewAlterTable(YBCGetDatabaseOidByRelid(relationId),
relationId,
YbGetStorageRelid(rel),
&db_handle));
handles = lappend(handles, db_handle);
ListCell *lcmd;
Expand Down Expand Up @@ -1334,9 +1334,18 @@ YBCRename(RenameStmt *stmt, Oid relationId)
YBCPgStatement handle = NULL;
Oid databaseId = YBCGetDatabaseOidByRelid(relationId);
char *db_name = get_database_name(databaseId);
Relation rel;

switch (stmt->renameType)
{
case OBJECT_MATVIEW:
rel = RelationIdGetRelation(relationId);
HandleYBStatus(YBCPgNewAlterTable(databaseId,
YbGetStorageRelid(rel),
&handle));
HandleYBStatus(YBCPgAlterTableRenameTable(handle, db_name, stmt->newname));
RelationClose(rel);
break;
case OBJECT_INDEX:
case OBJECT_TABLE:
HandleYBStatus(YBCPgNewAlterTable(databaseId,
Expand All @@ -1347,12 +1356,13 @@ YBCRename(RenameStmt *stmt, Oid relationId)

case OBJECT_COLUMN:
case OBJECT_ATTRIBUTE:

rel = RelationIdGetRelation(relationId);
HandleYBStatus(YBCPgNewAlterTable(databaseId,
relationId,
YbGetStorageRelid(rel),
&handle));

HandleYBStatus(YBCPgAlterTableRenameColumn(handle, stmt->subname, stmt->newname));
RelationClose(rel);
break;

default:
Expand Down
6 changes: 0 additions & 6 deletions src/postgres/src/backend/parser/gram.y
Original file line number Diff line number Diff line change
Expand Up @@ -2054,7 +2054,6 @@ AlterTableStmt:
}
| ALTER MATERIALIZED VIEW qualified_name alter_table_cmds
{
parser_ybc_signal_unsupported(@1, "ALTER MATERIALIZED VIEW", 1131);
AlterTableStmt *n = makeNode(AlterTableStmt);
n->relation = $4;
n->cmds = $5;
Expand All @@ -2064,7 +2063,6 @@ AlterTableStmt:
}
| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name alter_table_cmds
{
parser_ybc_signal_unsupported(@1, "ALTER MATERIALIZED VIEW", 1131);
AlterTableStmt *n = makeNode(AlterTableStmt);
n->relation = $6;
n->cmds = $7;
Expand Down Expand Up @@ -9467,7 +9465,6 @@ RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name
}
| ALTER MATERIALIZED VIEW qualified_name RENAME TO name
{
parser_ybc_signal_unsupported(@1, "ALTER MATERIALIZED VIEW", 1131);
RenameStmt *n = makeNode(RenameStmt);
n->renameType = OBJECT_MATVIEW;
n->relation = $4;
Expand All @@ -9478,7 +9475,6 @@ RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name
}
| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME TO name
{
parser_ybc_signal_unsupported(@1, "ALTER MATERIALIZED VIEW", 1131);
RenameStmt *n = makeNode(RenameStmt);
n->renameType = OBJECT_MATVIEW;
n->relation = $6;
Expand Down Expand Up @@ -9552,7 +9548,6 @@ RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name
}
| ALTER MATERIALIZED VIEW qualified_name RENAME opt_column name TO name
{
parser_ybc_signal_unsupported(@1, "ALTER MATERIALIZED VIEW", 1131);
RenameStmt *n = makeNode(RenameStmt);
n->renameType = OBJECT_COLUMN;
n->relationType = OBJECT_MATVIEW;
Expand All @@ -9564,7 +9559,6 @@ RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name
}
| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME opt_column name TO name
{
parser_ybc_signal_unsupported(@1, "ALTER MATERIALIZED VIEW", 1131);
RenameStmt *n = makeNode(RenameStmt);
n->renameType = OBJECT_COLUMN;
n->relationType = OBJECT_MATVIEW;
Expand Down
73 changes: 72 additions & 1 deletion src/postgres/src/test/regress/expected/yb_matview.out
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,77 @@ SELECT * from pg_temp__123;

DROP TABLE test_yb CASCADE;
NOTICE: drop cascades to materialized view mtest_yb
-- Alter materialized view - rename matview and rename columns
CREATE TABLE test_yb (id int NOT NULL PRIMARY KEY, type text NOT NULL, val numeric NOT NULL);
INSERT INTO test_yb VALUES (1, 'xyz', 2);
CREATE MATERIALIZED VIEW mtest_yb AS SELECT * FROM test_yb;
CREATE UNIQUE INDEX unique_IDX ON mtest_YB(id);
ALTER MATERIALIZED VIEW mtest_yb RENAME TO mtest_yb1;
SELECT * FROM mtest_yb; -- error
ERROR: relation "mtest_yb" does not exist
LINE 1: SELECT * FROM mtest_yb;
^
SELECT * from mtest_yb1; -- ok
id | type | val
----+------+-----
1 | xyz | 2
(1 row)

REFRESH MATERIALIZED VIEW mtest_yb1;
REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb1;
ALTER MATERIALIZED VIEW mtest_yb1 RENAME TO mtest_yb2;
SELECT * from mtest_yb2;
id | type | val
----+------+-----
1 | xyz | 2
(1 row)

REFRESH MATERIALIZED VIEW mtest_yb2;
REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb2;
ALTER MATERIALIZED VIEW mtest_yb2 RENAME val TO total; -- test Alter Rename Column
SELECT * FROM mtest_yb2;
id | type | total
----+------+-------
1 | xyz | 2
(1 row)

DROP TABLE test_yb CASCADE;
NOTICE: drop cascades to materialized view mtest_yb2
-- Alter materialized view - change owner
CREATE TABLE test_yb (id int NOT NULL PRIMARY KEY, type text NOT NULL, val numeric NOT NULL);
INSERT INTO test_yb VALUES (1, 'xyz', 2);
CREATE MATERIALIZED VIEW mtest_yb AS SELECT * FROM test_yb;
CREATE UNIQUE INDEX unique_IDX ON mtest_yb(id);
CREATE ROLE test_mv_user;
SET ROLE test_mv_user;
REFRESH MATERIALIZED VIEW mtest_yb; -- error
ERROR: must be owner of materialized view mtest_yb
REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- error
ERROR: must be owner of materialized view mtest_yb
SET ROLE yugabyte;
ALTER MATERIALIZED VIEW mtest_yb OWNER TO test_mv_user;
REFRESH MATERIALIZED VIEW mtest_yb; -- error
ERROR: permission denied for table test_yb
REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- error
ERROR: permission denied for table test_yb
ALTER TABLE test_yb OWNER TO test_mv_user;
REFRESH MATERIALIZED VIEW mtest_yb; -- ok
REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- ok
ALTER MATERIALIZED VIEW mtest_yb OWNER TO SESSION_USER;
ALTER TABLE test_yb OWNER TO SESSION_USER;
REFRESH MATERIALIZED VIEW mtest_yb; -- ok
REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- ok
ALTER MATERIALIZED VIEW mtest_yb RENAME val TO amt;
ALTER MATERIALIZED VIEW mtest_yb RENAME TO mtest_yb1;
CREATE ROLE test_mv_superuser SUPERUSER;
ALTER MATERIALIZED VIEW mtest_yb1 OWNER TO test_mv_superuser;
REFRESH MATERIALIZED VIEW mtest_yb1; -- ok
REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb1; -- ok
ALTER MATERIALIZED VIEW mtest_yb1 OWNER TO CURRENT_USER;
DROP ROLE test_mv_user;
DROP ROLE test_mv_superuser;
DROP TABLE test_yb CASCADE;
NOTICE: drop cascades to materialized view mtest_yb1
-- Test special characters in an attribute's name
CREATE TABLE test_yb ("xyzID''\\b" int NOT NULL, "y" int);
INSERT INTO test_yb VALUES (1);
Expand Down Expand Up @@ -277,4 +348,4 @@ DROP MATERIALIZED VIEW mv;
SELECT * FROM mv;
ERROR: relation "mv" does not exist
LINE 1: SELECT * FROM mv;
^
^
51 changes: 50 additions & 1 deletion src/postgres/src/test/regress/sql/yb_matview.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,55 @@ INSERT INTO pg_temp__123 values (1);
SELECT * from pg_temp__123;
DROP TABLE test_yb CASCADE;

-- Alter materialized view - rename matview and rename columns
CREATE TABLE test_yb (id int NOT NULL PRIMARY KEY, type text NOT NULL, val numeric NOT NULL);
INSERT INTO test_yb VALUES (1, 'xyz', 2);
CREATE MATERIALIZED VIEW mtest_yb AS SELECT * FROM test_yb;
CREATE UNIQUE INDEX unique_IDX ON mtest_YB(id);
ALTER MATERIALIZED VIEW mtest_yb RENAME TO mtest_yb1;
SELECT * FROM mtest_yb; -- error
SELECT * from mtest_yb1; -- ok
REFRESH MATERIALIZED VIEW mtest_yb1;
REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb1;
ALTER MATERIALIZED VIEW mtest_yb1 RENAME TO mtest_yb2;
SELECT * from mtest_yb2;
REFRESH MATERIALIZED VIEW mtest_yb2;
REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb2;
ALTER MATERIALIZED VIEW mtest_yb2 RENAME val TO total; -- test Alter Rename Column
SELECT * FROM mtest_yb2;
DROP TABLE test_yb CASCADE;

-- Alter materialized view - change owner
CREATE TABLE test_yb (id int NOT NULL PRIMARY KEY, type text NOT NULL, val numeric NOT NULL);
INSERT INTO test_yb VALUES (1, 'xyz', 2);
CREATE MATERIALIZED VIEW mtest_yb AS SELECT * FROM test_yb;
CREATE UNIQUE INDEX unique_IDX ON mtest_yb(id);
CREATE ROLE test_mv_user;
SET ROLE test_mv_user;
REFRESH MATERIALIZED VIEW mtest_yb; -- error
REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- error
SET ROLE yugabyte;
ALTER MATERIALIZED VIEW mtest_yb OWNER TO test_mv_user;
REFRESH MATERIALIZED VIEW mtest_yb; -- error
REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- error
ALTER TABLE test_yb OWNER TO test_mv_user;
REFRESH MATERIALIZED VIEW mtest_yb; -- ok
REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- ok
ALTER MATERIALIZED VIEW mtest_yb OWNER TO SESSION_USER;
ALTER TABLE test_yb OWNER TO SESSION_USER;
REFRESH MATERIALIZED VIEW mtest_yb; -- ok
REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- ok
ALTER MATERIALIZED VIEW mtest_yb RENAME val TO amt;
ALTER MATERIALIZED VIEW mtest_yb RENAME TO mtest_yb1;
CREATE ROLE test_mv_superuser SUPERUSER;
ALTER MATERIALIZED VIEW mtest_yb1 OWNER TO test_mv_superuser;
REFRESH MATERIALIZED VIEW mtest_yb1; -- ok
REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb1; -- ok
ALTER MATERIALIZED VIEW mtest_yb1 OWNER TO CURRENT_USER;
DROP ROLE test_mv_user;
DROP ROLE test_mv_superuser;
DROP TABLE test_yb CASCADE;

-- Test special characters in an attribute's name
CREATE TABLE test_yb ("xyzID''\\b" int NOT NULL, "y" int);
INSERT INTO test_yb VALUES (1);
Expand Down Expand Up @@ -167,4 +216,4 @@ CREATE UNIQUE INDEX ON mv(col);
REFRESH MATERIALIZED VIEW CONCURRENTLY mv;
SELECT * FROM mv ORDER BY col;
DROP MATERIALIZED VIEW mv;
SELECT * FROM mv;
SELECT * FROM mv;

0 comments on commit ec9b36b

Please sign in to comment.