Search in sources :

Example 16 with Metadata

use of com.facebook.presto.metadata.Metadata in project presto by prestodb.

the class TableWriterMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    TableWriterNode tableWriterNode = (TableWriterNode) node;
    if (!tableWriterNode.getColumnNames().equals(columnNames)) {
        return NO_MATCH;
    }
    if (!columns.stream().map(s -> Symbol.from(symbolAliases.get(s))).collect(toImmutableList()).equals(tableWriterNode.getColumns().stream().map(VariableReferenceExpression::getName).map(Symbol::new).collect(toImmutableList()))) {
        return NO_MATCH;
    }
    return match();
}
Also used : Session(com.facebook.presto.Session) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) StatsProvider(com.facebook.presto.cost.StatsProvider) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) TableWriterNode(com.facebook.presto.sql.planner.plan.TableWriterNode) Preconditions.checkState(com.google.common.base.Preconditions.checkState) PlanNode(com.facebook.presto.spi.plan.PlanNode) List(java.util.List) Symbol(com.facebook.presto.sql.planner.Symbol) MatchResult.match(com.facebook.presto.sql.planner.assertions.MatchResult.match) NO_MATCH(com.facebook.presto.sql.planner.assertions.MatchResult.NO_MATCH) Metadata(com.facebook.presto.metadata.Metadata) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) TableWriterNode(com.facebook.presto.sql.planner.plan.TableWriterNode)

Example 17 with Metadata

use of com.facebook.presto.metadata.Metadata in project presto by prestodb.

the class WindowMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    WindowNode windowNode = (WindowNode) node;
    if (!prePartitionedInputs.map(expectedInputs -> expectedInputs.stream().map(alias -> alias.toSymbol(symbolAliases)).collect(toImmutableSet()).equals(windowNode.getPrePartitionedInputs().stream().map(VariableReferenceExpression::getName).map(Symbol::new).collect(toImmutableSet()))).orElse(true)) {
        return NO_MATCH;
    }
    if (!specification.map(expectedSpecification -> matchSpecification(windowNode.getSpecification(), expectedSpecification.getExpectedValue(symbolAliases))).orElse(true)) {
        return NO_MATCH;
    }
    if (!preSortedOrderPrefix.map(Integer.valueOf(windowNode.getPreSortedOrderPrefix())::equals).orElse(true)) {
        return NO_MATCH;
    }
    if (!hashSymbol.map(expectedHashSymbol -> expectedHashSymbol.map(alias -> alias.toSymbol(symbolAliases)).map(Symbol::getName).equals(windowNode.getHashVariable().map(VariableReferenceExpression::getName))).orElse(true)) {
        return NO_MATCH;
    }
    /*
         * Window functions produce a symbol (the result of the function call) that we might
         * want to bind to an alias so we can reference it further up the tree. As such,
         * they need to be matched with an Alias matcher so we can bind the symbol if desired.
         */
    return match();
}
Also used : SpecificationProvider.matchSpecification(com.facebook.presto.sql.planner.assertions.SpecificationProvider.matchSpecification) SortOrder(com.facebook.presto.common.block.SortOrder) WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) Session(com.facebook.presto.Session) StatsProvider(com.facebook.presto.cost.StatsProvider) Set(java.util.Set) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Preconditions.checkState(com.google.common.base.Preconditions.checkState) PlanNode(com.facebook.presto.spi.plan.PlanNode) List(java.util.List) Symbol(com.facebook.presto.sql.planner.Symbol) Map(java.util.Map) MatchResult.match(com.facebook.presto.sql.planner.assertions.MatchResult.match) Objects.requireNonNull(java.util.Objects.requireNonNull) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) Optional(java.util.Optional) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) NO_MATCH(com.facebook.presto.sql.planner.assertions.MatchResult.NO_MATCH) LinkedList(java.util.LinkedList) Metadata(com.facebook.presto.metadata.Metadata) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) PlanMatchPattern.node(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.node) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Symbol(com.facebook.presto.sql.planner.Symbol)

Example 18 with Metadata

use of com.facebook.presto.metadata.Metadata in project presto by prestodb.

the class RowNumberMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    RowNumberNode rowNumberNode = (RowNumberNode) node;
    if (!partitionBy.map(expectedPartitionBy -> expectedPartitionBy.stream().map(alias -> alias.toSymbol(symbolAliases)).map(Symbol::getName).collect(toImmutableList()).equals(rowNumberNode.getPartitionBy().stream().map(VariableReferenceExpression::getName).collect(toImmutableList()))).orElse(true)) {
        return NO_MATCH;
    }
    if (!maxRowCountPerPartition.map(expectedMaxRowCountPerPartition -> expectedMaxRowCountPerPartition.equals(rowNumberNode.getMaxRowCountPerPartition())).orElse(true)) {
        return NO_MATCH;
    }
    if (!rowNumberSymbol.map(expectedRowNumberSymbol -> expectedRowNumberSymbol.toSymbol(symbolAliases).getName().equals(rowNumberNode.getRowNumberVariable().getName())).orElse(true)) {
        return NO_MATCH;
    }
    if (!hashSymbol.map(expectedHashSymbol -> expectedHashSymbol.map(symbolAlias -> symbolAlias.toSymbol(symbolAliases)).map(Symbol::getName).equals(rowNumberNode.getHashVariable().map(VariableReferenceExpression::getName))).orElse(true)) {
        return NO_MATCH;
    }
    return match();
}
Also used : RowNumberNode(com.facebook.presto.sql.planner.plan.RowNumberNode) Session(com.facebook.presto.Session) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) StatsProvider(com.facebook.presto.cost.StatsProvider) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Preconditions.checkState(com.google.common.base.Preconditions.checkState) PlanNode(com.facebook.presto.spi.plan.PlanNode) List(java.util.List) Symbol(com.facebook.presto.sql.planner.Symbol) MatchResult.match(com.facebook.presto.sql.planner.assertions.MatchResult.match) Objects.requireNonNull(java.util.Objects.requireNonNull) Optional(java.util.Optional) NO_MATCH(com.facebook.presto.sql.planner.assertions.MatchResult.NO_MATCH) Metadata(com.facebook.presto.metadata.Metadata) PlanMatchPattern.node(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.node) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) Symbol(com.facebook.presto.sql.planner.Symbol) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RowNumberNode(com.facebook.presto.sql.planner.plan.RowNumberNode)

Example 19 with Metadata

use of com.facebook.presto.metadata.Metadata 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 20 with Metadata

use of com.facebook.presto.metadata.Metadata in project presto by prestodb.

the class CreateTypeTask method execute.

@Override
public ListenableFuture<?> execute(CreateType statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
    TypeSignature signature;
    if (statement.getDistinctType().isPresent()) {
        signature = new TypeSignature(statement.getDistinctType().get());
    } else {
        List<TypeSignatureParameter> typeParameters = Streams.zip(statement.getParameterNames().stream(), statement.getParameterTypes().stream(), (name, type) -> TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName(name, false)), parseTypeSignature(type)))).collect(toImmutableList());
        signature = new TypeSignature(ROW, typeParameters);
    }
    UserDefinedType userDefinedType = new UserDefinedType(QualifiedObjectName.valueOf(statement.getTypeName().toString()), signature);
    metadata.getFunctionAndTypeManager().addUserDefinedType(userDefinedType);
    return immediateFuture(null);
}
Also used : WarningCollector(com.facebook.presto.spi.WarningCollector) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ROW(com.facebook.presto.common.type.StandardTypes.ROW) TypeSignature(com.facebook.presto.common.type.TypeSignature) Inject(javax.inject.Inject) Objects.requireNonNull(java.util.Objects.requireNonNull) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) UserDefinedType(com.facebook.presto.common.type.UserDefinedType) TransactionManager(com.facebook.presto.transaction.TransactionManager) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) Futures.immediateFuture(com.google.common.util.concurrent.Futures.immediateFuture) Session(com.facebook.presto.Session) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) CreateType(com.facebook.presto.sql.tree.CreateType) Streams(com.google.common.collect.Streams) String.format(java.lang.String.format) SqlParser(com.facebook.presto.sql.parser.SqlParser) List(java.util.List) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Expression(com.facebook.presto.sql.tree.Expression) Optional(java.util.Optional) RowFieldName(com.facebook.presto.common.type.RowFieldName) Metadata(com.facebook.presto.metadata.Metadata) AccessControl(com.facebook.presto.security.AccessControl) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) UserDefinedType(com.facebook.presto.common.type.UserDefinedType)

Aggregations

Metadata (com.facebook.presto.metadata.Metadata)59 Optional (java.util.Optional)38 Session (com.facebook.presto.Session)35 List (java.util.List)35 Objects.requireNonNull (java.util.Objects.requireNonNull)29 Map (java.util.Map)26 ImmutableList (com.google.common.collect.ImmutableList)24 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)24 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)21 RowExpression (com.facebook.presto.spi.relation.RowExpression)19 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)18 Expression (com.facebook.presto.sql.tree.Expression)18 PlanNode (com.facebook.presto.spi.plan.PlanNode)17 SqlParser (com.facebook.presto.sql.parser.SqlParser)16 ImmutableMap (com.google.common.collect.ImmutableMap)16 Preconditions.checkState (com.google.common.base.Preconditions.checkState)15 Type (com.facebook.presto.common.type.Type)14 TableHandle (com.facebook.presto.spi.TableHandle)14 WarningCollector (com.facebook.presto.spi.WarningCollector)14 Set (java.util.Set)14