use of io.prestosql.sql.tree.NotExpression 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()));
}
use of io.prestosql.sql.tree.NotExpression in project hetu-core by openlookeng.
the class ExpressionDomainTranslator method toPredicate.
private 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);
}
use of io.prestosql.sql.tree.NotExpression in project hetu-core by openlookeng.
the class ImplementExceptAsUnion method apply.
@Override
public Result apply(ExceptNode node, Captures captures, Context context) {
SetOperationNodeTranslator translator = new SetOperationNodeTranslator(metadata, context.getSymbolAllocator(), context.getIdAllocator());
SetOperationNodeTranslator.TranslationResult result = translator.makeSetContainmentPlan(node);
ImmutableList.Builder<Expression> predicatesBuilder = ImmutableList.builder();
List<Expression> presentExpression = result.getPresentExpressions();
predicatesBuilder.add(getFirst(presentExpression, null));
for (int i = 1; i < presentExpression.size(); i++) {
predicatesBuilder.add(new NotExpression(presentExpression.get(i)));
}
return Result.ofPlanNode(new ProjectNode(context.getIdAllocator().getNextId(), new FilterNode(context.getIdAllocator().getNextId(), result.getPlanNode(), castToRowExpression(and(predicatesBuilder.build()))), AssignmentUtils.identityAsSymbolReferences(node.getOutputSymbols())));
}
use of io.prestosql.sql.tree.NotExpression in project hetu-core by openlookeng.
the class TestSqlParser method testBetween.
@Test
public void testBetween() {
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"))));
}
Aggregations