Search in sources :

Example 11 with InPredicate

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);
}
Also used : GREATER_THAN_OR_EQUAL(io.prestosql.sql.tree.ComparisonExpression.Operator.GREATER_THAN_OR_EQUAL) LESS_THAN_OR_EQUAL(io.prestosql.sql.tree.ComparisonExpression.Operator.LESS_THAN_OR_EQUAL) DiscreteValues(io.prestosql.spi.predicate.DiscreteValues) OperatorNotFoundException(io.prestosql.metadata.OperatorNotFoundException) FALSE_LITERAL(io.prestosql.sql.tree.BooleanLiteral.FALSE_LITERAL) ValueSet(io.prestosql.spi.predicate.ValueSet) NullableValue(io.prestosql.spi.predicate.NullableValue) SqlParser(io.prestosql.sql.parser.SqlParser) PeekingIterator(com.google.common.collect.PeekingIterator) Cast(io.prestosql.sql.tree.Cast) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Slices(io.airlift.slice.Slices) Map(java.util.Map) EQUAL(io.prestosql.sql.tree.ComparisonExpression.Operator.EQUAL) Type(io.prestosql.spi.type.Type) TypeCoercion(io.prestosql.type.TypeCoercion) SliceUtf8.getCodePointAt(io.airlift.slice.SliceUtf8.getCodePointAt) IsNullPredicate(io.prestosql.sql.tree.IsNullPredicate) ExpressionUtils.or(io.prestosql.sql.ExpressionUtils.or) ImmutableMap(com.google.common.collect.ImmutableMap) CastType(io.prestosql.metadata.CastType) IsNotNullPredicate(io.prestosql.sql.tree.IsNotNullPredicate) LikeFunctions(io.prestosql.type.LikeFunctions) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) NullLiteral(io.prestosql.sql.tree.NullLiteral) SortedRangeSet(io.prestosql.spi.predicate.SortedRangeSet) Metadata(io.prestosql.metadata.Metadata) NodeRef(io.prestosql.sql.tree.NodeRef) Preconditions.checkState(com.google.common.base.Preconditions.checkState) FunctionHandle(io.prestosql.spi.function.FunctionHandle) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) List(java.util.List) ExpressionUtils(io.prestosql.sql.ExpressionUtils) SymbolReference(io.prestosql.sql.tree.SymbolReference) StringLiteral(io.prestosql.sql.tree.StringLiteral) Domain(io.prestosql.spi.predicate.Domain) Optional(java.util.Optional) BetweenPredicate(io.prestosql.sql.tree.BetweenPredicate) SymbolUtils.from(io.prestosql.sql.planner.SymbolUtils.from) Utils(io.prestosql.spi.predicate.Utils) InPredicate(io.prestosql.sql.tree.InPredicate) LESS_THAN(io.prestosql.sql.tree.ComparisonExpression.Operator.LESS_THAN) Slice(io.airlift.slice.Slice) InListExpression(io.prestosql.sql.tree.InListExpression) Marker(io.prestosql.spi.predicate.Marker) Collectors.collectingAndThen(java.util.stream.Collectors.collectingAndThen) TRUE_LITERAL(io.prestosql.sql.tree.BooleanLiteral.TRUE_LITERAL) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) InterpretedFunctionInvoker(io.prestosql.sql.InterpretedFunctionInvoker) BooleanLiteral(io.prestosql.sql.tree.BooleanLiteral) Range(io.prestosql.spi.predicate.Range) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) LikePredicate(io.prestosql.sql.tree.LikePredicate) CAST(io.prestosql.metadata.CastType.CAST) Comparator.comparing(java.util.Comparator.comparing) AstVisitor(io.prestosql.sql.tree.AstVisitor) Iterators.peekingIterator(com.google.common.collect.Iterators.peekingIterator) Block(io.prestosql.spi.block.Block) Nullable(javax.annotation.Nullable) GREATER_THAN(io.prestosql.sql.tree.ComparisonExpression.Operator.GREATER_THAN) Symbol(io.prestosql.spi.plan.Symbol) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) ExpressionUtils.combineConjuncts(io.prestosql.sql.ExpressionUtils.combineConjuncts) Ranges(io.prestosql.spi.predicate.Ranges) TupleDomain(io.prestosql.spi.predicate.TupleDomain) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) ExpressionUtils.and(io.prestosql.sql.ExpressionUtils.and) SliceUtf8.setCodePointAt(io.airlift.slice.SliceUtf8.setCodePointAt) ExpressionUtils.combineDisjunctsWithDefault(io.prestosql.sql.ExpressionUtils.combineDisjunctsWithDefault) Collectors.toList(java.util.stream.Collectors.toList) NOT_EQUAL(io.prestosql.sql.tree.ComparisonExpression.Operator.NOT_EQUAL) SliceUtf8.countCodePoints(io.airlift.slice.SliceUtf8.countCodePoints) VarcharType(io.prestosql.spi.type.VarcharType) Expression(io.prestosql.sql.tree.Expression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) InListExpression(io.prestosql.sql.tree.InListExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) InListExpression(io.prestosql.sql.tree.InListExpression) NotExpression(io.prestosql.sql.tree.NotExpression) InPredicate(io.prestosql.sql.tree.InPredicate)

Example 12 with InPredicate

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()));
}
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 13 with InPredicate

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());
}
Also used : CallExpression(io.prestosql.spi.relation.CallExpression) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) Symbol(io.prestosql.spi.plan.Symbol) Assignments(io.prestosql.spi.plan.Assignments) InPredicate(io.prestosql.sql.tree.InPredicate)

Example 14 with InPredicate

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);
}
Also used : OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) Expression(io.prestosql.sql.tree.Expression) Symbol(io.prestosql.spi.plan.Symbol) InPredicate(io.prestosql.sql.tree.InPredicate) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode)

Example 15 with InPredicate

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();
}
Also used : CallExpression(io.prestosql.spi.relation.CallExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Expression(io.prestosql.sql.tree.Expression) ApplyNode(io.prestosql.sql.planner.plan.ApplyNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) InPredicate(io.prestosql.sql.tree.InPredicate)

Aggregations

InPredicate (io.prestosql.sql.tree.InPredicate)20 Expression (io.prestosql.sql.tree.Expression)15 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)13 InListExpression (io.prestosql.sql.tree.InListExpression)12 Symbol (io.prestosql.spi.plan.Symbol)7 LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)7 NotExpression (io.prestosql.sql.tree.NotExpression)6 Test (org.testng.annotations.Test)6 RowExpression (io.prestosql.spi.relation.RowExpression)4 Type (io.prestosql.spi.type.Type)4 OriginalExpressionUtils.castToExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression)4 OriginalExpressionUtils.castToRowExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)3 Cast (io.prestosql.sql.tree.Cast)3 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)3 IsNotNullPredicate (io.prestosql.sql.tree.IsNotNullPredicate)3 ArrayList (java.util.ArrayList)3 ProjectNode (io.prestosql.spi.plan.ProjectNode)2