Search in sources :

Example 1 with PlanNodeDecorrelator

use of io.prestosql.sql.planner.optimizations.PlanNodeDecorrelator in project hetu-core by openlookeng.

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.getLookup());
    Optional<DecorrelatedNode> decorrelatedNodeOptional = planNodeDecorrelator.decorrelateFilters(subquery, lateralJoinNode.getCorrelation());
    return decorrelatedNodeOptional.map(decorrelatedNode -> {
        Expression joinFilter = combineConjuncts(decorrelatedNode.getCorrelatedPredicates().orElse(TRUE_LITERAL), lateralJoinNode.getFilter());
        return Result.ofPlanNode(new JoinNode(context.getIdAllocator().getNextId(), lateralJoinNode.getType().toJoinNodeType(), lateralJoinNode.getInput(), decorrelatedNode.getNode(), ImmutableList.of(), lateralJoinNode.getOutputSymbols(), joinFilter.equals(TRUE_LITERAL) ? Optional.empty() : Optional.of(castToRowExpression(joinFilter)), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableMap.of()));
    }).orElseGet(Result::empty);
}
Also used : Patterns.lateralJoin(io.prestosql.sql.planner.plan.Patterns.lateralJoin) LateralJoin.correlation(io.prestosql.sql.planner.plan.Patterns.LateralJoin.correlation) ExpressionUtils.combineConjuncts(io.prestosql.sql.ExpressionUtils.combineConjuncts) ImmutableMap(com.google.common.collect.ImmutableMap) Rule(io.prestosql.sql.planner.iterative.Rule) Pattern.nonEmpty(io.prestosql.matching.Pattern.nonEmpty) PlanNodeDecorrelator(io.prestosql.sql.planner.optimizations.PlanNodeDecorrelator) LateralJoinNode(io.prestosql.sql.planner.plan.LateralJoinNode) Pattern(io.prestosql.matching.Pattern) PlanNode(io.prestosql.spi.plan.PlanNode) TRUE_LITERAL(io.prestosql.sql.tree.BooleanLiteral.TRUE_LITERAL) Captures(io.prestosql.matching.Captures) ImmutableList(com.google.common.collect.ImmutableList) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) Optional(java.util.Optional) DecorrelatedNode(io.prestosql.sql.planner.optimizations.PlanNodeDecorrelator.DecorrelatedNode) Expression(io.prestosql.sql.tree.Expression) JoinNode(io.prestosql.spi.plan.JoinNode) PlanNodeDecorrelator(io.prestosql.sql.planner.optimizations.PlanNodeDecorrelator) PlanNode(io.prestosql.spi.plan.PlanNode) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) Expression(io.prestosql.sql.tree.Expression) LateralJoinNode(io.prestosql.sql.planner.plan.LateralJoinNode) JoinNode(io.prestosql.spi.plan.JoinNode) DecorrelatedNode(io.prestosql.sql.planner.optimizations.PlanNodeDecorrelator.DecorrelatedNode)

Example 2 with PlanNodeDecorrelator

use of io.prestosql.sql.planner.optimizations.PlanNodeDecorrelator in project hetu-core by openlookeng.

the class TransformExistsApplyToLateralNode method rewriteToNonDefaultAggregation.

private Optional<PlanNode> rewriteToNonDefaultAggregation(ApplyNode applyNode, Context context) {
    checkState(applyNode.getSubquery().getOutputSymbols().isEmpty(), "Expected subquery output symbols to be pruned");
    Symbol exists = getOnlyElement(applyNode.getSubqueryAssignments().getSymbols());
    Symbol subqueryTrue = context.getSymbolAllocator().newSymbol("subqueryTrue", BOOLEAN);
    Assignments.Builder assignments = Assignments.builder();
    assignments.putAll(AssignmentUtils.identityAsSymbolReferences(applyNode.getInput().getOutputSymbols()));
    assignments.put(exists, castToRowExpression(new CoalesceExpression(ImmutableList.of(toSymbolReference(subqueryTrue), BooleanLiteral.FALSE_LITERAL))));
    PlanNode subquery = new ProjectNode(context.getIdAllocator().getNextId(), new LimitNode(context.getIdAllocator().getNextId(), applyNode.getSubquery(), 1L, false), Assignments.of(subqueryTrue, castToRowExpression(TRUE_LITERAL)));
    PlanNodeDecorrelator decorrelator = new PlanNodeDecorrelator(context.getIdAllocator(), context.getLookup());
    if (!decorrelator.decorrelateFilters(subquery, applyNode.getCorrelation()).isPresent()) {
        return Optional.empty();
    }
    return Optional.of(new ProjectNode(context.getIdAllocator().getNextId(), new LateralJoinNode(applyNode.getId(), applyNode.getInput(), subquery, applyNode.getCorrelation(), LEFT, TRUE_LITERAL, applyNode.getOriginSubquery()), assignments.build()));
}
Also used : PlanNodeDecorrelator(io.prestosql.sql.planner.optimizations.PlanNodeDecorrelator) PlanNode(io.prestosql.spi.plan.PlanNode) LateralJoinNode(io.prestosql.sql.planner.plan.LateralJoinNode) LimitNode(io.prestosql.spi.plan.LimitNode) Symbol(io.prestosql.spi.plan.Symbol) Assignments(io.prestosql.spi.plan.Assignments) ProjectNode(io.prestosql.spi.plan.ProjectNode) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression)

Aggregations

PlanNode (io.prestosql.spi.plan.PlanNode)2 PlanNodeDecorrelator (io.prestosql.sql.planner.optimizations.PlanNodeDecorrelator)2 LateralJoinNode (io.prestosql.sql.planner.plan.LateralJoinNode)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Captures (io.prestosql.matching.Captures)1 Pattern (io.prestosql.matching.Pattern)1 Pattern.nonEmpty (io.prestosql.matching.Pattern.nonEmpty)1 Assignments (io.prestosql.spi.plan.Assignments)1 JoinNode (io.prestosql.spi.plan.JoinNode)1 LimitNode (io.prestosql.spi.plan.LimitNode)1 ProjectNode (io.prestosql.spi.plan.ProjectNode)1 Symbol (io.prestosql.spi.plan.Symbol)1 ExpressionUtils.combineConjuncts (io.prestosql.sql.ExpressionUtils.combineConjuncts)1 Rule (io.prestosql.sql.planner.iterative.Rule)1 DecorrelatedNode (io.prestosql.sql.planner.optimizations.PlanNodeDecorrelator.DecorrelatedNode)1 LateralJoin.correlation (io.prestosql.sql.planner.plan.Patterns.LateralJoin.correlation)1 Patterns.lateralJoin (io.prestosql.sql.planner.plan.Patterns.lateralJoin)1 OriginalExpressionUtils.castToRowExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression)1 TRUE_LITERAL (io.prestosql.sql.tree.BooleanLiteral.TRUE_LITERAL)1