Search in sources :

Example 1 with BetweenPredicate

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

the class TestSqlParser method testBetween.

@Test
public void testBetween() throws Exception {
    assertExpression("1 BETWEEN 2 AND 3", new BetweenPredicate(new LongLiteral("1"), new LongLiteral("2"), new LongLiteral("3")));
    assertExpression("1 NOT BETWEEN 2 AND 3", new NotExpression(new BetweenPredicate(new LongLiteral("1"), new LongLiteral("2"), new LongLiteral("3"))));
}
Also used : BetweenPredicate(com.facebook.presto.sql.tree.BetweenPredicate) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) NotExpression(com.facebook.presto.sql.tree.NotExpression) Test(org.testng.annotations.Test)

Example 2 with BetweenPredicate

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

the class DomainTranslator method processRange.

private static Expression processRange(Type type, Range range, SymbolReference reference) {
    if (range.isAll()) {
        return TRUE_LITERAL;
    }
    if (isBetween(range)) {
        // specialize the range with BETWEEN expression if possible b/c it is currently more efficient
        return new BetweenPredicate(reference, toExpression(range.getLow().getValue(), type), toExpression(range.getHigh().getValue(), type));
    }
    List<Expression> rangeConjuncts = new ArrayList<>();
    if (!range.getLow().isLowerUnbounded()) {
        switch(range.getLow().getBound()) {
            case ABOVE:
                rangeConjuncts.add(new ComparisonExpression(GREATER_THAN, reference, toExpression(range.getLow().getValue(), type)));
                break;
            case EXACTLY:
                rangeConjuncts.add(new ComparisonExpression(GREATER_THAN_OR_EQUAL, reference, toExpression(range.getLow().getValue(), type)));
                break;
            case BELOW:
                throw new IllegalStateException("Low Marker should never use BELOW bound: " + range);
            default:
                throw new AssertionError("Unhandled bound: " + range.getLow().getBound());
        }
    }
    if (!range.getHigh().isUpperUnbounded()) {
        switch(range.getHigh().getBound()) {
            case ABOVE:
                throw new IllegalStateException("High Marker should never use ABOVE bound: " + range);
            case EXACTLY:
                rangeConjuncts.add(new ComparisonExpression(LESS_THAN_OR_EQUAL, reference, toExpression(range.getHigh().getValue(), type)));
                break;
            case BELOW:
                rangeConjuncts.add(new ComparisonExpression(LESS_THAN, reference, toExpression(range.getHigh().getValue(), type)));
                break;
            default:
                throw new AssertionError("Unhandled bound: " + range.getHigh().getBound());
        }
    }
    // If rangeConjuncts is null, then the range was ALL, which should already have been checked for
    checkState(!rangeConjuncts.isEmpty());
    return combineConjuncts(rangeConjuncts);
}
Also used : ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) BetweenPredicate(com.facebook.presto.sql.tree.BetweenPredicate) NotExpression(com.facebook.presto.sql.tree.NotExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LiteralInterpreter.toExpression(com.facebook.presto.sql.planner.LiteralInterpreter.toExpression) Expression(com.facebook.presto.sql.tree.Expression) ArrayList(java.util.ArrayList)

Aggregations

BetweenPredicate (com.facebook.presto.sql.tree.BetweenPredicate)2 NotExpression (com.facebook.presto.sql.tree.NotExpression)2 LiteralInterpreter.toExpression (com.facebook.presto.sql.planner.LiteralInterpreter.toExpression)1 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)1 Expression (com.facebook.presto.sql.tree.Expression)1 InListExpression (com.facebook.presto.sql.tree.InListExpression)1 LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)1 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)1 ArrayList (java.util.ArrayList)1 Test (org.testng.annotations.Test)1