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)))));
}
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);
}
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));
}
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());
}
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();
}
Aggregations