Skip to content
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

Support renaming materialized views #9492

Merged
merged 3 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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 io.trino.execution;

import com.google.common.util.concurrent.ListenableFuture;
import io.trino.Session;
import io.trino.execution.warnings.WarningCollector;
import io.trino.metadata.Metadata;
import io.trino.metadata.QualifiedObjectName;
import io.trino.metadata.TableHandle;
import io.trino.security.AccessControl;
import io.trino.spi.connector.ConnectorMaterializedViewDefinition;
import io.trino.spi.connector.ConnectorViewDefinition;
import io.trino.sql.tree.Expression;
import io.trino.sql.tree.RenameMaterializedView;
import io.trino.transaction.TransactionManager;

import java.util.List;
import java.util.Optional;

import static com.google.common.util.concurrent.Futures.immediateVoidFuture;
import static io.trino.metadata.MetadataUtil.createQualifiedObjectName;
import static io.trino.spi.StandardErrorCode.CATALOG_NOT_FOUND;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static io.trino.spi.StandardErrorCode.TABLE_ALREADY_EXISTS;
import static io.trino.spi.StandardErrorCode.TABLE_NOT_FOUND;
import static io.trino.sql.analyzer.SemanticExceptions.semanticException;

public class RenameMaterializedViewTask
implements DataDefinitionTask<RenameMaterializedView>
{
@Override
public String getName()
{
return "RENAME MATERIALIZED VIEW";
}

@Override
public ListenableFuture<Void> execute(
RenameMaterializedView statement,
TransactionManager transactionManager,
Metadata metadata,
AccessControl accessControl,
QueryStateMachine stateMachine,
List<Expression> parameters,
WarningCollector warningCollector)
{
Session session = stateMachine.getSession();
QualifiedObjectName materializedViewName = createQualifiedObjectName(session, statement, statement.getSource());
Optional<ConnectorMaterializedViewDefinition> materializedView = metadata.getMaterializedView(session, materializedViewName);
if (materializedView.isEmpty()) {
Optional<ConnectorViewDefinition> view = metadata.getView(session, materializedViewName);
if (view.isPresent()) {
throw semanticException(
TABLE_NOT_FOUND,
statement,
"Materialized View '%s' does not exist, but a view with that name exists. Did you mean ALTER VIEW %s RENAME ...?", materializedViewName, materializedViewName);
}

Optional<TableHandle> table = metadata.getTableHandle(session, materializedViewName);
if (table.isPresent()) {
throw semanticException(
TABLE_NOT_FOUND,
statement,
"Materialized View '%s' does not exist, but a table with that name exists. Did you mean ALTER TABLE %s RENAME ...?", materializedViewName, materializedViewName);
}

if (statement.isExists()) {
return immediateVoidFuture();
}
throw semanticException(TABLE_NOT_FOUND, statement, "Materialized View '%s' does not exist", materializedViewName);
}

QualifiedObjectName target = createQualifiedObjectName(session, statement, statement.getTarget());
if (metadata.getCatalogHandle(session, target.getCatalogName()).isEmpty()) {
throw semanticException(CATALOG_NOT_FOUND, statement, "Target catalog '%s' does not exist", target.getCatalogName());
}
if (metadata.getMaterializedView(session, target).isPresent()) {
sopel39 marked this conversation as resolved.
Show resolved Hide resolved
throw semanticException(TABLE_ALREADY_EXISTS, statement, "Target materialized view '%s' already exists", target);
}
if (metadata.getView(session, target).isPresent()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can still fail when there is a Hive view that needs to be translated. I think we can leave with it

throw semanticException(TABLE_ALREADY_EXISTS, statement, "Target materialized view '%s' does not exist, but a view with that name exists.", target);
}
if (metadata.getTableHandle(session, target).isPresent()) {
throw semanticException(TABLE_ALREADY_EXISTS, statement, "Target materialized view '%s' does not exist, but a table with that name exists.", target);
}
if (!materializedViewName.getCatalogName().equals(target.getCatalogName())) {
throw semanticException(NOT_SUPPORTED, statement, "Materialized View rename across catalogs is not supported");
}

accessControl.checkCanRenameMaterializedView(session.toSecurityContext(), materializedViewName, target);

metadata.renameMaterializedView(session, materializedViewName, target);

return immediateVoidFuture();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ListenableFuture<Void> execute(
throw semanticException(
TABLE_NOT_FOUND,
statement,
"Table '%s' does not exist, but a materialized view with that name exists.", tableName);
sopel39 marked this conversation as resolved.
Show resolved Hide resolved
"Table '%s' does not exist, but a materialized view with that name exists. Did you mean ALTER MATERIALIZED VIEW %s RENAME ...?", tableName, tableName);
}
return immediateVoidFuture();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public ListenableFuture<Void> execute(
throw semanticException(
TABLE_NOT_FOUND,
statement,
"View '%s' does not exist, but a materialized view with that name exists.", viewName);
sopel39 marked this conversation as resolved.
Show resolved Hide resolved
"View '%s' does not exist, but a materialized view with that name exists. Did you mean ALTER MATERIALIZED VIEW %s RENAME ...?", viewName, viewName);
}

Optional<ConnectorViewDefinition> viewDefinition = metadata.getView(session, viewName);
Expand Down
5 changes: 5 additions & 0 deletions core/trino-main/src/main/java/io/trino/metadata/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,11 @@ default ResolvedFunction getCoercion(Type fromType, Type toType)
*/
MaterializedViewFreshness getMaterializedViewFreshness(Session session, QualifiedObjectName name);

/**
* Rename the specified materialized view.
*/
void renameMaterializedView(Session session, QualifiedObjectName existingViewName, QualifiedObjectName newViewName);

/**
* Returns the result of redirecting the table scan on a given table to a different table.
* This method is used by the engine during the plan optimization phase to allow a connector to offload table scans to any other connector.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,19 @@ public MaterializedViewFreshness getMaterializedViewFreshness(Session session, Q
return new MaterializedViewFreshness(false);
}

@Override
public void renameMaterializedView(Session session, QualifiedObjectName source, QualifiedObjectName target)
{
CatalogMetadata catalogMetadata = getCatalogMetadataForWrite(session, target.getCatalogName());
CatalogName catalogName = catalogMetadata.getCatalogName();
ConnectorMetadata metadata = catalogMetadata.getMetadata();
if (!source.getCatalogName().equals(catalogName.getCatalogName())) {
throw new TrinoException(SYNTAX_ERROR, "Cannot rename materialized views across catalogs");
}

metadata.renameMaterializedView(session.toConnectorSession(catalogName), source.asSchemaTableName(), target.asSchemaTableName());
}

@Override
public Optional<TableScanRedirectApplicationResult> applyTableScanRedirect(Session session, TableHandle tableHandle)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,13 @@ default void checkCanSetViewAuthorization(SecurityContext context, QualifiedObje
*/
void checkCanDropMaterializedView(SecurityContext context, QualifiedObjectName materializedViewName);

/**
* Check if identity is allowed to rename the specified materialized view.
*
* @throws AccessDeniedException if not allowed
*/
void checkCanRenameMaterializedView(SecurityContext context, QualifiedObjectName viewName, QualifiedObjectName newViewName);

/**
* Check if identity is allowed to create a view that executes the function.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,20 @@ public void checkCanDropMaterializedView(SecurityContext securityContext, Qualif
catalogAuthorizationCheck(materializedViewName.getCatalogName(), securityContext, (control, context) -> control.checkCanDropMaterializedView(context, materializedViewName.asSchemaTableName()));
}

@Override
public void checkCanRenameMaterializedView(SecurityContext securityContext, QualifiedObjectName viewName, QualifiedObjectName newViewName)
{
requireNonNull(securityContext, "securityContext is null");
requireNonNull(viewName, "viewName is null");
requireNonNull(newViewName, "newViewName is null");

checkCanAccessCatalog(securityContext, viewName.getCatalogName());

systemAuthorizationCheck(control -> control.checkCanRenameMaterializedView(securityContext.toSystemSecurityContext(), viewName.asCatalogSchemaTableName(), newViewName.asCatalogSchemaTableName()));

catalogAuthorizationCheck(viewName.getCatalogName(), securityContext, (control, context) -> control.checkCanRenameMaterializedView(context, viewName.asSchemaTableName(), newViewName.asSchemaTableName()));
}

@Override
public void checkCanGrantExecuteFunctionPrivilege(SecurityContext securityContext, String functionName, Identity grantee, boolean grantOption)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ public void checkCanDropMaterializedView(SecurityContext context, QualifiedObjec
{
}

@Override
public void checkCanRenameMaterializedView(SecurityContext context, QualifiedObjectName viewName, QualifiedObjectName newViewName)
{
}

@Override
public void checkCanGrantExecuteFunctionPrivilege(SecurityContext context, String functionName, Identity grantee, boolean grantOption)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import static io.trino.spi.security.AccessDeniedException.denyReadSystemInformationAccess;
import static io.trino.spi.security.AccessDeniedException.denyRefreshMaterializedView;
import static io.trino.spi.security.AccessDeniedException.denyRenameColumn;
import static io.trino.spi.security.AccessDeniedException.denyRenameMaterializedView;
import static io.trino.spi.security.AccessDeniedException.denyRenameSchema;
import static io.trino.spi.security.AccessDeniedException.denyRenameTable;
import static io.trino.spi.security.AccessDeniedException.denyRenameView;
Expand Down Expand Up @@ -331,6 +332,12 @@ public void checkCanDropMaterializedView(SecurityContext context, QualifiedObjec
denyDropMaterializedView(materializedViewName.toString());
}

@Override
public void checkCanRenameMaterializedView(SecurityContext context, QualifiedObjectName viewName, QualifiedObjectName newViewName)
{
denyRenameMaterializedView(viewName.toString(), newViewName.toString());
}

@Override
public void checkCanGrantExecuteFunctionPrivilege(SecurityContext context, String functionName, Identity grantee, boolean grantOption)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ public void checkCanDropMaterializedView(SecurityContext context, QualifiedObjec
delegate().checkCanDropMaterializedView(context, materializedViewName);
}

@Override
public void checkCanRenameMaterializedView(SecurityContext context, QualifiedObjectName viewName, QualifiedObjectName newViewName)
{
delegate().checkCanRenameMaterializedView(context, viewName, newViewName);
}

@Override
public void checkCanGrantExecuteFunctionPrivilege(SecurityContext context, String functionName, Identity grantee, boolean grantOption)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ public void checkCanDropMaterializedView(ConnectorSecurityContext context, Schem
accessControl.checkCanDropMaterializedView(securityContext, getQualifiedObjectName(materializedViewName));
}

@Override
public void checkCanRenameMaterializedView(ConnectorSecurityContext context, SchemaTableName viewName, SchemaTableName newViewName)
{
checkArgument(context == null, "context must be null");
accessControl.checkCanRenameMaterializedView(securityContext, getQualifiedObjectName(viewName), getQualifiedObjectName(newViewName));
}

@Override
public void checkCanSetCatalogSessionProperty(ConnectorSecurityContext context, String propertyName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.trino.execution.PrepareTask;
import io.trino.execution.QueryExecution.QueryExecutionFactory;
import io.trino.execution.RenameColumnTask;
import io.trino.execution.RenameMaterializedViewTask;
import io.trino.execution.RenameSchemaTask;
import io.trino.execution.RenameTableTask;
import io.trino.execution.RenameViewTask;
Expand Down Expand Up @@ -78,6 +79,7 @@
import io.trino.sql.tree.GrantRoles;
import io.trino.sql.tree.Prepare;
import io.trino.sql.tree.RenameColumn;
import io.trino.sql.tree.RenameMaterializedView;
import io.trino.sql.tree.RenameSchema;
import io.trino.sql.tree.RenameTable;
import io.trino.sql.tree.RenameView;
Expand Down Expand Up @@ -135,6 +137,7 @@ public void configure(Binder binder)
bindDataDefinitionTask(binder, executionBinder, GrantRoles.class, GrantRolesTask.class);
bindDataDefinitionTask(binder, executionBinder, Prepare.class, PrepareTask.class);
bindDataDefinitionTask(binder, executionBinder, RenameColumn.class, RenameColumnTask.class);
bindDataDefinitionTask(binder, executionBinder, RenameMaterializedView.class, RenameMaterializedViewTask.class);
bindDataDefinitionTask(binder, executionBinder, RenameSchema.class, RenameSchemaTask.class);
bindDataDefinitionTask(binder, executionBinder, RenameTable.class, RenameTableTask.class);
bindDataDefinitionTask(binder, executionBinder, RenameView.class, RenameViewTask.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
import io.trino.sql.tree.RefreshMaterializedView;
import io.trino.sql.tree.Relation;
import io.trino.sql.tree.RenameColumn;
import io.trino.sql.tree.RenameMaterializedView;
import io.trino.sql.tree.RenameSchema;
import io.trino.sql.tree.RenameTable;
import io.trino.sql.tree.RenameView;
Expand Down Expand Up @@ -978,6 +979,12 @@ protected Scope visitRenameView(RenameView node, Optional<Scope> scope)
return createAndAssignScope(node, scope);
}

@Override
protected Scope visitRenameMaterializedView(RenameMaterializedView node, Optional<Scope> scope)
{
return createAndAssignScope(node, scope);
}

@Override
protected Scope visitSetViewAuthorization(SetViewAuthorization node, Optional<Scope> scope)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import io.trino.execution.QueryPreparer;
import io.trino.execution.QueryPreparer.PreparedQuery;
import io.trino.execution.RenameColumnTask;
import io.trino.execution.RenameMaterializedViewTask;
import io.trino.execution.RenameTableTask;
import io.trino.execution.RenameViewTask;
import io.trino.execution.ResetSessionTask;
Expand Down Expand Up @@ -175,6 +176,7 @@
import io.trino.sql.tree.DropView;
import io.trino.sql.tree.Prepare;
import io.trino.sql.tree.RenameColumn;
import io.trino.sql.tree.RenameMaterializedView;
import io.trino.sql.tree.RenameTable;
import io.trino.sql.tree.RenameView;
import io.trino.sql.tree.ResetSession;
Expand Down Expand Up @@ -449,6 +451,7 @@ private LocalQueryRunner(
.put(DropTable.class, new DropTableTask())
.put(DropView.class, new DropViewTask())
.put(RenameColumn.class, new RenameColumnTask())
.put(RenameMaterializedView.class, new RenameMaterializedViewTask())
.put(RenameTable.class, new RenameTableTask())
.put(RenameView.class, new RenameViewTask())
.put(Comment.class, new CommentTask())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import static io.trino.spi.security.AccessDeniedException.denyKillQuery;
import static io.trino.spi.security.AccessDeniedException.denyRefreshMaterializedView;
import static io.trino.spi.security.AccessDeniedException.denyRenameColumn;
import static io.trino.spi.security.AccessDeniedException.denyRenameMaterializedView;
import static io.trino.spi.security.AccessDeniedException.denyRenameSchema;
import static io.trino.spi.security.AccessDeniedException.denyRenameTable;
import static io.trino.spi.security.AccessDeniedException.denyRenameView;
Expand Down Expand Up @@ -100,6 +101,7 @@
import static io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.KILL_QUERY;
import static io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.REFRESH_MATERIALIZED_VIEW;
import static io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.RENAME_COLUMN;
import static io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.RENAME_MATERIALIZED_VIEW;
import static io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.RENAME_SCHEMA;
import static io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.RENAME_TABLE;
import static io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.RENAME_VIEW;
Expand Down Expand Up @@ -526,6 +528,17 @@ public void checkCanDropMaterializedView(SecurityContext context, QualifiedObjec
}
}

@Override
public void checkCanRenameMaterializedView(SecurityContext context, QualifiedObjectName viewName, QualifiedObjectName newViewName)
{
if (shouldDenyPrivilege(context.getIdentity().getUser(), viewName.getObjectName(), RENAME_MATERIALIZED_VIEW)) {
denyRenameMaterializedView(viewName.toString(), newViewName.toString());
}
if (denyPrivileges.isEmpty()) {
super.checkCanRenameMaterializedView(context, viewName, newViewName);
}
}

@Override
public void checkCanGrantExecuteFunctionPrivilege(SecurityContext context, String functionName, Identity grantee, boolean grantOption)
{
Expand Down Expand Up @@ -645,7 +658,7 @@ public enum TestingPrivilegeType
SHOW_CREATE_TABLE, CREATE_TABLE, DROP_TABLE, RENAME_TABLE, COMMENT_TABLE, COMMENT_COLUMN, INSERT_TABLE, DELETE_TABLE, UPDATE_TABLE, SHOW_COLUMNS,
ADD_COLUMN, DROP_COLUMN, RENAME_COLUMN, SELECT_COLUMN,
CREATE_VIEW, RENAME_VIEW, DROP_VIEW, CREATE_VIEW_WITH_SELECT_COLUMNS,
CREATE_MATERIALIZED_VIEW, REFRESH_MATERIALIZED_VIEW, DROP_MATERIALIZED_VIEW,
CREATE_MATERIALIZED_VIEW, REFRESH_MATERIALIZED_VIEW, DROP_MATERIALIZED_VIEW, RENAME_MATERIALIZED_VIEW,
GRANT_EXECUTE_FUNCTION,
SET_SESSION
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import io.trino.sql.tree.Query;
import io.trino.sql.tree.RefreshMaterializedView;
import io.trino.sql.tree.RenameColumn;
import io.trino.sql.tree.RenameMaterializedView;
import io.trino.sql.tree.RenameSchema;
import io.trino.sql.tree.RenameTable;
import io.trino.sql.tree.RenameView;
Expand Down Expand Up @@ -141,6 +142,7 @@ private StatementUtils() {}
.put(GrantRoles.class, DATA_DEFINITION)
.put(Prepare.class, DATA_DEFINITION)
.put(RenameColumn.class, DATA_DEFINITION)
.put(RenameMaterializedView.class, DATA_DEFINITION)
.put(RenameSchema.class, DATA_DEFINITION)
.put(RenameTable.class, DATA_DEFINITION)
.put(RenameView.class, DATA_DEFINITION)
Expand Down
Loading