Search in sources :

Example 1 with QuantifiedComparisonExpression

use of com.facebook.presto.sql.tree.QuantifiedComparisonExpression in project presto by prestodb.

the class SubqueryPlanner method appendQuantifiedComparisonApplyNode.

private PlanBuilder appendQuantifiedComparisonApplyNode(PlanBuilder subPlan, QuantifiedComparisonExpression quantifiedComparison, boolean correlationAllowed) {
    if (subPlan.canTranslate(quantifiedComparison)) {
        // given subquery is already appended
        return subPlan;
    }
    switch(quantifiedComparison.getComparisonType()) {
        case EQUAL:
            switch(quantifiedComparison.getQuantifier()) {
                case ALL:
                    return planQuantifiedApplyNode(subPlan, quantifiedComparison, correlationAllowed);
                case ANY:
                case SOME:
                    // A = ANY B <=> A IN B
                    InPredicate inPredicate = new InPredicate(quantifiedComparison.getValue(), quantifiedComparison.getSubquery());
                    subPlan = appendInPredicateApplyNode(subPlan, inPredicate, correlationAllowed);
                    subPlan.getTranslations().put(quantifiedComparison, subPlan.translate(inPredicate));
                    return subPlan;
            }
            break;
        case NOT_EQUAL:
            switch(quantifiedComparison.getQuantifier()) {
                case ALL:
                    // A <> ALL B <=> !(A IN B) <=> !(A = ANY B)
                    QuantifiedComparisonExpression rewrittenAny = new QuantifiedComparisonExpression(EQUAL, Quantifier.ANY, quantifiedComparison.getValue(), quantifiedComparison.getSubquery());
                    Expression notAny = new NotExpression(rewrittenAny);
                    // "A <> ALL B" is equivalent to "NOT (A = ANY B)" so add a rewrite for the initial quantifiedComparison to notAny
                    subPlan.getTranslations().addIntermediateMapping(quantifiedComparison, notAny);
                    // now plan "A = ANY B" part by calling ourselves for rewrittenAny
                    return appendQuantifiedComparisonApplyNode(subPlan, rewrittenAny, correlationAllowed);
                case ANY:
                case SOME:
                    // A <> ANY B <=> min B <> max B || A <> min B <=> !(min B = max B && A = min B) <=> !(A = ALL B)
                    QuantifiedComparisonExpression rewrittenAll = new QuantifiedComparisonExpression(EQUAL, QuantifiedComparisonExpression.Quantifier.ALL, quantifiedComparison.getValue(), quantifiedComparison.getSubquery());
                    Expression notAll = new NotExpression(rewrittenAll);
                    // "A <> ANY B" is equivalent to "NOT (A = ALL B)" so add a rewrite for the initial quantifiedComparison to notAll
                    subPlan.getTranslations().addIntermediateMapping(quantifiedComparison, notAll);
                    // now plan "A = ALL B" part by calling ourselves for rewrittenAll
                    return appendQuantifiedComparisonApplyNode(subPlan, rewrittenAll, correlationAllowed);
            }
            break;
        case LESS_THAN:
        case LESS_THAN_OR_EQUAL:
        case GREATER_THAN:
        case GREATER_THAN_OR_EQUAL:
            return planQuantifiedApplyNode(subPlan, quantifiedComparison, correlationAllowed);
    }
    // all cases are checked, so this exception should never be thrown
    throw new IllegalArgumentException(format("Unexpected quantified comparison: '%s %s'", quantifiedComparison.getComparisonType().getValue(), quantifiedComparison.getQuantifier()));
}
Also used : SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) ExpressionNodeInliner.replaceExpression(com.facebook.presto.sql.planner.ExpressionNodeInliner.replaceExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) NotExpression(com.facebook.presto.sql.tree.NotExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) InPredicate(com.facebook.presto.sql.tree.InPredicate)

Example 2 with QuantifiedComparisonExpression

use of com.facebook.presto.sql.tree.QuantifiedComparisonExpression in project presto by prestodb.

the class SubqueryPlanner method planQuantifiedApplyNode.

private PlanBuilder planQuantifiedApplyNode(PlanBuilder subPlan, QuantifiedComparisonExpression quantifiedComparison, boolean correlationAllowed) {
    subPlan = subPlan.appendProjections(ImmutableList.of(quantifiedComparison.getValue()), variableAllocator, idAllocator);
    checkState(quantifiedComparison.getSubquery() instanceof SubqueryExpression);
    SubqueryExpression quantifiedSubquery = (SubqueryExpression) quantifiedComparison.getSubquery();
    SubqueryExpression uncoercedQuantifiedSubquery = uncoercedSubquery(quantifiedSubquery);
    PlanBuilder subqueryPlan = createPlanBuilder(uncoercedQuantifiedSubquery);
    subqueryPlan = subqueryPlan.appendProjections(ImmutableList.of(quantifiedSubquery), variableAllocator, idAllocator);
    QuantifiedComparisonExpression coercedQuantifiedComparison = new QuantifiedComparisonExpression(quantifiedComparison.getOperator(), quantifiedComparison.getQuantifier(), createSymbolReference(subPlan.translate(quantifiedComparison.getValue())), createSymbolReference(subqueryPlan.translate(quantifiedSubquery)));
    VariableReferenceExpression coercedQuantifiedComparisonVariable = variableAllocator.newVariable(coercedQuantifiedComparison, BOOLEAN);
    subPlan.getTranslations().put(quantifiedComparison, coercedQuantifiedComparisonVariable);
    return appendApplyNode(subPlan, quantifiedComparison.getSubquery(), subqueryPlan.getRoot(), Assignments.of(coercedQuantifiedComparisonVariable, castToRowExpression(coercedQuantifiedComparison)), correlationAllowed);
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression)

Example 3 with QuantifiedComparisonExpression

use of com.facebook.presto.sql.tree.QuantifiedComparisonExpression in project presto by prestodb.

the class TestSqlParser method testQuantifiedComparison.

@Test
public void testQuantifiedComparison() {
    assertExpression("col1 < ANY (SELECT col2 FROM table1)", new QuantifiedComparisonExpression(LESS_THAN, QuantifiedComparisonExpression.Quantifier.ANY, identifier("col1"), new SubqueryExpression(simpleQuery(selectList(new SingleColumn(identifier("col2"))), table(QualifiedName.of("table1"))))));
    assertExpression("col1 = ALL (VALUES ROW(1), ROW(2))", new QuantifiedComparisonExpression(ComparisonExpression.Operator.EQUAL, QuantifiedComparisonExpression.Quantifier.ALL, identifier("col1"), new SubqueryExpression(query(values(row(new LongLiteral("1")), row(new LongLiteral("2")))))));
    assertExpression("col1 >= SOME (SELECT 10)", new QuantifiedComparisonExpression(ComparisonExpression.Operator.GREATER_THAN_OR_EQUAL, QuantifiedComparisonExpression.Quantifier.SOME, identifier("col1"), new SubqueryExpression(simpleQuery(selectList(new LongLiteral("10"))))));
}
Also used : LongLiteral(com.facebook.presto.sql.tree.LongLiteral) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) Test(org.testng.annotations.Test)

Example 4 with QuantifiedComparisonExpression

use of com.facebook.presto.sql.tree.QuantifiedComparisonExpression in project presto by prestodb.

the class SubqueryPlanner method appendQuantifiedComparisonApplyNode.

private PlanBuilder appendQuantifiedComparisonApplyNode(PlanBuilder subPlan, QuantifiedComparisonExpression quantifiedComparison, boolean correlationAllowed, Node node) {
    if (subPlan.canTranslate(quantifiedComparison)) {
        // given subquery is already appended
        return subPlan;
    }
    switch(quantifiedComparison.getOperator()) {
        case EQUAL:
            switch(quantifiedComparison.getQuantifier()) {
                case ALL:
                    return planQuantifiedApplyNode(subPlan, quantifiedComparison, correlationAllowed);
                case ANY:
                case SOME:
                    // A = ANY B <=> A IN B
                    InPredicate inPredicate = new InPredicate(quantifiedComparison.getValue(), quantifiedComparison.getSubquery());
                    subPlan = appendInPredicateApplyNode(subPlan, inPredicate, correlationAllowed, node);
                    subPlan.getTranslations().put(quantifiedComparison, subPlan.translate(inPredicate));
                    return subPlan;
            }
            break;
        case NOT_EQUAL:
            switch(quantifiedComparison.getQuantifier()) {
                case ALL:
                    // A <> ALL B <=> !(A IN B) <=> !(A = ANY B)
                    QuantifiedComparisonExpression rewrittenAny = new QuantifiedComparisonExpression(EQUAL, Quantifier.ANY, quantifiedComparison.getValue(), quantifiedComparison.getSubquery());
                    Expression notAny = new NotExpression(rewrittenAny);
                    // "A <> ALL B" is equivalent to "NOT (A = ANY B)" so add a rewrite for the initial quantifiedComparison to notAny
                    subPlan.getTranslations().put(quantifiedComparison, subPlan.getTranslations().rewrite(notAny));
                    // now plan "A = ANY B" part by calling ourselves for rewrittenAny
                    return appendQuantifiedComparisonApplyNode(subPlan, rewrittenAny, correlationAllowed, node);
                case ANY:
                case SOME:
                    // A <> ANY B <=> min B <> max B || A <> min B <=> !(min B = max B && A = min B) <=> !(A = ALL B)
                    QuantifiedComparisonExpression rewrittenAll = new QuantifiedComparisonExpression(EQUAL, QuantifiedComparisonExpression.Quantifier.ALL, quantifiedComparison.getValue(), quantifiedComparison.getSubquery());
                    Expression notAll = new NotExpression(rewrittenAll);
                    // "A <> ANY B" is equivalent to "NOT (A = ALL B)" so add a rewrite for the initial quantifiedComparison to notAll
                    subPlan.getTranslations().put(quantifiedComparison, subPlan.getTranslations().rewrite(notAll));
                    // now plan "A = ALL B" part by calling ourselves for rewrittenAll
                    return appendQuantifiedComparisonApplyNode(subPlan, rewrittenAll, correlationAllowed, node);
            }
            break;
        case LESS_THAN:
        case LESS_THAN_OR_EQUAL:
        case GREATER_THAN:
        case GREATER_THAN_OR_EQUAL:
            return planQuantifiedApplyNode(subPlan, quantifiedComparison, correlationAllowed);
    }
    // all cases are checked, so this exception should never be thrown
    throw new IllegalArgumentException(format("Unexpected quantified comparison: '%s %s'", quantifiedComparison.getOperator().getValue(), quantifiedComparison.getQuantifier()));
}
Also used : SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) ExpressionNodeInliner.replaceExpression(com.facebook.presto.sql.planner.ExpressionNodeInliner.replaceExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) OriginalExpressionUtils.castToExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) InPredicate(com.facebook.presto.sql.tree.InPredicate)

Aggregations

QuantifiedComparisonExpression (com.facebook.presto.sql.tree.QuantifiedComparisonExpression)4 SubqueryExpression (com.facebook.presto.sql.tree.SubqueryExpression)4 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)2 ExpressionNodeInliner.replaceExpression (com.facebook.presto.sql.planner.ExpressionNodeInliner.replaceExpression)2 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)2 Expression (com.facebook.presto.sql.tree.Expression)2 InPredicate (com.facebook.presto.sql.tree.InPredicate)2 NotExpression (com.facebook.presto.sql.tree.NotExpression)2 RowExpression (com.facebook.presto.spi.relation.RowExpression)1 OriginalExpressionUtils.castToExpression (com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression)1 OriginalExpressionUtils.castToRowExpression (com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression)1 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)1 SingleColumn (com.facebook.presto.sql.tree.SingleColumn)1 Test (org.testng.annotations.Test)1