-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add support for ALTER VIEW RENAME TO statement #23749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6eb5338
17e5b01
5a61649
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| ========== | ||
| ALTER VIEW | ||
| ========== | ||
|
|
||
| Synopsis | ||
| -------- | ||
|
|
||
| .. code-block:: sql | ||
|
|
||
| ALTER VIEW [IF EXISTS] old_view_name RENAME TO new_view_name; | ||
|
|
||
| Description | ||
| ----------- | ||
|
|
||
| The ``ALTER VIEW [IF EXISTS] RENAME TO`` statement renames an existing view to a | ||
| new name. This allows you to change the name of a view without having to drop | ||
| and recreate it. The view's definition, security settings, and dependencies | ||
| remain unchanged; only the name of the view is updated. | ||
|
|
||
| The optional ``IF EXISTS`` clause prevents an error from being raised if the | ||
| view does not exist. Instead, no action is taken, and a notice is issued. | ||
|
|
||
| Renaming a view does not affect the data or structure of the underlying | ||
| query used to define the view. Any permissions or dependencies on the | ||
| view are retained, and queries or applications using the old name must | ||
| be updated to use the new name. | ||
|
|
||
| Examples | ||
| -------- | ||
|
|
||
| Rename the view ``users`` to ``people``:: | ||
|
|
||
| ALTER VIEW users RENAME TO people; | ||
|
|
||
| Rename the view ``users`` to ``people`` if view ``users`` exists:: | ||
|
|
||
| ALTER VIEW IF EXISTS users RENAME TO people; | ||
|
|
||
| See Also | ||
| -------- | ||
|
|
||
| :doc:`create-view` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,6 +70,7 @@ | |
| import static com.facebook.presto.spi.security.AccessDeniedException.denyRenameColumn; | ||
| import static com.facebook.presto.spi.security.AccessDeniedException.denyRenameSchema; | ||
| import static com.facebook.presto.spi.security.AccessDeniedException.denyRenameTable; | ||
| import static com.facebook.presto.spi.security.AccessDeniedException.denyRenameView; | ||
| import static com.facebook.presto.spi.security.AccessDeniedException.denyRevokeRoles; | ||
| import static com.facebook.presto.spi.security.AccessDeniedException.denyRevokeTablePrivilege; | ||
| import static com.facebook.presto.spi.security.AccessDeniedException.denySelectTable; | ||
|
|
@@ -451,6 +452,24 @@ public void checkCanCreateView(ConnectorTransactionHandle transaction, Connector | |
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void checkCanRenameView(ConnectorTransactionHandle transaction, ConnectorIdentity identity, AccessControlContext context, SchemaTableName viewName, SchemaTableName newViewName) | ||
| { | ||
| MetastoreContext metastoreContext = new MetastoreContext( | ||
| identity, context.getQueryId().getId(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: split these arguments onto two lines
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ZacBlanco - This syntax is used consistently for the other methods in the class. |
||
| context.getClientInfo(), | ||
| context.getClientTags(), | ||
| context.getSource(), | ||
| Optional.empty(), | ||
| false, | ||
| HiveColumnConverterProvider.DEFAULT_COLUMN_CONVERTER_PROVIDER, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: static import
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ZacBlanco - This syntax is used consistently for the other methods in the class. |
||
| context.getWarningCollector(), | ||
| context.getRuntimeStats()); | ||
| if (!isTableOwner(transaction, identity, metastoreContext, viewName)) { | ||
| denyRenameView(viewName.toString(), newViewName.toString()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void checkCanDropView(ConnectorTransactionHandle transaction, ConnectorIdentity identity, AccessControlContext context, SchemaTableName viewName) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,4 +153,25 @@ public void testTableValidation() | |
| assertQuerySucceeds("SELECT * FROM iceberg.test_schema.iceberg_table1"); | ||
| assertQueryFails("SELECT * FROM iceberg.test_schema.hive_table", "Not an Iceberg table: test_schema.hive_table"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRenameView() | ||
| { | ||
| assertQuerySucceeds("CREATE TABLE iceberg.test_schema.iceberg_test_table (_string VARCHAR, _integer INTEGER)"); | ||
| assertUpdate("CREATE VIEW iceberg.test_schema.test_view_to_be_renamed AS SELECT * FROM iceberg.test_schema.iceberg_test_table"); | ||
| assertUpdate("ALTER VIEW IF EXISTS iceberg.test_schema.test_view_to_be_renamed RENAME TO iceberg.test_schema.test_view_renamed"); | ||
dmariamgeorge marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a case without the |
||
| assertUpdate("CREATE VIEW iceberg.test_schema.test_view2_to_be_renamed AS SELECT * FROM iceberg.test_schema.iceberg_test_table"); | ||
| assertUpdate("ALTER VIEW iceberg.test_schema.test_view2_to_be_renamed RENAME TO iceberg.test_schema.test_view2_renamed"); | ||
| assertQuerySucceeds("SELECT * FROM iceberg.test_schema.test_view_renamed"); | ||
| assertQuerySucceeds("SELECT * FROM iceberg.test_schema.test_view2_renamed"); | ||
| assertUpdate("DROP VIEW iceberg.test_schema.test_view_renamed"); | ||
| assertUpdate("DROP VIEW iceberg.test_schema.test_view2_renamed"); | ||
| assertUpdate("DROP TABLE iceberg.test_schema.iceberg_test_table"); | ||
| } | ||
dmariamgeorge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Test | ||
| public void testRenameViewIfNotExists() | ||
| { | ||
| assertQueryFails("ALTER VIEW iceberg.test_schema.test_rename_view_not_exist RENAME TO iceberg.test_schema.test_renamed_view_not_exist", "line 1:1: View 'iceberg.test_schema.test_rename_view_not_exist' does not exist"); | ||
| assertQuerySucceeds("ALTER VIEW IF EXISTS iceberg.test_schema.test_rename_view_not_exist RENAME TO iceberg.test_schema.test_renamed_view_not_exist"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.facebook.presto.execution; | ||
|
|
||
| import com.facebook.presto.Session; | ||
| import com.facebook.presto.common.QualifiedObjectName; | ||
| import com.facebook.presto.metadata.Metadata; | ||
| import com.facebook.presto.spi.WarningCollector; | ||
| import com.facebook.presto.spi.analyzer.ViewDefinition; | ||
| import com.facebook.presto.spi.security.AccessControl; | ||
| import com.facebook.presto.sql.analyzer.SemanticException; | ||
| import com.facebook.presto.sql.tree.Expression; | ||
| import com.facebook.presto.sql.tree.RenameView; | ||
| import com.facebook.presto.transaction.TransactionManager; | ||
| import com.google.common.util.concurrent.ListenableFuture; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import static com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName; | ||
| import static com.facebook.presto.sql.analyzer.SemanticErrorCode.MISSING_CATALOG; | ||
| import static com.facebook.presto.sql.analyzer.SemanticErrorCode.MISSING_VIEW; | ||
| import static com.facebook.presto.sql.analyzer.SemanticErrorCode.NOT_SUPPORTED; | ||
| import static com.facebook.presto.sql.analyzer.SemanticErrorCode.VIEW_ALREADY_EXISTS; | ||
| import static com.google.common.util.concurrent.Futures.immediateFuture; | ||
|
|
||
| public class RenameViewTask | ||
| implements DDLDefinitionTask<RenameView> | ||
| { | ||
| @Override | ||
| public String getName() | ||
| { | ||
| return "RENAME VIEW"; | ||
| } | ||
|
|
||
| public ListenableFuture<?> execute(RenameView statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) | ||
| { | ||
| QualifiedObjectName viewName = createQualifiedObjectName(session, statement, statement.getSource()); | ||
|
|
||
| Optional<ViewDefinition> view = metadata.getMetadataResolver(session).getView(viewName); | ||
| if (!view.isPresent()) { | ||
| if (!statement.isExists()) { | ||
| throw new SemanticException(MISSING_VIEW, statement, "View '%s' does not exist", viewName); | ||
| } | ||
| return immediateFuture(null); | ||
| } | ||
|
|
||
| QualifiedObjectName target = createQualifiedObjectName(session, statement, statement.getTarget()); | ||
| if (!metadata.getCatalogHandle(session, target.getCatalogName()).isPresent()) { | ||
| throw new SemanticException(MISSING_CATALOG, statement, "Target catalog '%s' does not exist", target.getCatalogName()); | ||
| } | ||
| if (metadata.getMetadataResolver(session).getView(target).isPresent()) { | ||
| throw new SemanticException(VIEW_ALREADY_EXISTS, statement, "Target view '%s' already exists", target); | ||
| } | ||
| if (!viewName.getSchemaName().equals(target.getSchemaName())) { | ||
| throw new SemanticException(NOT_SUPPORTED, statement, "View rename across schemas is not supported"); | ||
| } | ||
| if (!viewName.getCatalogName().equals(target.getCatalogName())) { | ||
| throw new SemanticException(NOT_SUPPORTED, statement, "View rename across catalogs is not supported"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We support renaming across schemas though? Does it make sense to rename across schemas? What if the source tables don't exist in the new schema? |
||
| } | ||
|
|
||
| accessControl.checkCanRenameView(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), viewName, target); | ||
|
|
||
| metadata.renameView(session, viewName, target); | ||
|
|
||
| return immediateFuture(null); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.