Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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 com.facebook.presto.sql.planner;
 
 
 import java.util.List;
 
 import static com.facebook.presto.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.spi.type.VarbinaryType.VARBINARY;
 import static com.facebook.presto.sql.planner.plan.TableWriterNode.CreateName;
 import static com.facebook.presto.sql.planner.plan.TableWriterNode.InsertReference;
 import static com.facebook.presto.sql.planner.plan.TableWriterNode.WriterTarget;
 import static com.facebook.presto.util.ImmutableCollectors.toImmutableList;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 
 public class LogicalPlanner
 {
     private final PlanNodeIdAllocator idAllocator;
 
     private final Session session;
     private final List<PlanOptimizerplanOptimizers;
     private final SymbolAllocator symbolAllocator = new SymbolAllocator();
     private final Metadata metadata;
 
     public LogicalPlanner(Session session,
             List<PlanOptimizerplanOptimizers,
             PlanNodeIdAllocator idAllocator,
             Metadata metadata)
     {
         checkNotNull(session"session is null");
         checkNotNull(planOptimizers"planOptimizers is null");
         checkNotNull(idAllocator"idAllocator is null");
         checkNotNull(metadata"metadata is null");
 
         this. = session;
         this. = planOptimizers;
         this. = idAllocator;
         this. = metadata;
     }
 
     public Plan plan(Analysis analysis)
     {
         RelationPlan plan;
         if (analysis.getCreateTableDestination().isPresent()) {
             plan = createTableCreationPlan(analysis);
         }
         else if (analysis.getInsertTarget().isPresent()) {
             plan = createInsertPlan(analysis);
         }
         else {
             plan = createRelationPlan(analysis);
         }
 
         PlanNode root = createOutputPlan(plananalysis);
 
         // make sure we produce a valid plan. This is mainly to catch programming errors
         PlanSanityChecker.validate(root);
 
         for (PlanOptimizer optimizer : ) {
             root = optimizer.optimize(root.getTypes(), );
             checkNotNull(root"%s returned a null plan"optimizer.getClass().getName());
         }
 
         // make sure we produce a valid plan after optimizations run. This is mainly to catch programming errors
         PlanSanityChecker.validate(root);
 
         return new Plan(root);
     }
 
     private RelationPlan createTableCreationPlan(Analysis analysis)
    {
        QualifiedTableName destination = analysis.getCreateTableDestination().get();
        RelationPlan plan = createRelationPlan(analysis);
        TableMetadata tableMetadata = createTableMetadata(destinationgetOutputTableColumns(plan), plan.getSampleWeight().isPresent());
        checkState(!plan.getSampleWeight().isPresent() || .canCreateSampledTables(destination.getCatalogName()), "Cannot write sampled data to a store that doesn't support sampling");
        return createTableWriterPlan(
                analysis,
                plan,
                tableMetadata,
                new CreateName(destination.getCatalogName(), tableMetadata));
    }
    private RelationPlan createInsertPlan(Analysis analysis)
    {
        TableHandle target = analysis.getInsertTarget().get();
        return createTableWriterPlan(
                analysis,
                createRelationPlan(analysis),
                .getTableMetadata(target),
                new InsertReference(target));
    }
    private RelationPlan createTableWriterPlan(Analysis analysisRelationPlan planTableMetadata tableMetadataWriterTarget target)
    {
        List<SymbolwriterOutputs = ImmutableList.of(
                .newSymbol("partialrows"),
                .newSymbol("fragment"));
        TableWriterNode writerNode = new TableWriterNode(
                .getNextId(),
                plan.getRoot(),
                target,
                plan.getOutputSymbols(),
                getVisibleColumnNames(tableMetadata),
                writerOutputs,
                plan.getSampleWeight());
        List<Symboloutputs = ImmutableList.of(.newSymbol("rows"));
        TableCommitNode commitNode = new TableCommitNode(
                .getNextId(),
                writerNode,
                target,
                outputs);
        return new RelationPlan(commitNodeanalysis.getOutputDescriptor(), outputs, Optional.empty());
    }
    private PlanNode createOutputPlan(RelationPlan planAnalysis analysis)
    {
        ImmutableList.Builder<Symboloutputs = ImmutableList.builder();
        ImmutableList.Builder<Stringnames = ImmutableList.builder();
        int columnNumber = 0;
        TupleDescriptor outputDescriptor = analysis.getOutputDescriptor();
        for (Field field : outputDescriptor.getVisibleFields()) {
            String name = field.getName().orElse("_col" + columnNumber);
            names.add(name);
            int fieldIndex = outputDescriptor.indexOf(field);
            Symbol symbol = plan.getSymbol(fieldIndex);
            outputs.add(symbol);
            columnNumber++;
        }
        return new OutputNode(.getNextId(), plan.getRoot(), names.build(), outputs.build());
    }
    private RelationPlan createRelationPlan(Analysis analysis)
    {
        return new RelationPlanner(analysis)
                .process(analysis.getQuery(), null);
    }
    private TableMetadata createTableMetadata(QualifiedTableName tableList<ColumnMetadatacolumnsboolean sampled)
    {
        String owner = .getUser();
        ConnectorTableMetadata metadata = new ConnectorTableMetadata(table.asSchemaTableName(), columnsownersampled);
        // TODO: first argument should actually be connectorId
        return new TableMetadata(table.getCatalogName(), metadata);
    }
    private static List<ColumnMetadatagetOutputTableColumns(RelationPlan plan)
    {
        ImmutableList.Builder<ColumnMetadatacolumns = ImmutableList.builder();
        int ordinalPosition = 0;
        for (Field field : plan.getDescriptor().getVisibleFields()) {
            columns.add(new ColumnMetadata(field.getName().get(), field.getType(), ordinalPositionfalse));
            ordinalPosition++;
        }
        return columns.build();
    }
    private static List<StringgetVisibleColumnNames(TableMetadata tableMetadata)
    {
        return tableMetadata.getColumns().stream()
                .filter(column -> !column.isHidden())
                .map(ColumnMetadata::getName)
                .collect(toImmutableList());
    }
New to GrepCode? Check out our FAQ X