use of io.trino.spi.predicate.Domain in project trino by trinodb.
the class ExpressionConverter method toIcebergExpression.
public static Expression toIcebergExpression(TupleDomain<IcebergColumnHandle> tupleDomain) {
if (tupleDomain.isAll()) {
return alwaysTrue();
}
if (tupleDomain.getDomains().isEmpty()) {
return alwaysFalse();
}
Map<IcebergColumnHandle, Domain> domainMap = tupleDomain.getDomains().get();
Expression expression = alwaysTrue();
for (Map.Entry<IcebergColumnHandle, Domain> entry : domainMap.entrySet()) {
IcebergColumnHandle columnHandle = entry.getKey();
Domain domain = entry.getValue();
expression = and(expression, toIcebergExpression(columnHandle.getQualifiedName(), columnHandle.getType(), domain));
}
return expression;
}
use of io.trino.spi.predicate.Domain in project trino by trinodb.
the class IcebergSplitSource method fileMatchesPredicate.
@VisibleForTesting
static boolean fileMatchesPredicate(Map<Integer, Type.PrimitiveType> primitiveTypeForFieldId, TupleDomain<IcebergColumnHandle> dynamicFilterPredicate, @Nullable Map<Integer, ByteBuffer> lowerBounds, @Nullable Map<Integer, ByteBuffer> upperBounds, @Nullable Map<Integer, Long> nullValueCounts) {
if (dynamicFilterPredicate.isNone()) {
return false;
}
Map<IcebergColumnHandle, Domain> domains = dynamicFilterPredicate.getDomains().orElseThrow();
for (Map.Entry<IcebergColumnHandle, Domain> domainEntry : domains.entrySet()) {
IcebergColumnHandle column = domainEntry.getKey();
Domain domain = domainEntry.getValue();
int fieldId = column.getId();
boolean mayContainNulls;
if (nullValueCounts == null) {
mayContainNulls = true;
} else {
Long nullValueCount = nullValueCounts.get(fieldId);
mayContainNulls = nullValueCount == null || nullValueCount > 0;
}
Type type = primitiveTypeForFieldId.get(fieldId);
Domain statisticsDomain = domainForStatistics(column.getType(), lowerBounds == null ? null : fromByteBuffer(type, lowerBounds.get(fieldId)), upperBounds == null ? null : fromByteBuffer(type, upperBounds.get(fieldId)), mayContainNulls);
if (!domain.overlaps(statisticsDomain)) {
return false;
}
}
return true;
}
use of io.trino.spi.predicate.Domain in project trino by trinodb.
the class TestIcebergSplitSource method testBigintStatisticsPruning.
@Test
public void testBigintStatisticsPruning() {
IcebergColumnHandle bigintColumn = new IcebergColumnHandle(new ColumnIdentity(1, "name", ColumnIdentity.TypeCategory.PRIMITIVE, ImmutableList.of()), BIGINT, ImmutableList.of(), BIGINT, Optional.empty());
Map<Integer, Type.PrimitiveType> primitiveTypes = ImmutableMap.of(1, Types.LongType.get());
Map<Integer, ByteBuffer> lowerBound = ImmutableMap.of(1, Conversions.toByteBuffer(Types.LongType.get(), 1000L));
Map<Integer, ByteBuffer> upperBound = ImmutableMap.of(1, Conversions.toByteBuffer(Types.LongType.get(), 2000L));
assertFalse(IcebergSplitSource.fileMatchesPredicate(primitiveTypes, TupleDomain.fromFixedValues(ImmutableMap.of(bigintColumn, NullableValue.of(BIGINT, 0L))), lowerBound, upperBound, ImmutableMap.of(1, 0L)));
assertTrue(IcebergSplitSource.fileMatchesPredicate(primitiveTypes, TupleDomain.fromFixedValues(ImmutableMap.of(bigintColumn, NullableValue.of(BIGINT, 1000L))), lowerBound, upperBound, ImmutableMap.of(1, 0L)));
assertTrue(IcebergSplitSource.fileMatchesPredicate(primitiveTypes, TupleDomain.fromFixedValues(ImmutableMap.of(bigintColumn, NullableValue.of(BIGINT, 1500L))), lowerBound, upperBound, ImmutableMap.of(1, 0L)));
assertTrue(IcebergSplitSource.fileMatchesPredicate(primitiveTypes, TupleDomain.fromFixedValues(ImmutableMap.of(bigintColumn, NullableValue.of(BIGINT, 2000L))), lowerBound, upperBound, ImmutableMap.of(1, 0L)));
assertFalse(IcebergSplitSource.fileMatchesPredicate(primitiveTypes, TupleDomain.fromFixedValues(ImmutableMap.of(bigintColumn, NullableValue.of(BIGINT, 3000L))), lowerBound, upperBound, ImmutableMap.of(1, 0L)));
Domain outsideStatisticsRangeAllowNulls = Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 0L, true, 100L, true)), true);
assertFalse(IcebergSplitSource.fileMatchesPredicate(primitiveTypes, TupleDomain.withColumnDomains(ImmutableMap.of(bigintColumn, outsideStatisticsRangeAllowNulls)), lowerBound, upperBound, ImmutableMap.of(1, 0L)));
assertTrue(IcebergSplitSource.fileMatchesPredicate(primitiveTypes, TupleDomain.withColumnDomains(ImmutableMap.of(bigintColumn, outsideStatisticsRangeAllowNulls)), lowerBound, upperBound, ImmutableMap.of(1, 1L)));
Domain outsideStatisticsRangeNoNulls = Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 0L, true, 100L, true)), false);
assertFalse(IcebergSplitSource.fileMatchesPredicate(primitiveTypes, TupleDomain.withColumnDomains(ImmutableMap.of(bigintColumn, outsideStatisticsRangeNoNulls)), lowerBound, upperBound, ImmutableMap.of(1, 0L)));
assertFalse(IcebergSplitSource.fileMatchesPredicate(primitiveTypes, TupleDomain.withColumnDomains(ImmutableMap.of(bigintColumn, outsideStatisticsRangeNoNulls)), lowerBound, upperBound, ImmutableMap.of(1, 1L)));
Domain insideStatisticsRange = Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 1001L, true, 1002L, true)), false);
assertTrue(IcebergSplitSource.fileMatchesPredicate(primitiveTypes, TupleDomain.withColumnDomains(ImmutableMap.of(bigintColumn, insideStatisticsRange)), lowerBound, upperBound, ImmutableMap.of(1, 0L)));
assertTrue(IcebergSplitSource.fileMatchesPredicate(primitiveTypes, TupleDomain.withColumnDomains(ImmutableMap.of(bigintColumn, insideStatisticsRange)), lowerBound, upperBound, ImmutableMap.of(1, 1L)));
Domain overlappingStatisticsRange = Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 990L, true, 1010L, true)), false);
assertTrue(IcebergSplitSource.fileMatchesPredicate(primitiveTypes, TupleDomain.withColumnDomains(ImmutableMap.of(bigintColumn, overlappingStatisticsRange)), lowerBound, upperBound, ImmutableMap.of(1, 0L)));
assertTrue(IcebergSplitSource.fileMatchesPredicate(primitiveTypes, TupleDomain.withColumnDomains(ImmutableMap.of(bigintColumn, overlappingStatisticsRange)), lowerBound, upperBound, ImmutableMap.of(1, 1L)));
}
use of io.trino.spi.predicate.Domain in project trino by trinodb.
the class KuduClientSession method addConstraintPredicates.
/**
* translates TupleDomain to KuduPredicates.
*/
private void addConstraintPredicates(KuduTable table, KuduScanToken.KuduScanTokenBuilder builder, TupleDomain<ColumnHandle> constraintSummary) {
verify(!constraintSummary.isNone(), "constraintSummary is none");
if (constraintSummary.isAll()) {
return;
}
Schema schema = table.getSchema();
for (TupleDomain.ColumnDomain<ColumnHandle> columnDomain : constraintSummary.getColumnDomains().get()) {
int position = ((KuduColumnHandle) columnDomain.getColumn()).getOrdinalPosition();
ColumnSchema columnSchema = schema.getColumnByIndex(position);
Domain domain = columnDomain.getDomain();
verify(!domain.isNone(), "Domain is none");
if (domain.isAll()) {
// no restriction
} else if (domain.isOnlyNull()) {
builder.addPredicate(KuduPredicate.newIsNullPredicate(columnSchema));
} else if (!domain.getValues().isNone() && domain.isNullAllowed()) {
// no restriction
} else if (domain.getValues().isAll() && !domain.isNullAllowed()) {
builder.addPredicate(KuduPredicate.newIsNotNullPredicate(columnSchema));
} else if (domain.isSingleValue()) {
KuduPredicate predicate = createEqualsPredicate(columnSchema, domain.getSingleValue());
builder.addPredicate(predicate);
} else {
ValueSet valueSet = domain.getValues();
if (valueSet instanceof EquatableValueSet) {
DiscreteValues discreteValues = valueSet.getDiscreteValues();
KuduPredicate predicate = createInListPredicate(columnSchema, discreteValues);
builder.addPredicate(predicate);
} else if (valueSet instanceof SortedRangeSet) {
Ranges ranges = ((SortedRangeSet) valueSet).getRanges();
List<Range> rangeList = ranges.getOrderedRanges();
if (rangeList.stream().allMatch(Range::isSingleValue)) {
io.trino.spi.type.Type type = TypeHelper.fromKuduColumn(columnSchema);
List<Object> javaValues = rangeList.stream().map(range -> TypeHelper.getJavaValue(type, range.getSingleValue())).collect(toImmutableList());
KuduPredicate predicate = KuduPredicate.newInListPredicate(columnSchema, javaValues);
builder.addPredicate(predicate);
} else {
Range span = ranges.getSpan();
if (!span.isLowUnbounded()) {
KuduPredicate.ComparisonOp op = span.isLowInclusive() ? GREATER_EQUAL : GREATER;
KuduPredicate predicate = createComparisonPredicate(columnSchema, op, span.getLowBoundedValue());
builder.addPredicate(predicate);
}
if (!span.isHighUnbounded()) {
KuduPredicate.ComparisonOp op = span.isHighInclusive() ? LESS_EQUAL : LESS;
KuduPredicate predicate = createComparisonPredicate(columnSchema, op, span.getHighBoundedValue());
builder.addPredicate(predicate);
}
}
} else {
throw new IllegalStateException("Unexpected domain: " + domain);
}
}
}
}
use of io.trino.spi.predicate.Domain in project trino by trinodb.
the class TestKafkaFilterManager method testFilterValuesByDomain.
@Test
public void testFilterValuesByDomain() {
Set<Long> source = Set.of(1L, 2L, 3L, 4L, 5L, 6L);
Domain testDomain = Domain.singleValue(BIGINT, 1L);
assertEquals(KafkaFilterManager.filterValuesByDomain(testDomain, source), Set.of(1L));
testDomain = multipleValues(BIGINT, ImmutableList.of(3L, 8L));
assertEquals(KafkaFilterManager.filterValuesByDomain(testDomain, source), Set.of(3L));
testDomain = Domain.create(SortedRangeSet.copyOf(BIGINT, ImmutableList.of(Range.range(BIGINT, 2L, true, 4L, true))), false);
assertEquals(KafkaFilterManager.filterValuesByDomain(testDomain, source), Set.of(2L, 3L, 4L));
}
Aggregations