Search in sources :

Example 31 with TupleDomain

use of io.trino.spi.predicate.TupleDomain in project trino by trinodb.

the class TestDomainTranslator method testInOptimization.

@Test
public void testInOptimization() {
    Domain testDomain = Domain.create(ValueSet.all(BIGINT).subtract(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L), Range.equal(BIGINT, 3L))), false);
    TupleDomain<Symbol> tupleDomain = tupleDomain(C_BIGINT, testDomain);
    assertEquals(toPredicate(tupleDomain), not(in(C_BIGINT, ImmutableList.of(1L, 2L, 3L))));
    testDomain = Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 4L)).intersect(ValueSet.all(BIGINT).subtract(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L), Range.equal(BIGINT, 3L)))), false);
    tupleDomain = tupleDomain(C_BIGINT, testDomain);
    assertEquals(toPredicate(tupleDomain), and(lessThan(C_BIGINT, bigintLiteral(4L)), not(in(C_BIGINT, ImmutableList.of(1L, 2L, 3L)))));
    testDomain = Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 1L, true, 3L, true), Range.range(BIGINT, 5L, true, 7L, true), Range.range(BIGINT, 9L, true, 11L, true)), false);
    tupleDomain = tupleDomain(C_BIGINT, testDomain);
    assertEquals(toPredicate(tupleDomain), or(between(C_BIGINT, bigintLiteral(1L), bigintLiteral(3L)), between(C_BIGINT, bigintLiteral(5L), bigintLiteral(7L)), between(C_BIGINT, bigintLiteral(9L), bigintLiteral(11L))));
    testDomain = Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 4L)).intersect(ValueSet.all(BIGINT).subtract(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L), Range.equal(BIGINT, 3L)))).union(ValueSet.ofRanges(Range.range(BIGINT, 7L, true, 9L, true))), false);
    tupleDomain = tupleDomain(C_BIGINT, testDomain);
    assertEquals(toPredicate(tupleDomain), or(and(lessThan(C_BIGINT, bigintLiteral(4L)), not(in(C_BIGINT, ImmutableList.of(1L, 2L, 3L)))), between(C_BIGINT, bigintLiteral(7L), bigintLiteral(9L))));
    testDomain = Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 4L)).intersect(ValueSet.all(BIGINT).subtract(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L), Range.equal(BIGINT, 3L)))).union(ValueSet.ofRanges(Range.range(BIGINT, 7L, false, 9L, false), Range.range(BIGINT, 11L, false, 13L, false))), false);
    tupleDomain = tupleDomain(C_BIGINT, testDomain);
    assertEquals(toPredicate(tupleDomain), or(and(lessThan(C_BIGINT, bigintLiteral(4L)), not(in(C_BIGINT, ImmutableList.of(1L, 2L, 3L)))), and(greaterThan(C_BIGINT, bigintLiteral(7L)), lessThan(C_BIGINT, bigintLiteral(9L))), and(greaterThan(C_BIGINT, bigintLiteral(11L)), lessThan(C_BIGINT, bigintLiteral(13L)))));
}
Also used : Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) Test(org.testng.annotations.Test)

Example 32 with TupleDomain

use of io.trino.spi.predicate.TupleDomain 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);
}
Also used : ComparisonExpression(io.trino.sql.tree.ComparisonExpression) Expression(io.trino.sql.tree.Expression) InListExpression(io.trino.sql.tree.InListExpression) NotExpression(io.trino.sql.tree.NotExpression) ExtractionResult(io.trino.sql.planner.DomainTranslator.ExtractionResult) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) Test(org.testng.annotations.Test)

Example 33 with TupleDomain

use of io.trino.spi.predicate.TupleDomain in project trino by trinodb.

the class DefaultJdbcMetadata method applyFilter.

@Override
public Optional<ConstraintApplicationResult<ConnectorTableHandle>> applyFilter(ConnectorSession session, ConnectorTableHandle table, Constraint constraint) {
    JdbcTableHandle handle = (JdbcTableHandle) table;
    if (handle.getSortOrder().isPresent() && handle.getLimit().isPresent()) {
        handle = flushAttributesAsQuery(session, handle);
    }
    TupleDomain<ColumnHandle> oldDomain = handle.getConstraint();
    TupleDomain<ColumnHandle> newDomain = oldDomain.intersect(constraint.getSummary());
    List<String> newConstraintExpressions;
    TupleDomain<ColumnHandle> remainingFilter;
    Optional<ConnectorExpression> remainingExpression;
    if (newDomain.isNone()) {
        newConstraintExpressions = ImmutableList.of();
        remainingFilter = TupleDomain.all();
        remainingExpression = Optional.of(Constant.TRUE);
    } else {
        Map<ColumnHandle, Domain> domains = newDomain.getDomains().orElseThrow();
        List<JdbcColumnHandle> columnHandles = domains.keySet().stream().map(JdbcColumnHandle.class::cast).collect(toImmutableList());
        List<ColumnMapping> columnMappings = jdbcClient.toColumnMappings(session, columnHandles.stream().map(JdbcColumnHandle::getJdbcTypeHandle).collect(toImmutableList()));
        Map<ColumnHandle, Domain> supported = new HashMap<>();
        Map<ColumnHandle, Domain> unsupported = new HashMap<>();
        for (int i = 0; i < columnHandles.size(); i++) {
            JdbcColumnHandle column = columnHandles.get(i);
            ColumnMapping mapping = columnMappings.get(i);
            DomainPushdownResult pushdownResult = mapping.getPredicatePushdownController().apply(session, domains.get(column));
            supported.put(column, pushdownResult.getPushedDown());
            unsupported.put(column, pushdownResult.getRemainingFilter());
        }
        newDomain = TupleDomain.withColumnDomains(supported);
        remainingFilter = TupleDomain.withColumnDomains(unsupported);
        if (isComplexExpressionPushdown(session)) {
            List<String> newExpressions = new ArrayList<>();
            List<ConnectorExpression> remainingExpressions = new ArrayList<>();
            for (ConnectorExpression expression : extractConjuncts(constraint.getExpression())) {
                Optional<String> converted = jdbcClient.convertPredicate(session, expression, constraint.getAssignments());
                if (converted.isPresent()) {
                    newExpressions.add(converted.get());
                } else {
                    remainingExpressions.add(expression);
                }
            }
            newConstraintExpressions = ImmutableSet.<String>builder().addAll(handle.getConstraintExpressions()).addAll(newExpressions).build().asList();
            remainingExpression = Optional.of(and(remainingExpressions));
        } else {
            newConstraintExpressions = ImmutableList.of();
            remainingExpression = Optional.empty();
        }
    }
    if (oldDomain.equals(newDomain) && handle.getConstraintExpressions().equals(newConstraintExpressions)) {
        return Optional.empty();
    }
    handle = new JdbcTableHandle(handle.getRelationHandle(), newDomain, newConstraintExpressions, handle.getSortOrder(), handle.getLimit(), handle.getColumns(), handle.getOtherReferencedTables(), handle.getNextSyntheticColumnId());
    return Optional.of(remainingExpression.isPresent() ? new ConstraintApplicationResult<>(handle, remainingFilter, remainingExpression.get(), false) : new ConstraintApplicationResult<>(handle, remainingFilter, false));
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) HashMap(java.util.HashMap) ConnectorExpression(io.trino.spi.expression.ConnectorExpression) ArrayList(java.util.ArrayList) Constraint(io.trino.spi.connector.Constraint) ConstraintApplicationResult(io.trino.spi.connector.ConstraintApplicationResult) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) DomainPushdownResult(io.trino.plugin.jdbc.PredicatePushdownController.DomainPushdownResult)

Example 34 with TupleDomain

use of io.trino.spi.predicate.TupleDomain in project trino by trinodb.

the class DeltaLakePageSourceProvider method getParquetTupleDomain.

private static TupleDomain<HiveColumnHandle> getParquetTupleDomain(TupleDomain<DeltaLakeColumnHandle> effectivePredicate) {
    if (effectivePredicate.isNone()) {
        return TupleDomain.none();
    }
    ImmutableMap.Builder<HiveColumnHandle, Domain> predicate = ImmutableMap.builder();
    effectivePredicate.getDomains().get().forEach((columnHandle, domain) -> {
        String baseType = columnHandle.getType().getTypeSignature().getBase();
        // skip looking up predicates for complex types as Parquet only stores stats for primitives
        if (!baseType.equals(StandardTypes.MAP) && !baseType.equals(StandardTypes.ARRAY) && !baseType.equals(StandardTypes.ROW) && // TODO: Remove the next line once timestamp predicate pushdown works in Parquet reader
        !baseType.equals(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)) {
            HiveColumnHandle hiveColumnHandle = columnHandle.toHiveColumnHandle();
            predicate.put(hiveColumnHandle, domain);
        }
    });
    return TupleDomain.withColumnDomains(predicate.buildOrThrow());
}
Also used : Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) ImmutableMap(com.google.common.collect.ImmutableMap) HiveColumnHandle(io.trino.plugin.hive.HiveColumnHandle)

Example 35 with TupleDomain

use of io.trino.spi.predicate.TupleDomain in project trino by trinodb.

the class ElasticsearchQueryBuilder method buildSearchQuery.

public static QueryBuilder buildSearchQuery(TupleDomain<ElasticsearchColumnHandle> constraint, Optional<String> query, Map<String, String> regexes) {
    BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
    if (constraint.getDomains().isPresent()) {
        for (Map.Entry<ElasticsearchColumnHandle, Domain> entry : constraint.getDomains().get().entrySet()) {
            ElasticsearchColumnHandle column = entry.getKey();
            Domain domain = entry.getValue();
            checkArgument(!domain.isNone(), "Unexpected NONE domain for %s", column.getName());
            if (!domain.isAll()) {
                queryBuilder.filter(new BoolQueryBuilder().must(buildPredicate(column.getName(), domain, column.getType())));
            }
        }
    }
    regexes.forEach((name, value) -> queryBuilder.filter(new BoolQueryBuilder().must(((new RegexpQueryBuilder(name, value))))));
    query.map(QueryStringQueryBuilder::new).ifPresent(queryBuilder::must);
    if (queryBuilder.hasClauses()) {
        return queryBuilder;
    }
    return new MatchAllQueryBuilder();
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) Map(java.util.Map) RegexpQueryBuilder(org.elasticsearch.index.query.RegexpQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Aggregations

TupleDomain (io.trino.spi.predicate.TupleDomain)97 Domain (io.trino.spi.predicate.Domain)77 Map (java.util.Map)50 ColumnHandle (io.trino.spi.connector.ColumnHandle)48 ImmutableMap (com.google.common.collect.ImmutableMap)43 ImmutableList (com.google.common.collect.ImmutableList)41 List (java.util.List)40 Optional (java.util.Optional)36 Set (java.util.Set)33 Test (org.testng.annotations.Test)33 Objects.requireNonNull (java.util.Objects.requireNonNull)32 ConnectorSession (io.trino.spi.connector.ConnectorSession)29 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)28 ImmutableSet (com.google.common.collect.ImmutableSet)26 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)23 Range (io.trino.spi.predicate.Range)22 String.format (java.lang.String.format)22 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)20 TrinoException (io.trino.spi.TrinoException)20 Type (io.trino.spi.type.Type)19