use of io.trino.sql.planner.DomainTranslator.ExtractionResult in project trino by trinodb.
the class RemoveRedundantPredicateAboveTableScan method apply.
@Override
public Result apply(FilterNode filterNode, Captures captures, Context context) {
Session session = context.getSession();
TableScanNode node = captures.get(TABLE_SCAN);
Expression predicate = filterNode.getPredicate();
Expression deterministicPredicate = filterDeterministicConjuncts(plannerContext.getMetadata(), predicate);
Expression nonDeterministicPredicate = filterNonDeterministicConjuncts(plannerContext.getMetadata(), predicate);
ExtractionResult decomposedPredicate = getFullyExtractedPredicates(session, deterministicPredicate, context.getSymbolAllocator().getTypes());
if (decomposedPredicate.getTupleDomain().isAll()) {
// no conjunct could be fully converted to tuple domain
return Result.empty();
}
TupleDomain<ColumnHandle> predicateDomain = decomposedPredicate.getTupleDomain().transformKeys(node.getAssignments()::get);
if (predicateDomain.isNone()) {
// to turn the subtree into a Values node
return Result.ofPlanNode(new ValuesNode(node.getId(), node.getOutputSymbols(), ImmutableList.of()));
}
if (node.getEnforcedConstraint().isNone()) {
// table scans with none domain should be converted to ValuesNode
return Result.ofPlanNode(new ValuesNode(node.getId(), node.getOutputSymbols(), ImmutableList.of()));
}
// is not NONE
Map<ColumnHandle, Domain> enforcedColumnDomains = node.getEnforcedConstraint().getDomains().orElseThrow();
TupleDomain<ColumnHandle> unenforcedDomain = predicateDomain.transformDomains((columnHandle, predicateColumnDomain) -> {
Type type = predicateColumnDomain.getType();
Domain enforcedColumnDomain = Optional.ofNullable(enforcedColumnDomains.get(columnHandle)).orElseGet(() -> Domain.all(type));
if (predicateColumnDomain.contains(enforcedColumnDomain)) {
// full enforced
return Domain.all(type);
}
return predicateColumnDomain.intersect(enforcedColumnDomain);
});
if (unenforcedDomain.equals(predicateDomain)) {
// no change in filter predicate
return Result.empty();
}
Map<ColumnHandle, Symbol> assignments = ImmutableBiMap.copyOf(node.getAssignments()).inverse();
Expression resultingPredicate = createResultingPredicate(plannerContext, session, context.getSymbolAllocator(), typeAnalyzer, // Dynamic filters are included in decomposedPredicate.getRemainingExpression()
TRUE_LITERAL, new DomainTranslator(plannerContext).toPredicate(session, unenforcedDomain.transformKeys(assignments::get)), nonDeterministicPredicate, decomposedPredicate.getRemainingExpression());
if (!TRUE_LITERAL.equals(resultingPredicate)) {
return Result.ofPlanNode(new FilterNode(context.getIdAllocator().getNextId(), node, resultingPredicate));
}
return Result.ofPlanNode(node);
}
use of io.trino.sql.planner.DomainTranslator.ExtractionResult in project trino by trinodb.
the class TestDomainTranslator method testNoneRoundTrip.
@Test
public void testNoneRoundTrip() {
TupleDomain<Symbol> tupleDomain = TupleDomain.none();
ExtractionResult result = fromPredicate(toPredicate(tupleDomain));
assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
assertEquals(result.getTupleDomain(), tupleDomain);
}
use of io.trino.sql.planner.DomainTranslator.ExtractionResult in project trino by trinodb.
the class TestDomainTranslator method testAllRoundTrip.
@Test
public void testAllRoundTrip() {
TupleDomain<Symbol> tupleDomain = TupleDomain.all();
ExtractionResult result = fromPredicate(toPredicate(tupleDomain));
assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
assertEquals(result.getTupleDomain(), tupleDomain);
}
use of io.trino.sql.planner.DomainTranslator.ExtractionResult in project trino by trinodb.
the class TestDomainTranslator method testFromAndPredicate.
@Test
public void testFromAndPredicate() {
Expression originalPredicate = and(and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)), and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT)));
ExtractionResult result = fromPredicate(originalPredicate);
assertEquals(result.getRemainingExpression(), and(unprocessableExpression1(C_BIGINT), unprocessableExpression2(C_BIGINT)));
assertEquals(result.getTupleDomain(), tupleDomain(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 1L, false, 5L, false)), false)));
// Test complements
assertUnsupportedPredicate(not(and(and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)), and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT)))));
originalPredicate = not(and(not(and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT))), not(and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT)))));
result = fromPredicate(originalPredicate);
assertEquals(result.getRemainingExpression(), originalPredicate);
assertEquals(result.getTupleDomain(), tupleDomain(C_BIGINT, Domain.notNull(BIGINT)));
}
use of io.trino.sql.planner.DomainTranslator.ExtractionResult in project trino by trinodb.
the class TestDomainTranslator method testFromSingleBooleanReference.
@Test
public void testFromSingleBooleanReference() {
Expression originalPredicate = C_BOOLEAN.toSymbolReference();
ExtractionResult result = fromPredicate(originalPredicate);
assertEquals(result.getTupleDomain(), tupleDomain(C_BOOLEAN, Domain.create(ValueSet.ofRanges(Range.equal(BOOLEAN, true)), false)));
assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
originalPredicate = not(C_BOOLEAN.toSymbolReference());
result = fromPredicate(originalPredicate);
assertEquals(result.getTupleDomain(), tupleDomain(C_BOOLEAN, Domain.create(ValueSet.ofRanges(Range.equal(BOOLEAN, true)).complement(), false)));
assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
originalPredicate = and(C_BOOLEAN.toSymbolReference(), C_BOOLEAN_1.toSymbolReference());
result = fromPredicate(originalPredicate);
Domain domain = Domain.create(ValueSet.ofRanges(Range.equal(BOOLEAN, true)), false);
assertEquals(result.getTupleDomain(), tupleDomain(C_BOOLEAN, domain, C_BOOLEAN_1, domain));
assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
originalPredicate = or(C_BOOLEAN.toSymbolReference(), C_BOOLEAN_1.toSymbolReference());
result = fromPredicate(originalPredicate);
assertEquals(result.getTupleDomain(), TupleDomain.all());
assertEquals(result.getRemainingExpression(), originalPredicate);
originalPredicate = not(and(C_BOOLEAN.toSymbolReference(), C_BOOLEAN_1.toSymbolReference()));
result = fromPredicate(originalPredicate);
assertEquals(result.getTupleDomain(), TupleDomain.all());
assertEquals(result.getRemainingExpression(), originalPredicate);
}
Aggregations