Search in sources :

Example 1 with Analysis

use of com.facebook.presto.sql.analyzer.Analysis in project presto by prestodb.

the class SqlQueryExecution method doAnalyzeQuery.

private PlanRoot doAnalyzeQuery() {
    // time analysis phase
    long analysisStart = System.nanoTime();
    // analyze query
    Analyzer analyzer = new Analyzer(stateMachine.getSession(), metadata, sqlParser, accessControl, Optional.of(queryExplainer), parameters);
    Analysis analysis = analyzer.analyze(statement);
    stateMachine.setUpdateType(analysis.getUpdateType());
    // plan query
    PlanNodeIdAllocator idAllocator = new PlanNodeIdAllocator();
    LogicalPlanner logicalPlanner = new LogicalPlanner(stateMachine.getSession(), planOptimizers, idAllocator, metadata, sqlParser);
    Plan plan = logicalPlanner.plan(analysis);
    // extract inputs
    List<Input> inputs = new InputExtractor(metadata, stateMachine.getSession()).extractInputs(plan.getRoot());
    stateMachine.setInputs(inputs);
    // extract output
    Optional<Output> output = new OutputExtractor().extractOutput(plan.getRoot());
    stateMachine.setOutput(output);
    // fragment the plan
    SubPlan subplan = PlanFragmenter.createSubPlans(stateMachine.getSession(), metadata, plan);
    // record analysis time
    stateMachine.recordAnalysisTime(analysisStart);
    boolean explainAnalyze = analysis.getStatement() instanceof Explain && ((Explain) analysis.getStatement()).isAnalyze();
    return new PlanRoot(subplan, !explainAnalyze, extractConnectors(analysis));
}
Also used : LogicalPlanner(com.facebook.presto.sql.planner.LogicalPlanner) Explain(com.facebook.presto.sql.tree.Explain) InputExtractor(com.facebook.presto.sql.planner.InputExtractor) Analyzer(com.facebook.presto.sql.analyzer.Analyzer) StageExecutionPlan(com.facebook.presto.sql.planner.StageExecutionPlan) Plan(com.facebook.presto.sql.planner.Plan) SubPlan(com.facebook.presto.sql.planner.SubPlan) OutputExtractor(com.facebook.presto.sql.planner.OutputExtractor) PlanNodeIdAllocator(com.facebook.presto.sql.planner.PlanNodeIdAllocator) Analysis(com.facebook.presto.sql.analyzer.Analysis) SubPlan(com.facebook.presto.sql.planner.SubPlan)

Example 2 with Analysis

use of com.facebook.presto.sql.analyzer.Analysis in project presto by prestodb.

the class LogicalPlanner method createInsertPlan.

private RelationPlan createInsertPlan(Analysis analysis, Insert insertStatement) {
    Analysis.Insert insert = analysis.getInsert().get();
    TableMetadata tableMetadata = metadata.getTableMetadata(session, insert.getTarget());
    List<ColumnMetadata> visibleTableColumns = tableMetadata.getColumns().stream().filter(column -> !column.isHidden()).collect(toImmutableList());
    List<String> visibleTableColumnNames = visibleTableColumns.stream().map(ColumnMetadata::getName).collect(toImmutableList());
    RelationPlan plan = createRelationPlan(analysis, insertStatement.getQuery());
    Map<String, ColumnHandle> columns = metadata.getColumnHandles(session, insert.getTarget());
    Assignments.Builder assignments = Assignments.builder();
    for (ColumnMetadata column : tableMetadata.getColumns()) {
        if (column.isHidden()) {
            continue;
        }
        Symbol output = symbolAllocator.newSymbol(column.getName(), column.getType());
        int index = insert.getColumns().indexOf(columns.get(column.getName()));
        if (index < 0) {
            assignments.put(output, new NullLiteral());
        } else {
            Symbol input = plan.getSymbol(index);
            Type tableType = column.getType();
            Type queryType = symbolAllocator.getTypes().get(input);
            if (queryType.equals(tableType) || metadata.getTypeManager().isTypeOnlyCoercion(queryType, tableType)) {
                assignments.put(output, input.toSymbolReference());
            } else {
                Expression cast = new Cast(input.toSymbolReference(), tableType.getTypeSignature().toString());
                assignments.put(output, cast);
            }
        }
    }
    ProjectNode projectNode = new ProjectNode(idAllocator.getNextId(), plan.getRoot(), assignments.build());
    List<Field> fields = visibleTableColumns.stream().map(column -> Field.newUnqualified(column.getName(), column.getType())).collect(toImmutableList());
    Scope scope = Scope.builder().withRelationType(new RelationType(fields)).build();
    plan = new RelationPlan(projectNode, scope, projectNode.getOutputSymbols());
    Optional<NewTableLayout> newTableLayout = metadata.getInsertLayout(session, insert.getTarget());
    return createTableWriterPlan(analysis, plan, new InsertReference(insert.getTarget()), visibleTableColumnNames, newTableLayout);
}
Also used : QualifiedObjectName(com.facebook.presto.metadata.QualifiedObjectName) TableMetadata(com.facebook.presto.metadata.TableMetadata) ExplainAnalyzeNode(com.facebook.presto.sql.planner.plan.ExplainAnalyzeNode) NOT_FOUND(com.facebook.presto.spi.StandardErrorCode.NOT_FOUND) Field(com.facebook.presto.sql.analyzer.Field) TableWriterNode(com.facebook.presto.sql.planner.plan.TableWriterNode) ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) BIGINT(com.facebook.presto.spi.type.BigintType.BIGINT) Delete(com.facebook.presto.sql.tree.Delete) Map(java.util.Map) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) Assignments(com.facebook.presto.sql.planner.plan.Assignments) OutputNode(com.facebook.presto.sql.planner.plan.OutputNode) ImmutableSet(com.google.common.collect.ImmutableSet) Query(com.facebook.presto.sql.tree.Query) Explain(com.facebook.presto.sql.tree.Explain) String.format(java.lang.String.format) SqlParser(com.facebook.presto.sql.parser.SqlParser) Preconditions.checkState(com.google.common.base.Preconditions.checkState) NullLiteral(com.facebook.presto.sql.tree.NullLiteral) List(java.util.List) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) Analysis(com.facebook.presto.sql.analyzer.Analysis) Optional(java.util.Optional) ConnectorId(com.facebook.presto.connector.ConnectorId) LimitNode(com.facebook.presto.sql.planner.plan.LimitNode) PlanOptimizer(com.facebook.presto.sql.planner.optimizations.PlanOptimizer) LambdaArgumentDeclaration(com.facebook.presto.sql.tree.LambdaArgumentDeclaration) NewTableLayout(com.facebook.presto.metadata.NewTableLayout) WriterTarget(com.facebook.presto.sql.planner.plan.TableWriterNode.WriterTarget) PrestoException(com.facebook.presto.spi.PrestoException) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Type(com.facebook.presto.spi.type.Type) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Objects.requireNonNull(java.util.Objects.requireNonNull) Cast(com.facebook.presto.sql.tree.Cast) VARBINARY(com.facebook.presto.spi.type.VarbinaryType.VARBINARY) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) PlanSanityChecker(com.facebook.presto.sql.planner.sanity.PlanSanityChecker) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) Session(com.facebook.presto.Session) InsertReference(com.facebook.presto.sql.planner.plan.TableWriterNode.InsertReference) RelationType(com.facebook.presto.sql.analyzer.RelationType) DeleteNode(com.facebook.presto.sql.planner.plan.DeleteNode) Insert(com.facebook.presto.sql.tree.Insert) Scope(com.facebook.presto.sql.analyzer.Scope) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) Expression(com.facebook.presto.sql.tree.Expression) ColumnHandle(com.facebook.presto.spi.ColumnHandle) CreateName(com.facebook.presto.sql.planner.plan.TableWriterNode.CreateName) IdentityLinkedHashMap(com.facebook.presto.util.maps.IdentityLinkedHashMap) TableFinishNode(com.facebook.presto.sql.planner.plan.TableFinishNode) Metadata(com.facebook.presto.metadata.Metadata) Statement(com.facebook.presto.sql.tree.Statement) Cast(com.facebook.presto.sql.tree.Cast) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) NewTableLayout(com.facebook.presto.metadata.NewTableLayout) Assignments(com.facebook.presto.sql.planner.plan.Assignments) InsertReference(com.facebook.presto.sql.planner.plan.TableWriterNode.InsertReference) Field(com.facebook.presto.sql.analyzer.Field) RelationType(com.facebook.presto.sql.analyzer.RelationType) TableMetadata(com.facebook.presto.metadata.TableMetadata) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Type(com.facebook.presto.spi.type.Type) RelationType(com.facebook.presto.sql.analyzer.RelationType) Scope(com.facebook.presto.sql.analyzer.Scope) Expression(com.facebook.presto.sql.tree.Expression) Analysis(com.facebook.presto.sql.analyzer.Analysis) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) NullLiteral(com.facebook.presto.sql.tree.NullLiteral)

Example 3 with Analysis

use of com.facebook.presto.sql.analyzer.Analysis in project presto by prestodb.

the class LocalQueryRunner method createPlan.

public Plan createPlan(Session session, @Language("SQL") String sql, List<PlanOptimizer> optimizers, LogicalPlanner.Stage stage) {
    Statement wrapped = sqlParser.createStatement(sql);
    Statement statement = unwrapExecuteStatement(wrapped, sqlParser, session);
    List<Expression> parameters = emptyList();
    if (wrapped instanceof Execute) {
        parameters = ((Execute) wrapped).getParameters();
    }
    validateParameters(statement, parameters);
    assertFormattedSql(sqlParser, statement);
    PlanNodeIdAllocator idAllocator = new PlanNodeIdAllocator();
    QueryExplainer queryExplainer = new QueryExplainer(optimizers, metadata, accessControl, sqlParser, dataDefinitionTask);
    Analyzer analyzer = new Analyzer(session, metadata, sqlParser, accessControl, Optional.of(queryExplainer), parameters);
    LogicalPlanner logicalPlanner = new LogicalPlanner(session, optimizers, idAllocator, metadata, sqlParser);
    Analysis analysis = analyzer.analyze(statement);
    return logicalPlanner.plan(analysis, stage);
}
Also used : QueryExplainer(com.facebook.presto.sql.analyzer.QueryExplainer) LogicalPlanner(com.facebook.presto.sql.planner.LogicalPlanner) Execute(com.facebook.presto.sql.tree.Execute) Expression(com.facebook.presto.sql.tree.Expression) PlanNodeIdAllocator(com.facebook.presto.sql.planner.PlanNodeIdAllocator) SqlQueryManager.unwrapExecuteStatement(com.facebook.presto.execution.SqlQueryManager.unwrapExecuteStatement) Statement(com.facebook.presto.sql.tree.Statement) Analysis(com.facebook.presto.sql.analyzer.Analysis) Analyzer(com.facebook.presto.sql.analyzer.Analyzer)

Example 4 with Analysis

use of com.facebook.presto.sql.analyzer.Analysis in project presto by prestodb.

the class CreateViewTask method execute.

@Override
public ListenableFuture<?> execute(CreateView statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters) {
    Session session = stateMachine.getSession();
    QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName());
    accessControl.checkCanCreateView(session.getRequiredTransactionId(), session.getIdentity(), name);
    String sql = getFormattedSql(statement.getQuery(), sqlParser, Optional.of(parameters));
    Analysis analysis = analyzeStatement(statement, session, metadata, accessControl, parameters);
    List<ViewColumn> columns = analysis.getOutputDescriptor(statement.getQuery()).getVisibleFields().stream().map(field -> new ViewColumn(field.getName().get(), field.getType())).collect(toImmutableList());
    String data = codec.toJson(new ViewDefinition(sql, session.getCatalog(), session.getSchema(), columns, Optional.of(session.getUser())));
    metadata.createView(session, name, data, statement.isReplace());
    return immediateFuture(null);
}
Also used : QualifiedObjectName(com.facebook.presto.metadata.QualifiedObjectName) Futures.immediateFuture(com.google.common.util.concurrent.Futures.immediateFuture) Analyzer(com.facebook.presto.sql.analyzer.Analyzer) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ViewDefinition(com.facebook.presto.metadata.ViewDefinition) Session(com.facebook.presto.Session) SqlParser(com.facebook.presto.sql.parser.SqlParser) FeaturesConfig(com.facebook.presto.sql.analyzer.FeaturesConfig) Inject(javax.inject.Inject) SqlFormatterUtil.getFormattedSql(com.facebook.presto.sql.SqlFormatterUtil.getFormattedSql) List(java.util.List) Expression(com.facebook.presto.sql.tree.Expression) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) ViewColumn(com.facebook.presto.metadata.ViewDefinition.ViewColumn) Objects.requireNonNull(java.util.Objects.requireNonNull) Analysis(com.facebook.presto.sql.analyzer.Analysis) Optional(java.util.Optional) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) TransactionManager(com.facebook.presto.transaction.TransactionManager) Metadata(com.facebook.presto.metadata.Metadata) AccessControl(com.facebook.presto.security.AccessControl) CreateView(com.facebook.presto.sql.tree.CreateView) Statement(com.facebook.presto.sql.tree.Statement) JsonCodec(io.airlift.json.JsonCodec) Analysis(com.facebook.presto.sql.analyzer.Analysis) ViewColumn(com.facebook.presto.metadata.ViewDefinition.ViewColumn) ViewDefinition(com.facebook.presto.metadata.ViewDefinition) QualifiedObjectName(com.facebook.presto.metadata.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) Session(com.facebook.presto.Session)

Aggregations

Analysis (com.facebook.presto.sql.analyzer.Analysis)4 Analyzer (com.facebook.presto.sql.analyzer.Analyzer)3 Expression (com.facebook.presto.sql.tree.Expression)3 Statement (com.facebook.presto.sql.tree.Statement)3 Session (com.facebook.presto.Session)2 Metadata (com.facebook.presto.metadata.Metadata)2 QualifiedObjectName (com.facebook.presto.metadata.QualifiedObjectName)2 SqlParser (com.facebook.presto.sql.parser.SqlParser)2 LogicalPlanner (com.facebook.presto.sql.planner.LogicalPlanner)2 PlanNodeIdAllocator (com.facebook.presto.sql.planner.PlanNodeIdAllocator)2 Explain (com.facebook.presto.sql.tree.Explain)2 ConnectorId (com.facebook.presto.connector.ConnectorId)1 SqlQueryManager.unwrapExecuteStatement (com.facebook.presto.execution.SqlQueryManager.unwrapExecuteStatement)1 MetadataUtil.createQualifiedObjectName (com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName)1 NewTableLayout (com.facebook.presto.metadata.NewTableLayout)1 TableMetadata (com.facebook.presto.metadata.TableMetadata)1 ViewDefinition (com.facebook.presto.metadata.ViewDefinition)1 ViewColumn (com.facebook.presto.metadata.ViewDefinition.ViewColumn)1 AccessControl (com.facebook.presto.security.AccessControl)1 ColumnHandle (com.facebook.presto.spi.ColumnHandle)1