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