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