Search in sources :

Example 6 with LateralJoinNode

use of com.facebook.presto.sql.planner.plan.LateralJoinNode 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 7 with LateralJoinNode

use of com.facebook.presto.sql.planner.plan.LateralJoinNode 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 8 with LateralJoinNode

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

LateralJoinNode (com.facebook.presto.sql.planner.plan.LateralJoinNode)8 PlanNode (com.facebook.presto.spi.plan.PlanNode)5 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)5 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)4 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)3 Assignments (com.facebook.presto.spi.plan.Assignments)2 PlanNodeDecorrelator (com.facebook.presto.sql.planner.optimizations.PlanNodeDecorrelator)2 AssignUniqueId (com.facebook.presto.sql.planner.plan.AssignUniqueId)2 JoinNode (com.facebook.presto.sql.planner.plan.JoinNode)2 Cast (com.facebook.presto.sql.tree.Cast)2 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)2 Captures (com.facebook.presto.matching.Captures)1 Pattern (com.facebook.presto.matching.Pattern)1 Pattern.nonEmpty (com.facebook.presto.matching.Pattern.nonEmpty)1 Aggregation (com.facebook.presto.spi.plan.AggregationNode.Aggregation)1 AggregationNode.globalAggregation (com.facebook.presto.spi.plan.AggregationNode.globalAggregation)1 FilterNode (com.facebook.presto.spi.plan.FilterNode)1 LimitNode (com.facebook.presto.spi.plan.LimitNode)1 MarkDistinctNode (com.facebook.presto.spi.plan.MarkDistinctNode)1 CallExpression (com.facebook.presto.spi.relation.CallExpression)1