Search in sources :

Example 6 with RedirectionAwareTableHandle

use of io.trino.metadata.RedirectionAwareTableHandle in project trino by trinodb.

the class CommentTask method execute.

@Override
public ListenableFuture<Void> execute(Comment statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Session session = stateMachine.getSession();
    if (statement.getType() == Comment.Type.TABLE) {
        QualifiedObjectName originalTableName = createQualifiedObjectName(session, statement, statement.getName());
        RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, originalTableName);
        if (redirectionAwareTableHandle.getTableHandle().isEmpty()) {
            throw semanticException(TABLE_NOT_FOUND, statement, "Table does not exist: %s", originalTableName);
        }
        accessControl.checkCanSetTableComment(session.toSecurityContext(), redirectionAwareTableHandle.getRedirectedTableName().orElse(originalTableName));
        TableHandle tableHandle = redirectionAwareTableHandle.getTableHandle().get();
        metadata.setTableComment(session, tableHandle, statement.getComment());
    } else if (statement.getType() == Comment.Type.COLUMN) {
        Optional<QualifiedName> prefix = statement.getName().getPrefix();
        if (prefix.isEmpty()) {
            throw semanticException(MISSING_TABLE, statement, "Table must be specified");
        }
        QualifiedObjectName originalTableName = createQualifiedObjectName(session, statement, prefix.get());
        RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, originalTableName);
        if (redirectionAwareTableHandle.getTableHandle().isEmpty()) {
            throw semanticException(TABLE_NOT_FOUND, statement, "Table does not exist: " + originalTableName);
        }
        TableHandle tableHandle = redirectionAwareTableHandle.getTableHandle().get();
        String columnName = statement.getName().getSuffix();
        Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle);
        if (!columnHandles.containsKey(columnName)) {
            throw semanticException(COLUMN_NOT_FOUND, statement, "Column does not exist: " + columnName);
        }
        accessControl.checkCanSetColumnComment(session.toSecurityContext(), redirectionAwareTableHandle.getRedirectedTableName().orElse(originalTableName));
        metadata.setColumnComment(session, tableHandle, columnHandles.get(columnName), statement.getComment());
    } else {
        throw semanticException(NOT_SUPPORTED, statement, "Unsupported comment type: %s", statement.getType());
    }
    return immediateVoidFuture();
}
Also used : Optional(java.util.Optional) TableHandle(io.trino.metadata.TableHandle) RedirectionAwareTableHandle(io.trino.metadata.RedirectionAwareTableHandle) RedirectionAwareTableHandle(io.trino.metadata.RedirectionAwareTableHandle) Map(java.util.Map) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(io.trino.metadata.MetadataUtil.createQualifiedObjectName) Session(io.trino.Session)

Aggregations

Session (io.trino.Session)6 MetadataUtil.createQualifiedObjectName (io.trino.metadata.MetadataUtil.createQualifiedObjectName)6 QualifiedObjectName (io.trino.metadata.QualifiedObjectName)6 RedirectionAwareTableHandle (io.trino.metadata.RedirectionAwareTableHandle)6 TableHandle (io.trino.metadata.TableHandle)5 ColumnHandle (io.trino.spi.connector.ColumnHandle)3 CatalogName (io.trino.connector.CatalogName)2 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)2 Type (io.trino.spi.type.Type)2 TypeNotFoundException (io.trino.spi.type.TypeNotFoundException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 Futures.immediateVoidFuture (com.google.common.util.concurrent.Futures.immediateVoidFuture)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 WarningCollector (io.trino.execution.warnings.WarningCollector)1