Search in sources :

Example 6 with UnionNode

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);
}
Also used : PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) Type(com.facebook.presto.common.type.Type) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) UnionNode(com.facebook.presto.spi.plan.UnionNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Test(org.testng.annotations.Test)

Example 7 with UnionNode

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());
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) UnionNode(com.facebook.presto.spi.plan.UnionNode)

Example 8 with UnionNode

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()))));
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) UnionNode(com.facebook.presto.spi.plan.UnionNode) LimitNode(com.facebook.presto.spi.plan.LimitNode) ImmutableList(com.google.common.collect.ImmutableList)

Example 9 with UnionNode

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)));
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) UnionNode(com.facebook.presto.spi.plan.UnionNode) ImmutableList(com.google.common.collect.ImmutableList) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ArrayList(java.util.ArrayList) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap)

Example 10 with UnionNode

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)));
}
Also used : HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) Assignments(com.facebook.presto.spi.plan.Assignments) RowExpression(com.facebook.presto.spi.relation.RowExpression) PlanNode(com.facebook.presto.spi.plan.PlanNode) UnionNode(com.facebook.presto.spi.plan.UnionNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

UnionNode (com.facebook.presto.spi.plan.UnionNode)10 PlanNode (com.facebook.presto.spi.plan.PlanNode)9 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)7 Test (org.testng.annotations.Test)5 ImmutableList (com.google.common.collect.ImmutableList)4 RowExpression (com.facebook.presto.spi.relation.RowExpression)2 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)2 Map (java.util.Map)2 Type (com.facebook.presto.common.type.Type)1 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)1 Assignments (com.facebook.presto.spi.plan.Assignments)1 LimitNode (com.facebook.presto.spi.plan.LimitNode)1 PlanNodeId (com.facebook.presto.spi.plan.PlanNodeId)1 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)1 TableScanNode (com.facebook.presto.spi.plan.TableScanNode)1 PlanVariableAllocator (com.facebook.presto.sql.planner.PlanVariableAllocator)1 PlanMatchPattern (com.facebook.presto.sql.planner.assertions.PlanMatchPattern)1 PlanBuilder (com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)1 SymbolMapper (com.facebook.presto.sql.planner.optimizations.SymbolMapper)1 ImmutableMap (com.google.common.collect.ImmutableMap)1