Search in sources :

Example 1 with ExistsPredicate

use of com.facebook.presto.sql.tree.ExistsPredicate 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 2 with ExistsPredicate

use of com.facebook.presto.sql.tree.ExistsPredicate in project presto by prestodb.

the class TransformExistsApplyToLateralNode method apply.

@Override
public Result apply(ApplyNode parent, Captures captures, Context context) {
    if (parent.getSubqueryAssignments().size() != 1) {
        return Result.empty();
    }
    Expression expression = castToExpression(getOnlyElement(parent.getSubqueryAssignments().getExpressions()));
    if (!(expression instanceof ExistsPredicate)) {
        return Result.empty();
    }
    Optional<PlanNode> nonDefaultAggregation = rewriteToNonDefaultAggregation(parent, context);
    return nonDefaultAggregation.map(Result::ofPlanNode).orElseGet(() -> Result.ofPlanNode(rewriteToDefaultAggregation(parent, context)));
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) OriginalExpressionUtils.castToExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) ExistsPredicate(com.facebook.presto.sql.tree.ExistsPredicate)

Example 3 with ExistsPredicate

use of com.facebook.presto.sql.tree.ExistsPredicate in project presto by prestodb.

the class SubqueryPlanner method appendExistSubqueryApplyNode.

/**
 * Exists is modeled as:
 * <pre>
 *     - Project($0 > 0)
 *       - Aggregation(COUNT(*))
 *         - Limit(1)
 *           -- subquery
 * </pre>
 */
private PlanBuilder appendExistSubqueryApplyNode(PlanBuilder subPlan, ExistsPredicate existsPredicate, boolean correlationAllowed) {
    if (subPlan.canTranslate(existsPredicate)) {
        // given subquery is already appended
        return subPlan;
    }
    PlanBuilder subqueryPlan = createPlanBuilder(existsPredicate.getSubquery());
    PlanNode subqueryPlanRoot = subqueryPlan.getRoot();
    if (isAggregationWithEmptyGroupBy(subqueryPlanRoot)) {
        subPlan.getTranslations().put(existsPredicate, BooleanLiteral.TRUE_LITERAL);
        return subPlan;
    }
    // add an explicit projection that removes all columns
    PlanNode subqueryNode = new ProjectNode(idAllocator.getNextId(), subqueryPlan.getRoot(), Assignments.of());
    VariableReferenceExpression exists = variableAllocator.newVariable(getSourceLocation(existsPredicate), "exists", BOOLEAN);
    subPlan.getTranslations().put(existsPredicate, exists);
    ExistsPredicate rewrittenExistsPredicate = new ExistsPredicate(BooleanLiteral.TRUE_LITERAL);
    return appendApplyNode(subPlan, existsPredicate.getSubquery(), subqueryNode, Assignments.of(exists, castToRowExpression(rewrittenExistsPredicate)), correlationAllowed);
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ExistsPredicate(com.facebook.presto.sql.tree.ExistsPredicate) ProjectNode(com.facebook.presto.spi.plan.ProjectNode)

Aggregations

ExistsPredicate (com.facebook.presto.sql.tree.ExistsPredicate)3 PlanNode (com.facebook.presto.spi.plan.PlanNode)2 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)2 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)2 Expression (com.facebook.presto.sql.tree.Expression)2 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)1 CallExpression (com.facebook.presto.spi.relation.CallExpression)1 Symbol (com.facebook.presto.sql.planner.Symbol)1 AggregationNode (com.facebook.presto.sql.planner.plan.AggregationNode)1 ApplyNode (com.facebook.presto.sql.planner.plan.ApplyNode)1 LimitNode (com.facebook.presto.sql.planner.plan.LimitNode)1 ProjectNode (com.facebook.presto.sql.planner.plan.ProjectNode)1 OriginalExpressionUtils.castToExpression (com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression)1 OriginalExpressionUtils.castToRowExpression (com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression)1 Cast (com.facebook.presto.sql.tree.Cast)1 CoalesceExpression (com.facebook.presto.sql.tree.CoalesceExpression)1 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)1