Search in sources :

Example 1 with InPredicate

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

the class TestDomainTranslator method in.

private static InPredicate in(Expression expression, Type expressisonType, List<?> values) {
    List<Type> types = nCopies(values.size(), expressisonType);
    List<Expression> expressions = LiteralInterpreter.toExpressions(values, types);
    return new InPredicate(expression, new InListExpression(expressions));
}
Also used : ComparisonExpressionType(com.facebook.presto.sql.tree.ComparisonExpressionType) CharType.createCharType(com.facebook.presto.spi.type.CharType.createCharType) DecimalType(com.facebook.presto.spi.type.DecimalType) DecimalType.createDecimalType(com.facebook.presto.spi.type.DecimalType.createDecimalType) Type(com.facebook.presto.spi.type.Type) NotExpression(com.facebook.presto.sql.tree.NotExpression) 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) InListExpression(com.facebook.presto.sql.tree.InListExpression) InPredicate(com.facebook.presto.sql.tree.InPredicate)

Example 2 with InPredicate

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

the class ExpressionVerifier method visitInPredicate.

@Override
protected Boolean visitInPredicate(InPredicate actual, Expression expectedExpression) {
    if (expectedExpression instanceof InPredicate) {
        InPredicate expected = (InPredicate) expectedExpression;
        if (actual.getValueList() instanceof InListExpression) {
            return process(actual.getValue(), expected.getValue()) && process(actual.getValueList(), expected.getValueList());
        } else {
            checkState(expected.getValueList() instanceof InListExpression, "ExpressionVerifier doesn't support unpacked expected values. Feel free to add support if needed");
            /*
                 * If the expected value is a value list, but the actual is e.g. a SymbolReference,
                 * we need to unpack the value from the list so that when we hit visitSymbolReference, the
                 * expected.toString() call returns something that the symbolAliases actually contains.
                 * For example, InListExpression.toString returns "(onlyitem)" rather than "onlyitem".
                 *
                 * This is required because actual passes through the analyzer, planner, and possibly optimizers,
                 * one of which sometimes takes the liberty of unpacking the InListExpression.
                 *
                 * Since the expected value doesn't go through all of that, we have to deal with the case
                 * of the actual value being unpacked, but the expected value being an InListExpression.
                 */
            List<Expression> values = ((InListExpression) expected.getValueList()).getValues();
            checkState(values.size() == 1, "Multiple expressions in expected value list %s, but actual value is not a list", values, actual.getValue());
            Expression onlyExpectedExpression = values.get(0);
            return process(actual.getValue(), expected.getValue()) && process(actual.getValueList(), onlyExpectedExpression);
        }
    }
    return false;
}
Also used : ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) InPredicate(com.facebook.presto.sql.tree.InPredicate)

Example 3 with InPredicate

use of com.facebook.presto.sql.tree.InPredicate 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);
}
Also used : ComparisonExpressionType(com.facebook.presto.sql.tree.ComparisonExpressionType) Block(com.facebook.presto.spi.block.Block) GREATER_THAN(com.facebook.presto.sql.tree.ComparisonExpressionType.GREATER_THAN) GREATER_THAN_OR_EQUAL(com.facebook.presto.sql.tree.ComparisonExpressionType.GREATER_THAN_OR_EQUAL) IsNullPredicate(com.facebook.presto.sql.tree.IsNullPredicate) PeekingIterator(com.google.common.collect.PeekingIterator) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Map(java.util.Map) NullableValue(com.facebook.presto.spi.predicate.NullableValue) BetweenPredicate(com.facebook.presto.sql.tree.BetweenPredicate) NotExpression(com.facebook.presto.sql.tree.NotExpression) EQUAL(com.facebook.presto.sql.tree.ComparisonExpressionType.EQUAL) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) ImmutableMap(com.google.common.collect.ImmutableMap) Collections.emptyList(java.util.Collections.emptyList) SortedRangeSet(com.facebook.presto.spi.predicate.SortedRangeSet) Marker(com.facebook.presto.spi.predicate.Marker) SqlParser(com.facebook.presto.sql.parser.SqlParser) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TRUE_LITERAL(com.facebook.presto.sql.tree.BooleanLiteral.TRUE_LITERAL) LESS_THAN(com.facebook.presto.sql.tree.ComparisonExpressionType.LESS_THAN) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) BooleanLiteral(com.facebook.presto.sql.tree.BooleanLiteral) NullLiteral(com.facebook.presto.sql.tree.NullLiteral) ExpressionUtils.combineDisjunctsWithDefault(com.facebook.presto.sql.ExpressionUtils.combineDisjunctsWithDefault) Domain(com.facebook.presto.spi.predicate.Domain) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) List(java.util.List) Utils(com.facebook.presto.spi.predicate.Utils) InPredicate(com.facebook.presto.sql.tree.InPredicate) Optional(java.util.Optional) Ranges(com.facebook.presto.spi.predicate.Ranges) ExpressionUtils.combineConjuncts(com.facebook.presto.sql.ExpressionUtils.combineConjuncts) ExpressionUtils.and(com.facebook.presto.sql.ExpressionUtils.and) ArrayList(java.util.ArrayList) InListExpression(com.facebook.presto.sql.tree.InListExpression) ImmutableList(com.google.common.collect.ImmutableList) ValueSet(com.facebook.presto.spi.predicate.ValueSet) Type(com.facebook.presto.spi.type.Type) Objects.requireNonNull(java.util.Objects.requireNonNull) Cast(com.facebook.presto.sql.tree.Cast) Iterators.peekingIterator(com.google.common.collect.Iterators.peekingIterator) Nullable(javax.annotation.Nullable) ExpressionUtils.or(com.facebook.presto.sql.ExpressionUtils.or) FALSE_LITERAL(com.facebook.presto.sql.tree.BooleanLiteral.FALSE_LITERAL) Session(com.facebook.presto.Session) Signature(com.facebook.presto.metadata.Signature) AstVisitor(com.facebook.presto.sql.tree.AstVisitor) Signature.internalOperator(com.facebook.presto.metadata.Signature.internalOperator) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) IsNotNullPredicate(com.facebook.presto.sql.tree.IsNotNullPredicate) SATURATED_FLOOR_CAST(com.facebook.presto.spi.function.OperatorType.SATURATED_FLOOR_CAST) DiscreteValues(com.facebook.presto.spi.predicate.DiscreteValues) Range(com.facebook.presto.spi.predicate.Range) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LiteralInterpreter.toExpression(com.facebook.presto.sql.planner.LiteralInterpreter.toExpression) LESS_THAN_OR_EQUAL(com.facebook.presto.sql.tree.ComparisonExpressionType.LESS_THAN_OR_EQUAL) NOT_EQUAL(com.facebook.presto.sql.tree.ComparisonExpressionType.NOT_EQUAL) Collectors.toList(java.util.stream.Collectors.toList) ExpressionAnalyzer(com.facebook.presto.sql.analyzer.ExpressionAnalyzer) Expression(com.facebook.presto.sql.tree.Expression) FunctionInvoker(com.facebook.presto.sql.FunctionInvoker) IdentityLinkedHashMap(com.facebook.presto.util.maps.IdentityLinkedHashMap) Metadata(com.facebook.presto.metadata.Metadata) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) 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) InListExpression(com.facebook.presto.sql.tree.InListExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) InPredicate(com.facebook.presto.sql.tree.InPredicate)

Example 4 with InPredicate

use of com.facebook.presto.sql.tree.InPredicate 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()));
}
Also used : SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) ExpressionNodeInliner.replaceExpression(com.facebook.presto.sql.planner.ExpressionNodeInliner.replaceExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) NotExpression(com.facebook.presto.sql.tree.NotExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) InPredicate(com.facebook.presto.sql.tree.InPredicate)

Example 5 with InPredicate

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

the class SubqueryPlanner method appendInPredicateApplyNode.

private PlanBuilder appendInPredicateApplyNode(PlanBuilder subPlan, InPredicate inPredicate, boolean correlationAllowed) {
    if (subPlan.canTranslate(inPredicate)) {
        // given subquery is already appended
        return subPlan;
    }
    subPlan = subPlan.appendProjections(ImmutableList.of(inPredicate.getValue()), symbolAllocator, idAllocator);
    checkState(inPredicate.getValueList() instanceof SubqueryExpression);
    SubqueryExpression valueListSubquery = (SubqueryExpression) inPredicate.getValueList();
    SubqueryExpression uncoercedValueListSubquery = uncoercedSubquery(valueListSubquery);
    PlanBuilder subqueryPlan = createPlanBuilder(uncoercedValueListSubquery);
    subqueryPlan = subqueryPlan.appendProjections(ImmutableList.of(valueListSubquery), symbolAllocator, idAllocator);
    SymbolReference valueList = subqueryPlan.translate(valueListSubquery).toSymbolReference();
    InPredicate parametersReplaced = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(parameters, analysis), inPredicate);
    InPredicate inPredicateSubqueryExpression = new InPredicate(subPlan.translate(parametersReplaced.getValue()).toSymbolReference(), valueList);
    Symbol inPredicateSubquerySymbol = symbolAllocator.newSymbol(inPredicateSubqueryExpression, BOOLEAN);
    subPlan.getTranslations().put(parametersReplaced, inPredicateSubquerySymbol);
    subPlan.getTranslations().put(inPredicate, inPredicateSubquerySymbol);
    return appendApplyNode(subPlan, inPredicate, subqueryPlan, Assignments.of(inPredicateSubquerySymbol, inPredicateSubqueryExpression), correlationAllowed);
}
Also used : SymbolReference(com.facebook.presto.sql.tree.SymbolReference) InPredicate(com.facebook.presto.sql.tree.InPredicate) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression)

Aggregations

InPredicate (com.facebook.presto.sql.tree.InPredicate)22 Expression (com.facebook.presto.sql.tree.Expression)17 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)14 InListExpression (com.facebook.presto.sql.tree.InListExpression)14 NotExpression (com.facebook.presto.sql.tree.NotExpression)13 LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)7 SymbolReference (com.facebook.presto.sql.tree.SymbolReference)7 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)5 SubqueryExpression (com.facebook.presto.sql.tree.SubqueryExpression)5 ArrayList (java.util.ArrayList)5 Test (org.testng.annotations.Test)5 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)4 LiteralInterpreter.toExpression (com.facebook.presto.sql.planner.LiteralInterpreter.toExpression)4 Type (com.facebook.presto.common.type.Type)3 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)3 Session (com.facebook.presto.Session)2 Metadata (com.facebook.presto.metadata.Metadata)2 RowExpression (com.facebook.presto.spi.relation.RowExpression)2 Type (com.facebook.presto.spi.type.Type)2 ExpressionUtils.and (com.facebook.presto.sql.ExpressionUtils.and)2