Search in sources :

Example 6 with Captures

use of com.facebook.presto.matching.Captures 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)

Example 7 with Captures

use of com.facebook.presto.matching.Captures in project presto by prestodb.

the class TransformCorrelatedLateralJoinToJoin method apply.

@Override
public Result apply(LateralJoinNode lateralJoinNode, Captures captures, Context context) {
    PlanNode subquery = lateralJoinNode.getSubquery();
    PlanNodeDecorrelator planNodeDecorrelator = new PlanNodeDecorrelator(context.getIdAllocator(), context.getVariableAllocator(), context.getLookup());
    Optional<DecorrelatedNode> decorrelatedNodeOptional = planNodeDecorrelator.decorrelateFilters(subquery, lateralJoinNode.getCorrelation());
    return decorrelatedNodeOptional.map(decorrelatedNode -> Result.ofPlanNode(new JoinNode(lateralJoinNode.getSourceLocation(), context.getIdAllocator().getNextId(), lateralJoinNode.getType().toJoinNodeType(), lateralJoinNode.getInput(), decorrelatedNode.getNode(), ImmutableList.of(), lateralJoinNode.getOutputVariables(), decorrelatedNode.getCorrelatedPredicates().map(OriginalExpressionUtils::castToRowExpression), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableMap.of()))).orElseGet(Result::empty);
}
Also used : JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) PlanNodeDecorrelator(com.facebook.presto.sql.planner.optimizations.PlanNodeDecorrelator) DecorrelatedNode(com.facebook.presto.sql.planner.optimizations.PlanNodeDecorrelator.DecorrelatedNode) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) ImmutableMap(com.google.common.collect.ImmutableMap) OriginalExpressionUtils(com.facebook.presto.sql.relational.OriginalExpressionUtils) Rule(com.facebook.presto.sql.planner.iterative.Rule) Captures(com.facebook.presto.matching.Captures) Pattern.nonEmpty(com.facebook.presto.matching.Pattern.nonEmpty) Patterns.lateralJoin(com.facebook.presto.sql.planner.plan.Patterns.lateralJoin) LateralJoin.correlation(com.facebook.presto.sql.planner.plan.Patterns.LateralJoin.correlation) Pattern(com.facebook.presto.matching.Pattern) PlanNode(com.facebook.presto.spi.plan.PlanNode) ImmutableList(com.google.common.collect.ImmutableList) Optional(java.util.Optional) PlanNodeDecorrelator(com.facebook.presto.sql.planner.optimizations.PlanNodeDecorrelator) PlanNode(com.facebook.presto.spi.plan.PlanNode) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) DecorrelatedNode(com.facebook.presto.sql.planner.optimizations.PlanNodeDecorrelator.DecorrelatedNode) OriginalExpressionUtils(com.facebook.presto.sql.relational.OriginalExpressionUtils)

Aggregations

Captures (com.facebook.presto.matching.Captures)7 Pattern (com.facebook.presto.matching.Pattern)7 Rule (com.facebook.presto.sql.planner.iterative.Rule)7 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)6 RowExpression (com.facebook.presto.spi.relation.RowExpression)5 List (java.util.List)5 Map (java.util.Map)5 Assignments (com.facebook.presto.spi.plan.Assignments)4 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)4 Patterns.project (com.facebook.presto.sql.planner.plan.Patterns.project)4 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Optional (java.util.Optional)4 Set (java.util.Set)4 Capture (com.facebook.presto.matching.Capture)3 Capture.newCapture (com.facebook.presto.matching.Capture.newCapture)3 PlanNode (com.facebook.presto.spi.plan.PlanNode)3 Patterns.source (com.facebook.presto.sql.planner.plan.Patterns.source)3 OriginalExpressionUtils (com.facebook.presto.sql.relational.OriginalExpressionUtils)3 Collectors (java.util.stream.Collectors)3