Search in sources :

Example 1 with PlanNodeDecorrelator

use of com.facebook.presto.sql.planner.optimizations.PlanNodeDecorrelator 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)

Example 2 with PlanNodeDecorrelator

use of com.facebook.presto.sql.planner.optimizations.PlanNodeDecorrelator 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

PlanNode (com.facebook.presto.spi.plan.PlanNode)2 PlanNodeDecorrelator (com.facebook.presto.sql.planner.optimizations.PlanNodeDecorrelator)2 LateralJoinNode (com.facebook.presto.sql.planner.plan.LateralJoinNode)2 Captures (com.facebook.presto.matching.Captures)1 Pattern (com.facebook.presto.matching.Pattern)1 Pattern.nonEmpty (com.facebook.presto.matching.Pattern.nonEmpty)1 Assignments (com.facebook.presto.spi.plan.Assignments)1 LimitNode (com.facebook.presto.spi.plan.LimitNode)1 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)1 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)1 Rule (com.facebook.presto.sql.planner.iterative.Rule)1 DecorrelatedNode (com.facebook.presto.sql.planner.optimizations.PlanNodeDecorrelator.DecorrelatedNode)1 JoinNode (com.facebook.presto.sql.planner.plan.JoinNode)1 LateralJoin.correlation (com.facebook.presto.sql.planner.plan.Patterns.LateralJoin.correlation)1 Patterns.lateralJoin (com.facebook.presto.sql.planner.plan.Patterns.lateralJoin)1 OriginalExpressionUtils (com.facebook.presto.sql.relational.OriginalExpressionUtils)1 CoalesceExpression (com.facebook.presto.sql.tree.CoalesceExpression)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Optional (java.util.Optional)1