Search in sources :

Example 1 with NotExpression

use of io.prestosql.sql.tree.NotExpression in project hetu-core by openlookeng.

the class TestSqlParser method testExists.

@Test
public void testExists() {
    assertStatement("SELECT EXISTS(SELECT 1)", simpleQuery(selectList(exists(simpleQuery(selectList(new LongLiteral("1")))))));
    assertStatement("SELECT EXISTS(SELECT 1) = EXISTS(SELECT 2)", simpleQuery(selectList(new ComparisonExpression(ComparisonExpression.Operator.EQUAL, exists(simpleQuery(selectList(new LongLiteral("1")))), exists(simpleQuery(selectList(new LongLiteral("2"))))))));
    assertStatement("SELECT NOT EXISTS(SELECT 1) = EXISTS(SELECT 2)", simpleQuery(selectList(new NotExpression(new ComparisonExpression(ComparisonExpression.Operator.EQUAL, exists(simpleQuery(selectList(new LongLiteral("1")))), exists(simpleQuery(selectList(new LongLiteral("2")))))))));
    assertStatement("SELECT (NOT EXISTS(SELECT 1)) = EXISTS(SELECT 2)", simpleQuery(selectList(new ComparisonExpression(ComparisonExpression.Operator.EQUAL, new NotExpression(exists(simpleQuery(selectList(new LongLiteral("1"))))), exists(simpleQuery(selectList(new LongLiteral("2"))))))));
}
Also used : ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) LongLiteral(io.prestosql.sql.tree.LongLiteral) NotExpression(io.prestosql.sql.tree.NotExpression) Test(org.testng.annotations.Test)

Example 2 with NotExpression

use of io.prestosql.sql.tree.NotExpression in project hetu-core by openlookeng.

the class TestSqlParser method testPrecedenceAndAssociativity.

@Test
public void testPrecedenceAndAssociativity() {
    assertExpression("1 AND 2 OR 3", new LogicalBinaryExpression(LogicalBinaryExpression.Operator.OR, new LogicalBinaryExpression(LogicalBinaryExpression.Operator.AND, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3")));
    assertExpression("1 OR 2 AND 3", new LogicalBinaryExpression(LogicalBinaryExpression.Operator.OR, new LongLiteral("1"), new LogicalBinaryExpression(LogicalBinaryExpression.Operator.AND, new LongLiteral("2"), new LongLiteral("3"))));
    assertExpression("NOT 1 AND 2", new LogicalBinaryExpression(LogicalBinaryExpression.Operator.AND, new NotExpression(new LongLiteral("1")), new LongLiteral("2")));
    assertExpression("NOT 1 OR 2", new LogicalBinaryExpression(LogicalBinaryExpression.Operator.OR, new NotExpression(new LongLiteral("1")), new LongLiteral("2")));
    assertExpression("-1 + 2", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.ADD, new LongLiteral("-1"), new LongLiteral("2")));
    assertExpression("1 - 2 - 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.SUBTRACT, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.SUBTRACT, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3")));
    assertExpression("1 / 2 / 3", new ArithmeticBinaryExpression(DIVIDE, new ArithmeticBinaryExpression(DIVIDE, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3")));
    assertExpression("1 + 2 * 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.ADD, new LongLiteral("1"), new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new LongLiteral("2"), new LongLiteral("3"))));
}
Also used : LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) LongLiteral(io.prestosql.sql.tree.LongLiteral) NotExpression(io.prestosql.sql.tree.NotExpression) Test(org.testng.annotations.Test)

Example 3 with NotExpression

use of io.prestosql.sql.tree.NotExpression in project hetu-core by openlookeng.

the class TestExpressionUtils method testNormalize.

@Test
public void testNormalize() {
    assertNormalize(new ComparisonExpression(EQUAL, name("a"), new LongLiteral("1")));
    assertNormalize(new IsNullPredicate(name("a")));
    assertNormalize(new NotExpression(new LikePredicate(name("a"), new StringLiteral("x%"), Optional.empty())));
    assertNormalize(new NotExpression(new ComparisonExpression(EQUAL, name("a"), new LongLiteral("1"))), new ComparisonExpression(NOT_EQUAL, name("a"), new LongLiteral("1")));
    assertNormalize(new NotExpression(new ComparisonExpression(NOT_EQUAL, name("a"), new LongLiteral("1"))), new ComparisonExpression(EQUAL, name("a"), new LongLiteral("1")));
    // Cannot normalize IS DISTINCT FROM yet
    assertNormalize(new NotExpression(new ComparisonExpression(IS_DISTINCT_FROM, name("a"), new LongLiteral("1"))));
}
Also used : ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) StringLiteral(io.prestosql.sql.tree.StringLiteral) LongLiteral(io.prestosql.sql.tree.LongLiteral) IsNullPredicate(io.prestosql.sql.tree.IsNullPredicate) NotExpression(io.prestosql.sql.tree.NotExpression) LikePredicate(io.prestosql.sql.tree.LikePredicate) Test(org.testng.annotations.Test)

Example 4 with NotExpression

use of io.prestosql.sql.tree.NotExpression in project hetu-core by openlookeng.

the class SimpleFilterProjectSemiJoinStatsRule method extractSemiJoinOutputFilter.

private static Optional<SemiJoinOutputFilter> extractSemiJoinOutputFilter(Expression predicate, Symbol semiJoinOutput) {
    List<Expression> conjuncts = extractConjuncts(predicate);
    List<Expression> semiJoinOutputReferences = conjuncts.stream().filter(conjunct -> isSemiJoinOutputReference(conjunct, semiJoinOutput)).collect(toImmutableList());
    if (semiJoinOutputReferences.size() != 1) {
        return Optional.empty();
    }
    Expression semiJoinOutputReference = Iterables.getOnlyElement(semiJoinOutputReferences);
    Expression remainingPredicate = combineConjuncts(conjuncts.stream().filter(conjunct -> conjunct != semiJoinOutputReference).collect(toImmutableList()));
    boolean negated = semiJoinOutputReference instanceof NotExpression;
    return Optional.of(new SemiJoinOutputFilter(negated, castToRowExpression(remainingPredicate)));
}
Also used : Iterables(com.google.common.collect.Iterables) Lookup(io.prestosql.sql.planner.iterative.Lookup) LogicalRowExpressions(io.prestosql.expressions.LogicalRowExpressions) TypeProvider(io.prestosql.sql.planner.TypeProvider) Pattern(io.prestosql.matching.Pattern) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode) OriginalExpressionUtils.isExpression(io.prestosql.sql.relational.OriginalExpressionUtils.isExpression) CallExpression(io.prestosql.spi.relation.CallExpression) FilterNode(io.prestosql.spi.plan.FilterNode) Map(java.util.Map) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) ProjectNodeUtils.isIdentity(io.prestosql.sql.relational.ProjectNodeUtils.isIdentity) Symbol(io.prestosql.spi.plan.Symbol) SymbolUtils(io.prestosql.sql.planner.SymbolUtils) NotExpression(io.prestosql.sql.tree.NotExpression) UNKNOWN_FILTER_COEFFICIENT(io.prestosql.cost.FilterStatsCalculator.UNKNOWN_FILTER_COEFFICIENT) RowExpressionDeterminismEvaluator(io.prestosql.sql.relational.RowExpressionDeterminismEvaluator) SemiJoinStatsCalculator.computeSemiJoin(io.prestosql.cost.SemiJoinStatsCalculator.computeSemiJoin) SemiJoinStatsCalculator.computeAntiJoin(io.prestosql.cost.SemiJoinStatsCalculator.computeAntiJoin) ExpressionUtils.combineConjuncts(io.prestosql.sql.ExpressionUtils.combineConjuncts) Patterns.filter(io.prestosql.sql.planner.plan.Patterns.filter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) PlanNode(io.prestosql.spi.plan.PlanNode) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) ProjectNode(io.prestosql.spi.plan.ProjectNode) Metadata(io.prestosql.metadata.Metadata) Preconditions.checkState(com.google.common.base.Preconditions.checkState) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) List(java.util.List) FunctionResolution(io.prestosql.sql.relational.FunctionResolution) SymbolReference(io.prestosql.sql.tree.SymbolReference) RowExpression(io.prestosql.spi.relation.RowExpression) Optional(java.util.Optional) Expression(io.prestosql.sql.tree.Expression) ExpressionUtils.extractConjuncts(io.prestosql.sql.ExpressionUtils.extractConjuncts) OriginalExpressionUtils.isExpression(io.prestosql.sql.relational.OriginalExpressionUtils.isExpression) CallExpression(io.prestosql.spi.relation.CallExpression) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) NotExpression(io.prestosql.sql.tree.NotExpression) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Expression(io.prestosql.sql.tree.Expression) NotExpression(io.prestosql.sql.tree.NotExpression)

Example 5 with NotExpression

use of io.prestosql.sql.tree.NotExpression 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)

Aggregations

NotExpression (io.prestosql.sql.tree.NotExpression)9 Expression (io.prestosql.sql.tree.Expression)5 Preconditions.checkState (com.google.common.base.Preconditions.checkState)3 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 Session (io.prestosql.Session)3 Metadata (io.prestosql.metadata.Metadata)3 Symbol (io.prestosql.spi.plan.Symbol)3 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)3 LongLiteral (io.prestosql.sql.tree.LongLiteral)3 Test (org.testng.annotations.Test)3 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)2 Iterators.peekingIterator (com.google.common.collect.Iterators.peekingIterator)2 PeekingIterator (com.google.common.collect.PeekingIterator)2 Slice (io.airlift.slice.Slice)2 SliceUtf8.countCodePoints (io.airlift.slice.SliceUtf8.countCodePoints)2 SliceUtf8.getCodePointAt (io.airlift.slice.SliceUtf8.getCodePointAt)2 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)2