Search in sources :

Example 1 with REMOTE

use of com.facebook.presto.spi.plan.ProjectNode.Locality.REMOTE in project presto by prestodb.

the class InlineProjections method apply.

@Override
public Result apply(ProjectNode parent, Captures captures, Context context) {
    ProjectNode child = captures.get(CHILD);
    // Do not inline remote projections, or if parent and child has different locality
    if (parent.getLocality().equals(REMOTE) || child.getLocality().equals(REMOTE) || !parent.getLocality().equals(child.getLocality())) {
        return Result.empty();
    }
    Sets.SetView<VariableReferenceExpression> targets = extractInliningTargets(parent, child, context);
    if (targets.isEmpty()) {
        return Result.empty();
    }
    // inline the expressions
    Assignments assignments = child.getAssignments().filter(targets::contains);
    Map<VariableReferenceExpression, RowExpression> parentAssignments = parent.getAssignments().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> inlineReferences(entry.getValue(), assignments, context.getVariableAllocator().getTypes())));
    // Synthesize identity assignments for the inputs of expressions that were inlined
    // to place in the child projection.
    // If all assignments end up becoming identity assignments, they'll get pruned by
    // other rules
    Set<VariableReferenceExpression> inputs = child.getAssignments().entrySet().stream().filter(entry -> targets.contains(entry.getKey())).map(Map.Entry::getValue).flatMap(expression -> extractInputs(expression, context.getVariableAllocator().getTypes()).stream()).collect(toSet());
    Builder childAssignments = Assignments.builder();
    for (Map.Entry<VariableReferenceExpression, RowExpression> assignment : child.getAssignments().entrySet()) {
        if (!targets.contains(assignment.getKey())) {
            childAssignments.put(assignment);
        }
    }
    boolean allTranslated = child.getAssignments().entrySet().stream().map(Map.Entry::getValue).noneMatch(OriginalExpressionUtils::isExpression);
    for (VariableReferenceExpression input : inputs) {
        if (allTranslated) {
            childAssignments.put(input, input);
        } else {
            childAssignments.put(identityAsSymbolReference(input));
        }
    }
    return Result.ofPlanNode(new ProjectNode(parent.getSourceLocation(), parent.getId(), new ProjectNode(parent.getSourceLocation(), child.getId(), child.getSource(), childAssignments.build(), child.getLocality()), Assignments.copyOf(parentAssignments), parent.getLocality()));
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) Builder(com.facebook.presto.spi.plan.Assignments.Builder) OriginalExpressionUtils(com.facebook.presto.sql.relational.OriginalExpressionUtils) OriginalExpressionUtils.isExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.isExpression) Captures(com.facebook.presto.matching.Captures) Assignments(com.facebook.presto.spi.plan.Assignments) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) AstUtils(com.facebook.presto.sql.util.AstUtils) Function(java.util.function.Function) Pattern(com.facebook.presto.matching.Pattern) OriginalExpressionUtils.castToExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression) Capture(com.facebook.presto.matching.Capture) Literal(com.facebook.presto.sql.tree.Literal) TypeProvider(com.facebook.presto.sql.planner.TypeProvider) TryExpression(com.facebook.presto.sql.tree.TryExpression) Map(java.util.Map) AssignmentUtils.isIdentity(com.facebook.presto.sql.planner.plan.AssignmentUtils.isIdentity) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) CallExpression(com.facebook.presto.spi.relation.CallExpression) Collectors.toSet(java.util.stream.Collectors.toSet) VariablesExtractor(com.facebook.presto.sql.planner.VariablesExtractor) RowExpression(com.facebook.presto.spi.relation.RowExpression) ImmutableSet(com.google.common.collect.ImmutableSet) RowExpressionVariableInliner(com.facebook.presto.sql.planner.RowExpressionVariableInliner) Rule(com.facebook.presto.sql.planner.iterative.Rule) Patterns.project(com.facebook.presto.sql.planner.plan.Patterns.project) Set(java.util.Set) Collectors(java.util.stream.Collectors) ExpressionVariableInliner(com.facebook.presto.sql.planner.ExpressionVariableInliner) Sets(com.google.common.collect.Sets) Patterns.source(com.facebook.presto.sql.planner.plan.Patterns.source) List(java.util.List) REMOTE(com.facebook.presto.spi.plan.ProjectNode.Locality.REMOTE) Expression(com.facebook.presto.sql.tree.Expression) DefaultRowExpressionTraversalVisitor(com.facebook.presto.expressions.DefaultRowExpressionTraversalVisitor) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) Capture.newCapture(com.facebook.presto.matching.Capture.newCapture) AssignmentUtils.identityAsSymbolReference(com.facebook.presto.sql.planner.plan.AssignmentUtils.identityAsSymbolReference) ExpressionTreeUtils.createSymbolReference(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) Builder(com.facebook.presto.spi.plan.Assignments.Builder) Assignments(com.facebook.presto.spi.plan.Assignments) RowExpression(com.facebook.presto.spi.relation.RowExpression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) Sets(com.google.common.collect.Sets) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) OriginalExpressionUtils(com.facebook.presto.sql.relational.OriginalExpressionUtils) Map(java.util.Map)

Aggregations

DefaultRowExpressionTraversalVisitor (com.facebook.presto.expressions.DefaultRowExpressionTraversalVisitor)1 Capture (com.facebook.presto.matching.Capture)1 Capture.newCapture (com.facebook.presto.matching.Capture.newCapture)1 Captures (com.facebook.presto.matching.Captures)1 Pattern (com.facebook.presto.matching.Pattern)1 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)1 Assignments (com.facebook.presto.spi.plan.Assignments)1 Builder (com.facebook.presto.spi.plan.Assignments.Builder)1 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)1 REMOTE (com.facebook.presto.spi.plan.ProjectNode.Locality.REMOTE)1 CallExpression (com.facebook.presto.spi.relation.CallExpression)1 ConstantExpression (com.facebook.presto.spi.relation.ConstantExpression)1 RowExpression (com.facebook.presto.spi.relation.RowExpression)1 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)1 ExpressionTreeUtils.createSymbolReference (com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference)1 ExpressionVariableInliner (com.facebook.presto.sql.planner.ExpressionVariableInliner)1 RowExpressionVariableInliner (com.facebook.presto.sql.planner.RowExpressionVariableInliner)1 TypeProvider (com.facebook.presto.sql.planner.TypeProvider)1 VariablesExtractor (com.facebook.presto.sql.planner.VariablesExtractor)1 Rule (com.facebook.presto.sql.planner.iterative.Rule)1