Search in sources :

Example 1 with QuantifiedComparisonExpression

use of io.prestosql.sql.tree.QuantifiedComparisonExpression in project hetu-core by openlookeng.

the class SubqueryPlanner method planQuantifiedApplyNode.

private PlanBuilder planQuantifiedApplyNode(PlanBuilder inputSubPlan, QuantifiedComparisonExpression quantifiedComparison, boolean correlationAllowed) {
    PlanBuilder subPlan = inputSubPlan;
    subPlan = subPlan.appendProjections(ImmutableList.of(quantifiedComparison.getValue()), planSymbolAllocator, 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), planSymbolAllocator, idAllocator);
    QuantifiedComparisonExpression coercedQuantifiedComparison = new QuantifiedComparisonExpression(quantifiedComparison.getOperator(), quantifiedComparison.getQuantifier(), toSymbolReference(subPlan.translate(quantifiedComparison.getValue())), toSymbolReference(subqueryPlan.translate(quantifiedSubquery)));
    Symbol coercedQuantifiedComparisonSymbol = planSymbolAllocator.newSymbol(coercedQuantifiedComparison, BOOLEAN);
    subPlan.getTranslations().put(quantifiedComparison, coercedQuantifiedComparisonSymbol);
    return appendApplyNode(subPlan, quantifiedComparison.getSubquery(), subqueryPlan.getRoot(), Assignments.of(coercedQuantifiedComparisonSymbol, castToRowExpression(coercedQuantifiedComparison)), correlationAllowed);
}
Also used : Symbol(io.prestosql.spi.plan.Symbol) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression)

Example 2 with QuantifiedComparisonExpression

use of io.prestosql.sql.tree.QuantifiedComparisonExpression in project hetu-core by openlookeng.

the class SubqueryPlanner method appendQuantifiedComparisonApplyNode.

private PlanBuilder appendQuantifiedComparisonApplyNode(PlanBuilder inputSubPlan, QuantifiedComparisonExpression quantifiedComparison, boolean correlationAllowed, Node node) {
    PlanBuilder subPlan = inputSubPlan;
    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(String.format("Unexpected quantified comparison: '%s %s'", quantifiedComparison.getOperator().getValue(), quantifiedComparison.getQuantifier()));
}
Also used : OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) ExpressionNodeInliner.replaceExpression(io.prestosql.sql.planner.ExpressionNodeInliner.replaceExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) NotExpression(io.prestosql.sql.tree.NotExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Expression(io.prestosql.sql.tree.Expression) NotExpression(io.prestosql.sql.tree.NotExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) InPredicate(io.prestosql.sql.tree.InPredicate)

Example 3 with QuantifiedComparisonExpression

use of io.prestosql.sql.tree.QuantifiedComparisonExpression in project hetu-core by openlookeng.

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(io.prestosql.sql.tree.LongLiteral) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) SingleColumn(io.prestosql.sql.tree.SingleColumn) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) Test(org.testng.annotations.Test)

Aggregations

QuantifiedComparisonExpression (io.prestosql.sql.tree.QuantifiedComparisonExpression)3 SubqueryExpression (io.prestosql.sql.tree.SubqueryExpression)3 Symbol (io.prestosql.spi.plan.Symbol)1 RowExpression (io.prestosql.spi.relation.RowExpression)1 ExpressionNodeInliner.replaceExpression (io.prestosql.sql.planner.ExpressionNodeInliner.replaceExpression)1 OriginalExpressionUtils.castToExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression)1 OriginalExpressionUtils.castToRowExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression)1 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)1 Expression (io.prestosql.sql.tree.Expression)1 InPredicate (io.prestosql.sql.tree.InPredicate)1 LongLiteral (io.prestosql.sql.tree.LongLiteral)1 NotExpression (io.prestosql.sql.tree.NotExpression)1 SingleColumn (io.prestosql.sql.tree.SingleColumn)1 Test (org.testng.annotations.Test)1