Search in sources :

Example 56 with Domain

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;
}
Also used : Expression(org.apache.iceberg.expressions.Expression) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) Map(java.util.Map)

Example 57 with Domain

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;
}
Also used : TypeConverter.toIcebergType(io.trino.plugin.iceberg.TypeConverter.toIcebergType) Type(org.apache.iceberg.types.Type) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) Map(java.util.Map) HashMap(java.util.HashMap) Constraint(io.trino.spi.connector.Constraint) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 58 with Domain

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)));
}
Also used : Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) ByteBuffer(java.nio.ByteBuffer) Test(org.testng.annotations.Test)

Example 59 with Domain

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);
            }
        }
    }
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) Ranges(io.trino.spi.predicate.Ranges) Schema(org.apache.kudu.Schema) ColumnSchema(org.apache.kudu.ColumnSchema) HashBucketSchema(org.apache.kudu.client.PartitionSchema.HashBucketSchema) EquatableValueSet(io.trino.spi.predicate.EquatableValueSet) ColumnSchema(org.apache.kudu.ColumnSchema) Range(io.trino.spi.predicate.Range) KuduPredicate(org.apache.kudu.client.KuduPredicate) Type(org.apache.kudu.Type) DecimalType(io.trino.spi.type.DecimalType) TupleDomain(io.trino.spi.predicate.TupleDomain) SortedRangeSet(io.trino.spi.predicate.SortedRangeSet) DiscreteValues(io.trino.spi.predicate.DiscreteValues) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) EquatableValueSet(io.trino.spi.predicate.EquatableValueSet) ValueSet(io.trino.spi.predicate.ValueSet)

Example 60 with 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));
}
Also used : Domain(io.trino.spi.predicate.Domain) Test(org.testng.annotations.Test)

Aggregations

Domain (io.trino.spi.predicate.Domain)120 TupleDomain (io.trino.spi.predicate.TupleDomain)107 ColumnHandle (io.trino.spi.connector.ColumnHandle)51 Test (org.testng.annotations.Test)45 Map (java.util.Map)38 ImmutableList (com.google.common.collect.ImmutableList)36 ImmutableMap (com.google.common.collect.ImmutableMap)33 List (java.util.List)27 Optional (java.util.Optional)25 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)23 Type (io.trino.spi.type.Type)23 ConnectorSession (io.trino.spi.connector.ConnectorSession)21 SchemaTableName (io.trino.spi.connector.SchemaTableName)21 Objects.requireNonNull (java.util.Objects.requireNonNull)21 Set (java.util.Set)20 Range (io.trino.spi.predicate.Range)19 ValueSet (io.trino.spi.predicate.ValueSet)18 Constraint (io.trino.spi.connector.Constraint)17 String.format (java.lang.String.format)17 ImmutableSet (com.google.common.collect.ImmutableSet)16