use of io.trino.sql.tree.ExistsPredicate in project trino by trinodb.
the class SubqueryPlanner method planExists.
private PlanBuilder planExists(PlanBuilder subPlan, Cluster<ExistsPredicate> cluster) {
// Plan one of the predicates from the cluster
ExistsPredicate existsPredicate = cluster.getRepresentative();
Expression subquery = existsPredicate.getSubquery();
Symbol exists = symbolAllocator.newSymbol("exists", BOOLEAN);
return new PlanBuilder(subPlan.getTranslations().withAdditionalMappings(mapAll(cluster, subPlan.getScope(), exists)), new ApplyNode(idAllocator.getNextId(), subPlan.getRoot(), planSubquery(subquery, subPlan.getTranslations()).getRoot(), Assignments.of(exists, new ExistsPredicate(TRUE_LITERAL)), subPlan.getRoot().getOutputSymbols(), subquery));
}
use of io.trino.sql.tree.ExistsPredicate in project trino by trinodb.
the class TestRemoveRedundantExists method testDoesNotFire.
@Test
public void testDoesNotFire() {
tester().assertThat(new RemoveRedundantExists()).on(p -> p.apply(Assignments.of(p.symbol("exists"), new ExistsPredicate(TRUE_LITERAL)), ImmutableList.of(), p.values(1), p.tableScan(ImmutableList.of(), ImmutableMap.of()))).doesNotFire();
tester().assertThat(new RemoveRedundantExists()).on(p -> p.apply(Assignments.builder().put(p.symbol("exists"), new ExistsPredicate(TRUE_LITERAL)).put(p.symbol("other"), new InPredicate(new SymbolReference("value"), new SymbolReference("list"))).build(), ImmutableList.of(), p.values(1, p.symbol("value")), p.tableScan(ImmutableList.of(p.symbol("list")), ImmutableMap.of(p.symbol("list"), new TestingMetadata.TestingColumnHandle("list"))))).doesNotFire();
}
use of io.trino.sql.tree.ExistsPredicate in project trino by trinodb.
the class TransformExistsApplyToCorrelatedJoin method apply.
@Override
public Result apply(ApplyNode parent, Captures captures, Context context) {
if (parent.getSubqueryAssignments().size() != 1) {
return Result.empty();
}
Expression expression = getOnlyElement(parent.getSubqueryAssignments().getExpressions());
if (!(expression instanceof ExistsPredicate)) {
return Result.empty();
}
/*
Empty correlation list indicates that the subquery contains no correlation symbols from the
immediate outer scope. The subquery might be either not correlated at all, or correlated with
symbols from further outer scope.
Currently, the two cases are indistinguishable.
To support the latter case, the ApplyNode with empty correlation list is rewritten to default
aggregation, which is inefficient in the rare case of uncorrelated EXISTS subquery,
but currently allows to successfully decorrelate a correlated EXISTS subquery.
TODO: remove this condition when exploratory optimizer is implemented or support for decorrelating joins is implemented in PlanNodeDecorrelator
*/
if (parent.getCorrelation().isEmpty()) {
return Result.ofPlanNode(rewriteToDefaultAggregation(parent, context));
}
Optional<PlanNode> nonDefaultAggregation = rewriteToNonDefaultAggregation(parent, context);
return nonDefaultAggregation.map(Result::ofPlanNode).orElseGet(() -> Result.ofPlanNode(rewriteToDefaultAggregation(parent, context)));
}
Aggregations