Search in sources :

Example 1 with LateralJoinNode

use of com.facebook.presto.sql.planner.plan.LateralJoinNode in project presto by prestodb.

the class TransformCorrelatedScalarSubquery method apply.

@Override
public Result apply(LateralJoinNode lateralJoinNode, Captures captures, Context context) {
    PlanNode subquery = context.getLookup().resolve(lateralJoinNode.getSubquery());
    if (!searchFrom(subquery, context.getLookup()).where(EnforceSingleRowNode.class::isInstance).recurseOnlyWhen(ProjectNode.class::isInstance).matches()) {
        return Result.empty();
    }
    PlanNode rewrittenSubquery = searchFrom(subquery, context.getLookup()).where(EnforceSingleRowNode.class::isInstance).recurseOnlyWhen(ProjectNode.class::isInstance).removeFirst();
    if (isAtMostScalar(rewrittenSubquery, context.getLookup())) {
        return Result.ofPlanNode(new LateralJoinNode(lateralJoinNode.getSourceLocation(), context.getIdAllocator().getNextId(), lateralJoinNode.getInput(), rewrittenSubquery, lateralJoinNode.getCorrelation(), lateralJoinNode.getType(), lateralJoinNode.getOriginSubqueryError()));
    }
    VariableReferenceExpression unique = context.getVariableAllocator().newVariable("unique", BIGINT);
    LateralJoinNode rewrittenLateralJoinNode = new LateralJoinNode(lateralJoinNode.getSourceLocation(), context.getIdAllocator().getNextId(), new AssignUniqueId(lateralJoinNode.getSourceLocation(), context.getIdAllocator().getNextId(), lateralJoinNode.getInput(), unique), rewrittenSubquery, lateralJoinNode.getCorrelation(), lateralJoinNode.getType(), lateralJoinNode.getOriginSubqueryError());
    VariableReferenceExpression isDistinct = context.getVariableAllocator().newVariable("is_distinct", BooleanType.BOOLEAN);
    MarkDistinctNode markDistinctNode = new MarkDistinctNode(rewrittenLateralJoinNode.getSourceLocation(), context.getIdAllocator().getNextId(), rewrittenLateralJoinNode, isDistinct, rewrittenLateralJoinNode.getInput().getOutputVariables(), Optional.empty());
    FilterNode filterNode = new FilterNode(markDistinctNode.getSourceLocation(), context.getIdAllocator().getNextId(), markDistinctNode, castToRowExpression(new SimpleCaseExpression(createSymbolReference(isDistinct), ImmutableList.of(new WhenClause(TRUE_LITERAL, TRUE_LITERAL)), Optional.of(new Cast(new FunctionCall(QualifiedName.of("fail"), ImmutableList.of(new LongLiteral(Integer.toString(SUBQUERY_MULTIPLE_ROWS.toErrorCode().getCode())), new StringLiteral("Scalar sub-query has returned multiple rows"))), BOOLEAN)))));
    return Result.ofPlanNode(new ProjectNode(context.getIdAllocator().getNextId(), filterNode, identityAssignmentsAsSymbolReferences(lateralJoinNode.getOutputVariables())));
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) MarkDistinctNode(com.facebook.presto.spi.plan.MarkDistinctNode) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) FilterNode(com.facebook.presto.spi.plan.FilterNode) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) WhenClause(com.facebook.presto.sql.tree.WhenClause) PlanNode(com.facebook.presto.spi.plan.PlanNode) AssignUniqueId(com.facebook.presto.sql.planner.plan.AssignUniqueId) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) EnforceSingleRowNode(com.facebook.presto.sql.planner.plan.EnforceSingleRowNode) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) FunctionCall(com.facebook.presto.sql.tree.FunctionCall)

Example 2 with LateralJoinNode

use of com.facebook.presto.sql.planner.plan.LateralJoinNode in project presto by prestodb.

the class TransformExistsApplyToLateralNode method rewriteToDefaultAggregation.

private PlanNode rewriteToDefaultAggregation(ApplyNode parent, Context context) {
    VariableReferenceExpression count = context.getVariableAllocator().newVariable("count", BIGINT);
    VariableReferenceExpression exists = getOnlyElement(parent.getSubqueryAssignments().getVariables());
    return new LateralJoinNode(parent.getSourceLocation(), parent.getId(), parent.getInput(), new ProjectNode(context.getIdAllocator().getNextId(), new AggregationNode(parent.getSourceLocation(), context.getIdAllocator().getNextId(), parent.getSubquery(), ImmutableMap.of(count, new Aggregation(new CallExpression(exists.getSourceLocation(), "count", functionResolution.countFunction(), BIGINT, ImmutableList.of()), Optional.empty(), Optional.empty(), false, Optional.empty())), globalAggregation(), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty()), Assignments.of(exists, castToRowExpression(new ComparisonExpression(GREATER_THAN, asSymbolReference(count), new Cast(new LongLiteral("0"), BIGINT.toString()))))), parent.getCorrelation(), INNER, parent.getOriginSubqueryError());
}
Also used : AggregationNode.globalAggregation(com.facebook.presto.spi.plan.AggregationNode.globalAggregation) Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) Cast(com.facebook.presto.sql.tree.Cast) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) CallExpression(com.facebook.presto.spi.relation.CallExpression)

Example 3 with LateralJoinNode

use of com.facebook.presto.sql.planner.plan.LateralJoinNode in project presto by prestodb.

the class TransformCorrelatedScalarAggregationToJoin method apply.

@Override
public Result apply(LateralJoinNode lateralJoinNode, Captures captures, Context context) {
    PlanNode subquery = lateralJoinNode.getSubquery();
    if (!isScalar(subquery, context.getLookup())) {
        return Result.empty();
    }
    Optional<AggregationNode> aggregation = findAggregation(subquery, context.getLookup());
    if (!(aggregation.isPresent() && aggregation.get().getGroupingKeys().isEmpty())) {
        return Result.empty();
    }
    ScalarAggregationToJoinRewriter rewriter = new ScalarAggregationToJoinRewriter(functionAndTypeManager, context.getVariableAllocator(), context.getIdAllocator(), context.getLookup());
    PlanNode rewrittenNode = rewriter.rewriteScalarAggregation(lateralJoinNode, aggregation.get());
    if (rewrittenNode instanceof LateralJoinNode) {
        return Result.empty();
    }
    return Result.ofPlanNode(rewrittenNode);
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) ScalarAggregationToJoinRewriter(com.facebook.presto.sql.planner.optimizations.ScalarAggregationToJoinRewriter) AggregationNode(com.facebook.presto.spi.plan.AggregationNode)

Example 4 with LateralJoinNode

use of com.facebook.presto.sql.planner.plan.LateralJoinNode in project presto by prestodb.

the class CheckSubqueryNodesAreRewritten method optimize.

@Override
public PlanNode optimize(PlanNode plan, Session session, TypeProvider types, PlanVariableAllocator variableAllocator, PlanNodeIdAllocator idAllocator, WarningCollector warningCollector) {
    searchFrom(plan).where(ApplyNode.class::isInstance).findFirst().ifPresent(node -> {
        ApplyNode applyNode = (ApplyNode) node;
        error(applyNode.getCorrelation(), applyNode.getOriginSubqueryError());
    });
    searchFrom(plan).where(LateralJoinNode.class::isInstance).findFirst().ifPresent(node -> {
        LateralJoinNode lateralJoinNode = (LateralJoinNode) node;
        error(lateralJoinNode.getCorrelation(), lateralJoinNode.getOriginSubqueryError());
    });
    return plan;
}
Also used : LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) ApplyNode(com.facebook.presto.sql.planner.plan.ApplyNode)

Example 5 with LateralJoinNode

use of com.facebook.presto.sql.planner.plan.LateralJoinNode in project presto by prestodb.

the class SubqueryPlanner method appendLateralJoin.

public PlanBuilder appendLateralJoin(PlanBuilder subPlan, PlanBuilder subqueryPlan, Query query, boolean correlationAllowed, LateralJoinNode.Type type) {
    PlanNode subqueryNode = subqueryPlan.getRoot();
    Map<Expression, Expression> correlation = extractCorrelation(subPlan, subqueryNode);
    if (!correlationAllowed && !correlation.isEmpty()) {
        throw notSupportedException(query, "Correlated subquery in given context");
    }
    subqueryNode = replaceExpressionsWithSymbols(subqueryNode, correlation);
    return new PlanBuilder(subPlan.copyTranslations(), new LateralJoinNode(subPlan.getRoot().getSourceLocation(), idAllocator.getNextId(), subPlan.getRoot(), subqueryNode, ImmutableList.copyOf(VariablesExtractor.extractUnique(correlation.values(), variableAllocator.getTypes())), type, subQueryNotSupportedError(query, "Given correlated subquery")));
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) ExpressionNodeInliner.replaceExpression(com.facebook.presto.sql.planner.ExpressionNodeInliner.replaceExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) OriginalExpressionUtils.castToExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression)

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