Search in sources :

Example 1 with ExistsPredicate

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));
}
Also used : ComparisonExpression(io.trino.sql.tree.ComparisonExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) Expression(io.trino.sql.tree.Expression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) NotExpression(io.trino.sql.tree.NotExpression) ExistsPredicate(io.trino.sql.tree.ExistsPredicate) ApplyNode(io.trino.sql.planner.plan.ApplyNode) PlanBuilder.newPlanBuilder(io.trino.sql.planner.PlanBuilder.newPlanBuilder)

Example 2 with ExistsPredicate

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();
}
Also used : PlanMatchPattern.expression(io.trino.sql.planner.assertions.PlanMatchPattern.expression) ImmutableMap(com.google.common.collect.ImmutableMap) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) TestingMetadata(io.trino.testing.TestingMetadata) Assignments(io.trino.sql.planner.plan.Assignments) Test(org.testng.annotations.Test) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) TRUE_LITERAL(io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL) InPredicate(io.trino.sql.tree.InPredicate) ImmutableList(com.google.common.collect.ImmutableList) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) ExistsPredicate(io.trino.sql.tree.ExistsPredicate) SymbolReference(io.trino.sql.tree.SymbolReference) SymbolReference(io.trino.sql.tree.SymbolReference) ExistsPredicate(io.trino.sql.tree.ExistsPredicate) TestingMetadata(io.trino.testing.TestingMetadata) InPredicate(io.trino.sql.tree.InPredicate) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test)

Example 3 with ExistsPredicate

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)));
}
Also used : PlanNode(io.trino.sql.planner.plan.PlanNode) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) Expression(io.trino.sql.tree.Expression) ExistsPredicate(io.trino.sql.tree.ExistsPredicate)

Aggregations

ExistsPredicate (io.trino.sql.tree.ExistsPredicate)3 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)2 Expression (io.trino.sql.tree.Expression)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 PlanBuilder.newPlanBuilder (io.trino.sql.planner.PlanBuilder.newPlanBuilder)1 PlanMatchPattern.expression (io.trino.sql.planner.assertions.PlanMatchPattern.expression)1 PlanMatchPattern.project (io.trino.sql.planner.assertions.PlanMatchPattern.project)1 PlanMatchPattern.values (io.trino.sql.planner.assertions.PlanMatchPattern.values)1 BaseRuleTest (io.trino.sql.planner.iterative.rule.test.BaseRuleTest)1 ApplyNode (io.trino.sql.planner.plan.ApplyNode)1 Assignments (io.trino.sql.planner.plan.Assignments)1 PlanNode (io.trino.sql.planner.plan.PlanNode)1 TRUE_LITERAL (io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL)1 CoalesceExpression (io.trino.sql.tree.CoalesceExpression)1 InPredicate (io.trino.sql.tree.InPredicate)1 NotExpression (io.trino.sql.tree.NotExpression)1 QuantifiedComparisonExpression (io.trino.sql.tree.QuantifiedComparisonExpression)1 SubqueryExpression (io.trino.sql.tree.SubqueryExpression)1 SymbolReference (io.trino.sql.tree.SymbolReference)1