use of io.prestosql.sql.tree.InPredicate in project hetu-core by openlookeng.
the class ExpressionDomainTranslator method extractDisjuncts.
private List<Expression> extractDisjuncts(Type type, DiscreteValues discreteValues, SymbolReference reference) {
List<Expression> values = discreteValues.getValues().stream().map(object -> literalEncoder.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 io.prestosql.sql.tree.InPredicate 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.InPredicate in project hetu-core by openlookeng.
the class TransformCorrelatedInPredicateToJoin method apply.
@Override
public Result apply(ApplyNode apply, Captures captures, Context context) {
Assignments subqueryAssignments = apply.getSubqueryAssignments();
if (subqueryAssignments.size() != 1) {
return Result.empty();
}
Expression assignmentExpression = castToExpression(getOnlyElement(subqueryAssignments.getExpressions()));
if (!(assignmentExpression instanceof InPredicate)) {
return Result.empty();
}
InPredicate inPredicate = (InPredicate) assignmentExpression;
Symbol inPredicateOutputSymbol = getOnlyElement(subqueryAssignments.getSymbols());
return apply(apply, inPredicate, inPredicateOutputSymbol, context.getLookup(), context.getIdAllocator(), context.getSymbolAllocator());
}
use of io.prestosql.sql.tree.InPredicate in project hetu-core by openlookeng.
the class TransformUncorrelatedInPredicateSubqueryToSemiJoin method apply.
@Override
public Result apply(ApplyNode applyNode, Captures captures, Context context) {
if (applyNode.getSubqueryAssignments().size() != 1) {
return Result.empty();
}
Expression expression = castToExpression(getOnlyElement(applyNode.getSubqueryAssignments().getExpressions()));
if (!(expression instanceof InPredicate)) {
return Result.empty();
}
InPredicate inPredicate = (InPredicate) expression;
Symbol semiJoinSymbol = getOnlyElement(applyNode.getSubqueryAssignments().getSymbols());
SemiJoinNode replacement = new SemiJoinNode(context.getIdAllocator().getNextId(), applyNode.getInput(), applyNode.getSubquery(), SymbolUtils.from(inPredicate.getValue()), SymbolUtils.from(inPredicate.getValueList()), semiJoinSymbol, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
return Result.ofPlanNode(replacement);
}
use of io.prestosql.sql.tree.InPredicate in project hetu-core by openlookeng.
the class TransformUnCorrelatedInPredicateSubQuerySelfJoinToAggregate method apply.
@Override
public Result apply(ApplyNode node, Captures captures, Context context) {
if (node.getSubqueryAssignments().size() != 1) {
return Result.empty();
}
// Only in case of IN predicate this optimization makes sense.
Expression expression = OriginalExpressionUtils.castToExpression(getOnlyElement(node.getSubqueryAssignments().getExpressions()));
if (!(expression instanceof InPredicate)) {
return Result.empty();
}
ProjectNode projectNode = captures.get(PROJECT_NODE);
Optional<ProjectNode> transformed = transformProjectNode(context, projectNode);
if (transformed.isPresent()) {
return Result.ofPlanNode(new ApplyNode(node.getId(), node.getInput(), transformed.get(), node.getSubqueryAssignments(), node.getCorrelation(), node.getOriginSubquery()));
}
return Result.empty();
}
Aggregations