Search in sources :

Example 1 with ApplyNode

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

the class SubqueryPlanner method appendApplyNode.

private PlanBuilder appendApplyNode(PlanBuilder subPlan, Node subquery, PlanBuilder subqueryPlan, Assignments subqueryAssignments, boolean correlationAllowed) {
    PlanNode subqueryNode = subqueryPlan.getRoot();
    Map<Expression, Expression> correlation = extractCorrelation(subPlan, subqueryNode);
    if (!correlationAllowed && !correlation.isEmpty()) {
        throw notSupportedException(subquery, "Correlated subquery in given context");
    }
    subPlan = subPlan.appendProjections(correlation.keySet(), symbolAllocator, idAllocator);
    subqueryNode = replaceExpressionsWithSymbols(subqueryNode, correlation);
    TranslationMap translations = subPlan.copyTranslations();
    PlanNode root = subPlan.getRoot();
    return new PlanBuilder(translations, new ApplyNode(idAllocator.getNextId(), root, subqueryNode, subqueryAssignments, ImmutableList.copyOf(DependencyExtractor.extractUnique(correlation.values()))), analysis.getParameters());
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) ExpressionNodeInliner.replaceExpression(com.facebook.presto.sql.planner.ExpressionNodeInliner.replaceExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) ApplyNode(com.facebook.presto.sql.planner.plan.ApplyNode)

Example 2 with ApplyNode

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

the class TransformExistsApplyToScalarApply method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof ApplyNode)) {
        return Optional.empty();
    }
    ApplyNode parent = (ApplyNode) node;
    if (parent.getSubqueryAssignments().size() != 1) {
        return Optional.empty();
    }
    Expression expression = getOnlyElement(parent.getSubqueryAssignments().getExpressions());
    if (!(expression instanceof ExistsPredicate)) {
        return Optional.empty();
    }
    Symbol count = symbolAllocator.newSymbol(COUNT.toString(), BIGINT);
    Symbol exists = getOnlyElement(parent.getSubqueryAssignments().getSymbols());
    return Optional.of(new ApplyNode(node.getId(), parent.getInput(), new ProjectNode(idAllocator.getNextId(), new AggregationNode(idAllocator.getNextId(), new LimitNode(idAllocator.getNextId(), parent.getSubquery(), 1, false), ImmutableMap.of(count, COUNT_CALL), ImmutableMap.of(count, countSignature), ImmutableMap.of(), ImmutableList.of(ImmutableList.of()), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty()), Assignments.of(exists, new ComparisonExpression(GREATER_THAN, count.toSymbolReference(), new Cast(new LongLiteral("0"), BIGINT.toString())))), Assignments.of(exists, exists.toSymbolReference()), parent.getCorrelation()));
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) LimitNode(com.facebook.presto.sql.planner.plan.LimitNode) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) Symbol(com.facebook.presto.sql.planner.Symbol) ApplyNode(com.facebook.presto.sql.planner.plan.ApplyNode) ExistsPredicate(com.facebook.presto.sql.tree.ExistsPredicate) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode)

Example 3 with ApplyNode

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

the class TestVerifyNoOriginalExpression method testValidateForApplyFailed.

@Test(expectedExceptions = IllegalArgumentException.class)
public void testValidateForApplyFailed() {
    ImmutableMap<VariableReferenceExpression, RowExpression> map = ImmutableMap.of(VARIABLE_REFERENCE_EXPRESSION, castToRowExpression(new SymbolReference("count")));
    Assignments assignments = new Assignments(map);
    ImmutableList<VariableReferenceExpression> variableReferenceExpressions = ImmutableList.of(VARIABLE_REFERENCE_EXPRESSION);
    ApplyNode applyNode = builder.apply(assignments, variableReferenceExpressions, valuesNode, valuesNode);
    testValidation(applyNode);
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) ApplyNode(com.facebook.presto.sql.planner.plan.ApplyNode) Assignments(com.facebook.presto.spi.plan.Assignments) RowExpression(com.facebook.presto.spi.relation.RowExpression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) Test(org.testng.annotations.Test)

Example 4 with ApplyNode

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

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

the class SubqueryPlanner method appendApplyNode.

private PlanBuilder appendApplyNode(PlanBuilder subPlan, Node subquery, PlanNode subqueryNode, Assignments subqueryAssignments, boolean correlationAllowed) {
    Map<Expression, Expression> correlation = extractCorrelation(subPlan, subqueryNode);
    if (!correlationAllowed && !correlation.isEmpty()) {
        throw notSupportedException(subquery, "Correlated subquery in given context");
    }
    subPlan = subPlan.appendProjections(correlation.keySet(), variableAllocator, idAllocator);
    subqueryNode = replaceExpressionsWithSymbols(subqueryNode, correlation);
    TranslationMap translations = subPlan.copyTranslations();
    PlanNode root = subPlan.getRoot();
    verifySubquerySupported(subqueryAssignments);
    return new PlanBuilder(translations, new ApplyNode(subqueryNode.getSourceLocation(), idAllocator.getNextId(), root, subqueryNode, subqueryAssignments, ImmutableList.copyOf(VariablesExtractor.extractUnique(correlation.values(), variableAllocator.getTypes())), subQueryNotSupportedError(subquery, "Given correlated subquery")));
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) 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) ApplyNode(com.facebook.presto.sql.planner.plan.ApplyNode)

Aggregations

ApplyNode (com.facebook.presto.sql.planner.plan.ApplyNode)6 Expression (com.facebook.presto.sql.tree.Expression)3 RowExpression (com.facebook.presto.spi.relation.RowExpression)2 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)2 ExpressionNodeInliner.replaceExpression (com.facebook.presto.sql.planner.ExpressionNodeInliner.replaceExpression)2 OriginalExpressionUtils.castToRowExpression (com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression)2 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)2 NotExpression (com.facebook.presto.sql.tree.NotExpression)2 QuantifiedComparisonExpression (com.facebook.presto.sql.tree.QuantifiedComparisonExpression)2 SubqueryExpression (com.facebook.presto.sql.tree.SubqueryExpression)2 Assignments (com.facebook.presto.spi.plan.Assignments)1 PlanNode (com.facebook.presto.spi.plan.PlanNode)1 Symbol (com.facebook.presto.sql.planner.Symbol)1 BasePlanTest (com.facebook.presto.sql.planner.assertions.BasePlanTest)1 AggregationNode (com.facebook.presto.sql.planner.plan.AggregationNode)1 LateralJoinNode (com.facebook.presto.sql.planner.plan.LateralJoinNode)1 LimitNode (com.facebook.presto.sql.planner.plan.LimitNode)1 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)1 ProjectNode (com.facebook.presto.sql.planner.plan.ProjectNode)1 OriginalExpressionUtils.castToExpression (com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression)1