Search in sources :

Example 1 with Quantifier

use of io.trino.sql.tree.QuantifiedComparisonExpression.Quantifier in project trino by trinodb.

the class SubqueryPlanner method planQuantifiedComparison.

private PlanBuilder planQuantifiedComparison(PlanBuilder subPlan, Cluster<QuantifiedComparisonExpression> cluster, Analysis.SubqueryAnalysis subqueries) {
    // Plan one of the predicates from the cluster
    QuantifiedComparisonExpression quantifiedComparison = cluster.getRepresentative();
    ComparisonExpression.Operator operator = quantifiedComparison.getOperator();
    Quantifier quantifier = quantifiedComparison.getQuantifier();
    Expression value = quantifiedComparison.getValue();
    SubqueryExpression subquery = (SubqueryExpression) quantifiedComparison.getSubquery();
    subPlan = handleSubqueries(subPlan, value, subqueries);
    Symbol output = symbolAllocator.newSymbol(quantifiedComparison, BOOLEAN);
    Analysis.PredicateCoercions predicateCoercions = analysis.getPredicateCoercions(quantifiedComparison);
    switch(operator) {
        case EQUAL:
            switch(quantifier) {
                case ALL:
                    subPlan = planQuantifiedComparison(subPlan, operator, quantifier, value, subquery, output, predicateCoercions);
                    return new PlanBuilder(subPlan.getTranslations().withAdditionalMappings(ImmutableMap.of(scopeAwareKey(quantifiedComparison, analysis, subPlan.getScope()), output)), subPlan.getRoot());
                case ANY:
                case SOME:
                    // A = ANY B <=> A IN B
                    subPlan = planInPredicate(subPlan, value, subquery, output, quantifiedComparison, predicateCoercions);
                    return new PlanBuilder(subPlan.getTranslations().withAdditionalMappings(mapAll(cluster, subPlan.getScope(), output)), subPlan.getRoot());
            }
            break;
        case NOT_EQUAL:
            switch(quantifier) {
                case ALL:
                    {
                        // A <> ALL B <=> !(A IN B)
                        subPlan = planInPredicate(subPlan, value, subquery, output, quantifiedComparison, predicateCoercions);
                        return addNegation(subPlan, cluster, output);
                    }
                case ANY:
                case SOME:
                    {
                        // A <> ANY B <=> min B <> max B || A <> min B <=> !(min B = max B && A = min B) <=> !(A = ALL B)
                        // "A <> ANY B" is equivalent to "NOT (A = ALL B)" so add a rewrite for the initial quantifiedComparison to notAll
                        subPlan = planQuantifiedComparison(subPlan, EQUAL, Quantifier.ALL, value, subquery, output, predicateCoercions);
                        return addNegation(subPlan, cluster, output);
                    }
            }
            break;
        case LESS_THAN:
        case LESS_THAN_OR_EQUAL:
        case GREATER_THAN:
        case GREATER_THAN_OR_EQUAL:
            subPlan = planQuantifiedComparison(subPlan, operator, quantifier, value, subquery, output, predicateCoercions);
            return new PlanBuilder(subPlan.getTranslations().withAdditionalMappings(mapAll(cluster, subPlan.getScope(), output)), subPlan.getRoot());
        case IS_DISTINCT_FROM:
    }
    // all cases are checked, so this exception should never be thrown
    throw new IllegalArgumentException(format("Unexpected quantified comparison: '%s %s'", operator.getValue(), quantifier));
}
Also used : ComparisonExpression(io.trino.sql.tree.ComparisonExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) 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) Analysis(io.trino.sql.analyzer.Analysis) Quantifier(io.trino.sql.tree.QuantifiedComparisonExpression.Quantifier) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) PlanBuilder.newPlanBuilder(io.trino.sql.planner.PlanBuilder.newPlanBuilder) SubqueryExpression(io.trino.sql.tree.SubqueryExpression)

Aggregations

Analysis (io.trino.sql.analyzer.Analysis)1 PlanBuilder.newPlanBuilder (io.trino.sql.planner.PlanBuilder.newPlanBuilder)1 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)1 Expression (io.trino.sql.tree.Expression)1 NotExpression (io.trino.sql.tree.NotExpression)1 QuantifiedComparisonExpression (io.trino.sql.tree.QuantifiedComparisonExpression)1 Quantifier (io.trino.sql.tree.QuantifiedComparisonExpression.Quantifier)1 SubqueryExpression (io.trino.sql.tree.SubqueryExpression)1