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);
}
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);
}
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);
}
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();
}
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;
}
Aggregations