Search in sources :

Example 1 with DeleteNode

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

the class TableWriteInfo method createDeleteScanInfo.

private static Optional<DeleteScanInfo> createDeleteScanInfo(StreamingSubPlan plan, Optional<ExecutionWriterTarget> writerTarget, Metadata metadata, Session session) {
    if (writerTarget.isPresent() && writerTarget.get() instanceof ExecutionWriterTarget.DeleteHandle) {
        TableHandle tableHandle = ((ExecutionWriterTarget.DeleteHandle) writerTarget.get()).getHandle();
        DeleteNode delete = getOnlyElement(findPlanNodes(plan, DeleteNode.class));
        TableScanNode tableScan = getDeleteTableScan(delete);
        TupleDomain<ColumnHandle> originalEnforcedConstraint = tableScan.getEnforcedConstraint();
        TableLayoutResult layoutResult = metadata.getLayout(session, tableHandle, new Constraint<>(originalEnforcedConstraint), Optional.of(ImmutableSet.copyOf(tableScan.getAssignments().values())));
        return Optional.of(new DeleteScanInfo(tableScan.getId(), layoutResult.getLayout().getNewTableHandle()));
    }
    return Optional.empty();
}
Also used : DeleteNode(com.facebook.presto.sql.planner.plan.DeleteNode) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) TableHandle(com.facebook.presto.spi.TableHandle) AnalyzeTableHandle(com.facebook.presto.metadata.AnalyzeTableHandle) TableLayoutResult(com.facebook.presto.metadata.TableLayoutResult)

Example 2 with DeleteNode

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

the class RemoveEmptyDelete method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof TableFinishNode)) {
        return Optional.empty();
    }
    TableFinishNode finish = (TableFinishNode) node;
    PlanNode finishSource = lookup.resolve(finish.getSource());
    if (!(finishSource instanceof ExchangeNode)) {
        return Optional.empty();
    }
    ExchangeNode exchange = (ExchangeNode) finishSource;
    if (exchange.getSources().size() != 1) {
        return Optional.empty();
    }
    PlanNode exchangeSource = lookup.resolve(getOnlyElement(exchange.getSources()));
    if (!(exchangeSource instanceof DeleteNode)) {
        return Optional.empty();
    }
    DeleteNode delete = (DeleteNode) exchangeSource;
    PlanNode deleteSource = lookup.resolve(delete.getSource());
    if (!(deleteSource instanceof ValuesNode)) {
        return Optional.empty();
    }
    ValuesNode values = (ValuesNode) deleteSource;
    if (!values.getRows().isEmpty()) {
        return Optional.empty();
    }
    return Optional.of(new ValuesNode(node.getId(), node.getOutputSymbols(), ImmutableList.of(ImmutableList.of(new LongLiteral("0")))));
}
Also used : DeleteNode(com.facebook.presto.sql.planner.plan.DeleteNode) ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) ExchangeNode(com.facebook.presto.sql.planner.plan.ExchangeNode) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) TableFinishNode(com.facebook.presto.sql.planner.plan.TableFinishNode)

Example 3 with DeleteNode

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

the class LogicalPlanner method createDeletePlan.

private RelationPlan createDeletePlan(Analysis analysis, Delete node) {
    DeleteNode deleteNode = new QueryPlanner(analysis, variableAllocator, idAllocator, buildLambdaDeclarationToVariableMap(analysis, variableAllocator), metadata, session).plan(node);
    TableHandle handle = analysis.getTableHandle(node.getTable());
    DeleteHandle deleteHandle = new DeleteHandle(handle, metadata.getTableMetadata(session, handle).getTable());
    TableFinishNode commitNode = new TableFinishNode(deleteNode.getSourceLocation(), idAllocator.getNextId(), deleteNode, Optional.of(deleteHandle), variableAllocator.newVariable("rows", BIGINT), Optional.empty(), Optional.empty());
    return new RelationPlan(commitNode, analysis.getScope(node), commitNode.getOutputVariables());
}
Also used : DeleteNode(com.facebook.presto.sql.planner.plan.DeleteNode) DeleteHandle(com.facebook.presto.sql.planner.plan.TableWriterNode.DeleteHandle) TableHandle(com.facebook.presto.spi.TableHandle) TableFinishNode(com.facebook.presto.sql.planner.plan.TableFinishNode)

Example 4 with DeleteNode

use of com.facebook.presto.sql.planner.plan.DeleteNode 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<VariableReferenceExpression> outputVariablesBuilder = ImmutableList.builder();
    ImmutableMap.Builder<VariableReferenceExpression, ColumnHandle> columns = ImmutableMap.builder();
    ImmutableList.Builder<Field> fields = ImmutableList.builder();
    for (Field field : descriptor.getAllFields()) {
        VariableReferenceExpression variable = variableAllocator.newVariable(getSourceLocation(field.getNodeLocation()), field.getName().get(), field.getType());
        outputVariablesBuilder.add(variable);
        columns.put(variable, analysis.getColumn(field));
        fields.add(field);
    }
    // add rowId column
    Field rowIdField = Field.newUnqualified(node.getLocation(), Optional.empty(), rowIdType);
    VariableReferenceExpression rowIdVariable = variableAllocator.newVariable(getSourceLocation(node), "$rowId", rowIdField.getType());
    outputVariablesBuilder.add(rowIdVariable);
    columns.put(rowIdVariable, rowIdHandle);
    fields.add(rowIdField);
    // create table scan
    List<VariableReferenceExpression> outputVariables = outputVariablesBuilder.build();
    PlanNode tableScan = new TableScanNode(getSourceLocation(node), idAllocator.getNextId(), handle, outputVariables, columns.build(), TupleDomain.all(), TupleDomain.all());
    Scope scope = Scope.builder().withRelationType(RelationId.anonymous(), new RelationType(fields.build())).build();
    RelationPlan relationPlan = new RelationPlan(tableScan, scope, outputVariables);
    TranslationMap translations = new TranslationMap(relationPlan, analysis, lambdaDeclarationToVariableMap);
    translations.setFieldMappings(relationPlan.getFieldMappings());
    PlanBuilder builder = new PlanBuilder(translations, relationPlan.getRoot());
    if (node.getWhere().isPresent()) {
        builder = filter(builder, node.getWhere().get(), node);
    }
    // create delete node
    VariableReferenceExpression rowId = new VariableReferenceExpression(Optional.empty(), builder.translate(new FieldReference(relationPlan.getDescriptor().indexOf(rowIdField))).getName(), rowIdField.getType());
    List<VariableReferenceExpression> deleteNodeOutputVariables = ImmutableList.of(variableAllocator.newVariable("partialrows", BIGINT), variableAllocator.newVariable("fragment", VARBINARY));
    return new DeleteNode(getSourceLocation(node), idAllocator.getNextId(), builder.getRoot(), rowId, deleteNodeOutputVariables);
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) FieldReference(com.facebook.presto.sql.tree.FieldReference) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) 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) WindowNodeUtil.toBoundType(com.facebook.presto.sql.planner.optimizations.WindowNodeUtil.toBoundType) WindowNodeUtil.toWindowType(com.facebook.presto.sql.planner.optimizations.WindowNodeUtil.toWindowType) Type(com.facebook.presto.common.type.Type) RelationType(com.facebook.presto.sql.analyzer.RelationType) PlanNode(com.facebook.presto.spi.plan.PlanNode) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) Scope(com.facebook.presto.sql.analyzer.Scope) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RelationType(com.facebook.presto.sql.analyzer.RelationType) TableHandle(com.facebook.presto.spi.TableHandle)

Aggregations

DeleteNode (com.facebook.presto.sql.planner.plan.DeleteNode)4 TableHandle (com.facebook.presto.spi.TableHandle)3 ColumnHandle (com.facebook.presto.spi.ColumnHandle)2 TableScanNode (com.facebook.presto.spi.plan.TableScanNode)2 TableFinishNode (com.facebook.presto.sql.planner.plan.TableFinishNode)2 Type (com.facebook.presto.common.type.Type)1 AnalyzeTableHandle (com.facebook.presto.metadata.AnalyzeTableHandle)1 TableLayoutResult (com.facebook.presto.metadata.TableLayoutResult)1 PlanNode (com.facebook.presto.spi.plan.PlanNode)1 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)1 Field (com.facebook.presto.sql.analyzer.Field)1 RelationType (com.facebook.presto.sql.analyzer.RelationType)1 Scope (com.facebook.presto.sql.analyzer.Scope)1 WindowNodeUtil.toBoundType (com.facebook.presto.sql.planner.optimizations.WindowNodeUtil.toBoundType)1 WindowNodeUtil.toWindowType (com.facebook.presto.sql.planner.optimizations.WindowNodeUtil.toWindowType)1 ExchangeNode (com.facebook.presto.sql.planner.plan.ExchangeNode)1 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)1 DeleteHandle (com.facebook.presto.sql.planner.plan.TableWriterNode.DeleteHandle)1 ValuesNode (com.facebook.presto.sql.planner.plan.ValuesNode)1 FieldReference (com.facebook.presto.sql.tree.FieldReference)1