Search in sources :

Example 11 with ValueSet

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

the class PartitionFilterBuilder method addRanges.

public PartitionFilterBuilder addRanges(String columnName, Range range, Range... ranges) {
    ValueSet values = ValueSet.ofRanges(range, ranges);
    Domain domain = Domain.create(values, false);
    domains.merge(columnName, domain, Domain::union);
    return this;
}
Also used : Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) ValueSet(io.trino.spi.predicate.ValueSet)

Example 12 with ValueSet

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

the class KafkaFilterManager method filterValuesByDomain.

@VisibleForTesting
public static Set<Long> filterValuesByDomain(Domain domain, Set<Long> sourceValues) {
    requireNonNull(sourceValues, "sourceValues is none");
    if (domain.isSingleValue()) {
        long singleValue = (long) domain.getSingleValue();
        return sourceValues.stream().filter(sourceValue -> sourceValue == singleValue).collect(toImmutableSet());
    } else {
        ValueSet valueSet = domain.getValues();
        if (valueSet instanceof SortedRangeSet) {
            Ranges ranges = ((SortedRangeSet) valueSet).getRanges();
            List<io.trino.spi.predicate.Range> rangeList = ranges.getOrderedRanges();
            if (rangeList.stream().allMatch(io.trino.spi.predicate.Range::isSingleValue)) {
                return rangeList.stream().map(range -> (Long) range.getSingleValue()).filter(sourceValues::contains).collect(toImmutableSet());
            } else {
                // still return values for range case like (_partition_id > 1)
                io.trino.spi.predicate.Range span = ranges.getSpan();
                long low = getLowIncludedValue(span).orElse(0L);
                long high = getHighIncludedValue(span).orElse(Long.MAX_VALUE);
                return sourceValues.stream().filter(item -> item >= low && item <= high).collect(toImmutableSet());
            }
        }
    }
    return sourceValues;
}
Also used : Config(org.apache.kafka.clients.admin.Config) DescribeConfigsResult(org.apache.kafka.clients.admin.DescribeConfigsResult) MICROSECONDS_PER_MILLISECOND(io.trino.spi.type.Timestamps.MICROSECONDS_PER_MILLISECOND) Type(io.trino.spi.type.Type) OFFSET_TIMESTAMP_FIELD(io.trino.plugin.kafka.KafkaInternalFieldManager.OFFSET_TIMESTAMP_FIELD) Function(java.util.function.Function) PARTITION_ID_FIELD(io.trino.plugin.kafka.KafkaInternalFieldManager.PARTITION_ID_FIELD) TimestampType(io.trino.spi.type.TimestampType) Inject(javax.inject.Inject) ConfigResource(org.apache.kafka.common.config.ConfigResource) Verify.verify(com.google.common.base.Verify.verify) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ColumnHandle(io.trino.spi.connector.ColumnHandle) Admin(org.apache.kafka.clients.admin.Admin) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) KAFKA_SPLIT_ERROR(io.trino.plugin.kafka.KafkaErrorCode.KAFKA_SPLIT_ERROR) Math.floorDiv(java.lang.Math.floorDiv) TopicPartition(org.apache.kafka.common.TopicPartition) PARTITION_OFFSET_FIELD(io.trino.plugin.kafka.KafkaInternalFieldManager.PARTITION_OFFSET_FIELD) ImmutableMap(com.google.common.collect.ImmutableMap) Ranges(io.trino.spi.predicate.Ranges) Domain(io.trino.spi.predicate.Domain) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) TrinoException(io.trino.spi.TrinoException) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) ConnectorSession(io.trino.spi.connector.ConnectorSession) PartitionInfo(org.apache.kafka.common.PartitionInfo) TupleDomain(io.trino.spi.predicate.TupleDomain) OffsetAndTimestamp(org.apache.kafka.clients.consumer.OffsetAndTimestamp) String.format(java.lang.String.format) ValueSet(io.trino.spi.predicate.ValueSet) List(java.util.List) BIGINT(io.trino.spi.type.BigintType.BIGINT) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) SortedRangeSet(io.trino.spi.predicate.SortedRangeSet) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) Ranges(io.trino.spi.predicate.Ranges) SortedRangeSet(io.trino.spi.predicate.SortedRangeSet) ValueSet(io.trino.spi.predicate.ValueSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 13 with ValueSet

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

the class TestPrometheusSplit method testPredicatePushDownSetsUpperAndLowerBound.

@Test
public void testPredicatePushDownSetsUpperAndLowerBound() {
    long predicateHighValue = 1568638171999L;
    Range highRange = Range.equal(TIMESTAMP_COLUMN_TYPE, packDateTimeWithZone(predicateHighValue, UTC_KEY));
    long predicateLowValue = 1568638171999L - 600000L;
    Range lowRange = Range.equal(TIMESTAMP_COLUMN_TYPE, packDateTimeWithZone(predicateLowValue, UTC_KEY));
    ValueSet valueSet = ValueSet.ofRanges(lowRange, highRange);
    Domain testDomain = Domain.create(valueSet, false);
    TupleDomain<ColumnHandle> testTupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(new PrometheusColumnHandle("timestamp", TIMESTAMP_COLUMN_TYPE, 2), testDomain));
    PrometheusTableHandle prometheusTableHandle = new PrometheusTableHandle("schemaName", "tableName").withPredicate(testTupleDomain);
    io.airlift.units.Duration maxQueryRangeDuration = new io.airlift.units.Duration(120, TimeUnit.SECONDS);
    io.airlift.units.Duration queryChunkSizeDuration = new io.airlift.units.Duration(30, TimeUnit.SECONDS);
    Instant now = ofEpochMilli(1568638171999L + 1200000L);
    List<String> splitTimes = PrometheusSplitManager.generateTimesForSplits(now, maxQueryRangeDuration, queryChunkSizeDuration, prometheusTableHandle);
    TemporalAmount expectedMaxQueryAsTime = java.time.Duration.ofMillis(new io.airlift.units.Duration(10, TimeUnit.MINUTES).toMillis() + ((splitTimes.size() - 1) * OFFSET_MILLIS));
    String lastSplit = splitTimes.get(splitTimes.size() - 1);
    Instant lastSplitAsTime = ofEpochMilli(longFromDecimalSecondString(lastSplit));
    String earliestSplit = splitTimes.get(0);
    Instant earliestSplitAsTime = ofEpochMilli(longFromDecimalSecondString(earliestSplit));
    TemporalAmount queryChunkAsTime = java.time.Duration.ofMillis(queryChunkSizeDuration.toMillis());
    java.time.Duration actualMaxDuration = Duration.between(earliestSplitAsTime.minus(queryChunkAsTime), lastSplitAsTime);
    assertEquals(lastSplitAsTime.toEpochMilli(), 1568638171999L);
    assertEquals(actualMaxDuration, expectedMaxQueryAsTime);
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) Instant(java.time.Instant) Duration(java.time.Duration) PrometheusSplitManager.decimalSecondString(io.trino.plugin.prometheus.PrometheusSplitManager.decimalSecondString) Range(io.trino.spi.predicate.Range) TemporalAmount(java.time.temporal.TemporalAmount) Duration(java.time.Duration) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) ValueSet(io.trino.spi.predicate.ValueSet) Test(org.testng.annotations.Test)

Example 14 with ValueSet

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

the class TestPrometheusSplit method testPredicatePushDownLowerBoundDirect.

@Test
public void testPredicatePushDownLowerBoundDirect() {
    Range lowRange = Range.greaterThanOrEqual(TIMESTAMP_COLUMN_TYPE, packDateTimeWithZone(1570460709643L, UTC_KEY));
    ValueSet valueSet = ValueSet.ofRanges(lowRange);
    Domain testDomain = Domain.create(valueSet, false);
    TupleDomain<ColumnHandle> testTupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(new PrometheusColumnHandle("timestamp", TIMESTAMP_COLUMN_TYPE, 2), testDomain));
    PrometheusPredicateTimeInfo predicateTimes = PrometheusSplitManager.determinePredicateTimes(testTupleDomain).orElseThrow();
    Instant expected = ofEpochMilli(1570460709643L);
    assertEquals(predicateTimes.getPredicateLowerTimeBound().orElseThrow(), expected);
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) Instant(java.time.Instant) Range(io.trino.spi.predicate.Range) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) ValueSet(io.trino.spi.predicate.ValueSet) Test(org.testng.annotations.Test)

Example 15 with ValueSet

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

the class TestPrometheusSplit method testPredicatePushDownSetsLowerBoundOnly.

@Test(enabled = false)
public void testPredicatePushDownSetsLowerBoundOnly() {
    long predicateLowValue = 1568638171999L - 600000L;
    Range lowRange = Range.greaterThanOrEqual(TIMESTAMP_COLUMN_TYPE, packDateTimeWithZone(predicateLowValue, UTC_KEY));
    ValueSet valueSet = ValueSet.ofRanges(lowRange);
    Domain testDomain = Domain.create(valueSet, false);
    TupleDomain<ColumnHandle> testTupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(new PrometheusColumnHandle("timestamp", TIMESTAMP_COLUMN_TYPE, 2), testDomain));
    PrometheusTableHandle prometheusTableHandle = new PrometheusTableHandle("schemaName", "tableName").withPredicate(testTupleDomain);
    io.airlift.units.Duration maxQueryRangeDuration = new io.airlift.units.Duration(120, TimeUnit.SECONDS);
    io.airlift.units.Duration queryChunkSizeDuration = new io.airlift.units.Duration(30, TimeUnit.SECONDS);
    Instant now = ofEpochMilli(1568638171999L);
    TemporalAmount maxQueryAsTime = java.time.Duration.ofMillis(maxQueryRangeDuration.toMillis());
    List<String> splitTimes = PrometheusSplitManager.generateTimesForSplits(now, maxQueryRangeDuration, queryChunkSizeDuration, prometheusTableHandle);
    String earliestSplit = splitTimes.get(0);
    Instant earliestSplitAsTime = ofEpochMilli(longFromDecimalSecondString(earliestSplit));
    TemporalAmount queryChunkAsTime = java.time.Duration.ofMillis(queryChunkSizeDuration.toMillis());
    Instant startOfQuery = earliestSplitAsTime.minus(queryChunkAsTime);
    assertNotEquals(startOfQuery, now.minus(maxQueryAsTime).minus(java.time.Duration.ofMillis((splitTimes.size() - 1) * OFFSET_MILLIS)));
    assertEquals(startOfQuery.toEpochMilli(), ofEpochMilli(predicateLowValue).toEpochMilli() - ((splitTimes.size() - 1) * OFFSET_MILLIS));
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) Instant(java.time.Instant) Duration(java.time.Duration) PrometheusSplitManager.decimalSecondString(io.trino.plugin.prometheus.PrometheusSplitManager.decimalSecondString) Range(io.trino.spi.predicate.Range) TemporalAmount(java.time.temporal.TemporalAmount) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) ValueSet(io.trino.spi.predicate.ValueSet) Test(org.testng.annotations.Test)

Aggregations

ValueSet (io.trino.spi.predicate.ValueSet)15 Domain (io.trino.spi.predicate.Domain)13 TupleDomain (io.trino.spi.predicate.TupleDomain)13 Range (io.trino.spi.predicate.Range)11 ColumnHandle (io.trino.spi.connector.ColumnHandle)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 List (java.util.List)4 TrinoException (io.trino.spi.TrinoException)3 ConnectorSession (io.trino.spi.connector.ConnectorSession)3 Ranges (io.trino.spi.predicate.Ranges)3 SortedRangeSet (io.trino.spi.predicate.SortedRangeSet)3 String.format (java.lang.String.format)3 Instant (java.time.Instant)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Set (java.util.Set)3 Test (org.testng.annotations.Test)3 Verify.verify (com.google.common.base.Verify.verify)2 ImmutableList (com.google.common.collect.ImmutableList)2