use of com.facebook.presto.spi.plan.UnionNode in project presto by prestodb.
the class TestCostCalculator method testUnion.
@Test
public void testUnion() {
TableScanNode ts1 = tableScan("ts1", "orderkey");
TableScanNode ts2 = tableScan("ts2", "orderkey_0");
UnionNode union = new UnionNode(Optional.empty(), new PlanNodeId("union"), ImmutableList.of(ts1, ts2), ImmutableList.of(new VariableReferenceExpression(Optional.empty(), "orderkey_1", BIGINT)), ImmutableMap.of(new VariableReferenceExpression(Optional.empty(), "orderkey_1", BIGINT), ImmutableList.of(new VariableReferenceExpression(Optional.empty(), "orderkey", BIGINT), new VariableReferenceExpression(Optional.empty(), "orderkey_0", BIGINT))));
Map<String, PlanNodeStatsEstimate> stats = ImmutableMap.of("ts1", statsEstimate(ts1, 4000), "ts2", statsEstimate(ts2, 1000), "union", statsEstimate(ts1, 5000));
Map<String, PlanCostEstimate> costs = ImmutableMap.of("ts1", cpuCost(1000), "ts2", cpuCost(1000));
Map<String, Type> types = ImmutableMap.of("orderkey", BIGINT, "orderkey_0", BIGINT, "orderkey_1", BIGINT);
assertCost(union, costs, stats).cpu(2000).memory(0).network(0);
assertCostEstimatedExchanges(union, costs, stats).cpu(2000).memory(0).network(5000 * IS_NULL_OVERHEAD);
}
use of com.facebook.presto.spi.plan.UnionNode in project presto by prestodb.
the class RelationPlanner method visitUnion.
@Override
protected RelationPlan visitUnion(Union node, Void context) {
checkArgument(!node.getRelations().isEmpty(), "No relations specified for UNION");
SetOperationPlan setOperationPlan = process(node);
PlanNode planNode = new UnionNode(getSourceLocation(node), idAllocator.getNextId(), setOperationPlan.getSources(), setOperationPlan.getOutputVariables(), setOperationPlan.getVariableMapping());
if (node.isDistinct().orElse(true)) {
planNode = distinct(planNode);
}
return new RelationPlan(planNode, analysis.getScope(node), planNode.getOutputVariables());
}
use of com.facebook.presto.spi.plan.UnionNode in project presto by prestodb.
the class PushLimitThroughUnion method apply.
@Override
public Result apply(LimitNode parent, Captures captures, Context context) {
UnionNode unionNode = captures.get(CHILD);
if (unionNode.getSources().stream().allMatch(source -> isAtMost(source, context.getLookup(), parent.getCount()))) {
return Result.empty();
}
ImmutableList.Builder<PlanNode> builder = ImmutableList.builder();
for (PlanNode source : unionNode.getSources()) {
// This check is to ensure that we don't fire the optimizer if it was previously applied.
if (isAtMost(source, context.getLookup(), parent.getCount())) {
builder.add(source);
} else {
builder.add(new LimitNode(parent.getSourceLocation(), context.getIdAllocator().getNextId(), source, parent.getCount(), LimitNode.Step.PARTIAL));
}
}
return Result.ofPlanNode(parent.replaceChildren(ImmutableList.of(unionNode.replaceChildren(builder.build()))));
}
use of com.facebook.presto.spi.plan.UnionNode in project presto by prestodb.
the class PushTableWriteThroughUnion method apply.
@Override
public Result apply(TableWriterNode writerNode, Captures captures, Context context) {
UnionNode unionNode = captures.get(CHILD);
ImmutableList.Builder<PlanNode> rewrittenSources = ImmutableList.builder();
List<Map<VariableReferenceExpression, VariableReferenceExpression>> sourceMappings = new ArrayList<>();
for (int source = 0; source < unionNode.getSources().size(); source++) {
rewrittenSources.add(rewriteSource(writerNode, unionNode, source, sourceMappings, context));
}
ImmutableListMultimap.Builder<VariableReferenceExpression, VariableReferenceExpression> unionMappings = ImmutableListMultimap.builder();
sourceMappings.forEach(mappings -> mappings.forEach(unionMappings::put));
ListMultimap<VariableReferenceExpression, VariableReferenceExpression> mappings = unionMappings.build();
return Result.ofPlanNode(new UnionNode(writerNode.getSourceLocation(), context.getIdAllocator().getNextId(), rewrittenSources.build(), ImmutableList.copyOf(mappings.keySet()), fromListMultimap(mappings)));
}
use of com.facebook.presto.spi.plan.UnionNode in project presto by prestodb.
the class PushProjectionThroughUnion method apply.
@Override
public Result apply(ProjectNode parent, Captures captures, Context context) {
UnionNode source = captures.get(CHILD);
// OutputLayout of the resultant Union, will be same as the layout of the Project
List<VariableReferenceExpression> outputLayout = parent.getOutputVariables();
// Mapping from the output symbol to ordered list of symbols from each of the sources
ImmutableListMultimap.Builder<VariableReferenceExpression, VariableReferenceExpression> mappings = ImmutableListMultimap.builder();
// sources for the resultant UnionNode
ImmutableList.Builder<PlanNode> outputSources = ImmutableList.builder();
for (int i = 0; i < source.getSources().size(); i++) {
// assignments for the new ProjectNode
Assignments.Builder assignments = Assignments.builder();
// mapping from current ProjectNode to new ProjectNode, used to identify the output layout
Map<VariableReferenceExpression, VariableReferenceExpression> projectVariableMapping = new HashMap<>();
// Translate the assignments in the ProjectNode using symbols of the source of the UnionNode
for (Map.Entry<VariableReferenceExpression, RowExpression> entry : parent.getAssignments().entrySet()) {
RowExpression translatedExpression;
VariableReferenceExpression variable;
translatedExpression = RowExpressionVariableInliner.inlineVariables(source.sourceVariableMap(i), entry.getValue());
variable = context.getVariableAllocator().newVariable(translatedExpression);
assignments.put(variable, translatedExpression);
projectVariableMapping.put(entry.getKey(), variable);
}
outputSources.add(new ProjectNode(source.getSourceLocation(), context.getIdAllocator().getNextId(), source.getSources().get(i), assignments.build(), parent.getLocality()));
outputLayout.forEach(variable -> mappings.put(variable, projectVariableMapping.get(variable)));
}
ListMultimap<VariableReferenceExpression, VariableReferenceExpression> outputsToInputs = mappings.build();
return Result.ofPlanNode(new UnionNode(source.getSourceLocation(), parent.getId(), outputSources.build(), ImmutableList.copyOf(outputsToInputs.keySet()), fromListMultimap(outputsToInputs)));
}
Aggregations