Search in sources :

Example 1 with InListExpression

use of io.trino.sql.tree.InListExpression 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 2 with InListExpression

use of io.trino.sql.tree.InListExpression 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 3 with InListExpression

use of io.trino.sql.tree.InListExpression 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)

Example 4 with InListExpression

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

the class TestDomainTranslator method testInPredicateWithCasts.

@Test
public void testInPredicateWithCasts() {
    assertPredicateTranslates(new InPredicate(C_BIGINT.toSymbolReference(), new InListExpression(ImmutableList.of(cast(toExpression(1L, SMALLINT), BIGINT)))), tupleDomain(C_BIGINT, Domain.singleValue(BIGINT, 1L)));
    assertPredicateTranslates(new InPredicate(cast(C_SMALLINT, BIGINT), new InListExpression(ImmutableList.of(toExpression(1L, BIGINT)))), tupleDomain(C_SMALLINT, Domain.singleValue(SMALLINT, 1L)));
    assertUnsupportedPredicate(new InPredicate(cast(C_BIGINT, INTEGER), new InListExpression(ImmutableList.of(toExpression(1L, INTEGER)))));
}
Also used : InListExpression(io.trino.sql.tree.InListExpression) InPredicate(io.trino.sql.tree.InPredicate) Test(org.testng.annotations.Test)

Example 5 with InListExpression

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

the class TestEqualityInference method testExpressionsThatMayReturnNullOnNonNullInput.

@Test
public void testExpressionsThatMayReturnNullOnNonNullInput() {
    List<Expression> candidates = ImmutableList.of(// try_cast
    new Cast(nameReference("b"), toSqlType(BIGINT), true), functionResolution.functionCallBuilder(QualifiedName.of(TryFunction.NAME)).addArgument(new FunctionType(ImmutableList.of(), VARCHAR), new LambdaExpression(ImmutableList.of(), nameReference("b"))).build(), new NullIfExpression(nameReference("b"), number(1)), new IfExpression(nameReference("b"), number(1), new NullLiteral()), new InPredicate(nameReference("b"), new InListExpression(ImmutableList.of(new NullLiteral()))), new SearchedCaseExpression(ImmutableList.of(new WhenClause(new IsNotNullPredicate(nameReference("b")), new NullLiteral())), Optional.empty()), new SimpleCaseExpression(nameReference("b"), ImmutableList.of(new WhenClause(number(1), new NullLiteral())), Optional.empty()), new SubscriptExpression(new ArrayConstructor(ImmutableList.of(new NullLiteral())), nameReference("b")));
    for (Expression candidate : candidates) {
        EqualityInference inference = EqualityInference.newInstance(metadata, equals(nameReference("b"), nameReference("x")), equals(nameReference("a"), candidate));
        List<Expression> equalities = inference.generateEqualitiesPartitionedBy(symbols("b")).getScopeStraddlingEqualities();
        assertEquals(equalities.size(), 1);
        assertTrue(equalities.get(0).equals(equals(nameReference("x"), nameReference("b"))) || equalities.get(0).equals(equals(nameReference("b"), nameReference("x"))));
    }
}
Also used : Cast(io.trino.sql.tree.Cast) NullIfExpression(io.trino.sql.tree.NullIfExpression) IfExpression(io.trino.sql.tree.IfExpression) FunctionType(io.trino.type.FunctionType) InListExpression(io.trino.sql.tree.InListExpression) InPredicate(io.trino.sql.tree.InPredicate) IsNotNullPredicate(io.trino.sql.tree.IsNotNullPredicate) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) WhenClause(io.trino.sql.tree.WhenClause) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) LambdaExpression(io.trino.sql.tree.LambdaExpression) NullIfExpression(io.trino.sql.tree.NullIfExpression) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) InListExpression(io.trino.sql.tree.InListExpression) NullIfExpression(io.trino.sql.tree.NullIfExpression) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) ArrayConstructor(io.trino.sql.tree.ArrayConstructor) LambdaExpression(io.trino.sql.tree.LambdaExpression) NullLiteral(io.trino.sql.tree.NullLiteral) Test(org.testng.annotations.Test)

Aggregations

InListExpression (io.trino.sql.tree.InListExpression)9 InPredicate (io.trino.sql.tree.InPredicate)9 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)6 Expression (io.trino.sql.tree.Expression)6 NotExpression (io.trino.sql.tree.NotExpression)5 Test (org.testng.annotations.Test)5 DoubleType (io.trino.spi.type.DoubleType)3 RealType (io.trino.spi.type.RealType)3 Type (io.trino.spi.type.Type)3 Cast (io.trino.sql.tree.Cast)3 LogicalExpression (io.trino.sql.tree.LogicalExpression)3 NullLiteral (io.trino.sql.tree.NullLiteral)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ResolvedFunction (io.trino.metadata.ResolvedFunction)2 Range (io.trino.spi.predicate.Range)2 SortedRangeSet (io.trino.spi.predicate.SortedRangeSet)2 TypeSignatureTranslator.toSqlType (io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType)2 BetweenPredicate (io.trino.sql.tree.BetweenPredicate)2 FunctionCall (io.trino.sql.tree.FunctionCall)2