Search in sources :

Example 16 with Assignments

use of com.facebook.presto.spi.plan.Assignments 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)

Example 17 with Assignments

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

the class TestVerifyNoOriginalExpression method validateJoin.

private void validateJoin(RowExpression rowExpressionPredicate, Expression expressionPredicate, boolean ifRowExpression) {
    ImmutableMap<VariableReferenceExpression, RowExpression> map = ImmutableMap.of(VARIABLE_REFERENCE_EXPRESSION, VARIABLE_REFERENCE_EXPRESSION);
    ProjectNode projectNode = builder.project(new Assignments(map), valuesNode);
    JoinNode joinNode;
    if (ifRowExpression) {
        joinNode = builder.join(JoinNode.Type.INNER, projectNode, projectNode, rowExpressionPredicate);
    } else {
        joinNode = builder.join(JoinNode.Type.INNER, projectNode, projectNode, castToRowExpression(expressionPredicate));
    }
    testValidation(joinNode);
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) SpatialJoinNode(com.facebook.presto.sql.planner.plan.SpatialJoinNode) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) Assignments(com.facebook.presto.spi.plan.Assignments) RowExpression(com.facebook.presto.spi.relation.RowExpression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) ProjectNode(com.facebook.presto.spi.plan.ProjectNode)

Example 18 with Assignments

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

the class ScalarAggregationToJoinRewriter method rewriteScalarAggregation.

private PlanNode rewriteScalarAggregation(LateralJoinNode lateralJoinNode, AggregationNode scalarAggregation, PlanNode scalarAggregationSource, Optional<Expression> joinExpression, VariableReferenceExpression nonNull) {
    AssignUniqueId inputWithUniqueColumns = new AssignUniqueId(lateralJoinNode.getSourceLocation(), idAllocator.getNextId(), lateralJoinNode.getInput(), variableAllocator.newVariable(nonNull.getSourceLocation(), "unique", BIGINT));
    JoinNode leftOuterJoin = new JoinNode(scalarAggregation.getSourceLocation(), idAllocator.getNextId(), JoinNode.Type.LEFT, inputWithUniqueColumns, scalarAggregationSource, ImmutableList.of(), ImmutableList.<VariableReferenceExpression>builder().addAll(inputWithUniqueColumns.getOutputVariables()).addAll(scalarAggregationSource.getOutputVariables()).build(), joinExpression.map(OriginalExpressionUtils::castToRowExpression), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableMap.of());
    Optional<AggregationNode> aggregationNode = createAggregationNode(scalarAggregation, leftOuterJoin, nonNull);
    if (!aggregationNode.isPresent()) {
        return lateralJoinNode;
    }
    Optional<ProjectNode> subqueryProjection = searchFrom(lateralJoinNode.getSubquery(), lookup).where(ProjectNode.class::isInstance).recurseOnlyWhen(EnforceSingleRowNode.class::isInstance).findFirst();
    List<VariableReferenceExpression> aggregationOutputVariables = getTruncatedAggregationVariables(lateralJoinNode, aggregationNode.get());
    if (subqueryProjection.isPresent()) {
        Assignments assignments = Assignments.builder().putAll(identitiesAsSymbolReferences(aggregationOutputVariables)).putAll(subqueryProjection.get().getAssignments()).build();
        return new ProjectNode(idAllocator.getNextId(), aggregationNode.get(), assignments);
    } else {
        return new ProjectNode(idAllocator.getNextId(), aggregationNode.get(), identityAssignmentsAsSymbolReferences(aggregationOutputVariables));
    }
}
Also used : AssignUniqueId(com.facebook.presto.sql.planner.plan.AssignUniqueId) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Assignments(com.facebook.presto.spi.plan.Assignments) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) ProjectNode(com.facebook.presto.spi.plan.ProjectNode)

Example 19 with Assignments

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

the class RewriteFilterWithExternalFunctionToProject method apply.

@Override
public Result apply(FilterNode node, Captures captures, Context context) {
    if (!node.getPredicate().accept(new ExternalCallExpressionChecker(functionAndTypeManager), null)) {
        // No remote function in predicate
        return Result.empty();
    }
    VariableReferenceExpression predicateVariable = context.getVariableAllocator().newVariable(node.getPredicate());
    Assignments.Builder assignments = Assignments.builder();
    node.getOutputVariables().forEach(variable -> assignments.put(variable, variable));
    Assignments identityAssignments = assignments.build();
    assignments.put(predicateVariable, node.getPredicate());
    return Result.ofPlanNode(new ProjectNode(node.getSourceLocation(), context.getIdAllocator().getNextId(), new FilterNode(node.getSourceLocation(), context.getIdAllocator().getNextId(), new ProjectNode(context.getIdAllocator().getNextId(), node.getSource(), assignments.build()), predicateVariable), identityAssignments, LOCAL));
}
Also used : ExternalCallExpressionChecker(com.facebook.presto.sql.planner.optimizations.ExternalCallExpressionChecker) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) FilterNode(com.facebook.presto.spi.plan.FilterNode) Assignments(com.facebook.presto.spi.plan.Assignments) ProjectNode(com.facebook.presto.spi.plan.ProjectNode)

Example 20 with Assignments

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

the class TransformExistsApplyToLateralNode method rewriteToNonDefaultAggregation.

private Optional<PlanNode> rewriteToNonDefaultAggregation(ApplyNode applyNode, Context context) {
    checkState(applyNode.getSubquery().getOutputVariables().isEmpty(), "Expected subquery output variables to be pruned");
    VariableReferenceExpression exists = getOnlyElement(applyNode.getSubqueryAssignments().getVariables());
    VariableReferenceExpression subqueryTrue = context.getVariableAllocator().newVariable(exists.getSourceLocation(), "subqueryTrue", BOOLEAN);
    Assignments.Builder assignments = Assignments.builder();
    assignments.putAll(identitiesAsSymbolReferences(applyNode.getInput().getOutputVariables()));
    assignments.put(exists, castToRowExpression(new CoalesceExpression(ImmutableList.of(createSymbolReference(subqueryTrue), BooleanLiteral.FALSE_LITERAL))));
    PlanNode subquery = new ProjectNode(context.getIdAllocator().getNextId(), new LimitNode(applyNode.getSourceLocation(), context.getIdAllocator().getNextId(), applyNode.getSubquery(), 1L, FINAL), Assignments.of(subqueryTrue, castToRowExpression(TRUE_LITERAL)));
    PlanNodeDecorrelator decorrelator = new PlanNodeDecorrelator(context.getIdAllocator(), context.getVariableAllocator(), context.getLookup());
    if (!decorrelator.decorrelateFilters(subquery, applyNode.getCorrelation()).isPresent()) {
        return Optional.empty();
    }
    return Optional.of(new ProjectNode(context.getIdAllocator().getNextId(), new LateralJoinNode(applyNode.getSourceLocation(), applyNode.getId(), applyNode.getInput(), subquery, applyNode.getCorrelation(), LEFT, applyNode.getOriginSubqueryError()), assignments.build()));
}
Also used : PlanNodeDecorrelator(com.facebook.presto.sql.planner.optimizations.PlanNodeDecorrelator) PlanNode(com.facebook.presto.spi.plan.PlanNode) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) LimitNode(com.facebook.presto.spi.plan.LimitNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Assignments(com.facebook.presto.spi.plan.Assignments) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression)

Aggregations

Assignments (com.facebook.presto.spi.plan.Assignments)25 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)23 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)22 OriginalExpressionUtils.castToRowExpression (com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression)12 PlanNode (com.facebook.presto.spi.plan.PlanNode)11 RowExpression (com.facebook.presto.spi.relation.RowExpression)11 Expression (com.facebook.presto.sql.tree.Expression)10 CallExpression (com.facebook.presto.spi.relation.CallExpression)8 ImmutableList (com.google.common.collect.ImmutableList)8 Cast (com.facebook.presto.sql.tree.Cast)7 SymbolReference (com.facebook.presto.sql.tree.SymbolReference)7 Map (java.util.Map)6 Type (com.facebook.presto.common.type.Type)5 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)5 FilterNode (com.facebook.presto.spi.plan.FilterNode)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 ImmutableMap (com.google.common.collect.ImmutableMap)5 Session (com.facebook.presto.Session)4 Field (com.facebook.presto.sql.analyzer.Field)4 TupleDomain (com.facebook.presto.common.predicate.TupleDomain)3