use of com.facebook.presto.sql.tree.NotExpression 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"))));
}
use of com.facebook.presto.sql.tree.NotExpression in project presto by prestodb.
the class DomainTranslator method extractDisjuncts.
private static List<Expression> extractDisjuncts(Type type, DiscreteValues discreteValues, SymbolReference reference) {
List<Expression> values = discreteValues.getValues().stream().map(object -> toExpression(object, type)).collect(toList());
// If values is empty, then the equatableValues was either ALL or NONE, both of which should already have been checked for
checkState(!values.isEmpty());
Expression predicate;
if (values.size() == 1) {
predicate = new ComparisonExpression(EQUAL, reference, getOnlyElement(values));
} else {
predicate = new InPredicate(reference, new InListExpression(values));
}
if (!discreteValues.isWhiteList()) {
predicate = new NotExpression(predicate);
}
return ImmutableList.of(predicate);
}
use of com.facebook.presto.sql.tree.NotExpression 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()));
}
use of com.facebook.presto.sql.tree.NotExpression in project presto by prestodb.
the class TestExpressionUtils method testNormalize.
@Test
public void testNormalize() throws Exception {
assertNormalize(new ComparisonExpression(EQUAL, name("a"), new LongLiteral("1")));
assertNormalize(new IsNullPredicate(name("a")));
assertNormalize(new NotExpression(new LikePredicate(name("a"), new StringLiteral("x%"), null)));
assertNormalize(new NotExpression(new ComparisonExpression(EQUAL, name("a"), new LongLiteral("1"))), new ComparisonExpression(NOT_EQUAL, name("a"), new LongLiteral("1")));
assertNormalize(new NotExpression(new ComparisonExpression(NOT_EQUAL, name("a"), new LongLiteral("1"))), new ComparisonExpression(EQUAL, name("a"), new LongLiteral("1")));
// Cannot normalize IS DISTINCT FROM yet
assertNormalize(new NotExpression(new ComparisonExpression(IS_DISTINCT_FROM, name("a"), new LongLiteral("1"))));
}
use of com.facebook.presto.sql.tree.NotExpression in project presto by prestodb.
the class DomainTranslator method toPredicate.
private static Expression toPredicate(Domain domain, SymbolReference reference) {
if (domain.getValues().isNone()) {
return domain.isNullAllowed() ? new IsNullPredicate(reference) : FALSE_LITERAL;
}
if (domain.getValues().isAll()) {
return domain.isNullAllowed() ? TRUE_LITERAL : new NotExpression(new IsNullPredicate(reference));
}
List<Expression> disjuncts = new ArrayList<>();
disjuncts.addAll(domain.getValues().getValuesProcessor().transform(ranges -> extractDisjuncts(domain.getType(), ranges, reference), discreteValues -> extractDisjuncts(domain.getType(), discreteValues, reference), allOrNone -> {
throw new IllegalStateException("Case should not be reachable");
}));
// Add nullability disjuncts
if (domain.isNullAllowed()) {
disjuncts.add(new IsNullPredicate(reference));
}
return combineDisjunctsWithDefault(disjuncts, TRUE_LITERAL);
}
Aggregations