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()));
}
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)));
}
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);
}
Aggregations