Search in sources :

Example 1 with InPredicate

use of io.trino.sql.tree.InPredicate in project trino by trinodb.

the class UnwrapSingleColumnRowInApply method apply.

@Override
public Result apply(ApplyNode node, Captures captures, Context context) {
    Assignments.Builder inputAssignments = Assignments.builder().putIdentities(node.getInput().getOutputSymbols());
    Assignments.Builder nestedPlanAssignments = Assignments.builder().putIdentities(node.getSubquery().getOutputSymbols());
    boolean applied = false;
    Assignments.Builder applyAssignments = Assignments.builder();
    for (Map.Entry<Symbol, Expression> assignment : node.getSubqueryAssignments().entrySet()) {
        Symbol output = assignment.getKey();
        Expression expression = assignment.getValue();
        Optional<Unwrapping> unwrapped = Optional.empty();
        if (expression instanceof InPredicate) {
            InPredicate predicate = (InPredicate) expression;
            unwrapped = unwrapSingleColumnRow(context, predicate.getValue(), predicate.getValueList(), (value, list) -> new InPredicate(value.toSymbolReference(), list.toSymbolReference()));
        } else if (expression instanceof QuantifiedComparisonExpression) {
            QuantifiedComparisonExpression comparison = (QuantifiedComparisonExpression) expression;
            unwrapped = unwrapSingleColumnRow(context, comparison.getValue(), comparison.getSubquery(), (value, list) -> new QuantifiedComparisonExpression(comparison.getOperator(), comparison.getQuantifier(), value.toSymbolReference(), list.toSymbolReference()));
        }
        if (unwrapped.isPresent()) {
            applied = true;
            Unwrapping unwrapping = unwrapped.get();
            inputAssignments.add(unwrapping.getInputAssignment());
            nestedPlanAssignments.add(unwrapping.getNestedPlanAssignment());
            applyAssignments.put(output, unwrapping.getExpression());
        } else {
            applyAssignments.put(assignment);
        }
    }
    if (!applied) {
        return Result.empty();
    }
    return Result.ofPlanNode(new ProjectNode(context.getIdAllocator().getNextId(), new ApplyNode(node.getId(), new ProjectNode(context.getIdAllocator().getNextId(), node.getInput(), inputAssignments.build()), new ProjectNode(context.getIdAllocator().getNextId(), node.getSubquery(), nestedPlanAssignments.build()), applyAssignments.build(), node.getCorrelation(), node.getOriginSubquery()), Assignments.identity(node.getOutputSymbols())));
}
Also used : Assignment(io.trino.sql.planner.plan.Assignments.Assignment) Symbol(io.trino.sql.planner.Symbol) RowType(io.trino.spi.type.RowType) BiFunction(java.util.function.BiFunction) Type(io.trino.spi.type.Type) Assignments(io.trino.sql.planner.plan.Assignments) InPredicate(io.trino.sql.tree.InPredicate) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) Pattern(io.trino.matching.Pattern) TypeAnalyzer(io.trino.sql.planner.TypeAnalyzer) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) Captures(io.trino.matching.Captures) LongLiteral(io.trino.sql.tree.LongLiteral) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ApplyNode(io.trino.sql.planner.plan.ApplyNode) Optional(java.util.Optional) Rule(io.trino.sql.planner.iterative.Rule) Expression(io.trino.sql.tree.Expression) Patterns.applyNode(io.trino.sql.planner.plan.Patterns.applyNode) ProjectNode(io.trino.sql.planner.plan.ProjectNode) Symbol(io.trino.sql.planner.Symbol) ApplyNode(io.trino.sql.planner.plan.ApplyNode) Assignments(io.trino.sql.planner.plan.Assignments) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) InPredicate(io.trino.sql.tree.InPredicate) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) Expression(io.trino.sql.tree.Expression) ProjectNode(io.trino.sql.planner.plan.ProjectNode) Map(java.util.Map)

Example 2 with InPredicate

use of io.trino.sql.tree.InPredicate in project trino by trinodb.

the class SubqueryPlanner method planInPredicate.

private PlanBuilder planInPredicate(PlanBuilder subPlan, Cluster<InPredicate> cluster, Analysis.SubqueryAnalysis subqueries) {
    // Plan one of the predicates from the cluster
    InPredicate predicate = cluster.getRepresentative();
    Expression value = predicate.getValue();
    SubqueryExpression subquery = (SubqueryExpression) predicate.getValueList();
    Symbol output = symbolAllocator.newSymbol(predicate, BOOLEAN);
    subPlan = handleSubqueries(subPlan, value, subqueries);
    subPlan = planInPredicate(subPlan, value, subquery, output, predicate, analysis.getPredicateCoercions(predicate));
    return new PlanBuilder(subPlan.getTranslations().withAdditionalMappings(mapAll(cluster, subPlan.getScope(), output)), subPlan.getRoot());
}
Also used : ComparisonExpression(io.trino.sql.tree.ComparisonExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) Expression(io.trino.sql.tree.Expression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) NotExpression(io.trino.sql.tree.NotExpression) InPredicate(io.trino.sql.tree.InPredicate) PlanBuilder.newPlanBuilder(io.trino.sql.planner.PlanBuilder.newPlanBuilder) SubqueryExpression(io.trino.sql.tree.SubqueryExpression)

Example 3 with InPredicate

use of io.trino.sql.tree.InPredicate in project trino by trinodb.

the class ExpressionVerifier method visitInPredicate.

@Override
protected Boolean visitInPredicate(InPredicate actual, Node expectedExpression) {
    if (!(expectedExpression instanceof InPredicate)) {
        return false;
    }
    InPredicate expected = (InPredicate) expectedExpression;
    if (actual.getValueList() instanceof InListExpression || !(expected.getValueList() instanceof InListExpression)) {
        return process(actual.getValue(), expected.getValue()) && process(actual.getValueList(), expected.getValueList());
    }
    /*
         * In some cases, actual.getValueList() and expected.getValueList() might be of different types,
         * although they originated from identical single-element InListExpression.
         *
         * This happens 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.
         *
         * 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 to enable comparison: 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".
         */
    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);
}
Also used : SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) LambdaExpression(io.trino.sql.tree.LambdaExpression) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) NotExpression(io.trino.sql.tree.NotExpression) TryExpression(io.trino.sql.tree.TryExpression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) IfExpression(io.trino.sql.tree.IfExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) Expression(io.trino.sql.tree.Expression) InListExpression(io.trino.sql.tree.InListExpression) InListExpression(io.trino.sql.tree.InListExpression) InPredicate(io.trino.sql.tree.InPredicate)

Example 4 with InPredicate

use of io.trino.sql.tree.InPredicate in project trino by trinodb.

the class TestDomainTranslator method testFromUnprocessableInPredicate.

@Test
public void testFromUnprocessableInPredicate() {
    assertUnsupportedPredicate(new InPredicate(unprocessableExpression1(C_BIGINT), new InListExpression(ImmutableList.of(TRUE_LITERAL))));
    assertUnsupportedPredicate(new InPredicate(C_BOOLEAN.toSymbolReference(), new InListExpression(ImmutableList.of(unprocessableExpression1(C_BOOLEAN)))));
    assertUnsupportedPredicate(new InPredicate(C_BOOLEAN.toSymbolReference(), new InListExpression(ImmutableList.of(TRUE_LITERAL, unprocessableExpression1(C_BOOLEAN)))));
    assertPredicateTranslates(not(new InPredicate(C_BOOLEAN.toSymbolReference(), new InListExpression(ImmutableList.of(unprocessableExpression1(C_BOOLEAN))))), tupleDomain(C_BOOLEAN, Domain.notNull(BOOLEAN)), not(equal(C_BOOLEAN, unprocessableExpression1(C_BOOLEAN))));
}
Also used : InListExpression(io.trino.sql.tree.InListExpression) InPredicate(io.trino.sql.tree.InPredicate) Test(org.testng.annotations.Test)

Example 5 with InPredicate

use of io.trino.sql.tree.InPredicate in project trino by trinodb.

the class TestDomainTranslator method testFromInPredicateWithCastsAndNulls.

@Test
public void testFromInPredicateWithCastsAndNulls() {
    assertPredicateIsAlwaysFalse(new InPredicate(C_BIGINT.toSymbolReference(), new InListExpression(ImmutableList.of(cast(toExpression(null, SMALLINT), BIGINT)))));
    assertUnsupportedPredicate(not(new InPredicate(cast(C_SMALLINT, BIGINT), new InListExpression(ImmutableList.of(toExpression(null, BIGINT))))));
    assertPredicateTranslates(new InPredicate(C_BIGINT.toSymbolReference(), new InListExpression(ImmutableList.of(cast(toExpression(null, SMALLINT), BIGINT), toExpression(1L, BIGINT)))), tupleDomain(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L)), false)));
    assertPredicateIsAlwaysFalse(not(new InPredicate(C_BIGINT.toSymbolReference(), new InListExpression(ImmutableList.of(cast(toExpression(null, SMALLINT), BIGINT), toExpression(1L, SMALLINT))))));
}
Also used : InListExpression(io.trino.sql.tree.InListExpression) InPredicate(io.trino.sql.tree.InPredicate) Test(org.testng.annotations.Test)

Aggregations

InPredicate (io.trino.sql.tree.InPredicate)17 Expression (io.trino.sql.tree.Expression)11 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)10 InListExpression (io.trino.sql.tree.InListExpression)9 NotExpression (io.trino.sql.tree.NotExpression)7 Test (org.testng.annotations.Test)7 ImmutableList (com.google.common.collect.ImmutableList)4 Type (io.trino.spi.type.Type)4 Symbol (io.trino.sql.planner.Symbol)4 Assignments (io.trino.sql.planner.plan.Assignments)4 LogicalExpression (io.trino.sql.tree.LogicalExpression)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 DoubleType (io.trino.spi.type.DoubleType)3 RealType (io.trino.spi.type.RealType)3 ArithmeticBinaryExpression (io.trino.sql.tree.ArithmeticBinaryExpression)3 Cast (io.trino.sql.tree.Cast)3 FunctionCall (io.trino.sql.tree.FunctionCall)3 NullLiteral (io.trino.sql.tree.NullLiteral)3 QuantifiedComparisonExpression (io.trino.sql.tree.QuantifiedComparisonExpression)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2