use of io.trino.sql.tree.NotExpression in project trino by trinodb.
the class DomainTranslator method toPredicate.
private Expression toPredicate(Session session, Domain domain, SymbolReference reference) {
if (domain.getValues().isNone()) {
return domain.isNullAllowed() ? new IsNullPredicate(reference) : FALSE_LITERAL;
}
if (domain.getValues().isAll()) {
return domain.isNullAllowed() ? TRUE_LITERAL : new NotExpression(new IsNullPredicate(reference));
}
List<Expression> disjuncts = new ArrayList<>();
disjuncts.addAll(domain.getValues().getValuesProcessor().transform(ranges -> extractDisjuncts(session, domain.getType(), ranges, reference), discreteValues -> extractDisjuncts(session, domain.getType(), discreteValues, reference), allOrNone -> {
throw new IllegalStateException("Case should not be reachable");
}));
// Add nullability disjuncts
if (domain.isNullAllowed()) {
disjuncts.add(new IsNullPredicate(reference));
}
return combineDisjunctsWithDefault(plannerContext.getMetadata(), disjuncts, TRUE_LITERAL);
}
use of io.trino.sql.tree.NotExpression in project trino by trinodb.
the class SimpleFilterProjectSemiJoinStatsRule method extractSemiJoinOutputFilter.
private 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(metadata, conjuncts.stream().filter(conjunct -> conjunct != semiJoinOutputReference).collect(toImmutableList()));
boolean negated = semiJoinOutputReference instanceof NotExpression;
return Optional.of(new SemiJoinOutputFilter(negated, remainingPredicate));
}
use of io.trino.sql.tree.NotExpression in project trino by trinodb.
the class TestSqlParser method testPrecedenceAndAssociativity.
@Test
public void testPrecedenceAndAssociativity() {
assertThat(expression("1 AND 2 AND 3 AND 4")).isEqualTo(new LogicalExpression(location(1, 1), LogicalExpression.Operator.AND, ImmutableList.of(new LongLiteral(location(1, 1), "1"), new LongLiteral(location(1, 7), "2"), new LongLiteral(location(1, 13), "3"), new LongLiteral(location(1, 19), "4"))));
assertThat(expression("1 OR 2 OR 3 OR 4")).isEqualTo(new LogicalExpression(location(1, 1), LogicalExpression.Operator.OR, ImmutableList.of(new LongLiteral(location(1, 1), "1"), new LongLiteral(location(1, 6), "2"), new LongLiteral(location(1, 11), "3"), new LongLiteral(location(1, 16), "4"))));
assertThat(expression("1 AND 2 AND 3 OR 4 AND 5 AND 6 OR 7 AND 8 AND 9")).isEqualTo(new LogicalExpression(location(1, 1), LogicalExpression.Operator.OR, ImmutableList.of(new LogicalExpression(location(1, 1), LogicalExpression.Operator.AND, ImmutableList.of(new LongLiteral(location(1, 1), "1"), new LongLiteral(location(1, 7), "2"), new LongLiteral(location(1, 13), "3"))), new LogicalExpression(location(1, 18), LogicalExpression.Operator.AND, ImmutableList.of(new LongLiteral(location(1, 18), "4"), new LongLiteral(location(1, 24), "5"), new LongLiteral(location(1, 30), "6"))), new LogicalExpression(location(1, 35), LogicalExpression.Operator.AND, ImmutableList.of(new LongLiteral(location(1, 35), "7"), new LongLiteral(location(1, 41), "8"), new LongLiteral(location(1, 47), "9"))))));
assertExpression("1 AND 2 OR 3", LogicalExpression.or(LogicalExpression.and(new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3")));
assertExpression("1 OR 2 AND 3", LogicalExpression.or(new LongLiteral("1"), LogicalExpression.and(new LongLiteral("2"), new LongLiteral("3"))));
assertExpression("NOT 1 AND 2", LogicalExpression.and(new NotExpression(new LongLiteral("1")), new LongLiteral("2")));
assertExpression("NOT 1 OR 2", LogicalExpression.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(ArithmeticBinaryExpression.Operator.DIVIDE, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.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"))));
}
use of io.trino.sql.tree.NotExpression in project trino by trinodb.
the class JoinMatcher method matchDynamicFilters.
private boolean matchDynamicFilters(JoinNode joinNode, SymbolAliases symbolAliases) {
if (expectedDynamicFilter.isEmpty()) {
return true;
}
Set<DynamicFilterId> dynamicFilterIds = joinNode.getDynamicFilters().keySet();
List<DynamicFilters.Descriptor> descriptors = searchFrom(joinNode.getLeft()).where(FilterNode.class::isInstance).findAll().stream().flatMap(filterNode -> extractExpressions(filterNode).stream()).flatMap(expression -> extractDynamicFilters(expression).getDynamicConjuncts().stream()).filter(descriptor -> dynamicFilterIds.contains(descriptor.getId())).collect(toImmutableList());
Map<DynamicFilterId, Symbol> idToBuildSymbolMap = joinNode.getDynamicFilters();
Set<Expression> actual = new HashSet<>();
for (DynamicFilters.Descriptor descriptor : descriptors) {
Expression probe = descriptor.getInput();
Symbol build = idToBuildSymbolMap.get(descriptor.getId());
if (build == null) {
return false;
}
Expression expression;
if (descriptor.isNullAllowed()) {
expression = new NotExpression(new ComparisonExpression(IS_DISTINCT_FROM, probe, build.toSymbolReference()));
} else {
expression = new ComparisonExpression(descriptor.getOperator(), probe, build.toSymbolReference());
}
actual.add(expression);
}
Set<Expression> expected = expectedDynamicFilter.get().stream().map(pattern -> pattern.getExpression(symbolAliases)).collect(toImmutableSet());
return expected.equals(actual);
}
use of io.trino.sql.tree.NotExpression in project trino by trinodb.
the class TestEffectivePredicateExtractor method testValues.
@Test
public void testValues() {
TypeProvider types = TypeProvider.copyOf(ImmutableMap.<Symbol, Type>builder().put(A, BIGINT).put(B, BIGINT).put(D, DOUBLE).put(R, RowType.anonymous(ImmutableList.of(BIGINT, BIGINT))).buildOrThrow());
// one column
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(new Row(ImmutableList.of(bigintLiteral(1))), new Row(ImmutableList.of(bigintLiteral(2))))), types, typeAnalyzer), new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))));
// one column with null
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(new Row(ImmutableList.of(bigintLiteral(1))), new Row(ImmutableList.of(bigintLiteral(2))), new Row(ImmutableList.of(new Cast(new NullLiteral(), toSqlType(BIGINT)))))), types, typeAnalyzer), or(new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))), new IsNullPredicate(AE)));
// all nulls
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(new Row(ImmutableList.of(new Cast(new NullLiteral(), toSqlType(BIGINT)))))), types, typeAnalyzer), new IsNullPredicate(AE));
// nested row
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(R), ImmutableList.of(new Row(ImmutableList.of(new Row(ImmutableList.of(bigintLiteral(1), new NullLiteral())))))), types, typeAnalyzer), TRUE_LITERAL);
// many rows
List<Expression> rows = IntStream.range(0, 500).mapToObj(TestEffectivePredicateExtractor::bigintLiteral).map(ImmutableList::of).map(Row::new).collect(toImmutableList());
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), rows), types, typeAnalyzer), new BetweenPredicate(AE, bigintLiteral(0), bigintLiteral(499)));
// NaN
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(D), ImmutableList.of(new Row(ImmutableList.of(doubleLiteral(Double.NaN))))), types, typeAnalyzer), new NotExpression(new IsNullPredicate(DE)));
// NaN and NULL
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(D), ImmutableList.of(new Row(ImmutableList.of(new Cast(new NullLiteral(), toSqlType(DOUBLE)))), new Row(ImmutableList.of(doubleLiteral(Double.NaN))))), types, typeAnalyzer), TRUE_LITERAL);
// NaN and value
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(D), ImmutableList.of(new Row(ImmutableList.of(doubleLiteral(42.))), new Row(ImmutableList.of(doubleLiteral(Double.NaN))))), types, typeAnalyzer), new NotExpression(new IsNullPredicate(DE)));
// Real NaN
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(D), ImmutableList.of(new Row(ImmutableList.of(new Cast(doubleLiteral(Double.NaN), toSqlType(REAL)))))), TypeProvider.copyOf(ImmutableMap.of(D, REAL)), typeAnalyzer), new NotExpression(new IsNullPredicate(DE)));
// multiple columns
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(new Row(ImmutableList.of(bigintLiteral(1), bigintLiteral(100))), new Row(ImmutableList.of(bigintLiteral(2), bigintLiteral(200))))), types, typeAnalyzer), and(new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))), new InPredicate(BE, new InListExpression(ImmutableList.of(bigintLiteral(100), bigintLiteral(200))))));
// multiple columns with null
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(new Row(ImmutableList.of(bigintLiteral(1), new Cast(new NullLiteral(), toSqlType(BIGINT)))), new Row(ImmutableList.of(new Cast(new NullLiteral(), toSqlType(BIGINT)), bigintLiteral(200))))), types, typeAnalyzer), and(or(new ComparisonExpression(EQUAL, AE, bigintLiteral(1)), new IsNullPredicate(AE)), or(new ComparisonExpression(EQUAL, BE, bigintLiteral(200)), new IsNullPredicate(BE))));
// non-deterministic
ResolvedFunction rand = functionResolution.resolveFunction(QualifiedName.of("rand"), ImmutableList.of());
ValuesNode node = new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(new Row(ImmutableList.of(bigintLiteral(1), new FunctionCall(rand.toQualifiedName(), ImmutableList.of())))));
assertEquals(extract(types, node), new ComparisonExpression(EQUAL, AE, bigintLiteral(1)));
// non-constant
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(new Row(ImmutableList.of(bigintLiteral(1))), new Row(ImmutableList.of(BE)))), types, typeAnalyzer), TRUE_LITERAL);
}
Aggregations