Search in sources :

Example 1 with ConnectorMaterializedViewDefinition

use of com.facebook.presto.spi.ConnectorMaterializedViewDefinition in project presto by prestodb.

the class RenameColumnTask method execute.

@Override
public ListenableFuture<?> execute(RenameColumn statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
    QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getTable());
    Optional<TableHandle> tableHandleOptional = metadata.getTableHandle(session, tableName);
    if (!tableHandleOptional.isPresent()) {
        if (!statement.isTableExists()) {
            throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
        }
        return immediateFuture(null);
    }
    Optional<ConnectorMaterializedViewDefinition> optionalMaterializedView = metadata.getMaterializedView(session, tableName);
    if (optionalMaterializedView.isPresent()) {
        if (!statement.isTableExists()) {
            throw new SemanticException(NOT_SUPPORTED, statement, "'%s' is a materialized view, and rename column is not supported", tableName);
        }
        return immediateFuture(null);
    }
    TableHandle tableHandle = tableHandleOptional.get();
    String source = statement.getSource().getValue().toLowerCase(ENGLISH);
    String target = statement.getTarget().getValue().toLowerCase(ENGLISH);
    accessControl.checkCanRenameColumn(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), tableName);
    Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle);
    ColumnHandle columnHandle = columnHandles.get(source);
    if (columnHandle == null) {
        if (!statement.isColumnExists()) {
            throw new SemanticException(MISSING_COLUMN, statement, "Column '%s' does not exist", source);
        }
        return immediateFuture(null);
    }
    if (columnHandles.containsKey(target)) {
        throw new SemanticException(COLUMN_ALREADY_EXISTS, statement, "Column '%s' already exists", target);
    }
    if (metadata.getColumnMetadata(session, tableHandle, columnHandle).isHidden()) {
        throw new SemanticException(NOT_SUPPORTED, statement, "Cannot rename hidden column");
    }
    metadata.renameColumn(session, tableHandle, columnHandle, target);
    return immediateFuture(null);
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) ConnectorMaterializedViewDefinition(com.facebook.presto.spi.ConnectorMaterializedViewDefinition) TableHandle(com.facebook.presto.spi.TableHandle) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Example 2 with ConnectorMaterializedViewDefinition

use of com.facebook.presto.spi.ConnectorMaterializedViewDefinition in project presto by prestodb.

the class RenameTableTask method execute.

@Override
public ListenableFuture<?> execute(RenameTable statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
    QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getSource());
    Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName);
    if (!tableHandle.isPresent()) {
        if (!statement.isExists()) {
            throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
        }
        return immediateFuture(null);
    }
    Optional<ConnectorMaterializedViewDefinition> optionalMaterializedView = metadata.getMaterializedView(session, tableName);
    if (optionalMaterializedView.isPresent()) {
        if (!statement.isExists()) {
            throw new SemanticException(NOT_SUPPORTED, statement, "'%s' is a materialized view, and rename is not supported", tableName);
        }
        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.getTableHandle(session, target).isPresent()) {
        throw new SemanticException(TABLE_ALREADY_EXISTS, statement, "Target table '%s' already exists", target);
    }
    if (!tableName.getCatalogName().equals(target.getCatalogName())) {
        throw new SemanticException(NOT_SUPPORTED, statement, "Table rename across catalogs is not supported");
    }
    accessControl.checkCanRenameTable(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), tableName, target);
    metadata.renameTable(session, tableHandle.get(), target);
    return immediateFuture(null);
}
Also used : ConnectorMaterializedViewDefinition(com.facebook.presto.spi.ConnectorMaterializedViewDefinition) TableHandle(com.facebook.presto.spi.TableHandle) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Example 3 with ConnectorMaterializedViewDefinition

use of com.facebook.presto.spi.ConnectorMaterializedViewDefinition in project presto by prestodb.

the class DropMaterializedViewTask method execute.

@Override
public ListenableFuture<?> execute(DropMaterializedView statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
    QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName());
    Optional<ConnectorMaterializedViewDefinition> view = metadata.getMaterializedView(session, name);
    if (!view.isPresent()) {
        if (!statement.isExists()) {
            throw new SemanticException(MISSING_MATERIALIZED_VIEW, statement, "Materialized view '%s' does not exist", name);
        }
        return immediateFuture(null);
    }
    accessControl.checkCanDropTable(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), name);
    accessControl.checkCanDropView(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), name);
    metadata.dropMaterializedView(session, name);
    return immediateFuture(null);
}
Also used : ConnectorMaterializedViewDefinition(com.facebook.presto.spi.ConnectorMaterializedViewDefinition) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Example 4 with ConnectorMaterializedViewDefinition

use of com.facebook.presto.spi.ConnectorMaterializedViewDefinition in project presto by prestodb.

the class CreateMaterializedViewTask method execute.

@Override
public ListenableFuture<?> execute(CreateMaterializedView statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
    QualifiedObjectName viewName = createQualifiedObjectName(session, statement, statement.getName());
    Optional<TableHandle> viewHandle = metadata.getTableHandle(session, viewName);
    if (viewHandle.isPresent()) {
        if (!statement.isNotExists()) {
            throw new SemanticException(MATERIALIZED_VIEW_ALREADY_EXISTS, statement, "Materialized view '%s' already exists", viewName);
        }
        return immediateFuture(null);
    }
    accessControl.checkCanCreateTable(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), viewName);
    accessControl.checkCanCreateView(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), viewName);
    Analyzer analyzer = new Analyzer(session, metadata, sqlParser, accessControl, Optional.empty(), parameters, warningCollector);
    Analysis analysis = analyzer.analyze(statement);
    ConnectorId connectorId = metadata.getCatalogHandle(session, viewName.getCatalogName()).orElseThrow(() -> new PrestoException(NOT_FOUND, "Catalog does not exist: " + viewName.getCatalogName()));
    List<ColumnMetadata> columnMetadata = analysis.getOutputDescriptor(statement.getQuery()).getVisibleFields().stream().map(field -> new ColumnMetadata(field.getName().get(), field.getType())).collect(toImmutableList());
    Map<String, Expression> sqlProperties = mapFromProperties(statement.getProperties());
    Map<String, Object> properties = metadata.getTablePropertyManager().getProperties(connectorId, viewName.getCatalogName(), sqlProperties, session, metadata, parameters);
    ConnectorTableMetadata viewMetadata = new ConnectorTableMetadata(toSchemaTableName(viewName), columnMetadata, properties, statement.getComment());
    String sql = getFormattedSql(statement.getQuery(), sqlParser, Optional.of(parameters));
    List<SchemaTableName> baseTables = analysis.getTableNodes().stream().map(table -> {
        QualifiedObjectName tableName = createQualifiedObjectName(session, table, table.getName());
        if (!viewName.getCatalogName().equals(tableName.getCatalogName())) {
            throw new SemanticException(NOT_SUPPORTED, statement, "Materialized view %s created from a base table in a different catalog %s is not supported.", viewName, tableName);
        }
        return toSchemaTableName(tableName);
    }).distinct().collect(toImmutableList());
    MaterializedViewColumnMappingExtractor extractor = new MaterializedViewColumnMappingExtractor(analysis, session);
    ConnectorMaterializedViewDefinition viewDefinition = new ConnectorMaterializedViewDefinition(sql, viewName.getSchemaName(), viewName.getObjectName(), baseTables, Optional.of(session.getUser()), extractor.getMaterializedViewColumnMappings(), extractor.getMaterializedViewDirectColumnMappings(), extractor.getBaseTablesOnOuterJoinSide(), Optional.empty());
    try {
        metadata.createMaterializedView(session, viewName.getCatalogName(), viewMetadata, viewDefinition, statement.isNotExists());
    } catch (PrestoException e) {
        // connectors are not required to handle the ignoreExisting flag
        if (!e.getErrorCode().equals(ALREADY_EXISTS.toErrorCode()) || !statement.isNotExists()) {
            throw e;
        }
    }
    return immediateFuture(null);
}
Also used : WarningCollector(com.facebook.presto.spi.WarningCollector) Analyzer(com.facebook.presto.sql.analyzer.Analyzer) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) NOT_FOUND(com.facebook.presto.spi.StandardErrorCode.NOT_FOUND) PrestoException(com.facebook.presto.spi.PrestoException) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) Inject(javax.inject.Inject) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ALREADY_EXISTS(com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS) NodeUtils.mapFromProperties(com.facebook.presto.sql.NodeUtils.mapFromProperties) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) ConnectorMaterializedViewDefinition(com.facebook.presto.spi.ConnectorMaterializedViewDefinition) TableHandle(com.facebook.presto.spi.TableHandle) TransactionManager(com.facebook.presto.transaction.TransactionManager) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) Futures.immediateFuture(com.google.common.util.concurrent.Futures.immediateFuture) MetadataUtil.toSchemaTableName(com.facebook.presto.metadata.MetadataUtil.toSchemaTableName) Session(com.facebook.presto.Session) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) MaterializedViewColumnMappingExtractor(com.facebook.presto.sql.analyzer.MaterializedViewColumnMappingExtractor) NOT_SUPPORTED(com.facebook.presto.sql.analyzer.SemanticErrorCode.NOT_SUPPORTED) SqlParser(com.facebook.presto.sql.parser.SqlParser) CreateMaterializedView(com.facebook.presto.sql.tree.CreateMaterializedView) SqlFormatterUtil.getFormattedSql(com.facebook.presto.sql.SqlFormatterUtil.getFormattedSql) List(java.util.List) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) Expression(com.facebook.presto.sql.tree.Expression) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) MATERIALIZED_VIEW_ALREADY_EXISTS(com.facebook.presto.sql.analyzer.SemanticErrorCode.MATERIALIZED_VIEW_ALREADY_EXISTS) Analysis(com.facebook.presto.sql.analyzer.Analysis) Optional(java.util.Optional) ConnectorId(com.facebook.presto.spi.ConnectorId) Metadata(com.facebook.presto.metadata.Metadata) AccessControl(com.facebook.presto.security.AccessControl) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ConnectorMaterializedViewDefinition(com.facebook.presto.spi.ConnectorMaterializedViewDefinition) PrestoException(com.facebook.presto.spi.PrestoException) Analyzer(com.facebook.presto.sql.analyzer.Analyzer) SchemaTableName(com.facebook.presto.spi.SchemaTableName) MetadataUtil.toSchemaTableName(com.facebook.presto.metadata.MetadataUtil.toSchemaTableName) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) MaterializedViewColumnMappingExtractor(com.facebook.presto.sql.analyzer.MaterializedViewColumnMappingExtractor) Expression(com.facebook.presto.sql.tree.Expression) Analysis(com.facebook.presto.sql.analyzer.Analysis) TableHandle(com.facebook.presto.spi.TableHandle) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) ConnectorId(com.facebook.presto.spi.ConnectorId)

Example 5 with ConnectorMaterializedViewDefinition

use of com.facebook.presto.spi.ConnectorMaterializedViewDefinition in project presto by prestodb.

the class MaterializedViewOptimizationRewriteUtils method getQueryWithMaterializedViewOptimization.

private static Query getQueryWithMaterializedViewOptimization(Metadata metadata, Session session, SqlParser sqlParser, AccessControl accessControl, Query statement, QualifiedObjectName materializedViewQualifiedObjectName) {
    ConnectorMaterializedViewDefinition materializedView = metadata.getMaterializedView(session, materializedViewQualifiedObjectName).get();
    Table materializedViewTable = new Table(QualifiedName.of(materializedView.getTable()));
    Query materializedViewDefinition = (Query) sqlParser.createStatement(materializedView.getOriginalSql());
    return (Query) new MaterializedViewQueryOptimizer(metadata, session, sqlParser, accessControl, new RowExpressionDomainTranslator(metadata), materializedViewTable, materializedViewDefinition).rewrite(statement);
}
Also used : MaterializedViewQueryOptimizer(com.facebook.presto.sql.analyzer.MaterializedViewQueryOptimizer) Table(com.facebook.presto.sql.tree.Table) Query(com.facebook.presto.sql.tree.Query) RowExpressionDomainTranslator(com.facebook.presto.sql.relational.RowExpressionDomainTranslator) ConnectorMaterializedViewDefinition(com.facebook.presto.spi.ConnectorMaterializedViewDefinition)

Aggregations

ConnectorMaterializedViewDefinition (com.facebook.presto.spi.ConnectorMaterializedViewDefinition)13 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)7 MetadataUtil.createQualifiedObjectName (com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName)7 SemanticException (com.facebook.presto.sql.analyzer.SemanticException)7 PrestoException (com.facebook.presto.spi.PrestoException)6 TableHandle (com.facebook.presto.spi.TableHandle)6 Type (com.facebook.presto.common.type.Type)4 MetastoreContext (com.facebook.presto.hive.metastore.MetastoreContext)4 Table (com.facebook.presto.hive.metastore.Table)4 NullableValue (com.facebook.presto.common.predicate.NullableValue)3 TupleDomain (com.facebook.presto.common.predicate.TupleDomain)3 ColumnHandle (com.facebook.presto.spi.ColumnHandle)3 SchemaTableName (com.facebook.presto.spi.SchemaTableName)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 List (java.util.List)3 Map (java.util.Map)3 Optional (java.util.Optional)3 TupleDomain.extractFixedValues (com.facebook.presto.common.predicate.TupleDomain.extractFixedValues)2 TupleDomain.toLinkedMap (com.facebook.presto.common.predicate.TupleDomain.toLinkedMap)2 TypeManager (com.facebook.presto.common.type.TypeManager)2