Search in sources :

Example 46 with PlanNode

use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.

the class QueryPlanner method plan.

public DeleteNode plan(Delete node) {
    RelationType descriptor = analysis.getOutputDescriptor(node.getTable());
    TableHandle handle = analysis.getTableHandle(node.getTable());
    ColumnHandle rowIdHandle = metadata.getUpdateRowIdColumnHandle(session, handle);
    Type rowIdType = metadata.getColumnMetadata(session, handle, rowIdHandle).getType();
    // add table columns
    ImmutableList.Builder<Symbol> outputSymbols = ImmutableList.builder();
    ImmutableMap.Builder<Symbol, ColumnHandle> columns = ImmutableMap.builder();
    ImmutableList.Builder<Field> fields = ImmutableList.builder();
    for (Field field : descriptor.getAllFields()) {
        Symbol symbol = symbolAllocator.newSymbol(field.getName().get(), field.getType());
        outputSymbols.add(symbol);
        columns.put(symbol, analysis.getColumn(field));
        fields.add(field);
    }
    // add rowId column
    Field rowIdField = Field.newUnqualified(Optional.empty(), rowIdType);
    Symbol rowIdSymbol = symbolAllocator.newSymbol("$rowId", rowIdField.getType());
    outputSymbols.add(rowIdSymbol);
    columns.put(rowIdSymbol, rowIdHandle);
    fields.add(rowIdField);
    // create table scan
    PlanNode tableScan = new TableScanNode(idAllocator.getNextId(), handle, outputSymbols.build(), columns.build(), Optional.empty(), TupleDomain.all(), null);
    Scope scope = Scope.builder().withRelationType(new RelationType(fields.build())).build();
    RelationPlan relationPlan = new RelationPlan(tableScan, scope, outputSymbols.build());
    TranslationMap translations = new TranslationMap(relationPlan, analysis, lambdaDeclarationToSymbolMap);
    translations.setFieldMappings(relationPlan.getFieldMappings());
    PlanBuilder builder = new PlanBuilder(translations, relationPlan.getRoot(), analysis.getParameters());
    if (node.getWhere().isPresent()) {
        builder = filter(builder, node.getWhere().get(), node);
    }
    // create delete node
    Symbol rowId = builder.translate(new FieldReference(relationPlan.getDescriptor().indexOf(rowIdField)));
    List<Symbol> outputs = ImmutableList.of(symbolAllocator.newSymbol("partialrows", BIGINT), symbolAllocator.newSymbol("fragment", VARBINARY));
    return new DeleteNode(idAllocator.getNextId(), builder.getRoot(), new DeleteHandle(handle, metadata.getTableMetadata(session, handle).getTable()), rowId, outputs);
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) FieldReference(com.facebook.presto.sql.tree.FieldReference) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) Field(com.facebook.presto.sql.analyzer.Field) DeleteNode(com.facebook.presto.sql.planner.plan.DeleteNode) Type(com.facebook.presto.spi.type.Type) RelationType(com.facebook.presto.sql.analyzer.RelationType) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) TableScanNode(com.facebook.presto.sql.planner.plan.TableScanNode) Scope(com.facebook.presto.sql.analyzer.Scope) RelationType(com.facebook.presto.sql.analyzer.RelationType) DeleteHandle(com.facebook.presto.sql.planner.plan.TableWriterNode.DeleteHandle) TableHandle(com.facebook.presto.metadata.TableHandle)

Example 47 with PlanNode

use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.

the class LogicalPlanner method planStatement.

public PlanNode planStatement(Analysis analysis, Statement statement) {
    if (statement instanceof CreateTableAsSelect && analysis.isCreateTableAsSelectNoOp()) {
        checkState(analysis.getCreateTableDestination().isPresent(), "Table destination is missing");
        List<Expression> emptyRow = ImmutableList.of();
        PlanNode source = new ValuesNode(idAllocator.getNextId(), ImmutableList.of(), ImmutableList.of(emptyRow));
        return new OutputNode(idAllocator.getNextId(), source, ImmutableList.of(), ImmutableList.of());
    }
    return createOutputPlan(planStatementWithoutOutput(analysis, statement), analysis);
}
Also used : ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) OutputNode(com.facebook.presto.sql.planner.plan.OutputNode) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Expression(com.facebook.presto.sql.tree.Expression) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect)

Example 48 with PlanNode

use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.

the class LogicalPlanner method createTableWriterPlan.

private RelationPlan createTableWriterPlan(Analysis analysis, RelationPlan plan, WriterTarget target, List<String> columnNames, Optional<NewTableLayout> writeTableLayout) {
    List<Symbol> writerOutputs = ImmutableList.of(symbolAllocator.newSymbol("partialrows", BIGINT), symbolAllocator.newSymbol("fragment", VARBINARY));
    PlanNode source = plan.getRoot();
    if (!analysis.isCreateTableAsSelectWithData()) {
        source = new LimitNode(idAllocator.getNextId(), source, 0L, false);
    }
    // todo this should be checked in analysis
    writeTableLayout.ifPresent(layout -> {
        if (!ImmutableSet.copyOf(columnNames).containsAll(layout.getPartitionColumns())) {
            throw new PrestoException(NOT_SUPPORTED, "INSERT must write all distribution columns: " + layout.getPartitionColumns());
        }
    });
    List<Symbol> symbols = plan.getFieldMappings();
    Optional<PartitioningScheme> partitioningScheme = Optional.empty();
    if (writeTableLayout.isPresent()) {
        List<Symbol> partitionFunctionArguments = new ArrayList<>();
        writeTableLayout.get().getPartitionColumns().stream().mapToInt(columnNames::indexOf).mapToObj(symbols::get).forEach(partitionFunctionArguments::add);
        List<Symbol> outputLayout = new ArrayList<>(symbols);
        partitioningScheme = Optional.of(new PartitioningScheme(Partitioning.create(writeTableLayout.get().getPartitioning(), partitionFunctionArguments), outputLayout));
    }
    PlanNode writerNode = new TableWriterNode(idAllocator.getNextId(), source, target, symbols, columnNames, writerOutputs, partitioningScheme);
    List<Symbol> outputs = ImmutableList.of(symbolAllocator.newSymbol("rows", BIGINT));
    TableFinishNode commitNode = new TableFinishNode(idAllocator.getNextId(), writerNode, target, outputs);
    return new RelationPlan(commitNode, analysis.getRootScope(), outputs);
}
Also used : ArrayList(java.util.ArrayList) PrestoException(com.facebook.presto.spi.PrestoException) TableFinishNode(com.facebook.presto.sql.planner.plan.TableFinishNode) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) LimitNode(com.facebook.presto.sql.planner.plan.LimitNode) TableWriterNode(com.facebook.presto.sql.planner.plan.TableWriterNode)

Example 49 with PlanNode

use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.

the class IterativeOptimizer method optimize.

@Override
public PlanNode optimize(PlanNode plan, Session session, Map<Symbol, Type> types, SymbolAllocator symbolAllocator, PlanNodeIdAllocator idAllocator) {
    if (!SystemSessionProperties.isNewOptimizerEnabled(session)) {
        for (PlanOptimizer optimizer : legacyRules) {
            plan = optimizer.optimize(plan, session, symbolAllocator.getTypes(), symbolAllocator, idAllocator);
        }
        return plan;
    }
    Memo memo = new Memo(idAllocator, plan);
    Lookup lookup = node -> {
        if (node instanceof GroupReference) {
            return memo.getNode(((GroupReference) node).getGroupId());
        }
        return node;
    };
    Duration timeout = SystemSessionProperties.getOptimizerTimeout(session);
    exploreGroup(memo.getRootGroup(), new Context(memo, lookup, idAllocator, symbolAllocator, System.nanoTime(), timeout.toMillis()));
    return memo.extract();
}
Also used : StatsRecorder(com.facebook.presto.sql.planner.StatsRecorder) PlanNodeIdAllocator(com.facebook.presto.sql.planner.PlanNodeIdAllocator) PlanOptimizer(com.facebook.presto.sql.planner.optimizations.PlanOptimizer) ImmutableSet(com.google.common.collect.ImmutableSet) Session(com.facebook.presto.Session) Set(java.util.Set) PrestoException(com.facebook.presto.spi.PrestoException) String.format(java.lang.String.format) Duration(io.airlift.units.Duration) Preconditions.checkState(com.google.common.base.Preconditions.checkState) OPTIMIZER_TIMEOUT(com.facebook.presto.spi.StandardErrorCode.OPTIMIZER_TIMEOUT) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) Symbol(com.facebook.presto.sql.planner.Symbol) Type(com.facebook.presto.spi.type.Type) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Map(java.util.Map) Optional(java.util.Optional) SymbolAllocator(com.facebook.presto.sql.planner.SymbolAllocator) SystemSessionProperties(com.facebook.presto.SystemSessionProperties) PlanOptimizer(com.facebook.presto.sql.planner.optimizations.PlanOptimizer) Duration(io.airlift.units.Duration)

Example 50 with PlanNode

use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.

the class IterativeOptimizer method exploreNode.

private boolean exploreNode(int group, Context context) {
    PlanNode node = context.getMemo().getNode(group);
    boolean done = false;
    boolean progress = false;
    while (!done) {
        if (isTimeLimitExhausted(context)) {
            throw new PrestoException(OPTIMIZER_TIMEOUT, format("The optimizer exhausted the time limit of %d ms", context.getTimeoutInMilliseconds()));
        }
        done = true;
        for (Rule rule : rules) {
            Optional<PlanNode> transformed;
            long duration;
            try {
                long start = System.nanoTime();
                transformed = rule.apply(node, context.getLookup(), context.getIdAllocator(), context.getSymbolAllocator());
                duration = System.nanoTime() - start;
            } catch (RuntimeException e) {
                stats.recordFailure(rule);
                throw e;
            }
            stats.record(rule, duration, transformed.isPresent());
            if (transformed.isPresent()) {
                node = context.getMemo().replace(group, transformed.get(), rule.getClass().getName());
                done = false;
                progress = true;
            }
        }
    }
    return progress;
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) PrestoException(com.facebook.presto.spi.PrestoException)

Aggregations

PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)89 Test (org.testng.annotations.Test)41 Expression (com.facebook.presto.sql.tree.Expression)28 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)18 Symbol (com.facebook.presto.sql.planner.Symbol)12 JoinNode (com.facebook.presto.sql.planner.plan.JoinNode)12 ProjectNode (com.facebook.presto.sql.planner.plan.ProjectNode)12 TableScanNode (com.facebook.presto.sql.planner.plan.TableScanNode)12 ImmutableList (com.google.common.collect.ImmutableList)12 AggregationNode (com.facebook.presto.sql.planner.plan.AggregationNode)10 ColumnHandle (com.facebook.presto.spi.ColumnHandle)9 FilterNode (com.facebook.presto.sql.planner.plan.FilterNode)9 LimitNode (com.facebook.presto.sql.planner.plan.LimitNode)9 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)9 Signature (com.facebook.presto.metadata.Signature)7 SemiJoinNode (com.facebook.presto.sql.planner.plan.SemiJoinNode)7 List (java.util.List)7 JoinGraph (com.facebook.presto.sql.planner.optimizations.joins.JoinGraph)6 ValuesNode (com.facebook.presto.sql.planner.plan.ValuesNode)6 Session (com.facebook.presto.Session)5