Search in sources :

Example 66 with PlanNode

use of com.facebook.presto.spi.plan.PlanNode in project presto by prestodb.

the class LogicalPlanner method createExplainAnalyzePlan.

private RelationPlan createExplainAnalyzePlan(Analysis analysis, Explain statement) {
    RelationPlan underlyingPlan = planStatementWithoutOutput(analysis, statement.getStatement());
    PlanNode root = underlyingPlan.getRoot();
    Scope scope = analysis.getScope(statement);
    VariableReferenceExpression outputVariable = variableAllocator.newVariable(scope.getRelationType().getFieldByIndex(0));
    root = new ExplainAnalyzeNode(getSourceLocation(statement), idAllocator.getNextId(), root, outputVariable, statement.isVerbose());
    return new RelationPlan(root, scope, ImmutableList.of(outputVariable));
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) Scope(com.facebook.presto.sql.analyzer.Scope) ExplainAnalyzeNode(com.facebook.presto.sql.planner.plan.ExplainAnalyzeNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression)

Example 67 with PlanNode

use of com.facebook.presto.spi.plan.PlanNode in project presto by prestodb.

the class RelationPlanner method visitAliasedRelation.

@Override
protected RelationPlan visitAliasedRelation(AliasedRelation node, Void context) {
    RelationPlan subPlan = process(node.getRelation(), context);
    PlanNode root = subPlan.getRoot();
    List<VariableReferenceExpression> mappings = subPlan.getFieldMappings();
    if (node.getColumnNames() != null) {
        ImmutableList.Builder<VariableReferenceExpression> newMappings = ImmutableList.builder();
        Assignments.Builder assignments = Assignments.builder();
        // project only the visible columns from the underlying relation
        for (int i = 0; i < subPlan.getDescriptor().getAllFieldCount(); i++) {
            Field field = subPlan.getDescriptor().getFieldByIndex(i);
            if (!field.isHidden()) {
                VariableReferenceExpression aliasedColumn = variableAllocator.newVariable(mappings.get(i).getSourceLocation(), field);
                assignments.put(aliasedColumn, castToRowExpression(asSymbolReference(subPlan.getFieldMappings().get(i))));
                newMappings.add(aliasedColumn);
            }
        }
        root = new ProjectNode(getSourceLocation(node.getLocation()), idAllocator.getNextId(), subPlan.getRoot(), assignments.build(), LOCAL);
        mappings = newMappings.build();
    }
    return new RelationPlan(root, analysis.getScope(node), mappings);
}
Also used : Field(com.facebook.presto.sql.analyzer.Field) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Assignments(com.facebook.presto.spi.plan.Assignments) ProjectNode(com.facebook.presto.spi.plan.ProjectNode)

Example 68 with PlanNode

use of com.facebook.presto.spi.plan.PlanNode in project presto by prestodb.

the class CanonicalPlanGenerator method visitAggregation.

@Override
public Optional<PlanNode> visitAggregation(AggregationNode node, Map<VariableReferenceExpression, VariableReferenceExpression> context) {
    Optional<PlanNode> source = node.getSource().accept(this, context);
    if (!source.isPresent()) {
        return Optional.empty();
    }
    // Steps to get canonical aggregations:
    // 1. Transform aggregation into canonical form
    // 2. Sort based on canonical aggregation expression
    // 3. Get new variable reference for aggregation expression
    // 4. Record mapping from original variable reference to the new one
    List<AggregationReference> aggregationReferences = node.getAggregations().entrySet().stream().map(entry -> new AggregationReference(getCanonicalAggregation(entry.getValue(), context), entry.getKey())).sorted(comparing(aggregationReference -> aggregationReference.getAggregation().getCall().toString())).collect(toImmutableList());
    ImmutableMap.Builder<VariableReferenceExpression, Aggregation> aggregations = ImmutableMap.builder();
    for (AggregationReference aggregationReference : aggregationReferences) {
        VariableReferenceExpression reference = variableAllocator.newVariable(aggregationReference.getAggregation().getCall());
        context.put(aggregationReference.getVariableReferenceExpression(), reference);
        aggregations.put(reference, aggregationReference.getAggregation());
    }
    return Optional.of(new AggregationNode(node.getSourceLocation(), planNodeidAllocator.getNextId(), source.get(), aggregations.build(), getCanonicalGroupingSetDescriptor(node.getGroupingSets(), context), node.getPreGroupedVariables().stream().map(context::get).collect(toImmutableList()), node.getStep(), node.getHashVariable().map(ignored -> variableAllocator.newHashVariable()), node.getGroupIdVariable().map(context::get)));
}
Also used : Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 69 with PlanNode

use of com.facebook.presto.spi.plan.PlanNode in project presto by prestodb.

the class PlanFragmenter method reassignPartitioningHandleIfNecessaryHelper.

private SubPlan reassignPartitioningHandleIfNecessaryHelper(Session session, SubPlan subPlan, PartitioningHandle newOutputPartitioningHandle) {
    PlanFragment fragment = subPlan.getFragment();
    PlanNode newRoot = fragment.getRoot();
    // If the fragment's partitioning is SINGLE or COORDINATOR_ONLY, leave the sources as is (this is for single-node execution)
    if (!fragment.getPartitioning().isSingleNode()) {
        PartitioningHandleReassigner partitioningHandleReassigner = new PartitioningHandleReassigner(fragment.getPartitioning(), metadata, session);
        newRoot = SimplePlanRewriter.rewriteWith(partitioningHandleReassigner, newRoot);
    }
    PartitioningScheme outputPartitioningScheme = fragment.getPartitioningScheme();
    Partitioning newOutputPartitioning = outputPartitioningScheme.getPartitioning();
    if (outputPartitioningScheme.getPartitioning().getHandle().getConnectorId().isPresent()) {
        // Do not replace the handle if the source's output handle is a system one, e.g. broadcast.
        newOutputPartitioning = newOutputPartitioning.withAlternativePartitiongingHandle(newOutputPartitioningHandle);
    }
    PlanFragment newFragment = new PlanFragment(fragment.getId(), newRoot, fragment.getVariables(), fragment.getPartitioning(), fragment.getTableScanSchedulingOrder(), new PartitioningScheme(newOutputPartitioning, outputPartitioningScheme.getOutputLayout(), outputPartitioningScheme.getHashColumn(), outputPartitioningScheme.isReplicateNullsAndAny(), outputPartitioningScheme.getBucketToPartition()), fragment.getStageExecutionDescriptor(), fragment.isOutputTableWriterFragment(), fragment.getStatsAndCosts(), fragment.getJsonRepresentation());
    ImmutableList.Builder<SubPlan> childrenBuilder = ImmutableList.builder();
    for (SubPlan child : subPlan.getChildren()) {
        childrenBuilder.add(reassignPartitioningHandleIfNecessaryHelper(session, child, fragment.getPartitioning()));
    }
    return new SubPlan(newFragment, childrenBuilder.build());
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) TablePartitioning(com.facebook.presto.metadata.TableLayout.TablePartitioning) SystemPartitioningHandle.isCompatibleSystemPartitioning(com.facebook.presto.sql.planner.SystemPartitioningHandle.isCompatibleSystemPartitioning) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList)

Example 70 with PlanNode

use of com.facebook.presto.spi.plan.PlanNode in project presto by prestodb.

the class TestDruidPlanOptimizer method testUnionAll.

@Test
public void testUnionAll() {
    PlanBuilder planBuilder = createPlanBuilder(defaultSessionHolder);
    PlanVariableAllocator variableAllocator = new PlanVariableAllocator();
    AggregationNode aggregationOne = simpleAggregationSum(planBuilder, tableScan(planBuilder, druidTableOne, city, fare), variableAllocator, ImmutableList.of(city), fare);
    AggregationNode aggregationTwo = simpleAggregationSum(planBuilder, tableScan(planBuilder, druidTableTwo, city, fare), variableAllocator, ImmutableList.of(city), fare);
    VariableReferenceExpression groupByColumn = variableAllocator.newVariable(city.getColumnName(), city.getColumnType());
    VariableReferenceExpression sumColumn = variableAllocator.newVariable(fare.getColumnName(), fare.getColumnType());
    PlanNode originalPlan = new UnionNode(Optional.empty(), planBuilder.getIdAllocator().getNextId(), ImmutableList.of(aggregationOne, aggregationTwo), ImmutableList.of(groupByColumn, sumColumn), ImmutableMap.of(groupByColumn, Stream.concat(aggregationOne.getGroupingKeys().stream(), aggregationTwo.getGroupingKeys().stream()).collect(toImmutableList()), sumColumn, ImmutableList.of(Iterables.getOnlyElement(aggregationOne.getAggregations().keySet()), Iterables.getOnlyElement(aggregationTwo.getAggregations().keySet()))));
    PlanNode optimizedPlan = getOptimizedPlan(planBuilder, originalPlan);
    PlanMatchPattern tableScanMatcherOne = DruidTableScanMatcher.match(druidTableOne.getTableName(), "SELECT \"city\", sum(fare) FROM \"realtimeOnly\" GROUP BY \"city\"");
    PlanMatchPattern tableScanMatcherTwo = DruidTableScanMatcher.match(druidTableTwo.getTableName(), "SELECT \"city\", sum(fare) FROM \"hybrid\" GROUP BY \"city\"");
    assertPlanMatch(optimizedPlan, PlanMatchPattern.union(tableScanMatcherOne, tableScanMatcherTwo), typeProvider);
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) UnionNode(com.facebook.presto.spi.plan.UnionNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) PlanMatchPattern(com.facebook.presto.sql.planner.assertions.PlanMatchPattern) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) Test(org.testng.annotations.Test)

Aggregations

PlanNode (com.facebook.presto.spi.plan.PlanNode)228 Test (org.testng.annotations.Test)114 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)94 ImmutableList (com.google.common.collect.ImmutableList)56 RowExpression (com.facebook.presto.spi.relation.RowExpression)45 ImmutableMap (com.google.common.collect.ImmutableMap)44 PlanBuilder (com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)42 TableScanNode (com.facebook.presto.spi.plan.TableScanNode)41 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)40 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)40 Map (java.util.Map)39 Optional (java.util.Optional)38 List (java.util.List)36 JoinNode (com.facebook.presto.sql.planner.plan.JoinNode)35 Set (java.util.Set)30 Assert.assertEquals (org.testng.Assert.assertEquals)23 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)22 OrderingScheme (com.facebook.presto.spi.plan.OrderingScheme)20 Function (java.util.function.Function)20 Metadata (com.facebook.presto.metadata.Metadata)19